diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClaimsSchemaValidator.java b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClaimsSchemaValidator.java index 60c70f73e..bfc01f35a 100644 --- a/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClaimsSchemaValidator.java +++ b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClaimsSchemaValidator.java @@ -14,12 +14,11 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; +import javax.annotation.PostConstruct; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import java.io.*; -import java.nio.charset.StandardCharsets; import java.util.Set; -import java.util.stream.Collectors; @Slf4j @@ -29,7 +28,7 @@ public class ClaimsSchemaValidator implements ConstraintValidator errors = null; try { JsonNode jsonNode = objectMapper.valueToTree(claims); - errors = getCachedSchema().validate(jsonNode); + errors = schema.validate(jsonNode); if(errors.isEmpty())return true; } catch (Exception e) { log.error("Error validating claims schema", e); @@ -52,18 +57,6 @@ public boolean isValid(ClaimsV2 claims, ConstraintValidatorContext context) { return false; } - private JsonSchema getCachedSchema() throws EsignetException { - if(cachedSchema!=null ) return cachedSchema; - synchronized (this) { - if (cachedSchema == null) { - InputStream schemaResponse = getResource(schemaUrl); - JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); - cachedSchema = jsonSchemaFactory.getSchema(schemaResponse); - } - } - return cachedSchema; - } - private InputStream getResource(String url) { try{ Resource resource = resourceLoader.getResource(url); diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientAdditionalConfigValidator.java b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientAdditionalConfigValidator.java index 3139725e5..441894e94 100644 --- a/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientAdditionalConfigValidator.java +++ b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientAdditionalConfigValidator.java @@ -13,6 +13,7 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; +import javax.annotation.PostConstruct; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import java.io.IOException; @@ -27,7 +28,7 @@ public class ClientAdditionalConfigValidator implements @Value("${mosip.esignet.additional-config.schema.url}") private String schemaUrl; - private volatile JsonSchema cachedSchema; + private JsonSchema schema; @Autowired private ObjectMapper objectMapper; @@ -35,6 +36,13 @@ public class ClientAdditionalConfigValidator implements @Autowired private ResourceLoader resourceLoader; + @PostConstruct + public void initSchema() { + InputStream schemaResponse = getResource(schemaUrl); + JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); + schema = jsonSchemaFactory.getSchema(schemaResponse); + } + @Override public void initialize(ClientAdditionalConfig constraintAnnotation) { ConstraintValidator.super.initialize(constraintAnnotation); @@ -48,7 +56,7 @@ public boolean isValid(Map additionalConfig, ConstraintValidator Set errors = null; try { JsonNode jsonNode = objectMapper.valueToTree(additionalConfig); - errors = getCachedSchema().validate(jsonNode); + errors = schema.validate(jsonNode); if (errors.isEmpty()) return true; } catch (Exception e) { log.error("Error validating additional_config schema: ", e); @@ -57,18 +65,6 @@ public boolean isValid(Map additionalConfig, ConstraintValidator return false; } - private JsonSchema getCachedSchema() throws EsignetException { - if(cachedSchema!=null ) return cachedSchema; - synchronized (this) { - if (cachedSchema == null) { - InputStream schemaResponse = getResource(schemaUrl); - JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); - cachedSchema = jsonSchemaFactory.getSchema(schemaResponse); - } - } - return cachedSchema; - } - private InputStream getResource(String url) { try { Resource resource = resourceLoader.getResource(url); diff --git a/esignet-core/src/test/java/io/mosip/esignet/core/ValidatorTest.java b/esignet-core/src/test/java/io/mosip/esignet/core/ValidatorTest.java index 5ca5da8b5..402c8215c 100644 --- a/esignet-core/src/test/java/io/mosip/esignet/core/ValidatorTest.java +++ b/esignet-core/src/test/java/io/mosip/esignet/core/ValidatorTest.java @@ -79,6 +79,16 @@ public void setup() throws EsignetException { discoveryMap.put("claims_supported", Arrays.asList("name", "gender", "address")); when(authenticationContextClassRefUtil.getSupportedACRValues()).thenReturn(mockACRs); when(authenticator.isSupportedOtpChannel("email")).thenReturn(true); + + ReflectionTestUtils.setField(claimSchemaValidator,"resourceLoader",resourceLoader); + ReflectionTestUtils.setField(claimSchemaValidator,"objectMapper",mapper); + ReflectionTestUtils.setField(claimSchemaValidator,"schemaUrl","classpath:/verified_claims_request_schema_test.json"); + claimSchemaValidator.initSchema(); + + ReflectionTestUtils.setField(clientAdditionalConfigValidator, "resourceLoader", resourceLoader); + ReflectionTestUtils.setField(clientAdditionalConfigValidator, "objectMapper", mapper); + ReflectionTestUtils.setField(clientAdditionalConfigValidator, "schemaUrl", "classpath:additional_config_request_schema.json"); + clientAdditionalConfigValidator.initSchema(); } // ============================ Display Validator ========================= @@ -677,10 +687,6 @@ public void test_ClientNameLangValidator_WithInValidDetail_thenFail() { @Test public void claimSchemaValidator_withValidDetails_thenPass() throws IOException { - ReflectionTestUtils.setField(claimSchemaValidator, "resourceLoader", resourceLoader); - ReflectionTestUtils.setField(claimSchemaValidator, "objectMapper", mapper); - ReflectionTestUtils.setField(claimSchemaValidator, "schemaUrl", "classpath:/verified_claims_request_schema_test.json"); - String address = "{\"essential\":true}"; String verifiedClaims = "[{\"verification\":{\"trust_framework\":{\"value\":\"income-tax\"}},\"claims\":{\"name\":null,\"email\":{\"essential\":true}}},{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"birthdate\":{\"essential\":true},\"address\":null}},{\"verification\":{\"trust_framework\":{\"value\":\"kaif\"}},\"claims\":{\"gender\":{\"essential\":true},\"email\":{\"essential\":true}}}]"; @@ -705,10 +711,6 @@ public void claimSchemaValidator_withValidDetails_thenPass() throws IOException @Test public void claimSchemaValidator_withTrustFrameWorkAsNull_thenFail() throws IOException { - ReflectionTestUtils.setField(claimSchemaValidator, "resourceLoader", resourceLoader); - ReflectionTestUtils.setField(claimSchemaValidator, "objectMapper", mapper); - ReflectionTestUtils.setField(claimSchemaValidator, "schemaUrl", "classpath:/verified_claims_request_schema_test.json"); - String address = "{\"essential\":true}"; String verifiedClaims = "[{\"verification\":{\"trust_framework\":{\"value\":null}},\"claims\":{\"name\":null,\"email\":{\"essential\":true}}},{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"birthdate\":{\"essential\":true},\"address\":null}},{\"verification\":{\"trust_framework\":{\"value\":\"kaif\"}},\"claims\":{\"gender\":{\"essential\":true},\"email\":{\"essential\":true}}}]"; @@ -733,10 +735,6 @@ public void claimSchemaValidator_withTrustFrameWorkAsNull_thenFail() throws IOEx @Test public void claimSchemaValidator_withEssentialAsNonBoolean_thenFail() throws IOException { - ReflectionTestUtils.setField(claimSchemaValidator, "resourceLoader", resourceLoader); - ReflectionTestUtils.setField(claimSchemaValidator, "objectMapper", mapper); - ReflectionTestUtils.setField(claimSchemaValidator, "schemaUrl", "classpath:/verified_claims_request_schema_test.json"); - String address = "{\"essential\":true}"; String verifiedClaims = "[{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"name\":null,\"email\":{\"essential\":1}}},{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"birthdate\":{\"essential\":true},\"address\":null}},{\"verification\":{\"trust_framework\":{\"value\":\"kaif\"}},\"claims\":{\"gender\":{\"essential\":true},\"email\":{\"essential\":true}}}]"; @@ -761,10 +759,6 @@ public void claimSchemaValidator_withEssentialAsNonBoolean_thenFail() throws IOE @Test public void test_ClaimSchemaValidator_withInvalidValue_thenFail() throws IOException { - ReflectionTestUtils.setField(claimSchemaValidator, "resourceLoader", resourceLoader); - ReflectionTestUtils.setField(claimSchemaValidator, "objectMapper", mapper); - ReflectionTestUtils.setField(claimSchemaValidator, "schemaUrl", "classpath:/verified_claims_request_schema_test.json"); - String address = "{\"essential\":true}"; String verifiedClaims = "[{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"name\":null,\"email\":{\"essential\":1}}},{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"birthdate\":{\"essential\":true},\"address\":null}},{\"verification\":{\"trust_framework\":{\"value\":\"kf\"}},\"claims\":{\"gender\":{\"essential\":true},\"email\":{\"essential\":true}}}]"; @@ -839,18 +833,12 @@ public static List> getInvalidAdditionalConfigs() { @Test public void test_ClientAdditionalConfigValidator_withValidValue_thenPass() { - ReflectionTestUtils.setField(clientAdditionalConfigValidator, "resourceLoader", resourceLoader); - ReflectionTestUtils.setField(clientAdditionalConfigValidator, "objectMapper", mapper); - ReflectionTestUtils.setField(clientAdditionalConfigValidator, "schemaUrl", "classpath:additional_config_request_schema.json"); Map validAdditionalConfig = getValidAdditionalConfig(); Assert.assertTrue(clientAdditionalConfigValidator.isValid(validAdditionalConfig, null)); } @Test public void test_ClientAdditionalConfigValidator_withInvalidValue_thenFail() { - ReflectionTestUtils.setField(clientAdditionalConfigValidator, "resourceLoader", resourceLoader); - ReflectionTestUtils.setField(clientAdditionalConfigValidator, "objectMapper", mapper); - ReflectionTestUtils.setField(clientAdditionalConfigValidator, "schemaUrl", "classpath:additional_config_request_schema.json"); for (Map additionalConfig : getInvalidAdditionalConfigs()) { Assert.assertFalse(clientAdditionalConfigValidator.isValid(additionalConfig, null)); }