diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/org/onap/dcae/FileReader.java | 48 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/restapi/EventValidatorTest.java | 56 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java | 2 | ||||
-rw-r--r-- | src/test/resources/testcollector.properties | 2 | ||||
-rw-r--r-- | src/test/resources/ves7_invalid_30_1_1_event.json (renamed from src/test/resources/ves7_invalid.json) | 0 | ||||
-rw-r--r-- | src/test/resources/ves7_valid_30_1_1_event.json (renamed from src/test/resources/ves7_valid.json) | 0 | ||||
-rw-r--r-- | src/test/resources/ves7_valid_eventWithStndDefinedFields.json | 49 |
7 files changed, 153 insertions, 4 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 diff --git a/src/test/resources/testcollector.properties b/src/test/resources/testcollector.properties index c3fcca62..3001001c 100644 --- a/src/test/resources/testcollector.properties +++ b/src/test/resources/testcollector.properties @@ -3,7 +3,7 @@ collector.service.secure.port=8443 collector.keystore.file.location=../etc/keystore
collector.keystore.passwordfile=./etc/passwordfile
collector.schema.checkflag=1
-collector.schema.file={\"v1\":\"./etc/CommonEventFormat_27.2.json\",\"v2\":\"./etc/CommonEventFormat_27.2.json\",\"v3\":\"./etc/CommonEventFormat_27.2.json\",\"v4\":\"./etc/CommonEventFormat_27.2.json\",\"v5\":\"./etc/CommonEventFormat_28.4.json\"}
+collector.schema.file={\"v1\":\"./etc/CommonEventFormat_27.2.json\",\"v2\":\"./etc/CommonEventFormat_27.2.json\",\"v3\":\"./etc/CommonEventFormat_27.2.json\",\"v4\":\"./etc/CommonEventFormat_27.2.json\",\"v5\":\"./etc/CommonEventFormat_28.4.json\",\"v7\":\"./etc/CommonEventFormat_30.2_ONAP.json\"}
collector.dmaap.streamid=fault=sec_fault|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert|voiceQuality=ves_voicequality|sipSignaling=ves_sipsignaling
collector.dmaapfile=./etc/DmaapConfig.json
auth.method=noAuth
diff --git a/src/test/resources/ves7_invalid.json b/src/test/resources/ves7_invalid_30_1_1_event.json index 74c0a92f..74c0a92f 100644 --- a/src/test/resources/ves7_invalid.json +++ b/src/test/resources/ves7_invalid_30_1_1_event.json diff --git a/src/test/resources/ves7_valid.json b/src/test/resources/ves7_valid_30_1_1_event.json index 19116b15..19116b15 100644 --- a/src/test/resources/ves7_valid.json +++ b/src/test/resources/ves7_valid_30_1_1_event.json diff --git a/src/test/resources/ves7_valid_eventWithStndDefinedFields.json b/src/test/resources/ves7_valid_eventWithStndDefinedFields.json new file mode 100644 index 00000000..5d40b9d9 --- /dev/null +++ b/src/test/resources/ves7_valid_eventWithStndDefinedFields.json @@ -0,0 +1,49 @@ +{ + "event": { + "commonEventHeader": { + "domain": "stndDefined", + "eventId": "stndDefined-gNB_Nokia000001", + "eventName": "stndDefined-gNB-Nokia-PowerLost", + "stndDefinedNamespace": "3GPP-FaultSupervision", + "lastEpochMicrosec": 1234567890, + "priority": "Normal", + "reportingEntityName": "Nokia123456", + "sequence": 0, + "sourceName": "Nokia123456", + "startEpochMicrosec": 1234567890, + "version": "4.1", + "vesEventListenerVersion": "7.2" + }, + "stndDefinedFields": { + "schemaReference": "https://www.3gpp.org/Rel-16/TS28532_generic_fault_supervision.json#definitions/schemas/notifyNewAlarm-NotifType", + "data": { + "header": { + "uri": "xyz", + "notificationId": "xyz", + "notificationType": "notifyNewAlarm", + "eventTime": "xyz", + "systemDN": "xyz" + }, + "body": { + "probableCause": "xyz", + "perceivedSeverity": "Major", + "rootCauseIndicator": false, + "specificProblem": "xyz", + "correlatedNotifications": [], + "backedUpStatus": true, + "backUpObject": "xyz", + "trendIndication": "No change", + "thresholdInfo": {}, + "stateChangeDefinition": [], + "monitoredAttributes": [], + "proposedRepairActions": "xyz", + "additionalText": "xyz", + "additionalInformation": [], + "alarmId": "xyz", + "alarmType": "Environmental Alarm" + } + }, + "stndDefinedFieldsVersion": "1.0" + } + } +}
\ No newline at end of file |