-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ES-1887] Added additionalConfig validator and new client-mgmt apis (#…
…1016) * Added additionalConfig validator and new client-mgmt apis Signed-off-by: Sachin Rana <[email protected]> * handled review comments Signed-off-by: Sachin Rana <[email protected]> * resolved review comments Signed-off-by: Sachin Rana <[email protected]> * resolved review comments Signed-off-by: Sachin Rana <[email protected]> * worked on testcases, code improvement and db script changes Signed-off-by: Sachin Rana <[email protected]> * removed redundant constraint Signed-off-by: Sachin Rana <[email protected]> --------- Signed-off-by: Sachin Rana <[email protected]> Signed-off-by: sacrana0 <[email protected]>
- Loading branch information
Showing
20 changed files
with
1,509 additions
and
790 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientAdditionalConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.mosip.esignet.core.validator; | ||
|
||
import io.mosip.esignet.core.constants.ErrorConstants; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({FIELD}) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = ClientAdditionalConfigValidator.class) | ||
@Documented | ||
public @interface ClientAdditionalConfig { | ||
String message() default ErrorConstants.INVALID_ADDITIONAL_CONFIG; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
81 changes: 81 additions & 0 deletions
81
...t-core/src/main/java/io/mosip/esignet/core/validator/ClientAdditionalConfigValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.mosip.esignet.core.validator; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.networknt.schema.JsonSchema; | ||
import com.networknt.schema.JsonSchemaFactory; | ||
import com.networknt.schema.SpecVersion; | ||
import com.networknt.schema.ValidationMessage; | ||
import io.mosip.esignet.core.exception.EsignetException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@Slf4j | ||
public class ClientAdditionalConfigValidator implements | ||
ConstraintValidator<ClientAdditionalConfig, Map<String, Object>> { | ||
|
||
@Value("${mosip.esignet.additional-config.schema.url}") | ||
private String schemaUrl; | ||
|
||
private volatile JsonSchema cachedSchema; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
private ResourceLoader resourceLoader; | ||
|
||
@Override | ||
public void initialize(ClientAdditionalConfig constraintAnnotation) { | ||
ConstraintValidator.super.initialize(constraintAnnotation); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Map<String, Object> additionalConfig, ConstraintValidatorContext context) { | ||
if (additionalConfig == null) { | ||
return false; | ||
} | ||
Set<ValidationMessage> errors = null; | ||
try { | ||
JsonNode jsonNode = objectMapper.valueToTree(additionalConfig); | ||
errors = getCachedSchema().validate(jsonNode); | ||
if (errors.isEmpty()) return true; | ||
} catch (Exception e) { | ||
log.error("Error validating additional_config schema: ", e); | ||
} | ||
log.error("Validation failed for additional_config ---> {}", errors); | ||
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); | ||
return resource.getInputStream(); | ||
} catch (IOException e) { | ||
log.error("Failed to parse data: {}", url, e); | ||
} | ||
throw new EsignetException("invalid_configuration"); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
esignet-core/src/test/java/io/mosip/esignet/core/ClientAdditionalConfigConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.mosip.esignet.core; | ||
|
||
import io.mosip.esignet.core.exception.InvalidClientException; | ||
import io.mosip.esignet.core.util.ClientAdditionalConfigConverter; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ClientAdditionalConfigConverterTest { | ||
|
||
private ClientAdditionalConfigConverter converter; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
converter = new ClientAdditionalConfigConverter(); | ||
} | ||
|
||
@Test | ||
public void convertToDatabaseColumn_NullMap_ReturnsNull() { | ||
Assertions.assertNull(converter.convertToDatabaseColumn(null)); | ||
} | ||
|
||
@Test | ||
public void convertToEntityAttribute_NullJson_ReturnsNull() { | ||
Assertions.assertNull(converter.convertToEntityAttribute(null)); | ||
} | ||
|
||
@Test | ||
public void convertToEntityAttribute_InvalidJson_ThrowsException() { | ||
String invalidJson = "{\"invalid: value}"; | ||
Assertions.assertThrows(InvalidClientException.class, () -> | ||
converter.convertToEntityAttribute(invalidJson) | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.