diff options
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/org/onap/dcae/ApplicationSettingsTest.java | 121 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/TLSTest.java | 15 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java (renamed from src/test/java/org/onap/dcae/restapi/EventValidatorTest.java) | 21 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/common/validator/StndDefinedDataValidatorTest.java | 149 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/common/validator/StndDefinedValidatorResolverTest.java | 65 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java | 49 |
6 files changed, 371 insertions, 49 deletions
diff --git a/src/test/java/org/onap/dcae/ApplicationSettingsTest.java b/src/test/java/org/onap/dcae/ApplicationSettingsTest.java index 6560b1ce..6ea94ab5 100644 --- a/src/test/java/org/onap/dcae/ApplicationSettingsTest.java +++ b/src/test/java/org/onap/dcae/ApplicationSettingsTest.java @@ -21,20 +21,13 @@ package org.onap.dcae; -import static java.util.Collections.singletonList; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.onap.dcae.CLIUtils.processCmdLine; -import static org.onap.dcae.TestingUtilities.createTemporaryFile; - import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.networknt.schema.JsonSchema; import io.vavr.collection.HashMap; import io.vavr.collection.Map; +import org.junit.Test; + import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -42,7 +35,15 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.Objects; -import org.junit.Test; + +import static java.util.Collections.singletonList; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.onap.dcae.CLIUtils.processCmdLine; +import static org.onap.dcae.TestingUtilities.createTemporaryFile; public class ApplicationSettingsTest { @@ -70,7 +71,7 @@ public class ApplicationSettingsTest { @Test public void shouldMakeApplicationSettingsOutOfCLIArgumentsAndAConfigurationFile() - throws IOException { + throws IOException { // given File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter"); Files.write(tempConfFile.toPath(), Arrays.asList("section.subSection1=abc", "section.subSection2=zxc")); @@ -111,7 +112,7 @@ public class ApplicationSettingsTest { public void shouldReturnHTTPPort() throws IOException { // when int applicationPort = fromTemporaryConfiguration("collector.service.port=8090") - .httpPort(); + .httpPort(); // then assertEquals(8090, applicationPort); @@ -130,7 +131,7 @@ public class ApplicationSettingsTest { public void shouldReturnIfHTTPSIsEnabled() throws IOException { // when boolean httpsEnabled = fromTemporaryConfiguration("collector.service.secure.port=8443") - .httpsEnabled(); + .httpsEnabled(); // then assertTrue(httpsEnabled); @@ -157,7 +158,7 @@ public class ApplicationSettingsTest { public void shouldReturnHTTPSPort() throws IOException { // when int httpsPort = fromTemporaryConfiguration("collector.service.secure.port=8443") - .httpsPort(); + .httpsPort(); // then assertEquals(8443, httpsPort); @@ -167,7 +168,7 @@ public class ApplicationSettingsTest { public void shouldReturnConfigurationUpdateInterval() throws IOException { // when int updateFrequency = fromTemporaryConfiguration("collector.dynamic.config.update.frequency=10") - .configurationUpdateFrequency(); + .configurationUpdateFrequency(); // then assertEquals(10, updateFrequency); @@ -177,7 +178,7 @@ public class ApplicationSettingsTest { public void shouldReturnDefaultConfigurationUpdateInterval() throws IOException { // when int updateFrequency = fromTemporaryConfiguration() - .configurationUpdateFrequency(); + .configurationUpdateFrequency(); // then assertEquals(5, updateFrequency); @@ -187,7 +188,7 @@ public class ApplicationSettingsTest { public void shouldReturnLocationOfThePasswordFile() throws IOException { // when String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password") - .keystorePasswordFileLocation(); + .keystorePasswordFileLocation(); // then assertEquals(sanitizePath("/somewhere/password"), passwordFileLocation); @@ -206,7 +207,7 @@ public class ApplicationSettingsTest { public void shouldReturnLocationOfTheKeystoreFile() throws IOException { // when String keystoreFileLocation = fromTemporaryConfiguration("collector.keystore.file.location=/somewhere/keystore") - .keystoreFileLocation(); + .keystoreFileLocation(); // then assertEquals(sanitizePath("/somewhere/keystore"), keystoreFileLocation); @@ -225,7 +226,7 @@ public class ApplicationSettingsTest { public void shouldReturnDMAAPConfigFileLocation() throws IOException { // when String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile") - .dMaaPConfigurationFileLocation(); + .dMaaPConfigurationFileLocation(); // then assertEquals(sanitizePath("/somewhere/dmaapFile"), dmaapConfigFileLocation); @@ -244,7 +245,7 @@ public class ApplicationSettingsTest { public void shouldTellIfSchemaValidationIsEnabled() throws IOException { // when boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1") - .eventSchemaValidationEnabled(); + .eventSchemaValidationEnabled(); // then assertTrue(jsonSchemaValidationEnabled); @@ -266,8 +267,8 @@ public class ApplicationSettingsTest { // when JsonSchema schema = fromTemporaryConfiguration( - String.format("collector.schema.file={\"v1\": \"%s\"}", temporarySchemaFile)) - .jsonSchema("v1"); + String.format("collector.schema.file={\"v1\": \"%s\"}", temporarySchemaFile)) + .jsonSchema("v1"); // then JsonNode incorrectTestObject = new ObjectMapper().readTree("{ \"state\": 1 }"); @@ -295,7 +296,7 @@ public class ApplicationSettingsTest { public void shouldReturnExceptionConfigFileLocation() throws IOException { // when String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile") - .exceptionConfigFileLocation(); + .exceptionConfigFileLocation(); // then assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation); @@ -315,14 +316,14 @@ public class ApplicationSettingsTest { public void shouldReturnDMAAPStreamId() throws IOException { // given Map<String, String[]> expected = HashMap.of( - "log", new String[]{"ves-syslog", "ves-auditlog"}, - "fault", new String[]{"ves-fault"} + "log", new String[]{"ves-syslog", "ves-auditlog"}, + "fault", new String[]{"ves-fault"} ); // when Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration( - "collector.dmaap.streamid=fault=ves-fault|log=ves-syslog,ves-auditlog") - .getDmaapStreamIds(); + "collector.dmaap.streamid=fault=ves-fault|log=ves-syslog,ves-auditlog") + .getDmaapStreamIds(); // then assertArrayEquals(expected.get("log").get(), Objects.requireNonNull(dmaapStreamID).get("log").get()); @@ -352,11 +353,11 @@ public class ApplicationSettingsTest { public void shouldReturnValidCredentials() throws IOException { // when Map<String, String> allowedUsers = fromTemporaryConfiguration( - "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg==" + "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg==" ).validAuthorizationCredentials(); // then - assertEquals( "c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("pasza").get()); + assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("pasza").get()); assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("someoneelse").get()); } @@ -364,7 +365,7 @@ public class ApplicationSettingsTest { public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException { // when Map<String, String> userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration(). - validAuthorizationCredentials(); + validAuthorizationCredentials(); // then assertTrue(userToBase64PasswordDelimitedByCommaSeparatedByPipes.isEmpty()); @@ -374,7 +375,7 @@ public class ApplicationSettingsTest { public void shouldReturnIfEventTransformingIsEnabled() throws IOException { // when boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0") - .eventTransformingEnabled(); + .eventTransformingEnabled(); // then assertFalse(isEventTransformingEnabled); @@ -393,8 +394,8 @@ public class ApplicationSettingsTest { public void shouldReturnCambriaConfigurationFileLocation() throws IOException { // when String cambriaConfigurationFileLocation = fromTemporaryConfiguration( - "collector.dmaapfile=/somewhere/dmaapConfig") - .dMaaPConfigurationFileLocation(); + "collector.dmaapfile=/somewhere/dmaapConfig") + .dMaaPConfigurationFileLocation(); // then assertEquals(sanitizePath("/somewhere/dmaapConfig"), cambriaConfigurationFileLocation); @@ -404,14 +405,64 @@ public class ApplicationSettingsTest { public void shouldReturnDefaultCambriaConfigurationFileLocation() throws IOException { // when String cambriaConfigurationFileLocation = fromTemporaryConfiguration() - .dMaaPConfigurationFileLocation(); + .dMaaPConfigurationFileLocation(); // then assertEquals(sanitizePath("etc/DmaapConfig.json"), cambriaConfigurationFileLocation); } + @Test + public void shouldReturnDefaultExternalSchemaSchemasLocation() throws IOException { + //when + String externalSchemaSchemasLocation = fromTemporaryConfiguration() + .getExternalSchemaSchemasLocation(); + + //then + assertEquals(sanitizePath("./etc/externalRepo"), externalSchemaSchemasLocation); + } + + @Test + public void shouldReturnDefaultExternalSchemaMappingFileLocation() throws IOException { + //when + String externalSchemaMappingFileLocation = fromTemporaryConfiguration() + .getExternalSchemaMappingFileLocation(); + + //then + assertEquals(sanitizePath("./etc/externalRepo/schema-map.json"), externalSchemaMappingFileLocation); + } + + @Test + public void shouldReturnDefaultExternalSchemaSchemaRefPath() throws IOException { + //when + String externalSchemaSchemaRefPath = fromTemporaryConfiguration() + .getExternalSchemaSchemaRefPath(); + + //then + assertEquals(sanitizePath("/event/stndDefinedFields/schemaReference"), externalSchemaSchemaRefPath); + } + + @Test + public void shouldReturnDefaultExternalSchemaStndDefinedDataPath() throws IOException { + //when + String externalSchemaStndDefinedDataPath = fromTemporaryConfiguration() + .getExternalSchemaStndDefinedDataPath(); + + //then + assertEquals(sanitizePath("/event/stndDefinedFields/data"), externalSchemaStndDefinedDataPath); + } + + @Test + public void shouldReturnEnabledExternalSchema2ndStageValidation() throws IOException { + //when + boolean externalSchema2ndStageValidation = fromTemporaryConfiguration("collector.externalSchema.2ndStageValidation=-1") + .getExternalSchemaValidationCheckflag(); + + //then + assertFalse(externalSchema2ndStageValidation); + } + private static ApplicationSettings fromTemporaryConfiguration(String... fileLines) - throws IOException { + throws IOException { File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter"); Files.write(tempConfFile.toPath(), Arrays.asList(fileLines)); tempConfFile.deleteOnExit(); diff --git a/src/test/java/org/onap/dcae/TLSTest.java b/src/test/java/org/onap/dcae/TLSTest.java index e55b6052..424ddf8b 100644 --- a/src/test/java/org/onap/dcae/TLSTest.java +++ b/src/test/java/org/onap/dcae/TLSTest.java @@ -37,6 +37,11 @@ import static org.onap.dcae.TLSTest.HttpsConfigurationWithTLSAuthenticationAndBa public class TLSTest extends TLSTestBase { + private static final String MAPPING_FILE_LOCATION = "./etc/externalRepo/schema-map.json"; + private static final String SCHEMA_FILES_LOCATION = "./etc/externalRepo"; + private static final String STND_DEFINED_DATA_PATH = "/event/stndDefinedFields/data"; + private static final String SCHEMA_REF_PATH = "/event/stndDefinedFields/schemaReference"; + @Nested @Import(HttpConfiguration.class) class HttpTest extends TestClassBase { @@ -69,10 +74,16 @@ public class TLSTest extends TLSTestBase { // ApplicationSettings configurations static class HttpConfiguration extends TLSTestBase.ConfigurationBase { + @Override protected void configureSettings(ApplicationSettings settings) { when(settings.authMethod()).thenReturn(AuthMethodType.NO_AUTH.value()); when(settings.httpPort()).thenReturn(1111); + when(settings.getExternalSchemaMappingFileLocation()).thenReturn(MAPPING_FILE_LOCATION); + when(settings.getExternalSchemaSchemasLocation()).thenReturn(SCHEMA_FILES_LOCATION); + when(settings.getExternalSchemaSchemaRefPath()).thenReturn(SCHEMA_REF_PATH); + when(settings.getExternalSchemaStndDefinedDataPath()).thenReturn(STND_DEFINED_DATA_PATH); + } } @@ -89,6 +100,10 @@ public class TLSTest extends TLSTestBase { when(settings.truststorePasswordFileLocation()).thenReturn(TRUSTSTORE_PASSWORD_FILE.toString()); when(settings.certSubjectMatcher()).thenReturn(CERT_SUBJECT_MATCHER.toString()); when(settings.httpPort()).thenReturn(1111); + when(settings.getExternalSchemaMappingFileLocation()).thenReturn(MAPPING_FILE_LOCATION); + when(settings.getExternalSchemaSchemasLocation()).thenReturn(SCHEMA_FILES_LOCATION); + when(settings.getExternalSchemaSchemaRefPath()).thenReturn(SCHEMA_REF_PATH); + when(settings.getExternalSchemaStndDefinedDataPath()).thenReturn(STND_DEFINED_DATA_PATH); } } }
\ No newline at end of file diff --git a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java b/src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java index 0ca5c424..e5e21177 100644 --- a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java +++ b/src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java @@ -18,31 +18,34 @@ * ============LICENSE_END========================================================= */ -package org.onap.dcae.restapi; +package org.onap.dcae.common.validator; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; import org.json.JSONObject; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.onap.dcae.ApplicationSettings; import org.onap.dcae.FileReader; import org.onap.dcae.common.model.VesEvent; +import org.onap.dcae.restapi.ApiException; +import org.onap.dcae.restapi.EventValidatorException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.never; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) -class EventValidatorTest { +class GeneralEventValidatorTest { private static final String DUMMY_SCHEMA_VERSION = "v5"; private static final String DUMMY_TYPE = "type"; private final String newSchemaV7 = FileReader.readFileAsString("etc/CommonEventFormat_30.2_ONAP.json"); @@ -54,9 +57,9 @@ class EventValidatorTest { @Mock private ApplicationSettings settings; - private SchemaValidator schemaValidator = spy( new SchemaValidator()); + private SchemaValidator schemaValidator = Mockito.spy( new SchemaValidator()); - private EventValidator sut; + private GeneralEventValidator sut; @BeforeAll @@ -66,7 +69,7 @@ class EventValidatorTest { @BeforeEach public void setUp(){ - this.sut = new EventValidator(settings, schemaValidator); + this.sut = new GeneralEventValidator(settings, schemaValidator); } @Test @@ -92,7 +95,7 @@ class EventValidatorTest { sut.validate(new VesEvent(jsonObject), "wrongType", DUMMY_SCHEMA_VERSION); } catch (EventValidatorException e) { //then - assertEquals(ApiException.INVALID_JSON_INPUT, e.getApiException()); + Assertions.assertEquals(ApiException.INVALID_JSON_INPUT, e.getApiException()); } @@ -149,7 +152,7 @@ class EventValidatorTest { @Test void shouldReturnNoErrorsWhenValidatingValidEventWithStndDefinedFields() { //given - sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_valid_eventWithStndDefinedFields.json")); + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves_stdnDefined_valid.json")); mockJsonSchema(newSchemaV7); when(settings.eventSchemaValidationEnabled()).thenReturn(true); @@ -191,4 +194,4 @@ class EventValidatorTest { JsonSchema schema = factory.getSchema(jsonSchemaContent); when(settings.jsonSchema(any())).thenReturn(schema); } -} +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/validator/StndDefinedDataValidatorTest.java b/src/test/java/org/onap/dcae/common/validator/StndDefinedDataValidatorTest.java new file mode 100644 index 00000000..1058b21f --- /dev/null +++ b/src/test/java/org/onap/dcae/common/validator/StndDefinedDataValidatorTest.java @@ -0,0 +1,149 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * 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.dcae.common.validator; + +import org.jetbrains.annotations.NotNull; +import org.json.JSONObject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcae.ApplicationSettings; +import org.onap.dcae.FileReader; +import org.onap.dcae.common.model.VesEvent; +import org.onap.dcae.restapi.ApiException; +import org.onap.dcae.restapi.EventValidatorException; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class StndDefinedDataValidatorTest { + + @Mock + private ApplicationSettings settings; + private StndDefinedDataValidator stndDefinedDataValidator; + + private static final String MAPPING_FILE_LOCATION = "./src/test/resources/stndDefined/schema-map.json"; + private static final String SCHEMA_FILES_LOCATION = "./src/test/resources/stndDefined"; + private static final String STND_DEFINED_DATA_PATH = "/event/stndDefinedFields/data"; + private static final String SCHEMA_REF_PATH = "/event/stndDefinedFields/schemaReference"; + + @BeforeEach + public void setUp() { + mockStndDefinedValidationProps(); + StndDefinedValidatorResolver stndDefinedValidatorResolver = new StndDefinedValidatorResolver(settings); + stndDefinedDataValidator = new StndDefinedDataValidator(stndDefinedValidatorResolver.resolve()); + } + + @Test + public void shouldReturnTrueWhenEventIsValid() throws EventValidatorException { + //given + VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_valid.json"); + + //when + //then + assertDoesNotThrow(() -> stndDefinedDataValidator.validate(event)); + } + + @Test + public void shouldReturnFalseWhenEventIsInvalid() throws EventValidatorException { + //given + VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_invalid.json"); + + try { + //when + stndDefinedDataValidator.validate(event); + } catch (EventValidatorException e) { + //then + assertEquals(ApiException.STND_DEFINED_VALIDATION_FAILED, e.getApiException()); + } + } + + @Test + void shouldReturnErrorWhenMissingLocalSchemaReferenceInMappingFile() { + //given + VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_missing_local_schema_reference.json"); + try { + //when + stndDefinedDataValidator.validate(event); + } catch (EventValidatorException e) { + //then + assertEquals(ApiException.NO_LOCAL_SCHEMA_REFERENCE, e.getApiException()); + } + } + + @Test + void shouldReturnErrorWhenIncorrectInternalFileReference() { + //given + VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_wrong_internal_file_reference.json"); + try { + //when + stndDefinedDataValidator.validate(event); + } catch (EventValidatorException e) { + //then + assertEquals(ApiException.INCORRECT_INTERNAL_FILE_REFERENCE, e.getApiException()); + } + } + + @Test + void shouldReturnErrorWhenStndDefinedFieldsDataIsEmpty() { + //given + VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_with_empty_stndDefined_fields_data.json"); + try { + //when + stndDefinedDataValidator.validate(event); + } catch (EventValidatorException e) { + //then + assertEquals(ApiException.STND_DEFINED_VALIDATION_FAILED, e.getApiException()); + } + } + + @Test + void shouldNotReturnErrorWhenValidatingInvalidEventAndStndDefinedReferenceMissing() { + //given + VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_without_schema_reference.json"); + + //when + //then + assertDoesNotThrow(() -> stndDefinedDataValidator.validate(event)); + } + + @NotNull + private VesEvent getVesEvent(String filename) { + JSONObject jsonObjectEvent = getJsonObjectEvent(filename); + return new VesEvent(jsonObjectEvent); + } + + private JSONObject getJsonObjectEvent(String fileName) { + String eventContent = FileReader.readFileAsString(fileName); + return new JSONObject(eventContent); + } + + private void mockStndDefinedValidationProps() { + when(settings.getExternalSchemaMappingFileLocation()).thenReturn(MAPPING_FILE_LOCATION); + when(settings.getExternalSchemaSchemaRefPath()).thenReturn(SCHEMA_REF_PATH); + when(settings.getExternalSchemaSchemasLocation()).thenReturn(SCHEMA_FILES_LOCATION); + when(settings.getExternalSchemaStndDefinedDataPath()).thenReturn(STND_DEFINED_DATA_PATH); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/validator/StndDefinedValidatorResolverTest.java b/src/test/java/org/onap/dcae/common/validator/StndDefinedValidatorResolverTest.java new file mode 100644 index 00000000..9cdfdcbd --- /dev/null +++ b/src/test/java/org/onap/dcae/common/validator/StndDefinedValidatorResolverTest.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * 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.dcae.common.validator; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcae.ApplicationSettings; + +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class StndDefinedValidatorResolverTest { + + @Mock + private ApplicationSettings settings; + + private static final String MAPPING_FILE_LOCATION = "./src/test/resources/stndDefined/schema-map.json"; + private static final String SCHEMA_FILES_LOCATION = "./src/test/resources/stndDefined"; + private static final String STND_DEFINED_DATA_PATH = "/event/stndDefinedFields/data"; + private static final String SCHEMA_REF_PATH = "/event/stndDefinedFields/schemaReference"; + StndDefinedValidatorResolver stndDefinedValidatorResolver; + + @BeforeEach + public void setUp() { + mockStndDefinedValidationProps(); + stndDefinedValidatorResolver = new StndDefinedValidatorResolver(settings); + } + + @Test + public void shouldReturnStndValidatorWithDefaultSchemaConfigurations() { + //then + Assertions.assertDoesNotThrow(() -> stndDefinedValidatorResolver.resolve()); + } + + private void mockStndDefinedValidationProps() { + when(settings.getExternalSchemaMappingFileLocation()).thenReturn(MAPPING_FILE_LOCATION); + when(settings.getExternalSchemaSchemaRefPath()).thenReturn(SCHEMA_REF_PATH); + when(settings.getExternalSchemaSchemasLocation()).thenReturn(SCHEMA_FILES_LOCATION); + when(settings.getExternalSchemaStndDefinedDataPath()).thenReturn(STND_DEFINED_DATA_PATH); + } + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java index 10423053..765f2b43 100644 --- a/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java +++ b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java @@ -40,6 +40,7 @@ import org.onap.dcae.common.EventSender; import org.onap.dcae.common.EventTransformation; import org.onap.dcae.common.HeaderUtils; import org.onap.dcae.common.JsonDataLoader; +import org.onap.dcae.common.validator.StndDefinedDataValidator; import org.onap.dcae.common.publishing.EventPublisher; import org.slf4j.Logger; import org.springframework.http.ResponseEntity; @@ -85,16 +86,17 @@ public class VesRestControllerTest { @Mock private EventPublisher eventPublisher; + @Mock + private StndDefinedDataValidator stndDefinedDataValidator; + @Before public void setUp(){ - final HashMap<String, String[]> streamIds = HashMap.of( "fault", new String[]{VES_FAULT_TOPIC}, "3GPP-FaultSupervision", new String[]{VES_3_GPP_FAULT_SUPERVISION_TOPIC} ); - this.vesRestController = new VesRestController( - applicationSettings, logger, new EventSender(eventPublisher, streamIds),headerUtils - ); + this.vesRestController = new VesRestController(applicationSettings, logger, + new EventSender(eventPublisher, streamIds), headerUtils, stndDefinedDataValidator); } @Test @@ -229,7 +231,6 @@ public class VesRestControllerTest { configureHeadersForEventListener(); MockHttpServletRequest request = givenMockHttpServletRequest(); - String validEvent = JsonDataLoader.loadContent("/ves_stdnDefined_valid_unknown_topic.json"); //when @@ -241,6 +242,44 @@ public class VesRestControllerTest { verifyThatEventWasNotSend(); } + @Test + public void shouldExecuteStndDefinedValidationWhenFlagIsOnTrue() throws IOException { + //given + configureEventTransformations(); + configureHeadersForEventListener(); + + MockHttpServletRequest request = givenMockHttpServletRequest(); + String validEvent = JsonDataLoader.loadContent("/ves7_batch_with_stndDefined_valid.json"); + when(applicationSettings.getExternalSchemaValidationCheckflag()).thenReturn(true); + + //when + final ResponseEntity<String> response = vesRestController.events(validEvent, VERSION_V7, request); + + //then + assertThat(response.getStatusCodeValue()).isEqualTo(HttpStatus.SC_ACCEPTED); + assertThat(response.getBody()).isEqualTo(ACCEPTED); + verify(stndDefinedDataValidator, times(2)).validate(any()); + } + + @Test + public void shouldNotExecuteStndDefinedValidationWhenFlagIsOnFalse() throws IOException { + //given + configureEventTransformations(); + configureHeadersForEventListener(); + + MockHttpServletRequest request = givenMockHttpServletRequest(); + String validEvent = JsonDataLoader.loadContent("/ves7_batch_with_stndDefined_valid.json"); + when(applicationSettings.getExternalSchemaValidationCheckflag()).thenReturn(false); + + //when + final ResponseEntity<String> response = vesRestController.events(validEvent, VERSION_V7, request); + + //then + assertThat(response.getStatusCodeValue()).isEqualTo(HttpStatus.SC_ACCEPTED); + assertThat(response.getBody()).isEqualTo(ACCEPTED); + verify(stndDefinedDataValidator, times(0)).validate(any()); + } + private void verifyThatEventWasNotSend() { verify(eventPublisher, never()).sendEvent(any(), any()); } |