Skip to content

Commit

Permalink
EAC-9 - adds more UT
Browse files Browse the repository at this point in the history
  • Loading branch information
mightycox committed Oct 8, 2024
1 parent 7c652f5 commit cc95fb8
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ private URL(){
}

public static final String BASE_URL_STUDENT="/api/v1/eas/student";
public static final String PAGINATED="/paginated";

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

Expand All @@ -26,18 +27,19 @@ public interface AssessmentStudentEndpoint {
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "NOT FOUND"), @ApiResponse(responseCode = "400", description = "BAD REQUEST"), @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR.")})
AssessmentStudent updateStudent(@Validated @RequestBody AssessmentStudent assessmentStudent, @PathVariable UUID assessmentStudentID);

@PostMapping("/{assessmentStudentID}")
@PostMapping
@PreAuthorize("hasAuthority('SCOPE_WRITE_EAS_STUDENT')")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "NOT FOUND"), @ApiResponse(responseCode = "400", description = "BAD REQUEST"), @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR.")})
AssessmentStudent createStudent(@PathVariable AssessmentStudent assessmentStudentID);
AssessmentStudent createStudent(@Validated @RequestBody AssessmentStudent assessmentStudent);

@DeleteMapping("/{assessmentStudentID}")
@PreAuthorize("hasAuthority('SCOPE_DELETE_EAS_STUDENT')")
@ApiResponses(value = {@ApiResponse(responseCode = "204", description = "NO CONTENT"), @ApiResponse(responseCode = "404", description = "NOT FOUND"), @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR.")})
ResponseEntity<Void> deleteStudent(@PathVariable UUID assessmentStudentID);

@GetMapping()
@GetMapping(URL.PAGINATED)
@PreAuthorize("hasAuthority('SCOPE_READ_EAS_STUDENT')")
@Transactional(readOnly = true)
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "400", description = "BAD REQUEST"), @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR.")})
CompletableFuture<Page<AssessmentStudent>> findAll(@RequestParam(name = "pageNumber", defaultValue = "0") Integer pageNumber,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
Expand All @@ -29,20 +31,13 @@
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class AssessmentStudentSearchService extends BaseSearchService {

@Getter
private final AssessmentStudentFilterSpecs studentFilterSpecs;
private final AssessmentStudentRepository repository;

/**
* Instantiates a new Student search service.
*
* @param studentFilterSpecs the student filter specs
*/
public AssessmentStudentSearchService(AssessmentStudentFilterSpecs studentFilterSpecs, AssessmentStudentRepository repository) {
this.studentFilterSpecs = studentFilterSpecs;
this.repository = repository;
}

@Transactional(propagation = Propagation.SUPPORTS)
public CompletableFuture<Page<AssessmentStudentEntity>> findAll(Specification<AssessmentStudentEntity> specs, final Integer pageNumber, final Integer pageSize, final List<Sort.Order> sorts) {
log.trace("In find all query: {}", specs);
Expand All @@ -69,7 +64,7 @@ public Specification<AssessmentStudentEntity> setSpecificationAndSortCriteria(St
});
int i = 0;
for (var search : searches) {
studentSpecs = getSpecifications(studentSpecs, i, search, this.studentFilterSpecs, AssessmentStudentEntity.class);
studentSpecs = getSpecifications(studentSpecs, i, search, this.getStudentFilterSpecs(), AssessmentStudentEntity.class);
i++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class AssessmentStudent extends BaseRequest implements Serializable {
@Size(max = 12)
private String localID;

@Size(max = 1)
private Boolean isElectronicExam;

@Size(max = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ public class AssessmentStudentValidator {
public List<FieldError> validatePayload(AssessmentStudent assessmentStudent, boolean isCreateOperation) {
final List<FieldError> apiValidationErrors = new ArrayList<>();

if(isCreateOperation && assessmentStudent.getAssessmentStudentID() != null) {
if(isCreateOperation && StringUtils.isNotEmpty(assessmentStudent.getAssessmentStudentID())) {
apiValidationErrors.add(ValidationUtil.createFieldError(ASSESSMENT_STUDENT, "assessmentStudentID", assessmentStudent.getAssessmentStudentID(), "assessmentStudentID should be null for post operation."));
}
if (!isCreateOperation && assessmentStudent.getAssessmentStudentID() == null) {
if (!isCreateOperation && StringUtils.isEmpty(assessmentStudent.getAssessmentStudentID())) {
apiValidationErrors.add(ValidationUtil.createFieldError(ASSESSMENT_STUDENT, "assessmentStudentID", null, "assessmentStudentID cannot be null for put operation."));
}
if (StringUtils.isNotEmpty(assessmentStudent.getPen()) && !PenUtil.validCheckDigit(assessmentStudent.getPen())) {
apiValidationErrors.add(ValidationUtil.createFieldError(ASSESSMENT_STUDENT, "studentPen", assessmentStudent.getPen(), "Invalid Student Pen."));
apiValidationErrors.add(ValidationUtil.createFieldError(ASSESSMENT_STUDENT, "pen", assessmentStudent.getPen(), "Invalid Student Pen."));
}
if (!EnumUtils.isValidEnum(AssessmentTypeCodes.class, assessmentStudent.getAssessmentTypeCode())) {
apiValidationErrors.add(ValidationUtil.createFieldError(ASSESSMENT_STUDENT, "assessmentTypeCode", assessmentStudent.getAssessmentTypeCode(), "Invalid assessment type code."));
}
if (!EnumUtils.isValidEnum(ProvincialSpecialCaseCodes.class, assessmentStudent.getProvincialSpecialCaseCode())) {
if (assessmentStudent.getProvincialSpecialCaseCode() != null && !EnumUtils.isValidEnum(ProvincialSpecialCaseCodes.class, assessmentStudent.getProvincialSpecialCaseCode())) {
apiValidationErrors.add(ValidationUtil.createFieldError(ASSESSMENT_STUDENT, "provincialSpecialCaseCode", assessmentStudent.getProvincialSpecialCaseCode(), "Invalid provincial special case code."));
}
if (!EnumUtils.isValidEnum(CourseStatusCodes.class, assessmentStudent.getCourseStatusCode())) {
if (assessmentStudent.getCourseStatusCode() != null && !EnumUtils.isValidEnum(CourseStatusCodes.class, assessmentStudent.getCourseStatusCode())) {
apiValidationErrors.add(ValidationUtil.createFieldError(ASSESSMENT_STUDENT, "courseStatusCode", assessmentStudent.getCourseStatusCode(), "Invalid course status code."));
}
return apiValidationErrors;
Expand Down
48 changes: 48 additions & 0 deletions api/src/test/java/ca/bc/gov/educ/eas/api/BaseEasAPITest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package ca.bc.gov.educ.eas.api;

import ca.bc.gov.educ.eas.api.constants.v1.AssessmentTypeCodes;
import ca.bc.gov.educ.eas.api.constants.v1.StatusCodes;
import ca.bc.gov.educ.eas.api.model.v1.AssessmentStudentEntity;
import ca.bc.gov.educ.eas.api.model.v1.SessionEntity;
import ca.bc.gov.educ.eas.api.struct.external.institute.v1.*;
import ca.bc.gov.educ.eas.api.struct.v1.AssessmentStudent;
import ca.bc.gov.educ.eas.api.struct.v1.Session;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand Down Expand Up @@ -52,6 +58,38 @@ public SessionEntity createMockSessionEntity() {
.statusCode(StatusCodes.OPEN.getCode())
.activeFromDate(currentDate.minusMonths(2))
.activeUntilDate(currentDate.plusMonths(2))
.createUser("ABC")
.createDate(LocalDateTime.now())
.updateUser("ABC")
.updateDate(LocalDateTime.now())
.build();
}

public AssessmentStudent createMockStudent() {
return AssessmentStudent.builder()
.assessmentStudentID(UUID.randomUUID().toString())
.sessionID(UUID.randomUUID().toString())
.assessmentTypeCode(AssessmentTypeCodes.LTP10.getCode())
.schoolID(UUID.randomUUID().toString())
.studentID(UUID.randomUUID().toString())
.pen("120164447")
.localID("123")
.build();
}

public AssessmentStudentEntity createMockStudentEntity(SessionEntity sessionEntity) {
return AssessmentStudentEntity.builder()
.assessmentStudentID(UUID.randomUUID())
.sessionEntity(sessionEntity)
.assessmentTypeCode(AssessmentTypeCodes.LTP10.getCode())
.schoolID(UUID.randomUUID())
.studentID(UUID.randomUUID())
.pen("120164447")
.localID("123")
.createUser("ABC")
.createDate(LocalDateTime.now())
.updateUser("ABC")
.updateDate(LocalDateTime.now())
.build();
}

Expand Down Expand Up @@ -130,4 +168,14 @@ public IndependentAuthority createMockAuthority() {
independentAuthority.setPhoneNumber("123456789");
return independentAuthority;
}

public static String asJsonString(final Object obj) {
try {
ObjectMapper om = new ObjectMapper();
om.registerModule(new JavaTimeModule()).configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return om.writeValueAsString(obj);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit cc95fb8

Please sign in to comment.