Skip to content

Commit

Permalink
fix: add validation for bulk upload patch
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanta2018 committed Jan 20, 2025
1 parent 53c6c5b commit 9b09123
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.transformuk.hee.tis.tcs.api.dto.CurriculumMembershipDTO;
import com.transformuk.hee.tis.tcs.api.dto.validation.Create;
import com.transformuk.hee.tis.tcs.api.dto.validation.Update;
import com.transformuk.hee.tis.tcs.service.api.util.HeaderUtil;
import com.transformuk.hee.tis.tcs.service.api.validation.CurriculumMembershipValidator;
import com.transformuk.hee.tis.tcs.service.service.CurriculumMembershipService;
Expand Down Expand Up @@ -75,16 +74,15 @@ public ResponseEntity<CurriculumMembershipDTO> createCurriculumMembership(
@PatchMapping("/curriculum-memberships")
@PreAuthorize("hasPermission('tis:people::person:', 'Update')")
public ResponseEntity<CurriculumMembershipDTO> patchCurriculumMembership(
@RequestBody @Validated(Update.class) CurriculumMembershipDTO curriculumMembershipDto)
throws MethodArgumentNotValidException, NoSuchMethodException {
@RequestBody CurriculumMembershipDTO curriculumMembershipDto) {
log.debug("REST request to patch CurriculumMembership : {}", curriculumMembershipDto);

if (curriculumMembershipDto.getId() == null) {
return ResponseEntity.badRequest()
.headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "id_missing",
"The ID is required for patching an existing curriculumMembership.")).body(null);
}
cmValidator.validate(curriculumMembershipDto);
cmValidator.validateForBulkUploadPatch(curriculumMembershipDto);
CurriculumMembershipDTO result = cmService.patch(curriculumMembershipDto);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -29,6 +31,7 @@
@Component
public class CurriculumMembershipValidator {

private static final Logger LOGGER = LoggerFactory.getLogger(CurriculumMembershipValidator.class);
protected static final String CURRICULUM_MEMBERSHIP_DTO_NAME = "CurriculumMembershipDTO";
protected static final String FIELD_CURRICULUM_ID = "Curriculum Id";
protected static final String FIELD_PM_UUID = "ProgrammeMembership Uuid";
Expand Down Expand Up @@ -93,6 +96,26 @@ public void validate(CurriculumMembershipDTO cmDto)
}
}

/**
* Validate a CurriculumMembershipDTO.
* for bulk update
* @param cmDto the curriculumMembership Dto to validate
*/
@Transactional(readOnly = true)
public void validateForBulkUploadPatch(CurriculumMembershipDTO cmDto) {
List<FieldError> fieldErrors = new ArrayList<>();
if (cmDto.getCurriculumEndDate() != null && cmDto.getCurriculumStartDate() != null) {
fieldErrors.addAll(checkCmWithPm(cmDto));
fieldErrors.addAll(checkCmDates(cmDto));
}

for (FieldError fieldError : fieldErrors) {
String errorMessage = fieldError.getDefaultMessage();
cmDto.addMessage(errorMessage);
LOGGER.debug("Validation error: {}", errorMessage);
}
}

// Checks for existing CMs that are duplicates of the assessment in the DTO.
private List<FieldError> checkDuplicate(CurriculumMembershipDTO cmDto) {
List<FieldError> fieldErrors = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,57 @@ void shouldNotThrowExceptionsWhenValidationPasses() {
// When.
assertDoesNotThrow(() -> cmValidator.validate(dto));
}

@Test
void shouldNotHaveAnyErrorIfCmStartAndEndDateIsNull() {
CurriculumMembershipDTO dto = createDto(CURRICULUM_ID, PM_UUID, null, null);

ProgrammeMembership pm = new ProgrammeMembership();
pm.setProgrammeStartDate(START_DATE_1);
pm.setProgrammeEndDate(END_DATE_1);

Programme programme = new Programme();
programme.setId(PROGRAMME_ID);

Curriculum curriculum1 = new Curriculum();
curriculum1.setId(1L);
curriculum1.setStatus(Status.CURRENT);
ProgrammeCurriculum pc1 = new ProgrammeCurriculum();
pc1.setCurriculum(curriculum1);

programme.getCurricula().addAll(Lists.newArrayList(pc1));
pm.setProgramme(programme);

cmValidator.validateForBulkUploadPatch(dto);
assertThat("should contain 0 error", dto.getMessageList().size(), is(0));
}

@Test
void shouldThrowExceptionsWhenCmDatesNotValid_ForBulk() {
CurriculumMembershipDTO dto = createDto(CURRICULUM_ID, PM_UUID, END_DATE_2, START_DATE_2);

ProgrammeMembership pm = new ProgrammeMembership();
pm.setProgrammeStartDate(START_DATE_1);
pm.setProgrammeEndDate(END_DATE_1);

Programme programme = new Programme();
programme.setId(PROGRAMME_ID);

Curriculum curriculum1 = new Curriculum();
curriculum1.setId(1L);
curriculum1.setStatus(Status.CURRENT);
ProgrammeCurriculum pc1 = new ProgrammeCurriculum();
pc1.setCurriculum(curriculum1);

programme.getCurricula().addAll(Lists.newArrayList(pc1));
pm.setProgramme(programme);

when(pmRepository.findByUuid(PM_UUID)).thenReturn(Optional.of(pm));

cmValidator.validateForBulkUploadPatch(dto);

assertThat("Unexpected error count.", dto.getMessageList().size(), is(1));
assertThat("Validation error", dto.getMessageList().get(0),
is("Curriculum membership start date must not be later than end date."));
}
}

0 comments on commit 9b09123

Please sign in to comment.