diff options
9 files changed, 97 insertions, 26 deletions
diff --git a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/model/SchemaReference.java b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/model/SchemaReference.java index 5da34430..4ff0944f 100644 --- a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/model/SchemaReference.java +++ b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/model/SchemaReference.java @@ -22,6 +22,9 @@ package org.onap.dcaegen2.services.sdk.services.external.schema.manager.model; import org.onap.dcaegen2.services.sdk.services.external.schema.manager.service.SchemaReferenceResolver; +/** + * A SchemaReference model contains information about schema URL which will be used to validate json content. + */ public class SchemaReference { public static final String URL_SEPARATOR = "#"; @@ -29,9 +32,12 @@ public class SchemaReference { private final String url; private final String internalReference; + /** + * Constructor + * @param schemaReferenceResolver to resolve schema reference + */ public SchemaReference(SchemaReferenceResolver schemaReferenceResolver) { - this.url = schemaReferenceResolver.resolveUrl(); - this.internalReference = schemaReferenceResolver.resolveInternalReference(); + this(schemaReferenceResolver.resolveUrl(), schemaReferenceResolver.resolveInternalReference()); } public SchemaReference(String url, String internalReference) { @@ -39,10 +45,16 @@ public class SchemaReference { this.internalReference = internalReference; } + /** + * @return url to schema reference + */ public String getUrl() { return url; } + /** + * @return internal schema reference path + */ public String getInternalReference() { return internalReference; } diff --git a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReader.java b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReader.java index e49266e3..0b10f574 100644 --- a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReader.java +++ b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReader.java @@ -28,17 +28,27 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +/** + * A FileReader is used to load a file content. + */ class FileReader { private static final Logger logger = LoggerFactory.getLogger(FileReader.class); private final String filename; - FileReader(String filename) { - this.filename = filename; + /** + * Constructor + * @param filePath path to file which will be read + */ + FileReader(String filePath) { + this.filename = filePath; } - String readFile() { + /** + * @return all file content + */ + String getContent() { String fileContent = ""; try { fileContent = getFileContent(); @@ -48,6 +58,9 @@ class FileReader { return fileContent; } + /** + * @return true if file exists; otherwise false + */ boolean doesFileExists() { return new File(filename).exists(); } @@ -59,4 +72,4 @@ class FileReader { private byte[] readBytes() throws IOException { return Files.readAllBytes(Paths.get(filename)); } -}
\ No newline at end of file +} diff --git a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceResolver.java b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceResolver.java index 0dd1b805..a4c23a4d 100644 --- a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceResolver.java +++ b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceResolver.java @@ -22,7 +22,9 @@ package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service; import org.onap.dcaegen2.services.sdk.services.external.schema.manager.model.SchemaReference; - +/** + * A SchemaReferenceResolver is used to resolve a schema reference which will be used to validate json data. + */ public class SchemaReferenceResolver { private static final String ROOT_REFERENCE = "/"; @@ -32,6 +34,10 @@ public class SchemaReferenceResolver { private final String schemaReference; + /** + * Constructor + * @param schemaReference is represented by an URL with optional internal reference path (for example: http://someExternalUrl/external#schemaName) + */ public SchemaReferenceResolver(String schemaReference) { this.schemaReference = schemaReference; } diff --git a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidator.java b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidator.java index 8d4e4bdb..57266b6b 100644 --- a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidator.java +++ b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidator.java @@ -31,18 +31,31 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; +/** + * A StndDefinedValidator is used to validate a JsonNode objects. + */ public class StndDefinedValidator { private static final Logger logger = LoggerFactory.getLogger(StndDefinedValidator.class); private final String schemaRefPath; private final String stndDefinedDataPath; private final ValidatorCache validatorCache; + /** + * Constructor + * + * @param schemaRefPath path to schema reference in json + * @param stndDefinedDataPath path to data in json which is described by schemaRefPath + * @param validatorCache contains cached validators + */ private StndDefinedValidator(String schemaRefPath, String stndDefinedDataPath, ValidatorCache validatorCache) { this.schemaRefPath = schemaRefPath; this.stndDefinedDataPath = stndDefinedDataPath; this.validatorCache = validatorCache; } + /** + * @return cached validators + */ ValidatorCache getValidatorCache() { return validatorCache; } @@ -71,6 +84,9 @@ public class StndDefinedValidator { return validationResult; } + /** + * An event validator builder. + */ public static final class ValidatorBuilder { public static final String DEFAULT_MAPPING_FILE_PATH = "etc/externalRepo/schema-map.json"; diff --git a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapper.java b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapper.java index 5edaf460..d5502230 100644 --- a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapper.java +++ b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapper.java @@ -23,19 +23,34 @@ package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service; import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException; import java.util.Map; - +/** + * An UrlMapper contains mapping between public urls received in events and schemas stored locally in the system. + * */ final class UrlMapper { private final Map<String, String> mappingsCache; + /** + * Constructor + * @param mappings contain mapping public urls to local schema file + */ UrlMapper(Map<String, String> mappings) { this.mappingsCache = Map.copyOf(mappings); } + /** + * @return cached mapping of public urls to local schemas + */ Map<String, String> getMappingsCache() { return Map.copyOf(mappingsCache); } + /** + * Map public url received in the event to url of local schema file + * @param publicUrl public url + * @return Urt to local schema file + * @throws NoLocalReferenceException when local schema file doesn't exist for selected public url + */ String mapToLocalUrl(String publicUrl) throws NoLocalReferenceException { String externalUrl = mappingsCache.get(publicUrl); if (externalUrl == null) { @@ -45,4 +60,4 @@ final class UrlMapper { } -}
\ No newline at end of file +} diff --git a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapperFactory.java b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapperFactory.java index 730bc17e..49c8b1fe 100644 --- a/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapperFactory.java +++ b/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapperFactory.java @@ -33,10 +33,19 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +/** + * An UrlMapperFactory is used to produced {@link UrlMapper}. + */ public class UrlMapperFactory { private static final Logger logger = LoggerFactory.getLogger(UrlMapperFactory.class); + /** + * Returns UrlMapper instance + * @param mappingFilePath path to file with mappings + * @param schemasPath path to local schemas repository + * @return {@link UrlMapper} + */ UrlMapper getUrlMapper(String mappingFilePath, String schemasPath) { Map<String, String> mappings = new HashMap<>(); try { @@ -56,7 +65,7 @@ public class UrlMapperFactory { FileReader fileReader = new FileReader(mappingFilePath); Map<String, String> mappings = new HashMap<>(); - for (JsonNode mapping : objectMapper.readTree(fileReader.readFile())) { + for (JsonNode mapping : objectMapper.readTree(fileReader.getContent())) { String localURL = mapping.get("localURL").asText(); if (isMappingValid(schemasPath, localURL)) { mappings.put(mapping.get("publicURL").asText(), localURL); @@ -73,7 +82,7 @@ public class UrlMapperFactory { } private boolean isFileValidSchema(String schemaRelativePath) throws IOException { - String schemaContent = new FileReader(schemaRelativePath).readFile(); + String schemaContent = new FileReader(schemaRelativePath).getContent(); return isNotEmpty(schemaContent, schemaRelativePath) && isYaml(schemaContent, schemaRelativePath); } diff --git a/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReaderTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReaderTest.java index 8f9f54c5..7154e620 100644 --- a/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReaderTest.java +++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReaderTest.java @@ -29,15 +29,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class FileReaderTest { public static final String TEST_RESOURCES = "src/main/test/resources/"; + public static final String DUMMY_FILE_PATH = "dummyFileName"; @Test void shouldReturnEmptyStringWhenFileNotFound() { //given String expectedContent = ""; - String fileName = "dummyFileName"; //when - String actualContent = new FileReader(fileName).readFile(); + String actualContent = new FileReader(DUMMY_FILE_PATH).getContent(); //then assertEquals(expectedContent, actualContent); @@ -49,10 +49,10 @@ class FileReaderTest { String expectedContent = "{\n" + " \"someObject\": \"dummyValue\"\n" + "}"; - String filename = TEST_RESOURCES + "file_with_one_line.json"; + String filePath = TEST_RESOURCES + "file_with_one_line.json"; //when - String actualContent = new FileReader(filename).readFile(); + String actualContent = new FileReader(filePath).getContent(); //then assertEquals(expectedContent, actualContent); @@ -61,7 +61,7 @@ class FileReaderTest { @Test void shouldReturnFalseWhenFileDoesNotExist() { //when - boolean doesFileExists = new FileReader("dummyFileName").doesFileExists(); + boolean doesFileExists = new FileReader(DUMMY_FILE_PATH).doesFileExists(); //then assertFalse(doesFileExists); @@ -75,4 +75,4 @@ class FileReaderTest { //then assertTrue(doesFileExists); } -}
\ No newline at end of file +} diff --git a/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorBuilderTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorBuilderTest.java index 5e21be57..560d9d26 100644 --- a/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorBuilderTest.java +++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorBuilderTest.java @@ -106,4 +106,4 @@ class StndDefinedValidatorBuilderTest { .getUrlMapper() .getMappingsCache(); } -}
\ No newline at end of file +} diff --git a/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorTest.java index f53b70c5..960f9107 100644 --- a/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorTest.java +++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorTest.java @@ -54,7 +54,7 @@ class StndDefinedValidatorTest { throws IOException { //given FileReader fileReader = new FileReader(VALID_EVENT_PATH); - JsonNode validEventNode = objectMapper.readTree(fileReader.readFile()); + JsonNode validEventNode = objectMapper.readTree(fileReader.getContent()); //when boolean validationResult = validator.validate(validEventNode); @@ -68,7 +68,7 @@ class StndDefinedValidatorTest { throws IOException { //given FileReader fileReader = new FileReader(INVALID_EVENT_PATH); - JsonNode invalidEventNode = objectMapper.readTree(fileReader.readFile()); + JsonNode invalidEventNode = objectMapper.readTree(fileReader.getContent()); //when boolean validationResult = validator.validate(invalidEventNode); @@ -82,7 +82,7 @@ class StndDefinedValidatorTest { throws IOException { //given FileReader fileReader = new FileReader(VALID_NO_HASH_EVENT_PATH); - JsonNode validEventNode = objectMapper.readTree(fileReader.readFile()); + JsonNode validEventNode = objectMapper.readTree(fileReader.getContent()); //when boolean validationResult = validator.validate(validEventNode); @@ -98,7 +98,7 @@ class StndDefinedValidatorTest { String noLocalResourceMappingFilePath = TEST_RESOURCES + "externalRepo/schema-map-no-local-resource.json"; StndDefinedValidator validator = getValidator(noLocalResourceMappingFilePath); FileReader fileReader = new FileReader(VALID_EVENT_PATH); - JsonNode validEventNode = objectMapper.readTree(fileReader.readFile()); + JsonNode validEventNode = objectMapper.readTree(fileReader.getContent()); //when //then @@ -112,7 +112,7 @@ class StndDefinedValidatorTest { String noLocalResourceMappingFilePath = TEST_RESOURCES + "externalRepo/schema-map-empty-content.json"; StndDefinedValidator validator = getValidator(noLocalResourceMappingFilePath); FileReader fileReader = new FileReader(VALID_EVENT_PATH); - JsonNode validEventNode = objectMapper.readTree(fileReader.readFile()); + JsonNode validEventNode = objectMapper.readTree(fileReader.getContent()); //when //then @@ -125,7 +125,7 @@ class StndDefinedValidatorTest { //given String noLocalResourceMappingFilePath = TEST_RESOURCES + "externalRepo/schema-map-incorrect-yaml-format.json"; StndDefinedValidator validator = getValidator(noLocalResourceMappingFilePath); - JsonNode validEventNode = objectMapper.readTree(new FileReader(VALID_EVENT_PATH).readFile()); + JsonNode validEventNode = objectMapper.readTree(new FileReader(VALID_EVENT_PATH).getContent()); //when //then @@ -136,7 +136,7 @@ class StndDefinedValidatorTest { void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToIncorrectInternalFileReference() throws IOException { //given - JsonNode validEventNode = objectMapper.readTree(new FileReader(INCORRECT_INTERNAL_REF_EVENT_PATH).readFile()); + JsonNode validEventNode = objectMapper.readTree(new FileReader(INCORRECT_INTERNAL_REF_EVENT_PATH).getContent()); //when //then @@ -150,4 +150,4 @@ class StndDefinedValidatorTest { .schemasPath(SCHEMAS_PATH) .build(); } -}
\ No newline at end of file +} |