diff options
Diffstat (limited to 'src/test')
5 files changed, 580 insertions, 19 deletions
diff --git a/src/test/java/org/onap/dcae/ApplicationSettingsTest.java b/src/test/java/org/onap/dcae/ApplicationSettingsTest.java index c4ba586e..3d8a1a1e 100644 --- a/src/test/java/org/onap/dcae/ApplicationSettingsTest.java +++ b/src/test/java/org/onap/dcae/ApplicationSettingsTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.dcaegen2.collectors.ves * ================================================================================ - * Copyright (C) 2018 - 2019 Nokia. All rights reserved. + * Copyright (C) 2018 - 2020 Nokia. All rights reserved. * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -244,7 +244,7 @@ public class ApplicationSettingsTest { public void shouldTellIfSchemaValidationIsEnabled() throws IOException { // when boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1") - .jsonSchemaValidationEnabled(); + .eventSchemaValidationEnabled(); // then assertTrue(jsonSchemaValidationEnabled); @@ -253,7 +253,7 @@ public class ApplicationSettingsTest { @Test public void shouldByDefaultSchemaValidationBeDisabled() throws IOException { // when - boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().jsonSchemaValidationEnabled(); + boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().eventSchemaValidationEnabled(); // then assertFalse(jsonSchemaValidationEnabled); @@ -422,4 +422,4 @@ public class ApplicationSettingsTest { private String sanitizePath(String path) { return Paths.get(path).toString(); } -}
\ 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 4ac3c487..53595100 100644 --- a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java +++ b/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.dcaegen2.collectors.ves * ================================================================================ - * Copyright (C) 2019 Nokia. All rights reserved. + * 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. @@ -20,8 +20,13 @@ package org.onap.dcae.restapi; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +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; @@ -32,13 +37,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.onap.dcae.ApplicationSettings; import org.springframework.http.ResponseEntity; -import java.io.IOException; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) public class EventValidatorTest { private static final String DUMMY_SCHEMA_VERSION = "v5"; @@ -60,7 +58,7 @@ public class EventValidatorTest { @Test public void shouldReturnEmptyOptionalOnJsonSchemaValidationDisabled() { //given - when(settings.jsonSchemaValidationEnabled()).thenReturn(false); + when(settings.eventSchemaValidationEnabled()).thenReturn(false); //when Optional<ResponseEntity<String>> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); @@ -73,7 +71,7 @@ public class EventValidatorTest { @Test public void shouldReturnInvalidJsonErrorOnWrongType() { //given - when(settings.jsonSchemaValidationEnabled()).thenReturn(true); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when Optional<ResponseEntity<String>> result = sut.validate(jsonObject, "wrongType", DUMMY_SCHEMA_VERSION); @@ -83,11 +81,11 @@ public class EventValidatorTest { } @Test - public void shouldReturnSchemaValidationFailedErrorOnInvalidJsonObjectSchema() throws IOException { + public void shouldReturnSchemaValidationFailedErrorOnInvalidJsonObjectSchema() { //given String schemaRejectingEverything = "{\"not\":{}}"; mockJsonSchema(schemaRejectingEverything); - when(settings.jsonSchemaValidationEnabled()).thenReturn(true); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when Optional<ResponseEntity<String>> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); @@ -97,11 +95,11 @@ public class EventValidatorTest { } @Test - public void shouldReturnEmptyOptionalOnValidJsonObjectSchema() throws IOException { + public void shouldReturnEmptyOptionalOnValidJsonObjectSchema() { //given String schemaAcceptingEverything = "{}"; mockJsonSchema(schemaAcceptingEverything); - when(settings.jsonSchemaValidationEnabled()).thenReturn(true); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when Optional<ResponseEntity<String>> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); @@ -110,6 +108,7 @@ public class EventValidatorTest { assertEquals(Optional.empty(), result); } + private void mockJsonSchema(String jsonSchemaContent) { JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); @@ -121,4 +120,4 @@ public class EventValidatorTest { return Optional.of(ResponseEntity.status(schemaValidationFailed.httpStatusCode) .body(schemaValidationFailed.toJSON().toString())); } -}
\ 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 new file mode 100644 index 00000000..444e28e6 --- /dev/null +++ b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java @@ -0,0 +1,159 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved.s + * ================================================================================ + * 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.restapi; + +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; +import org.jetbrains.annotations.NotNull; +import org.json.JSONArray; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.dcae.ApplicationSettings; +import org.onap.dcae.common.EventSender; +import org.onap.dcae.common.EventTransformation; +import org.onap.dcae.common.HeaderUtils; +import org.slf4j.Logger; +import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.Type; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class VesRestControllerTest { + + private static final String EVENT_TRANSFORM_FILE_PATH = "/eventTransform.json"; + + @InjectMocks + VesRestController vesRestController; + + @Mock + ApplicationSettings applicationSettings; + + @Mock + Logger logger; + + @Mock + EventSender eventSender; + + @Mock + HeaderUtils headerUtils; + + @Test + public void shouldReportThatApiVersionIsNotSupported() { + // given + when(applicationSettings.isVersionSupported("v20")).thenReturn(false); + MockHttpServletRequest request = givenMockHttpServletRequest(); + + // when + final ResponseEntity<String> event = vesRestController.event("", "v20", request); + + // then + assertThat(event.getStatusCodeValue()).isEqualTo(400); + assertThat(event.getBody()).isEqualTo("API version v20 is not supported"); + verify(eventSender, never()).send(any(JSONArray.class)); + } + + @Test + public void shouldTransformEventAccordingToEventTransformFile() throws IOException { + //given + configureEventTransformations(); + configureHeadersForEventListener(); + + MockHttpServletRequest request = givenMockHttpServletRequest(); + + String validEvent = new String( + Files.readAllBytes(Paths.get(this.getClass().getResource("/ves7_valid.json").getPath())) + ); + + //when + final ResponseEntity<String> response = vesRestController.event(validEvent, "v7", request); + + //then + assertThat(response.getStatusCodeValue()).isEqualTo(202); + assertThat(response.getBody()).isEqualTo("Accepted"); + verifyThatTransformedEventWasSend(eventSender, validEvent); + } + + private void configureEventTransformations() throws IOException { + final List<EventTransformation> eventTransformations = loadEventTransformations(); + when(applicationSettings.isVersionSupported("v7")).thenReturn(true); + when(applicationSettings.eventTransformingEnabled()).thenReturn(true); + when(applicationSettings.getEventTransformations()).thenReturn(eventTransformations); + } + + private void configureHeadersForEventListener() { + when(headerUtils.getRestApiIdentify(anyString())).thenReturn("eventListener"); + when(headerUtils.getApiVerFilePath(anyString())).thenReturn( + this.getClass().getResource("/api_version_config.json").getPath() + ); + } + + private void verifyThatTransformedEventWasSend(EventSender eventSender, String eventBeforeTransformation) { + // event before transformation + assertThat(eventBeforeTransformation).contains("\"version\": \"4.0.1\""); + assertThat(eventBeforeTransformation).contains("\"faultFieldsVersion\": \"4.0\""); + + ArgumentCaptor<JSONArray> argument = ArgumentCaptor.forClass(JSONArray.class); + verify(eventSender).send(argument.capture()); + + final String transformedEvent = argument.getValue().toString(); + + // event after transformation + assertThat(transformedEvent).contains("\"priority\":\"High\",\"version\":3,"); + assertThat(transformedEvent).contains(",\"faultFieldsVersion\":3,\"specificProblem"); + } + + @NotNull + private MockHttpServletRequest givenMockHttpServletRequest() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setContentType("application/json"); + + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + return request; + } + + private List<EventTransformation> loadEventTransformations() throws IOException { + Type EVENT_TRANSFORM_LIST_TYPE = new TypeToken<List<EventTransformation>>() { + }.getType(); + + try (FileReader fr = new FileReader(this.getClass().getResource(EVENT_TRANSFORM_FILE_PATH).getPath())) { + return new Gson().fromJson(fr, EVENT_TRANSFORM_LIST_TYPE); + } + } +} diff --git a/src/test/resources/api_version_config.json b/src/test/resources/api_version_config.json new file mode 100644 index 00000000..23f585f9 --- /dev/null +++ b/src/test/resources/api_version_config.json @@ -0,0 +1,7 @@ +{ + "apiVersion": + { + "eventListener": ["4.7.2","5.4.1","7.0.1"], + "eventListener_eventBatch": ["4.7.2","5.4.1","7.0.1"] + } +} diff --git a/src/test/resources/eventTransform.json b/src/test/resources/eventTransform.json new file mode 100644 index 00000000..ab37a6eb --- /dev/null +++ b/src/test/resources/eventTransform.json @@ -0,0 +1,396 @@ +[ + { + "filter": { + "event.commonEventHeader.domain": "heartbeat", + "VESversion": "v7" + }, + "processors": [ + { + "functionName": "addAttribute", + "args": { + "field": "event.commonEventHeader.version", + "value": "3.0", + "fieldType": "number" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.heartbeatFields.heartbeatFieldsVersion", + "value": "2.0", + "fieldType": "number" + } + }, + { + "functionName": "map", + "args": { + "field": "event.heartbeatFields.additionalFields", + "mapType": "HashmapToNameValueArray" + } + } + ] + }, + { + "filter": { + "event.commonEventHeader.domain": "fault", + "VESversion": "v7" + }, + "processors": [ + { + "functionName": "addAttribute", + "args": { + "field": "event.commonEventHeader.version", + "value": "3.0", + "fieldType": "number" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.faultFields.faultFieldsVersion", + "value": "3.0", + "fieldType": "number" + } + }, + { + "functionName": "map", + "args": { + "field": "event.faultFields.alarmAdditionalInformation", + "mapType": "HashmapToNameValueArray" + } + } + ] + }, + { + "filter": { + "event.commonEventHeader.domain": "thresholdCrossingAlert", + "VESversion": "v7" + }, + "processors": [ + { + "functionName": "addAttribute", + "args": { + "field": "event.commonEventHeader.version", + "value": "3.0", + "fieldType": "number" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.thresholdCrossingFields.thresholdCrossingFieldsVersion", + "value": "3.0", + "fieldType": "number" + } + }, + { + "functionName": "map", + "args": { + "field": "event.thresholdCrossingFields.additionalFields", + "mapType": "HashmapToNameValueArray" + } + } + ] + }, + { + "filter": { + "event.commonEventHeader.domain": "measurement", + "VESversion": "v7" + }, + "processors": [ + { + "functionName": "addAttribute", + "args": { + "field": "event.commonEventHeader.version", + "value": "3.0", + "fieldType": "number" + } + }, + { + "functionName": "removeAttribute", + "args": { + "field": "event.measurementFields.measurementFieldsVersion" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.measurementFields.measurementsForVfScalingVersion", + "value": "3.0", + "fieldType": "number" + } + }, + { + "functionName": "map", + "args": { + "field": "event.measurementFields.vNicPerformanceArray[]", + "oldField": "event.measurementFields.nicPerformanceArray[]", + "attrMap": { + "nicIdentifier": "vNicIdentifier" + } + } + }, + { + "functionName": "map", + "args": { + "field": "event.measurementFields.additionalFields", + "oldField": "event.measurementFields.additionalFields", + "mapType": "hashmapToNameValueArray" + } + }, + { + "functionName": "map", + "args": { + "field": "event.measurementsForVfScalingFields", + "oldField": "event.measurementFields", + "mapType": "renameObject" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.commonEventHeader.domain", + "value": "measurementsForVfScaling" + } + } + ] + }, + { + "filter": { + "event.commonEventHeader.domain": "heartbeat", + "VESversion": "v4" + }, + "processors": [ + { + "functionName": "concatenateValue", + "args": { + "field": "event.commonEventHeader.eventName", + "concatenate": [ + "$event.commonEventHeader.domain", + "$event.commonEventHeader.eventType", + "$event.faultFields.alarmCondition" + ], + "delimiter": "_" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.heartbeatFields.heartbeatFieldsVersion", + "value": "1.0", + "fieldType": "number" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.heartbeatFields.heartbeatInterval", + "value": "0", + "fieldType": "integer" + } + }, + { + "functionName": "map", + "args": { + "field": "event.commonEventHeader.nfNamingCode", + "oldField": "event.commonEventHeader.functionalRole" + } + } + ] + }, + { + "filter": { + "event.commonEventHeader.domain": "fault", + "VESversion": "v4" + }, + "processors": [ + { + "functionName": "concatenateValue", + "args": { + "field": "event.commonEventHeader.eventName", + "concatenate": [ + "$event.commonEventHeader.domain", + "$event.commonEventHeader.eventType", + "$event.faultFields.alarmCondition" + ], + "delimiter": "_" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.faultFields.faultFieldsVersion", + "value": "2.0", + "fieldType": "number" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.commonEventHeader.version", + "value": "3.0", + "fieldType": "number" + } + }, + { + "functionName": "map", + "args": { + "field": "event.commonEventHeader.nfNamingCode", + "oldField": "event.commonEventHeader.functionalRole" + } + } + ] + }, + { + "filter": { + "event.commonEventHeader.domain": "thresholdCrossingAlert", + "VESversion": "v4" + }, + "processors": [ + { + "functionName": "concatenateValue", + "args": { + "field": "event.commonEventHeader.eventName", + "concatenate": [ + "$event.commonEventHeader.domain", + "$event.commonEventHeader.elementType", + "$event.faultFields.alertDescription" + ], + "delimiter": "_" + } + }, + { + "functionName": "map", + "args": { + "field": "event.commonEventHeader.nfNamingCode", + "oldField": "event.commonEventHeader.functionalRole" + } + } + ] + }, + { + "filter": { + "event.commonEventHeader.domain": "measurementsForVfScaling", + "VESversion": "v4", + "not": { + "event.commonEventHeader.reportingEntityName": "matches:.*ircc|irpr.*" + } + }, + "processors": [ + { + "functionName": "concatenateValue", + "args": { + "field": "event.commonEventHeader.eventName", + "concatenate": [ + "$event.commonEventHeader.domain", + "$event.commonEventHeader.eventType", + "$event.faultFields.alarmCondition" + ], + "delimiter": "_" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.measurementsForVfScalingFields.measurementsForVfScalingVersion", + "value": "2.0", + "fieldType": "number" + } + }, + { + "functionName": "map", + "args": { + "field": "event.measurementsForVfScalingFields.additionalMeasurements[].arrayOfFields[]", + "oldField": "event.measurementsForVfScalingFields.additionalMeasurements[].measurements[]" + } + }, + { + "functionName": "map", + "args": { + "oldField": "event.measurementsForVfScalingFields.aggregateCpuUsage", + "field": "event.measurementsForVfScalingFields.cpuUsageArray[0].percentUsage" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.measurementsForVfScalingFields.cpuUsageArray[0].cpuIdentifier", + "value": "$event.commonEventHeader.sourceName" + } + }, + { + "functionName": "map", + "args": { + "field": "event.measurementsForVfScalingFields.memoryUsageArray[0].memoryConfigured", + "oldField": "event.measurementsForVfScalingFields.memoryConfigured", + "operation": "convertMBtoKB" + } + }, + { + "functionName": "map", + "args": { + "field": "event.measurementsForVfScalingFields.memoryUsageArray[0].memoryUsed", + "oldField": "event.measurementsForVfScalingFields.memoryUsed", + "operation": "convertMBtoKB" + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.measurementsForVfScalingFields.memoryUsageArray[0].vmIdentifier", + "value": "$event.commonEventHeader.sourceName" + } + }, + { + "functionName": "subtractValue", + "args": { + "field": "event.measurementsForVfScalingFields.memoryUsageArray[0].memoryFree", + "subtract": [ + "$event.measurementsForVfScalingFields.memoryUsageArray[0].memoryConfigured", + "$event.measurementsForVfScalingFields.memoryUsageArray[0].memoryUsed" + ] + } + }, + { + "functionName": "map", + "args": { + "field": "event.measurementsForVfScalingFields.vNicPerformanceArray[]", + "oldField": "event.measurementsForVfScalingFields.vNicUsageArray[]", + "attrMap": { + "broadcastPacketsIn": "receivedBroadcastPacketsAccumulated", + "multicastPacketsIn": "receivedMulticastPacketsAccumulated", + "bytesIn": "receivedOctetsAccumulated", + "packetsIn": "receivedTotalPacketsAccumulated", + "unicastPacketsIn": "receivedUnicastPacketsAccumulated", + "broadcastPacketsOut": "transmittedBroadcastPacketsAccumulated", + "multicastPacketsOut": "transmittedMulticastPacketsAccumulated", + "bytesOut": "transmittedOctetsAccumulated", + "packetsOut": "transmittedTotalPacketsAccumulated", + "unicastPacketsOut": "transmittedUnicastPacketsAccumulated" + } + } + }, + { + "functionName": "map", + "args": { + "field": "event.measurementsForVfScalingFields.vNicPerformanceArray[]", + "oldField": "event.measurementsForVfScalingFields.errors", + "attrMap": { + "receiveDiscards": "receivedDiscardedPacketsAccumulated", + "receiveErrors": "receivedErrorPacketsAccumulated", + "transmitDiscards": "transmittedDiscardedPacketsAccumulated", + "transmitErrors": "transmittedErrorPacketsAccumulated" + } + } + }, + { + "functionName": "addAttribute", + "args": { + "field": "event.measurementsForVfScalingFields.vNicPerformanceArray[0].valuesAreSuspect", + "value": "false" + } + } + ] + } +] + |