diff options
Diffstat (limited to 'src/test/java/org/onap')
3 files changed, 103 insertions, 3 deletions
diff --git a/src/test/java/org/onap/dcae/FileReader.java b/src/test/java/org/onap/dcae/FileReader.java new file mode 100644 index 00000000..7e85474c --- /dev/null +++ b/src/test/java/org/onap/dcae/FileReader.java @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * 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; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + + +public class FileReader { + + private static final Logger log = LoggerFactory.getLogger(FileReader.class); + + private FileReader() { + } + + public static String readFileAsString(String fileName) { + String fileContent = ""; + try { + fileContent = new String(Files.readAllBytes(Paths.get(fileName))); + } catch (IOException e) { + log.error("Error while reading file.", e); + } + return fileContent; + } +}
\ 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/restapi/EventValidatorTest.java index 53595100..0d38fdac 100644 --- a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java +++ b/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java @@ -26,7 +26,9 @@ import static org.mockito.Mockito.when; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; + import java.util.Optional; + import org.json.JSONObject; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -35,14 +37,18 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.onap.dcae.ApplicationSettings; +import org.onap.dcae.FileReader; import org.springframework.http.ResponseEntity; @ExtendWith(MockitoExtension.class) public class EventValidatorTest { 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"); + private JSONObject sentEvent; + private static final String V7_VERSION = "v7"; private static JSONObject jsonObject; + private static final String EVENT_TYPE = "event"; @Mock private static ApplicationSettings settings; @@ -50,8 +56,9 @@ public class EventValidatorTest { @InjectMocks private static EventValidator sut; + @BeforeAll - static void setupTests(){ + static void setupTests() { jsonObject = new JSONObject("{" + DUMMY_TYPE + ":dummy}"); } @@ -108,6 +115,51 @@ public class EventValidatorTest { assertEquals(Optional.empty(), result); } + @Test + public void shouldReturnNoErrorsWhenValidating30_1_1ValidEvent() { + //given + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_valid_30_1_1_event.json")); + + mockJsonSchema(newSchemaV7); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + + //when + Optional<ResponseEntity<String>> result = sut.validate(sentEvent, EVENT_TYPE, V7_VERSION); + + //then + assertEquals(Optional.empty(), result); + } + + @Test + public void shouldReturnNoErrorsWhenValidatingValidEventWithStndDefinedFields() { + //given + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_valid_eventWithStndDefinedFields.json")); + + mockJsonSchema(newSchemaV7); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + + //when + Optional<ResponseEntity<String>> result = sut.validate(sentEvent, EVENT_TYPE, V7_VERSION); + + //then + assertEquals(Optional.empty(), result); + } + + @Test + public void shouldReturnSchemaValidationFailedWhenValidating30_1_1InvalidEvent() { + //given + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_invalid_30_1_1_event.json")); + + mockJsonSchema(newSchemaV7); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + + //when + Optional<ResponseEntity<String>> result = sut.validate(this.sentEvent, EVENT_TYPE, V7_VERSION); + + //then + assertEquals(generateResponseOptional(ApiException.SCHEMA_VALIDATION_FAILED), result); + } + private void mockJsonSchema(String jsonSchemaContent) { JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); diff --git a/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java index 444e28e6..6b89a356 100644 --- a/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java +++ b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java @@ -98,7 +98,7 @@ public class VesRestControllerTest { MockHttpServletRequest request = givenMockHttpServletRequest(); String validEvent = new String( - Files.readAllBytes(Paths.get(this.getClass().getResource("/ves7_valid.json").getPath())) + Files.readAllBytes(Paths.get(this.getClass().getResource("/ves7_valid_30_1_1_event.json").getPath())) ); //when |