diff options
Diffstat (limited to 'pnfsimulator/src')
11 files changed, 336 insertions, 92 deletions
diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java index 2b06658..d00b311 100644 --- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java +++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java @@ -48,4 +48,8 @@ public class SimulatorRequest { @JsonDeserialize(using = JsonObjectDeserializer.class) private JsonObject patch; + @Nullable + @JsonDeserialize(using = JsonObjectDeserializer.class) + private JsonObject variables; + } diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java index 02f50e4..e5576b8 100644 --- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java +++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java @@ -45,6 +45,7 @@ import java.util.Optional; public class SimulatorService { private final TemplatePatcher templatePatcher; + private final TemplateVariablesReplacer templateVariablesReplacer; private final TemplateReader templateReader; private final EventDataService eventDataService; private final EventScheduler eventScheduler; @@ -53,14 +54,20 @@ public class SimulatorService { private static final JsonObject EMPTY_JSON_OBJECT = new JsonObject(); @Autowired - public SimulatorService(TemplatePatcher templatePatcher, TemplateReader templateReader, - EventScheduler eventScheduler, EventDataService eventDataService, - SimulatorConfigService simulatorConfigService, SslAuthenticationHelper sslAuthenticationHelper) { + public SimulatorService( + TemplatePatcher templatePatcher, + TemplateReader templateReader, + EventScheduler eventScheduler, + EventDataService eventDataService, + SimulatorConfigService simulatorConfigService, + TemplateVariablesReplacer templateVariablesReplacer, + SslAuthenticationHelper sslAuthenticationHelper) { this.templatePatcher = templatePatcher; this.templateReader = templateReader; this.eventDataService = eventDataService; this.eventScheduler = eventScheduler; this.simulatorConfigService = simulatorConfigService; + this.templateVariablesReplacer = templateVariablesReplacer; this.sslAuthenticationHelper = sslAuthenticationHelper; } @@ -71,16 +78,19 @@ public class SimulatorService { JsonObject input = Optional.ofNullable(simulatorRequest.getPatch()).orElse(new JsonObject()); JsonObject patchedJson = templatePatcher .mergeTemplateWithPatch(template, input); + JsonObject variables = Optional.ofNullable(simulatorRequest.getVariables()).orElse(new JsonObject()); + JsonObject patchedJsonWithVariablesSubstituted = templateVariablesReplacer.substituteVariables(patchedJson, variables); + JsonObject keywords = new JsonObject(); - EventData eventData = eventDataService.persistEventData(template, patchedJson, input, keywords); + EventData eventData = eventDataService.persistEventData(template, patchedJsonWithVariablesSubstituted, input, keywords); String targetVesUrl = getDefaultUrlIfNotProvided(simulatorParams.getVesServerUrl()); return eventScheduler .scheduleEvent(targetVesUrl, Optional.ofNullable(simulatorParams.getRepeatInterval()).orElse(1), Optional.ofNullable(simulatorParams.getRepeatCount()).orElse(1), simulatorRequest.getTemplateName(), eventData.getId(), - patchedJson); + patchedJsonWithVariablesSubstituted); } public void triggerOneTimeEvent(FullEvent event) throws IOException, GeneralSecurityException { diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacer.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacer.java new file mode 100644 index 0000000..eb0b141 --- /dev/null +++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacer.java @@ -0,0 +1,50 @@ +/* + * ============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.JsonElement; +import com.google.gson.JsonObject; + +import java.util.Map.Entry; + +import lombok.val; +import org.springframework.stereotype.Component; + +@Component +public class TemplateVariablesReplacer { + private static final Gson GSON = new Gson(); + private static final String OBJECT_KEYWORD_MARK = "#"; + private static final String ESCAPED_QUOTE = "\""; + private static final String STRING_KEYWORD_MARK = ESCAPED_QUOTE + OBJECT_KEYWORD_MARK + "%s" + ESCAPED_QUOTE; + + JsonObject substituteVariables(JsonObject source, JsonObject variables) { + var result = source.toString(); + for (val variable : variables.entrySet()) { + result = substituteVariable(result, variable); + } + return GSON.fromJson(result, JsonObject.class); + } + + private String substituteVariable(String sourceAsString, Entry<String, JsonElement> variable) { + return sourceAsString.replaceAll(String.format(STRING_KEYWORD_MARK, variable.getKey()), variable.getValue().toString()); + } + +} diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java index 7ddef86..a881698 100644 --- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java +++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java @@ -50,20 +50,20 @@ public class HttpClientAdapterImpl implements HttpClientAdapter { private static final String CONTENT_TYPE = "Content-Type"; private static final String APPLICATION_JSON = "application/json"; private static final RequestConfig CONFIG = RequestConfig.custom() - .setConnectTimeout(CONNECTION_TIMEOUT) - .setConnectionRequestTimeout(CONNECTION_TIMEOUT) - .setSocketTimeout(CONNECTION_TIMEOUT) - .build(); + .setConnectTimeout(CONNECTION_TIMEOUT) + .setConnectionRequestTimeout(CONNECTION_TIMEOUT) + .setSocketTimeout(CONNECTION_TIMEOUT) + .build(); private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE"); private SslSupportLevel sslSupportLevel; private HttpClient client; private final String targetUrl; public HttpClientAdapterImpl(String targetUrl, SslAuthenticationHelper sslAuthenticationHelper) - throws IOException, GeneralSecurityException { + throws IOException, GeneralSecurityException { this.sslSupportLevel = sslAuthenticationHelper.isClientCertificateEnabled() - ? SslSupportLevel.CLIENT_CERT_AUTH - : SslSupportLevel.getSupportLevelBasedOnProtocol(targetUrl); + ? SslSupportLevel.CLIENT_CERT_AUTH + : SslSupportLevel.getSupportLevelBasedOnProtocol(targetUrl); this.client = sslSupportLevel.getClient(CONFIG, sslAuthenticationHelper); this.targetUrl = targetUrl; } @@ -76,11 +76,8 @@ public class HttpClientAdapterImpl implements HttpClientAdapter { @Override public void send(String content) { try { - HttpPost request = createRequest(content); - HttpResponse response = client.execute(request); - - //response has to be fully consumed otherwise apache won't release connection - EntityUtils.consumeQuietly(response.getEntity()); + HttpResponse response = sendAndRetrieve(content); + EntityUtils.consumeQuietly(response.getEntity()); //response has to be fully consumed otherwise apache won't release connection LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine()); } catch (IOException e) { LOGGER.warn("Error sending message to ves: {}", e.getMessage(), e.getCause()); @@ -91,6 +88,13 @@ public class HttpClientAdapterImpl implements HttpClientAdapter { return sslSupportLevel; } + private HttpResponse sendAndRetrieve(String content) throws IOException { + HttpPost request = createRequest(content); + HttpResponse httpResponse = client.execute(request); + request.releaseConnection(); + return httpResponse; + } + private HttpPost createRequest(String content) throws UnsupportedEncodingException { HttpPost request = new HttpPost(this.targetUrl); StringEntity stringEntity = new StringEntity(content); @@ -101,5 +105,4 @@ public class HttpClientAdapterImpl implements HttpClientAdapter { return request; } - } diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java index 21e0466..a467370 100644 --- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java +++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java @@ -73,6 +73,8 @@ public class EventJob implements Job { private Optional<HttpClientAdapter> getHttpClientAdapter(JobDataMap jobDataMap, String vesUrl) { HttpClientAdapter adapter = null; try { + adapter = (HttpClientAdapter) (jobDataMap.containsKey(CLIENT_ADAPTER) ? jobDataMap.get(CLIENT_ADAPTER) + : new HttpClientAdapterImpl(vesUrl, new SslAuthenticationHelper())); adapter = (HttpClientAdapter) ( jobDataMap.containsKey(CLIENT_ADAPTER) ? jobDataMap.get(CLIENT_ADAPTER) diff --git a/pnfsimulator/src/main/resources/logback.xml b/pnfsimulator/src/main/resources/logback.xml index 8569b56..b93fedf 100644 --- a/pnfsimulator/src/main/resources/logback.xml +++ b/pnfsimulator/src/main/resources/logback.xml @@ -21,50 +21,35 @@ <Configuration complete="true" compact="true"> - <Property name="outputFilename" value="pnfsimulator_output"/> - <Property name="log-path" value="/var/log/ONAP/pnfsimulator"/> - <Property name="archive" value="/var/log/ONAP/pnfsimulator/archive"/> - <property name="maxFileSize" value="50MB"/> - <property name="maxHistory" value="30"/> - <property name="totalSizeCap" value="10GB"/> + <Property name="outputFilename" value="pnfsimulator_output"/> + <Property name="log-path" value="/var/log/ONAP/pnfsimulator"/> + <Property name="archive" value="/var/log/ONAP/pnfsimulator/archive"/> + <property name="maxFileSize" value="50MB"/> + <property name="maxHistory" value="30"/> + <property name="totalSizeCap" value="10GB"/> + <include resource="org/springframework/boot/logging/logback/defaults.xml"/> - <appender name="Console" target="SYSTEM_OUT" class="ch.qos.logback.core.ConsoleAppender"> - <encoder> - <Pattern>%nopexception%logger - |%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} - |%level - |%replace(%replace(%message){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%mdc){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%rootException){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%marker){'\t','\\\\t'}){'\n','\\\\n'} - |%thread - |%n</Pattern> - </encoder> - </appender> + <appender name="Console" target="SYSTEM_OUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>${CONSOLE_LOG_PATTERN}</pattern> + </encoder> + </appender> - <appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> - <encoder> - <pattern>%nopexception%logger - |%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} - |%level - |%replace(%replace(%message){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%mdc){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%rootException){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%marker){'\t','\\\\t'}){'\n','\\\\n'} - |%thread - |%n</pattern> - </encoder> - <File>${log-path}/${outputFilename}.log</File> - <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> - <FileNamePattern>${archive}/${outputFilename}.%d{yyyy-MM-dd}.%i.log.zip</FileNamePattern> - <MaxFileSize>${maxFileSize}</MaxFileSize> - <MaxHistory>${maxHistory}</MaxHistory> - <TotalSizeCap>${totalSizeCap}</TotalSizeCap> - </rollingPolicy> - </appender> + <appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <encoder> + <pattern>${FILE_LOG_PATTERN}</pattern> + </encoder> + <File>${log-path}/${outputFilename}.log</File> + <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> + <FileNamePattern>${archive}/${outputFilename}.%d{yyyy-MM-dd}.%i.log.zip</FileNamePattern> + <MaxFileSize>${maxFileSize}</MaxFileSize> + <MaxHistory>${maxHistory}</MaxHistory> + <TotalSizeCap>${totalSizeCap}</TotalSizeCap> + </rollingPolicy> + </appender> - <root level="info"> - <appender-ref ref="Console" /> - <appender-ref ref="ROLLING-FILE" /> - </root> + <root level="info"> + <appender-ref ref="Console"/> + <appender-ref ref="ROLLING-FILE"/> + </root> </Configuration> 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 diff --git a/pnfsimulator/src/test/resources/logback-test.xml b/pnfsimulator/src/test/resources/logback-test.xml index ad4f0c8..0dedeba 100644 --- a/pnfsimulator/src/test/resources/logback-test.xml +++ b/pnfsimulator/src/test/resources/logback-test.xml @@ -26,32 +26,18 @@ <property name="maxFileSize" value="50MB"/> <property name="maxHistory" value="30"/> <property name="totalSizeCap" value="10GB"/> + <include resource="org/springframework/boot/logging/logback/defaults.xml"/> + <appender name="Console" target="SYSTEM_OUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> - <Pattern>%nopexception%logger - |%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} - |%level - |%replace(%replace(%message){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%mdc){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%rootException){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%marker){'\t','\\\\t'}){'\n','\\\\n'} - |%thread - |%n</Pattern> + <Pattern>${CONSOLE_LOG_PATTERN}</Pattern> </encoder> </appender> <appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> - <pattern>%nopexception%logger - |%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} - |%level - |%replace(%replace(%message){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%mdc){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%rootException){'\t','\\\\t'}){'\n','\\\\n'} - |%replace(%replace(%marker){'\t','\\\\t'}){'\n','\\\\n'} - |%thread - |%n</pattern> + <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> <File>${log-path}/${outputFilename}.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> diff --git a/pnfsimulator/src/test/resources/org/onap/pnfsimulator/simulator/validExampleMeasurementEvent.json b/pnfsimulator/src/test/resources/org/onap/pnfsimulator/simulator/validExampleMeasurementEvent.json index 989d6ea..a240b93 100644 --- a/pnfsimulator/src/test/resources/org/onap/pnfsimulator/simulator/validExampleMeasurementEvent.json +++ b/pnfsimulator/src/test/resources/org/onap/pnfsimulator/simulator/validExampleMeasurementEvent.json @@ -6,7 +6,7 @@ "eventId": "4cfc-91cf-31a46", "nfType": "mrfx", "priority": "Normal", - "reportingEntityName": "myVNF", + "reportingEntityName": "#dn", "sequence": 1, "sourceName": "ClosedLoopVNF", "startEpochMicrosec": 1531616794, @@ -45,10 +45,7 @@ { "name": "licenseUsage", "arrayOfFields": [ - { - "name": "G711AudioPort", - "value": "1" - }, + "#measurement", { "name": "G729AudioPort", "value": "1" |