diff --git a/mock-identity-system/src/main/java/io/mosip/esignet/mock/identitysystem/validator/IdentitySchemaValidator.java b/mock-identity-system/src/main/java/io/mosip/esignet/mock/identitysystem/validator/IdentitySchemaValidator.java index 2c4888d0..132383e1 100644 --- a/mock-identity-system/src/main/java/io/mosip/esignet/mock/identitysystem/validator/IdentitySchemaValidator.java +++ b/mock-identity-system/src/main/java/io/mosip/esignet/mock/identitysystem/validator/IdentitySchemaValidator.java @@ -11,13 +11,13 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import java.io.IOException; import java.io.InputStream; import java.util.Set; +import java.util.stream.Collectors; @Component @Slf4j @@ -56,35 +56,37 @@ public boolean isValid(Object object, ConstraintValidatorContext context) { } IdentityData identityData=(IdentityData) requestObject; JsonNode identityJsonNode = objectMapper.valueToTree(identityData); - Set errors = getSchema().validate(identityJsonNode); - boolean isValid=true; - String errorCode=""; - if(!isCreate){ - for(ValidationMessage validationMessage: errors){ - String field=validationMessage. - getInstanceLocation().getName(0); - // Ignore validation errors with code 1029 (null value) for exempted fields when validating updateIdentity - if(!validationMessage.getCode().equals("1029") || !nonMandatoryFieldsOnUpdate.contains(field)){ - errorCode="invalid_"+field.toLowerCase(); - isValid=false; - break; - } - } - }else{ - isValid=errors.isEmpty(); - } - if (!isValid) { - if(StringUtils.isEmpty(errorCode)) - errorCode="invalid_"+errors.iterator().next().getInstanceLocation().getName(0).toLowerCase(); - context.disableDefaultConstraintViolation(); - context.buildConstraintViolationWithTemplate(errorCode) - .addConstraintViolation(); - log.error("Validation failed for IdentityData: {}", errors); + Set validationErrors = validateIdentityData(identityJsonNode); + + // Handle validation errors + if (!validationErrors.isEmpty()) { + addValidationErrorCode(validationErrors,context); return false; } return true; } + private Set validateIdentityData(JsonNode identityJsonNode) { + Set errors = getSchema().validate(identityJsonNode); + // If not a create operation, filter out specific errors + if (!isCreate) { + // Ignore validation errors with code 1029 (null value) and for exempted fields when validating updateIdentity + errors = errors.stream() + .filter(error -> !error.getCode().equals("1029") || + !nonMandatoryFieldsOnUpdate.contains(error. + getInstanceLocation().getName(0))) + .collect(Collectors.toSet()); + } + return errors; + } + + private void addValidationErrorCode(Set errors, ConstraintValidatorContext context) { + context.disableDefaultConstraintViolation(); + errors.forEach(error->context. + buildConstraintViolationWithTemplate("invalid_"+error.getInstanceLocation().getName(0).toLowerCase()) + .addConstraintViolation()); + } + private JsonSchema getSchema() { if(schema !=null ) return schema; synchronized (this) { diff --git a/mock-identity-system/src/main/resources/application-local.properties b/mock-identity-system/src/main/resources/application-local.properties index e6dfac3c..499cdf19 100644 --- a/mock-identity-system/src/main/resources/application-local.properties +++ b/mock-identity-system/src/main/resources/application-local.properties @@ -1,6 +1,6 @@ ##----------------------------------------- Database properties -------------------------------------------------------- -spring.datasource.url=jdbc:postgresql://localhost:5432/mosip_mockidentitysystem?currentSchema=mockidentitysystem +spring.datasource.url=jdbc:postgresql://localhost:5455/mosip_mockidentitysystem?currentSchema=mockidentitysystem spring.datasource.username=postgres spring.datasource.password=postgres spring.datasource.driver-class-name=org.postgresql.Driver diff --git a/mock-identity-system/src/main/resources/bootstrap.properties b/mock-identity-system/src/main/resources/bootstrap.properties index cc80ac20..f5ea042c 100644 --- a/mock-identity-system/src/main/resources/bootstrap.properties +++ b/mock-identity-system/src/main/resources/bootstrap.properties @@ -1,6 +1,6 @@ spring.application.name=mock-identity-system #spring.cloud.config.uri=http://localhost:8888 -spring.profiles.active=local +spring.profiles.active=default,local #spring.cloud.config.label=master spring.cloud.config.name=mock-identity-system spring.main.allow-bean-definition-overriding=true diff --git a/mock-identity-system/src/test/java/io/mosip/esignet/mock/identitysystem/controller/IdentityControllerTest.java b/mock-identity-system/src/test/java/io/mosip/esignet/mock/identitysystem/controller/IdentityControllerTest.java index 00a46c6a..56896d18 100644 --- a/mock-identity-system/src/test/java/io/mosip/esignet/mock/identitysystem/controller/IdentityControllerTest.java +++ b/mock-identity-system/src/test/java/io/mosip/esignet/mock/identitysystem/controller/IdentityControllerTest.java @@ -132,6 +132,22 @@ public void createIdentity_withInvalidIdentity_returnErrorResponse() throws Exce .andExpect(jsonPath("$.errors[0].errorCode").value("invalid_individualid")); } + @Test + public void createIdentity_withInvalidNameAndLocale_returnErrorResponse() throws Exception { + RequestWrapper requestWrapper = new RequestWrapper(); + ZonedDateTime requestTime = ZonedDateTime.now(ZoneOffset.UTC); + requestWrapper.setRequestTime(requestTime.format(DateTimeFormatter.ofPattern(UTC_DATETIME_PATTERN))); + identityRequest.setName(null); + identityRequest.setLocale(null); + requestWrapper.setRequest(identityRequest); + + Mockito.doNothing().when(identityService).addIdentity(identityRequest); + mockMvc.perform(post("/identity").content(objectMapper.writeValueAsString(requestWrapper)) + .contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors.size()").value(2)); + } + @Test public void createIdentity_withInvalidFullName_returnErrorResponse() throws Exception { RequestWrapper requestWrapper = new RequestWrapper();