32dd532aa393956df1ace0810690230ff4edcfa0
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.simulator;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonSyntaxException;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.mockito.ArgumentCaptor;
30 import org.onap.pnfsimulator.event.EventData;
31 import org.onap.pnfsimulator.event.EventDataService;
32 import org.onap.pnfsimulator.rest.model.FullEvent;
33 import org.onap.pnfsimulator.rest.model.SimulatorParams;
34 import org.onap.pnfsimulator.rest.model.SimulatorRequest;
35 import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
36 import org.onap.pnfsimulator.simulator.scheduler.EventScheduler;
37 import org.onap.pnfsimulator.simulatorconfig.SimulatorConfig;
38 import org.onap.pnfsimulator.simulatorconfig.SimulatorConfigService;
39 import org.quartz.SchedulerException;
40
41 import java.io.IOException;
42 import java.net.MalformedURLException;
43 import java.net.URL;
44
45 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
46 import static org.junit.jupiter.api.Assertions.assertThrows;
47 import static org.mockito.ArgumentMatchers.any;
48 import static org.mockito.Mockito.doNothing;
49 import static org.mockito.Mockito.doReturn;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.spy;
52 import static org.mockito.Mockito.verify;
53 import static org.mockito.Mockito.when;
54 import static org.mockito.internal.verification.VerificationModeFactory.times;
55
56 class SimulatorServiceTest {
57
58     private static final String VES_URL = "http://0.0.0.0:8080";
59     private static final Gson GSON = new Gson();
60     private static final JsonObject VALID_PATCH = GSON.fromJson("{\"event\": {\n" +
61             "    \"commonEventHeader\": {\n" +
62             "      \"sourceName\": \"SomeCustomSource\"}}}\n", JsonObject.class);
63     private static JsonObject VALID_FULL_EVENT = GSON.fromJson("{\"event\": {\n" +
64             "    \"commonEventHeader\": {\n" +
65             "      \"domain\": \"notification\",\n" +
66             "      \"eventName\": \"vFirewallBroadcastPackets\"\n" +
67             "    },\n" +
68             "    \"notificationFields\": {\n" +
69             "      \"arrayOfNamedHashMap\": [{\n" +
70             "        \"name\": \"A20161221.1031-1041.bin.gz\",\n" +
71             "        \"hashMap\": {\n" +
72             "          \"fileformatType\": \"org.3GPP.32.435#measCollec\"}}]}}}", JsonObject.class);
73     private static JsonObject FULL_EVENT_WITH_KEYWORDS = GSON.fromJson("{\"event\":{  \n" +
74             "      \"commonEventHeader\":{  \n" +
75             "         \"domain\":\"notification\",\n" +
76             "         \"eventName\":\"#RandomString(20)\",\n" +
77             "         \"eventOrderNo\":\"#Increment\"}}}", JsonObject.class);
78     private static final String SOME_CUSTOM_SOURCE = "SomeCustomSource";
79     private static final String CLOSED_LOOP_VNF ="ClosedLoopVNF";
80     private static final String SAMPLE_ID = "sampleId";
81     private static final EventData SAMPLE_EVENT = EventData.builder().id("1").build();
82     private final ArgumentCaptor<JsonObject> bodyCaptor = ArgumentCaptor.forClass(JsonObject.class);
83     private final ArgumentCaptor<Integer> intervalCaptor = ArgumentCaptor.forClass(Integer.class);
84     private final ArgumentCaptor<Integer> repeatCountCaptor = ArgumentCaptor
85             .forClass(Integer.class);
86     private final ArgumentCaptor<String> templateNameCaptor = ArgumentCaptor.forClass(String.class);
87     private final ArgumentCaptor<String> eventIdCaptor = ArgumentCaptor.forClass(String.class);
88     private final ArgumentCaptor<String> vesUrlCaptor = ArgumentCaptor.forClass(String.class);
89     private final ArgumentCaptor<String> eventContentCaptor = ArgumentCaptor.forClass(String.class);
90     private SimulatorService simulatorService;
91     private EventDataService eventDataService;
92     private EventScheduler eventScheduler;
93     private SimulatorConfigService simulatorConfigService;
94     private static TemplatePatcher templatePatcher = new TemplatePatcher();
95     private static TemplateReader templateReader = new FilesystemTemplateReader(
96         "src/test/resources/org/onap/pnfsimulator/simulator/", GSON);
97
98     @BeforeEach
99     void setUp() {
100         eventDataService = mock(EventDataService.class);
101         eventScheduler = mock(EventScheduler.class);
102         simulatorConfigService = mock(SimulatorConfigService.class);
103
104         simulatorService = new SimulatorService(templatePatcher, templateReader,
105                 eventScheduler, eventDataService, simulatorConfigService);
106     }
107
108     @Test
109     void shouldTriggerEventWithGivenParams() throws IOException, SchedulerException {
110         String templateName = "validExampleMeasurementEvent.json";
111         SimulatorParams simulatorParams = new SimulatorParams(VES_URL, 1, 1);
112         SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
113                 templateName, VALID_PATCH);
114
115         doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
116
117         simulatorService.triggerEvent(simulatorRequest);
118
119         assertEventHasExpectedStructure(VES_URL, templateName, SOME_CUSTOM_SOURCE);
120     }
121
122     @Test
123     void shouldTriggerEventWithDefaultVesUrlWhenNotProvidedInRequest() throws IOException, SchedulerException {
124         String templateName = "validExampleMeasurementEvent.json";
125         SimulatorRequest simulatorRequest = new SimulatorRequest(
126                 new SimulatorParams("", 1, 1),
127                 templateName, VALID_PATCH);
128
129         URL inDbVesUrl = new URL("http://0.0.0.0:8080/eventListener/v6");
130         doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
131         when(simulatorConfigService.getConfiguration()).thenReturn(new SimulatorConfig(SAMPLE_ID, inDbVesUrl));
132
133         simulatorService.triggerEvent(simulatorRequest);
134
135         assertEventHasExpectedStructure(inDbVesUrl.toString(), templateName, SOME_CUSTOM_SOURCE);
136     }
137
138     @Test
139     void shouldThrowJsonSyntaxWhenInvalidJson() {
140         //given
141         JsonObject patch = GSON.fromJson("{\n" +
142                 "  \"event\": {\n" +
143                 "    \"commonEventHeader\": {\n" +
144                 "      \"sourceName\": \"" + SOME_CUSTOM_SOURCE + "\"\n" +
145                 "    }\n" +
146                 "  }\n" +
147                 "}\n", JsonObject.class);
148         EventData eventData = EventData.builder().id("1").build();
149
150         SimulatorParams simulatorParams = new SimulatorParams(VES_URL, 1, 1);
151         SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
152                 "invalidJsonStructureEvent.json", patch);
153         doReturn(eventData).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
154
155         //when
156         assertThrows(JsonSyntaxException.class,
157                 () -> simulatorService.triggerEvent(simulatorRequest));
158     }
159
160     @Test
161     void shouldHandleNonExistingPatchSection() throws IOException, SchedulerException {
162         String templateName = "validExampleMeasurementEvent.json";
163         JsonObject nullPatch = null;
164         SimulatorRequest simulatorRequest = new SimulatorRequest(
165             new SimulatorParams("", 1, 1),
166             templateName, nullPatch);
167
168         URL inDbVesUrl = new URL("http://0.0.0.0:8080/eventListener/v6");
169         doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
170         doReturn(new SimulatorConfig(SAMPLE_ID, inDbVesUrl)).when(simulatorConfigService).getConfiguration();
171
172         simulatorService.triggerEvent(simulatorRequest);
173
174         assertEventHasExpectedStructure(inDbVesUrl.toString(), templateName, CLOSED_LOOP_VNF);
175     }
176
177     @Test
178     void shouldSuccessfullySendOneTimeEventWithVesUrlWhenPassed() throws MalformedURLException {
179         SimulatorService spiedTestedService = spy(new SimulatorService(templatePatcher,templateReader, eventScheduler, eventDataService, simulatorConfigService));
180
181         HttpClientAdapter adapterMock = mock(HttpClientAdapter.class);
182         doNothing().when(adapterMock).send(eventContentCaptor.capture());
183         doReturn(adapterMock).when(spiedTestedService).createHttpClientAdapter(any(String.class));
184         FullEvent event = new FullEvent(VES_URL, VALID_FULL_EVENT);
185
186         spiedTestedService.triggerOneTimeEvent(event);
187
188         assertThat(eventContentCaptor.getValue()).isEqualTo(VALID_FULL_EVENT.toString());
189         verify(eventDataService, times(1)).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
190         verify(adapterMock, times(1)).send(VALID_FULL_EVENT.toString());
191     }
192
193     @Test
194     void shouldSubstituteKeywordsAndSuccessfullySendOneTimeEvent() throws MalformedURLException {
195         SimulatorService spiedTestedService = spy(new SimulatorService(templatePatcher,templateReader, eventScheduler, eventDataService, simulatorConfigService));
196
197         HttpClientAdapter adapterMock = mock(HttpClientAdapter.class);
198         doNothing().when(adapterMock).send(eventContentCaptor.capture());
199         doReturn(adapterMock).when(spiedTestedService).createHttpClientAdapter(any(String.class));
200         FullEvent event = new FullEvent(VES_URL, FULL_EVENT_WITH_KEYWORDS);
201
202         spiedTestedService.triggerOneTimeEvent(event);
203
204         JsonObject sentContent = GSON.fromJson(eventContentCaptor.getValue(), JsonElement.class).getAsJsonObject();
205         assertThat(sentContent.getAsJsonObject("event").getAsJsonObject("commonEventHeader").get("eventOrderNo").getAsString()).isEqualTo("1");
206         assertThat(sentContent.getAsJsonObject("event").getAsJsonObject("commonEventHeader").get("eventName").getAsString()).hasSize(20);
207     }
208
209
210     private void assertEventHasExpectedStructure(String expectedVesUrl, String templateName, String sourceNameString) throws SchedulerException, MalformedURLException {
211         verify(eventScheduler, times(1)).scheduleEvent(vesUrlCaptor.capture(), intervalCaptor.capture(),
212                 repeatCountCaptor.capture(), templateNameCaptor.capture(), eventIdCaptor.capture(), bodyCaptor.capture());
213         assertThat(vesUrlCaptor.getValue()).isEqualTo(expectedVesUrl);
214         assertThat(intervalCaptor.getValue()).isEqualTo(1);
215         assertThat(repeatCountCaptor.getValue()).isEqualTo(1);
216         assertThat(templateNameCaptor.getValue()).isEqualTo(templateName);
217         String actualSourceName = GSON.fromJson(bodyCaptor.getValue(), JsonObject.class)
218                 .get("event").getAsJsonObject()
219                 .get("commonEventHeader").getAsJsonObject()
220                 .get("sourceName").getAsString();
221         assertThat(actualSourceName).isEqualTo(sourceNameString);
222         verify(eventDataService)
223                 .persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class),
224                         any(JsonObject.class));
225     }
226 }