aboutsummaryrefslogtreecommitdiffstats
path: root/services/external-schema-manager/src/main/test/org
diff options
context:
space:
mode:
Diffstat (limited to 'services/external-schema-manager/src/main/test/org')
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReaderTest.java78
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/JsonFragmentRetrieverTest.java67
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceJsonGeneratorTest.java61
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceMapperTest.java70
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceResolverTest.java106
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorBuilderTest.java109
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorTest.java153
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapperTest.java98
-rw-r--r--services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/externalschemamanager/MainTest.java25
9 files changed, 742 insertions, 25 deletions
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
new file mode 100644
index 00000000..5d391a69
--- /dev/null
+++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/FileReaderTest.java
@@ -0,0 +1,78 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class FileReaderTest {
+
+ public static final String TEST_RESOURCES = "src/main/test/resources/";
+
+ @Test
+ public void shouldReturnEmptyStringWhenFileNotFound() {
+ //given
+ String expectedContent = "";
+ String fileName = "dummyFileName";
+
+ //when
+ String actualContent = new FileReader(fileName).readFile();
+
+ //then
+ assertEquals(expectedContent, actualContent);
+ }
+
+ @Test
+ public void shouldReturnFileContentWhenFileExists() {
+ //given
+ String expectedContent = "{\n" +
+ " \"someObject\": \"dummyValue\"\n" +
+ "}";
+ String filename = TEST_RESOURCES + "file_with_one_line.json";
+
+ //when
+ String actualContent = new FileReader(filename).readFile();
+
+ //then
+ assertEquals(expectedContent, actualContent);
+ }
+
+ @Test
+ public void shouldReturnFalseWhenFileDoesNotExist() {
+ //when
+ boolean doesFileExists = new FileReader("dummyFileName").doesFileExists();
+
+ //then
+ assertFalse(doesFileExists);
+ }
+
+ @Test
+ public void shouldReturnTrueWhenFileExists() {
+ //when
+ boolean doesFileExists = new FileReader(TEST_RESOURCES + "file_with_one_line.json").doesFileExists();
+
+ //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/JsonFragmentRetrieverTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/JsonFragmentRetrieverTest.java
new file mode 100644
index 00000000..f1cae3f5
--- /dev/null
+++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/JsonFragmentRetrieverTest.java
@@ -0,0 +1,67 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class JsonFragmentRetrieverTest {
+
+ public static final String VALID_JSON_CONTENT = "{\n" +
+ " \"validObject\":{\n" +
+ " \"someEvent\":{\n" +
+ " \"someObject\":true\n" +
+ " }\n" +
+ " }\n" +
+ "}";
+ private ObjectMapper objectMapper = new ObjectMapper();
+
+ @Test
+ public void shouldReturnJsonFragmentAtValidPath() throws IOException {
+ //given
+ JsonNode jsonContent = objectMapper.readTree(VALID_JSON_CONTENT);
+ JsonNode expectedJsonNode = objectMapper.readTree("true");
+ String validPath = "/validObject/someEvent/someObject";
+
+ //when
+ JsonNode actualJsonNode = JsonFragmentRetriever.getFragment(jsonContent, validPath);
+
+ //then
+ assertEquals(expectedJsonNode, actualJsonNode);
+ }
+
+ @Test
+ public void shouldThrowErrorWhenPathDoesNotExistInJsonContent() throws IOException {
+ //given
+ JsonNode jsonContent = objectMapper.readTree(VALID_JSON_CONTENT);
+ String dummyPath = "dummyPath";
+
+ //when
+ //then
+ assertThrows(IllegalArgumentException.class, () -> JsonFragmentRetriever.getFragment(jsonContent, dummyPath));
+ }
+} \ 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/SchemaReferenceJsonGeneratorTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceJsonGeneratorTest.java
new file mode 100644
index 00000000..42bc21c1
--- /dev/null
+++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceJsonGeneratorTest.java
@@ -0,0 +1,61 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.model.SchemaReference;
+
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class SchemaReferenceJsonGeneratorTest {
+
+ @Test
+ public void shouldReturnValidSchemaReferenceWhenUrlIsInValidFormat() throws IOException {
+ //given
+ String validUrl = "src/main/test/resources/file_with_one_line.json#/elo";
+ String schemaReferenceContent = "{\"$ref\":\"" + validUrl + "\"}";
+ JsonNode expectedSchemaReference = new ObjectMapper().readTree(schemaReferenceContent);
+ SchemaReferenceResolver schemaReferenceResolver = new SchemaReferenceResolver(validUrl);
+ SchemaReference schemaReference = new SchemaReference(schemaReferenceResolver);
+
+ //when
+ JsonNode actualSchemaReference = SchemaReferenceJsonGenerator.getSchemaReferenceJson(schemaReference);
+
+ //then
+ assertEquals(expectedSchemaReference, actualSchemaReference);
+ }
+
+ @Test
+ public void shouldThrowErrorWhenUrlIsInInvalidFormat() {
+ //given
+ String invalidFormatUrl = "\"someDummyValue\n\t";
+ SchemaReferenceResolver schemaReferenceResolver = new SchemaReferenceResolver(invalidFormatUrl);
+ SchemaReference schemaReference = new SchemaReference(schemaReferenceResolver);
+ //when
+ //then
+ assertThrows(IOException.class, () -> SchemaReferenceJsonGenerator.getSchemaReferenceJson(schemaReference));
+ }
+} \ 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/SchemaReferenceMapperTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceMapperTest.java
new file mode 100644
index 00000000..1f5ddfb2
--- /dev/null
+++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceMapperTest.java
@@ -0,0 +1,70 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.model.SchemaReference;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class SchemaReferenceMapperTest {
+
+ public static final String SCHEMAS_PATH = "src/main/test/resources";
+
+ @Test
+ public void shouldReturnProperReferenceWhenSchemaReferenceHasNoHash() {
+ //given
+ SchemaReferenceMapper schemaReferenceMapper = getSchemaReferenceMapper();
+ String publicUrlWithoutHash = "http://someExternalUrl/external";
+ SchemaReferenceResolver schemaReferenceResolver = new SchemaReferenceResolver(publicUrlWithoutHash);
+ SchemaReference schemaReference = new SchemaReference(schemaReferenceResolver);
+
+ String expectedReference = SCHEMAS_PATH + "/file_with_one_line.json#/";
+
+ //when
+ String actualReference = schemaReferenceMapper.mapToLocalSchema(schemaReference).getFullSchemaReference();
+
+ //then
+ assertEquals(expectedReference, actualReference);
+ }
+
+ @Test
+ public void shouldReturnProperReferenceWhenSchemaReferenceContainsHash() {
+ //given
+ SchemaReferenceMapper schemaReferenceMapper = getSchemaReferenceMapper();
+ String publicUrlWithHash = "http://someExternalUrl/external#someString";
+ SchemaReferenceResolver schemaReferenceResolver = new SchemaReferenceResolver(publicUrlWithHash);
+ SchemaReference schemaReference = new SchemaReference(schemaReferenceResolver);
+ String expectedReference = SCHEMAS_PATH + "/file_with_one_line.json#/someString";
+
+ //when
+ String actualReference = schemaReferenceMapper.mapToLocalSchema(schemaReference).getFullSchemaReference();
+
+ //then
+ assertEquals(expectedReference, actualReference);
+ }
+
+ private SchemaReferenceMapper getSchemaReferenceMapper() {
+ String mappingFilePath = "src/main/test/resources/schema-map-to-tests.json";
+ UrlMapper urlMapper = new UrlMapperFactory().getUrlMapper(mappingFilePath, SCHEMAS_PATH);
+ return new SchemaReferenceMapper(urlMapper, SCHEMAS_PATH);
+ }
+} \ 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/SchemaReferenceResolverTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceResolverTest.java
new file mode 100644
index 00000000..2fe7239f
--- /dev/null
+++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/SchemaReferenceResolverTest.java
@@ -0,0 +1,106 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+
+package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class SchemaReferenceResolverTest {
+
+ @Test
+ void shouldResolveUrlFromSchemaReference() {
+ //given
+ String expectedUrl = "http://localhost:8080/test";
+ String schemaReference = expectedUrl + "#test/internal/ref";
+
+ //when
+ String actualUrl = new SchemaReferenceResolver(schemaReference).resolveUrl();
+
+ //then
+ assertEquals(expectedUrl, actualUrl);
+ }
+
+ @Test
+ void shouldResolveUrlFromSchemaReferenceWhenInternalReferenceDoesNotExist() {
+ //given
+ String expectedUrl = "http://localhost:8080/test";
+
+ //when
+ String actualUrl = new SchemaReferenceResolver(expectedUrl).resolveUrl();
+
+ //then
+ assertEquals(expectedUrl, actualUrl);
+ }
+
+ @Test
+ void shouldResolveInternalReferenceFromSchemaReference() {
+ //given
+ String schemaReference = "http://localhost:8080/test#/internal/ref/test";
+ String expectedInternalReference = "/internal/ref/test";
+
+ //when
+ String actualInternalReference = new SchemaReferenceResolver(schemaReference).resolveInternalReference();
+
+ //then
+ assertEquals(expectedInternalReference, actualInternalReference);
+ }
+
+ @Test
+ void shouldResolveInternalReferenceFromSchemaReferenceWhenInternalReferenceDoesNotExist() {
+ //given
+ String schemaReference = "http://localhost:8080/test";
+ String expectedInternalReference = "/";
+
+ //when
+ String actualInternalReference = new SchemaReferenceResolver(schemaReference).resolveInternalReference();
+
+ //then
+ assertEquals(expectedInternalReference, actualInternalReference);
+ }
+
+ @Test
+ void shouldResolveInternalReferenceFromSchemaReferenceWhenInternalReferenceIsRoot() {
+ //given
+ String schemaReference = "http://localhost:8080/test#/";
+ String expectedInternalReference = "/";
+
+ //when
+ String actualInternalReference = new SchemaReferenceResolver(schemaReference).resolveInternalReference();
+
+ //then
+ assertEquals(expectedInternalReference, actualInternalReference);
+ }
+
+ @Test
+ void shouldResolveInternalReferenceFromSchemaReferenceWhenInternalReferenceIsEmpty() {
+ //given
+ String schemaReference = "http://localhost:8080/test#";
+ String expectedInternalReference = "/";
+
+ //when
+ String actualInternalReference = new SchemaReferenceResolver(schemaReference).resolveInternalReference();
+
+ //then
+ assertEquals(expectedInternalReference, actualInternalReference);
+ }
+} \ 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
new file mode 100644
index 00000000..56631543
--- /dev/null
+++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorBuilderTest.java
@@ -0,0 +1,109 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class StndDefinedValidatorBuilderTest {
+
+ private static final String TEST_RESOURCES = "src/main/test/resources/externalRepo/";
+
+ @Test
+ void shouldGenerateValidatorWithAllSchemaMappings() {
+ //when
+ StndDefinedValidator validator = getValidator("schema-map.json");
+
+ Map<String, String> mappingsCache = getMappingsCache(validator);
+
+ //then
+ assertThat(mappingsCache.size()).isEqualTo(6);
+ }
+
+ @Test
+ void shouldGenerateValidatorWithoutSchemaMappingsWithReferenceToNotExistingLocalResource() {
+ //when
+ StndDefinedValidator validator = getValidator("schema-map-no-local-resource.json");
+ Map<String, String> mappingsCache = getMappingsCache(validator);
+
+ //then
+ assertThat(mappingsCache.size()).isEqualTo(5);
+ }
+
+ @Test
+ void shouldGenerateValidatorWithoutSchemaMappingsWithEmptyLocalFileContent() {
+ //when
+ StndDefinedValidator validator = getValidator("schema-map-empty-content.json");
+ Map<String, String> mappingsCache = getMappingsCache(validator);
+
+ //then
+ assertThat(mappingsCache.size()).isEqualTo(4);
+ }
+
+ @Test
+ void shouldGenerateValidatorWithoutSchemaMappingsWithIncorrectYamlFormat() {
+ //when
+ StndDefinedValidator validator = getValidator("schema-map-incorrect-yaml-format.json");
+ Map<String, String> mappingsCache = getMappingsCache(validator);
+
+ //then
+ assertThat(mappingsCache.size()).isEqualTo(3);
+ }
+
+ @Test
+ void shouldGenerateValidatorWithoutSchemaMappingsWhenSchemaMappingFileHasNotJsonFormat() {
+ //when
+ StndDefinedValidator validator = getValidator("schema-map-no-json-format.json");
+ Map<String, String> mappingsCache = getMappingsCache(validator);
+
+ //then
+ assertThat(mappingsCache.size()).isEqualTo(0);
+ }
+
+ @Test
+ void shouldGenerateValidatorWithoutSchemaMappingsWhenSchemaMappingFileHasWrongFieldName() {
+ //when
+ StndDefinedValidator validator = getValidator("schema-map-wrong-field-name.json");
+ Map<String, String> mappingsCache = getMappingsCache(validator);
+
+ //then
+ assertThat(mappingsCache.size()).isEqualTo(0);
+ }
+
+ private StndDefinedValidator getValidator(String mappingFilePath) {
+ return new StndDefinedValidator.ValidatorBuilder()
+ .mappingFilePath(TEST_RESOURCES + mappingFilePath)
+ .schemasPath(TEST_RESOURCES)
+ .schemaRefPath("/event/stndDefinedFields/schemaReference")
+ .stndDefinedDataPath("/event/stndDefinedFields/data")
+ .build();
+ }
+
+ private Map<String, String> getMappingsCache(StndDefinedValidator validator) {
+ return validator.getValidatorCache()
+ .getSchemaReferenceMapper()
+ .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
new file mode 100644
index 00000000..be4bea9d
--- /dev/null
+++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidatorTest.java
@@ -0,0 +1,153 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.IncorrectInternalFileReferenceException;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
+
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class StndDefinedValidatorTest {
+
+ private static final String TEST_RESOURCES = "src/main/test/resources/";
+ private static final String MAPPING_FILE_PATH = TEST_RESOURCES + "externalRepo/schema-map.json";
+ private static final String SCHEMAS_PATH = TEST_RESOURCES + "externalRepo";
+
+ private static final String VALID_EVENT_PATH = TEST_RESOURCES + "externalRepo/validEvent.json";
+ private static final String INVALID_EVENT_PATH = TEST_RESOURCES + "externalRepo/invalidEvent.json";
+ private static final String VALID_NO_HASH_EVENT_PATH = TEST_RESOURCES + "externalRepo/validNoHashEvent.json";
+ private static final String INCORRECT_INTERNAL_REF_EVENT_PATH = TEST_RESOURCES + "externalRepo/incorrectHashEvent.json";
+
+ private final ObjectMapper objectMapper = new ObjectMapper();
+ private final StndDefinedValidator validator = new StndDefinedValidator.ValidatorBuilder()
+ .mappingFilePath(MAPPING_FILE_PATH)
+ .schemasPath(SCHEMAS_PATH)
+ .build();
+
+ @Test
+ void shouldValidateStndDefinedFieldsInEventAndReturnTrueWhenValidEventIsGiven()
+ throws IOException {
+ //given
+ FileReader fileReader = new FileReader(VALID_EVENT_PATH);
+ JsonNode validEventNode = objectMapper.readTree(fileReader.readFile());
+
+ //when
+ boolean validationResult = validator.validate(validEventNode);
+
+ //then
+ assertTrue(validationResult);
+ }
+
+ @Test
+ void shouldValidateStndDefinedFieldsInEventAndReturnFalseWhenInvalidEventIsGiven()
+ throws IOException {
+ //given
+ FileReader fileReader = new FileReader(INVALID_EVENT_PATH);
+ JsonNode invalidEventNode = objectMapper.readTree(fileReader.readFile());
+
+ //when
+ boolean validationResult = validator.validate(invalidEventNode);
+
+ //then
+ assertFalse(validationResult);
+ }
+
+ @Test
+ void shouldValidateStndDefinedFieldsInEventAndReturnTrueWhenValidSchemaReferenceHasNoHash()
+ throws IOException {
+ //given
+ FileReader fileReader = new FileReader(VALID_NO_HASH_EVENT_PATH);
+ JsonNode validEventNode = objectMapper.readTree(fileReader.readFile());
+
+ //when
+ boolean validationResult = validator.validate(validEventNode);
+
+ //then
+ assertTrue(validationResult);
+ }
+
+ @Test
+ void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToNotExistingLocalSchema()
+ throws IOException {
+ //given
+ 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());
+
+ //when
+ //then
+ assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
+ }
+
+ @Test
+ void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToEmptySchema()
+ throws IOException {
+ //given
+ 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());
+
+ //when
+ //then
+ assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
+ }
+
+ @Test
+ void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToIncorrectYamlFormatSchema()
+ throws IOException {
+ //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());
+
+ //when
+ //then
+ assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
+ }
+
+ @Test
+ void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToIncorrectInternalFileReference()
+ throws IOException {
+ //given
+ JsonNode validEventNode = objectMapper.readTree(new FileReader(INCORRECT_INTERNAL_REF_EVENT_PATH).readFile());
+
+ //when
+ //then
+ assertThrows(IncorrectInternalFileReferenceException.class, () -> validator.validate(validEventNode));
+ }
+
+
+ private StndDefinedValidator getValidator(String noLocalResourceMappingFilePath) {
+ return new StndDefinedValidator.ValidatorBuilder()
+ .mappingFilePath(noLocalResourceMappingFilePath)
+ .schemasPath(SCHEMAS_PATH)
+ .build();
+ }
+} \ 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/UrlMapperTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapperTest.java
new file mode 100644
index 00000000..60214eb8
--- /dev/null
+++ b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/UrlMapperTest.java
@@ -0,0 +1,98 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class UrlMapperTest {
+ private static final String MAPPING_FILE_PATH = "src/main/test/resources/schema-map-to-tests.json";
+ private static final String SCHEMAS_PATH = "src/main/test/resources";
+ private static UrlMapper urlMapper;
+
+ @BeforeAll
+ public static void setUp() {
+ urlMapper = new UrlMapperFactory().getUrlMapper(MAPPING_FILE_PATH, SCHEMAS_PATH);
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenNoMappingExists() {
+ //given
+ String notMappedPublicUrl = "http://localhost:8080/notExisting";
+
+ //when
+ //then
+ assertThrows(NoLocalReferenceException.class, () -> urlMapper.mapToLocalUrl(notMappedPublicUrl));
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenLocalSchemaFileIsEmpty() {
+ //given
+ String publicUrlToEmptyLocal = "http://someExternalUrl/emptySchema";
+
+ //when
+ //then
+ assertThrows(NoLocalReferenceException.class, () -> urlMapper.mapToLocalUrl(publicUrlToEmptyLocal));
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenFileHasInvalidYamlStructure() {
+ //given
+ String publicUrlToInvalidYamlLocal = "http://someExternalUrl/invalidYamlFile";
+
+ //when
+ //then
+ assertThrows(NoLocalReferenceException.class, () -> urlMapper.mapToLocalUrl(publicUrlToInvalidYamlLocal));
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenLocalFileDoesNotExist() {
+ //given
+ String publicUrlToNotExistingLocalFile = "http://someExternalUrl/missingFile";
+
+ //when
+ //then
+ assertThrows(NoLocalReferenceException.class, () -> urlMapper.mapToLocalUrl(publicUrlToNotExistingLocalFile));
+ }
+
+ @Test
+ public void shouldReturnLocalUrlWhenFileValidAndFound() {
+ //given
+ String publicUrl = "http://someExternalUrl/external";
+
+ //when
+ //then
+ assertEquals("file_with_one_line.json", urlMapper.mapToLocalUrl(publicUrl));
+ }
+
+ @Test
+ public void shouldNotThrowExceptionWhenMappingFileDoesNotExist() {
+ String invalidMappingFilePath = "src/main/test/resources/missing-schema.json";
+
+ Assertions.assertDoesNotThrow(() -> new UrlMapperFactory().getUrlMapper(invalidMappingFilePath, SCHEMAS_PATH));
+ }
+} \ No newline at end of file
diff --git a/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/externalschemamanager/MainTest.java b/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/externalschemamanager/MainTest.java
deleted file mode 100644
index da219d07..00000000
--- a/services/external-schema-manager/src/main/test/org/onap/dcaegen2/services/sdk/services/externalschemamanager/MainTest.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * DCAEGEN2-SERVICES-SDK
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.dcaegen2.services.sdk.services.externalschemamanager;
-
-class MainTest {
-
-} \ No newline at end of file