summaryrefslogtreecommitdiffstats
path: root/src/test/java
diff options
context:
space:
mode:
authorawudzins <adam.wudzinski@nokia.com>2020-01-13 11:43:08 +0100
committerawudzins <adam.wudzinski@nokia.com>2020-01-17 14:39:20 +0100
commitab6421bcf7b480f9955b6b4376554ec9677e71ed (patch)
tree347d29e9629a04888daf1fd216284f15b72a7149 /src/test/java
parent7e5a7d1d8cd74e42c8549e02724ec2df589e4117 (diff)
Changed EventTransformation schema to be loaded only once
Issue-ID: DCAEGEN2-1774 Signed-off-by: adamwudzinski <adam.wudzinski@nokia.com> Change-Id: I50f3da2c11201a40be948ab199aaca89bbbb38db
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/onap/dcae/ApplicationSettingsTest.java8
-rw-r--r--src/test/java/org/onap/dcae/restapi/EventValidatorTest.java29
-rw-r--r--src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java159
3 files changed, 177 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);
+ }
+ }
+}