summaryrefslogtreecommitdiffstats
path: root/pnfsimulator/src/test/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'pnfsimulator/src/test/java/org')
-rw-r--r--pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java2
-rw-r--r--pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorServiceTest.java47
-rw-r--r--pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacerTest.java174
3 files changed, 215 insertions, 8 deletions
diff --git a/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java b/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java
index 0b87f69..4fa5021 100644
--- a/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java
+++ b/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java
@@ -94,7 +94,7 @@ class SimulatorControllerTest {
static void beforeAll() {
SimulatorParams simulatorParams = new SimulatorParams("http://0.0.0.0:8080", 1, 1);
SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
- "testTemplate.json", new JsonObject());
+ "testTemplate.json", new JsonObject(), new JsonObject());
simulatorRequestBody = GSON_OBJ.toJson(simulatorRequest);
}
diff --git a/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorServiceTest.java b/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorServiceTest.java
index c83c5cf..45e1ed4 100644
--- a/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorServiceTest.java
+++ b/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorServiceTest.java
@@ -82,6 +82,10 @@ class SimulatorServiceTest {
+ " \"domain\":\"notification\",\n"
+ " \"eventName\":\"#RandomString(20)\",\n"
+ " \"eventOrderNo\":\"#Increment\"}}}", JsonObject.class);
+ private static final JsonObject VALID_VARIABLES = GSON.fromJson("{\"dn\": \"TestDN-1\", \"measurement\":{\n"
+ + " \"name\": \"AdditionalM\",\n"
+ + " \"value\": \"1.5\"\n"
+ + " }}", JsonObject.class);
private static final String SOME_CUSTOM_SOURCE = "SomeCustomSource";
private static final String CLOSED_LOOP_VNF = "ClosedLoopVNF";
private static final String SAMPLE_ID = "sampleId";
@@ -112,7 +116,8 @@ class SimulatorServiceTest {
simulatorConfigService = mock(SimulatorConfigService.class);
simulatorService = new SimulatorService(templatePatcher, templateReader,
- eventScheduler, eventDataService, simulatorConfigService, sslAuthenticationHelper);
+ eventScheduler, eventDataService, simulatorConfigService,
+ new TemplateVariablesReplacer(),new SslAuthenticationHelper());
}
@Test
@@ -120,13 +125,14 @@ class SimulatorServiceTest {
String templateName = "validExampleMeasurementEvent.json";
SimulatorParams simulatorParams = new SimulatorParams(VES_URL, 1, 1);
SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
- templateName, VALID_PATCH);
+ templateName, VALID_PATCH, VALID_VARIABLES);
doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
simulatorService.triggerEvent(simulatorRequest);
assertEventHasExpectedStructure(VES_URL, templateName, SOME_CUSTOM_SOURCE);
+ assertEventHasReplacedVariables();
}
@Test
@@ -134,7 +140,7 @@ class SimulatorServiceTest {
String templateName = "validExampleMeasurementEvent.json";
SimulatorRequest simulatorRequest = new SimulatorRequest(
new SimulatorParams("", 1, 1),
- templateName, VALID_PATCH);
+ templateName, VALID_PATCH, new JsonObject());
doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
when(simulatorConfigService.getConfiguration()).thenReturn(new SimulatorConfig(SAMPLE_ID, inDbVesUrl));
@@ -159,7 +165,7 @@ class SimulatorServiceTest {
SimulatorParams simulatorParams = new SimulatorParams(VES_URL, 1, 1);
SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
- "invalidJsonStructureEvent.json", patch);
+ "invalidJsonStructureEvent.json", patch, new JsonObject());
doReturn(eventData).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
//when
@@ -172,7 +178,7 @@ class SimulatorServiceTest {
String templateName = "validExampleMeasurementEvent.json";
SimulatorRequest simulatorRequest = new SimulatorRequest(
new SimulatorParams("", 1, 1),
- templateName, null);
+ templateName, null, new JsonObject());
doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
doReturn(new SimulatorConfig(SAMPLE_ID, inDbVesUrl)).when(simulatorConfigService).getConfiguration();
@@ -184,7 +190,11 @@ class SimulatorServiceTest {
@Test
void shouldSuccessfullySendOneTimeEventWithVesUrlWhenPassed() throws IOException, GeneralSecurityException {
- SimulatorService spiedTestedService = spy(new SimulatorService(templatePatcher, templateReader, eventScheduler, eventDataService, simulatorConfigService, new SslAuthenticationHelper()));
+ SimulatorService spiedTestedService = spy(new SimulatorService(
+ templatePatcher, templateReader, eventScheduler,
+ eventDataService, simulatorConfigService,
+ new TemplateVariablesReplacer(),
+ new SslAuthenticationHelper()));
HttpClientAdapter adapterMock = mock(HttpClientAdapter.class);
doNothing().when(adapterMock).send(eventContentCaptor.capture());
@@ -200,7 +210,12 @@ class SimulatorServiceTest {
@Test
void shouldSubstituteKeywordsAndSuccessfullySendOneTimeEvent() throws IOException, GeneralSecurityException {
- SimulatorService spiedTestedService = spy(new SimulatorService(templatePatcher, templateReader, eventScheduler, eventDataService, simulatorConfigService, new SslAuthenticationHelper()));
+ SimulatorService spiedTestedService = spy(new SimulatorService(
+ templatePatcher, templateReader, eventScheduler,
+ eventDataService, simulatorConfigService,
+ new TemplateVariablesReplacer(),
+ new SslAuthenticationHelper())
+ );
HttpClientAdapter adapterMock = mock(HttpClientAdapter.class);
doNothing().when(adapterMock).send(eventContentCaptor.capture());
@@ -267,4 +282,22 @@ class SimulatorServiceTest {
private SimulatorConfig getSimulatorConfig() {
return new SimulatorConfig(SAMPLE_ID, inDbVesUrl);
}
+
+ private void assertEventHasReplacedVariables() {
+ String measurementName = GSON.fromJson(bodyCaptor.getValue(), JsonObject.class)
+ .get("event").getAsJsonObject()
+ .get("measurementsForVfScalingFields").getAsJsonObject()
+ .get("additionalMeasurements").getAsJsonArray().get(0).getAsJsonObject()
+ .get("arrayOfFields").getAsJsonArray().get(0).getAsJsonObject()
+ .get("name").getAsString();
+
+ String reportingEntityName = GSON.fromJson(bodyCaptor.getValue(), JsonObject.class)
+ .get("event").getAsJsonObject()
+ .get("commonEventHeader").getAsJsonObject()
+ .get("reportingEntityName").getAsString();
+
+ assertThat(measurementName).isEqualTo("AdditionalM");
+ assertThat(reportingEntityName).isEqualTo("TestDN-1");
+ }
+
}
diff --git a/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacerTest.java b/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacerTest.java
new file mode 100644
index 0000000..5b7e4f4
--- /dev/null
+++ b/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacerTest.java
@@ -0,0 +1,174 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PNF-REGISTRATION-HANDLER
+ * ================================================================================
+ * Copyright (C) 2019 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.pnfsimulator.simulator;
+
+
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import lombok.val;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+import org.skyscreamer.jsonassert.JSONAssert;
+
+@TestInstance(Lifecycle.PER_CLASS)
+class TemplateVariablesReplacerTest {
+
+ private static final Gson GSON = new Gson();
+
+ private TemplateVariablesReplacer replacer;
+
+ @BeforeAll
+ void setUp() {
+ replacer = new TemplateVariablesReplacer();
+ }
+
+ @Test
+ void shouldReplaceStringVariable() {
+ val sourceAsString = "{\"test1\":\"#variable1\", \"variable1\":\"value2 #variable1\"}";
+ val expectedAsString = "{\"test1\":\"valueOfVariable1\", \"variable1\":\"value2 #variable1\"}";
+ val variablesAsString = "{\"variable1\":\"valueOfVariable1\"}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceStringAndNumberVariable() {
+ val sourceAsString = "{\"test1\":\"#variable1\", \"test2\":\"#variable2\"}";
+ val expectedAsString = "{\"test1\":\"valueOfVariable1=1\", \"test2\":2}";
+ val variablesAsString = "{\"variable1\":\"valueOfVariable1=1\", \"variable2\":2}";
+
+ val source = new Gson().fromJson(sourceAsString, JsonObject.class);
+ val variables = new Gson().fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceSimpleStringVariable() {
+ val sourceAsString = "{\"test1\":\"value1\", \"variable1\":\"#variable1\"}";
+ val expectedAsString = "{\"test1\":\"value1\", \"variable1\":\"valueOfVariable1\"}";
+ val variablesAsString = "{\"variable1\":\"valueOfVariable1\"}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceObjectVariable() {
+ val sourceAsString = "{\"test1\":\"value1\", \"variable1\":\"#variable1\"}";
+ val expectedAsString = "{\"test1\":\"value1\", \"variable1\":{\"replaced1\":\"valueOfVariable1\"}}";
+ val variablesAsString = "{\"variable1\":{\"replaced1\":\"valueOfVariable1\"}}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceIntegerVariable() {
+ val sourceAsString = "{\"test1\":\"value1\", \"variable1\":\"#variable1\"}";
+ val expectedAsString = "{\"test1\":\"value1\", \"variable1\": 1}";
+ val variablesAsString = "{\"variable1\": 1}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceBoolVariable() {
+ val sourceAsString = "{\"test1\":\"value1\", \"variable1\":\"#variable1\"}";
+ val expectedAsString = "{\"test1\":\"value1\", \"variable1\": true}";
+ val variablesAsString = "{\"variable1\": true}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceDifferentVariables() {
+ val sourceAsString = "{\"test1\":\"value1\", \"variable1\":\"#variable\", \"variable2\":\"text #variable\"}";
+ val expectedAsString = "{\"test1\":\"value1\", \"variable1\":{\"replaced1\":\"valueOfVariable1\"}, \"variable2\":\"text #variable\"}";
+ val variablesAsString = "{\"variable\":{\"replaced1\":\"valueOfVariable1\"}}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceArrayVariables() {
+ val sourceAsString = "{\"test1\":\"value1\", \"variable1\":\"#variable1\"}";
+ val expectedAsString = "{\"test1\":\"value1\", \"variable1\":[1,2,3]}";
+ val variablesAsString = "{\"variable1\":[1,2,3]}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceArrayWithStringVariables() {
+ val sourceAsString = "{\"test1\":\"value1\", \"variable1\":\"#variable1\"}";
+ val expectedAsString = "{\"test1\":\"value1\", \"variable1\":[\"1\",\"2\",\"3\"]}";
+ val variablesAsString = "{\"variable1\":[\"1\",\"2\",\"3\"]}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+ @Test
+ void shouldReplaceArrayAsStringVariables() {
+ val sourceAsString = "{\"test1\":\"#variable1\", \"variable1\":\"Text #variable1\"}";
+ val expectedAsString = "{\"test1\":[1,2,3], \"variable1\": \"Text #variable1\"}";
+ val variablesAsString = "{\"variable1\":[1,2,3]}";
+
+ val source = GSON.fromJson(sourceAsString, JsonObject.class);
+ val variables = GSON.fromJson(variablesAsString, JsonObject.class);
+
+ JsonObject result = replacer.substituteVariables(source, variables);
+ JSONAssert.assertEquals(expectedAsString, result.toString(), true);
+ }
+
+} \ No newline at end of file