Skip to content

Commit

Permalink
Merge pull request #327 from bcgov/feature/phys
Browse files Browse the repository at this point in the history
Validation to ensure we're not creating physical addresses on offshore
  • Loading branch information
arcshiftsolutions authored Jun 6, 2024
2 parents 9505ad0 + 9299104 commit c1e0c25
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class EdxSagaController implements EdxSagaEndpoint {
@Getter(AccessLevel.PRIVATE)
private final EdxUserPayloadValidator edxUserPayLoadValidator;

@Getter(AccessLevel.PRIVATE)
private final CreateSchoolSagaPayloadValidator createSchoolSagaPayloadValidator;

@Getter(AccessLevel.PRIVATE)
private final EdxActivationCodeSagaDataPayloadValidator edxActivationCodeSagaDataPayLoadValidator;

Expand All @@ -61,10 +64,11 @@ public class EdxSagaController implements EdxSagaEndpoint {

private static final SagaDataMapper SAGA_DATA_MAPPER = SagaDataMapper.mapper;

public EdxSagaController(EdxActivationCodeSagaDataPayloadValidator edxActivationCodeSagaDataPayLoadValidator, SagaService sagaService, List<Orchestrator> orchestrators, SecureExchangePayloadValidator secureExchangePayloadValidator, SecureExchangeCommentSagaValidator secureExchangeCommentSagaValidator, CreateSecureExchangeSagaPayloadValidator createSecureExchangeSagaPayloadValidator, EdxUserPayloadValidator edxUserPayLoadValidator, EdxFileOnboardingService edxFileOnboardingService) {
public EdxSagaController(EdxActivationCodeSagaDataPayloadValidator edxActivationCodeSagaDataPayLoadValidator, SagaService sagaService, List<Orchestrator> orchestrators, CreateSchoolSagaPayloadValidator createSchoolSagaPayloadValidator, SecureExchangePayloadValidator secureExchangePayloadValidator, SecureExchangeCommentSagaValidator secureExchangeCommentSagaValidator, CreateSecureExchangeSagaPayloadValidator createSecureExchangeSagaPayloadValidator, EdxUserPayloadValidator edxUserPayLoadValidator, EdxFileOnboardingService edxFileOnboardingService) {
this.edxActivationCodeSagaDataPayLoadValidator = edxActivationCodeSagaDataPayLoadValidator;
this.sagaService = sagaService;
this.secureExchangePayloadValidator = secureExchangePayloadValidator;
this.createSchoolSagaPayloadValidator = createSchoolSagaPayloadValidator;
this.secureExchangePayloadValidator = secureExchangePayloadValidator;
this.secureExchangeCommentSagaValidator = secureExchangeCommentSagaValidator;
this.createSecureExchangeSagaPayloadValidator = createSecureExchangeSagaPayloadValidator;
this.edxUserPayLoadValidator = edxUserPayLoadValidator;
Expand Down Expand Up @@ -122,6 +126,7 @@ public ResponseEntity<String> createSchool(CreateSchoolSagaData sagaData) {
if (edxUser != null) {
validatePayload(() -> getEdxUserPayLoadValidator().validateEdxUserPayload(edxUser, true));
}
validatePayload(() -> getCreateSchoolSagaPayloadValidator().validateCreateSchoolSagaPayload(sagaData));
return this.processNewSchoolSaga(CREATE_NEW_SCHOOL_SAGA, sagaData);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ca.bc.gov.educ.api.edx.validator;

import ca.bc.gov.educ.api.edx.struct.v1.CreateSchoolSagaData;
import ca.bc.gov.educ.api.edx.struct.v1.School;
import org.springframework.stereotype.Component;
import org.springframework.validation.FieldError;

import java.util.ArrayList;
import java.util.List;

@Component
public class CreateSchoolSagaPayloadValidator {
public List<FieldError> validateCreateSchoolSagaPayload(CreateSchoolSagaData sagaData) {
List<FieldError> apiValidationErrors = new ArrayList<>();

School school = sagaData.getSchool();

if(school.getSchoolCategoryCode().equalsIgnoreCase("OFFSHORE") && school.getAddresses().stream().anyMatch(schoolAddress -> schoolAddress.getAddressTypeCode().equalsIgnoreCase("PHYSICAL"))){
String message = "Offshore schools cannot have a physical address.";
apiValidationErrors.add(createFieldError("addresses", null, message));
}

return apiValidationErrors;
}

private FieldError createFieldError(String fieldName, Object rejectedValue, String message) {
return new FieldError("EdxUser", fieldName, rejectedValue, false, null, null, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,28 @@ void testCreateSchool_GivenAValidPayload_ShouldReturnStatusAcceptedRequest() thr
.andDo(print()).andExpect(status().isAccepted());
}

@Test
void testCreateSchool_GivenInvalidPayload_ShouldReturnStatusBadRequest() throws Exception {
School school = createDummySchool();
school.setSchoolCategoryCode("OFFSHORE");
var addresses = new ArrayList<SchoolAddress>();
addresses.add(createSchoolAddress(school.getSchoolId(),"MAILING"));
addresses.add(createSchoolAddress(school.getSchoolId(),"PHYSICAL"));
school.setAddresses(addresses);
EdxUser user = createDummyUser();

CreateSchoolSagaData sagaData = new CreateSchoolSagaData();
sagaData.setSchool(school);
sagaData.setInitialEdxUser(user);

this.mockMvc.perform(post(URL.BASE_URL_SECURE_EXCHANGE + "/create-school-saga")
.contentType(MediaType.APPLICATION_JSON)
.content(getJsonString(sagaData))
.accept(MediaType.APPLICATION_JSON)
.with(jwt().jwt(jwt -> jwt.claim("scope", "CREATE_SCHOOL_SAGA"))))
.andDo(print()).andExpect(status().isBadRequest());
}

@Test
void testCreateSchool_GivenInvalidEdxUser_ShouldReturnBadRequest() throws Exception {
School school = createDummySchool();
Expand Down Expand Up @@ -1014,6 +1036,14 @@ private EdxUser createDummyUser() {
return user;
}

private SchoolAddress createSchoolAddress(String schoolID, String addressTypeCode) {
SchoolAddress schoolAddress = new SchoolAddress();
schoolAddress.setSchoolId(schoolID);
schoolAddress.setAddressTypeCode(addressTypeCode);
schoolAddress.setCreateUser("TEST");
schoolAddress.setUpdateUser("TEST");
return schoolAddress;
}
private SchoolGrade createSchoolGrade() {
SchoolGrade schoolGrade = new SchoolGrade();
schoolGrade.setSchoolGradeCode("01");
Expand Down

0 comments on commit c1e0c25

Please sign in to comment.