0196eb05c239402a4ec5c29189349e83c2828aec
[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.client.utils.ssl.SSLAuthenticationHelper;
37 import org.onap.pnfsimulator.simulator.scheduler.EventScheduler;
38 import org.onap.pnfsimulator.simulatorconfig.SimulatorConfig;
39 import org.onap.pnfsimulator.simulatorconfig.SimulatorConfigService;
40 import org.quartz.SchedulerException;
41
42 import java.io.IOException;
43 import java.net.MalformedURLException;
44 import java.net.URL;
45 import java.security.GeneralSecurityException;
46
47 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
48 import static org.junit.jupiter.api.Assertions.assertThrows;
49 import static org.mockito.ArgumentMatchers.any;
50 import static org.mockito.Mockito.doNothing;
51 import static org.mockito.Mockito.doReturn;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.spy;
54 import static org.mockito.Mockito.verify;
55 import static org.mockito.Mockito.when;
56 import static org.mockito.internal.verification.VerificationModeFactory.times;
57
58 class SimulatorServiceTest {
59
60     private static final String VES_URL = "http://0.0.0.0:8080";
61     private static final Gson GSON = new Gson();
62     private static final JsonObject VALID_PATCH = GSON.fromJson("{\"event\": {\n" +
63             "    \"commonEventHeader\": {\n" +
64             "      \"sourceName\": \"SomeCustomSource\"}}}\n", JsonObject.class);
65     private static JsonObject VALID_FULL_EVENT = GSON.fromJson("{\"event\": {\n" +
66             "    \"commonEventHeader\": {\n" +
67             "      \"domain\": \"notification\",\n" +
68             "      \"eventName\": \"vFirewallBroadcastPackets\"\n" +
69             "    },\n" +
70             "    \"notificationFields\": {\n" +
71             "      \"arrayOfNamedHashMap\": [{\n" +
72             "        \"name\": \"A20161221.1031-1041.bin.gz\",\n" +
73             "        \"hashMap\": {\n" +
74             "          \"fileformatType\": \"org.3GPP.32.435#measCollec\"}}]}}}", JsonObject.class);
75     private static JsonObject FULL_EVENT_WITH_KEYWORDS = GSON.fromJson("{\"event\":{  \n" +
76             "      \"commonEventHeader\":{  \n" +
77             "         \"domain\":\"notification\",\n" +
78             "         \"eventName\":\"#RandomString(20)\",\n" +
79             "         \"eventOrderNo\":\"#Increment\"}}}", JsonObject.class);
80     private static final String SOME_CUSTOM_SOURCE = "SomeCustomSource";
81     private static final String CLOSED_LOOP_VNF ="ClosedLoopVNF";
82     private static final String SAMPLE_ID = "sampleId";
83     private static final EventData SAMPLE_EVENT = EventData.builder().id("1").build();
84     private final ArgumentCaptor<JsonObject> bodyCaptor = ArgumentCaptor.forClass(JsonObject.class);
85     private final ArgumentCaptor<Integer> intervalCaptor = ArgumentCaptor.forClass(Integer.class);
86     private final ArgumentCaptor<Integer> repeatCountCaptor = ArgumentCaptor
87             .forClass(Integer.class);
88     private final ArgumentCaptor<String> templateNameCaptor = ArgumentCaptor.forClass(String.class);
89     private final ArgumentCaptor<String> eventIdCaptor = ArgumentCaptor.forClass(String.class);
90     private final ArgumentCaptor<String> vesUrlCaptor = ArgumentCaptor.forClass(String.class);
91     private final ArgumentCaptor<String> eventContentCaptor = ArgumentCaptor.forClass(String.class);
92     private SimulatorService simulatorService;
93     private EventDataService eventDataService;
94     private EventScheduler eventScheduler;
95     private SimulatorConfigService simulatorConfigService;
96     private static TemplatePatcher templatePatcher = new TemplatePatcher();
97     private static TemplateReader templateReader = new FilesystemTemplateReader(
98         "src/test/resources/org/onap/pnfsimulator/simulator/", GSON);
99
100     @BeforeEach
101     void setUp() {
102         eventDataService = mock(EventDataService.class);
103         eventScheduler = mock(EventScheduler.class);
104         simulatorConfigService = mock(SimulatorConfigService.class);
105
106         simulatorService = new SimulatorService(templatePatcher, templateReader,
107                 eventScheduler, eventDataService, simulatorConfigService, new SSLAuthenticationHelper());
108     }
109
110     @Test
111     void shouldTriggerEventWithGivenParams() throws IOException, SchedulerException, GeneralSecurityException {
112         String templateName = "validExampleMeasurementEvent.json";
113         SimulatorParams simulatorParams = new SimulatorParams(VES_URL, 1, 1);
114         SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
115                 templateName, VALID_PATCH);
116
117         doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
118
119         simulatorService.triggerEvent(simulatorRequest);
120
121         assertEventHasExpectedStructure(VES_URL, templateName, SOME_CUSTOM_SOURCE);
122     }
123
124     @Test
125     void shouldTriggerEventWithDefaultVesUrlWhenNotProvidedInRequest() throws IOException, SchedulerException, GeneralSecurityException {
126         String templateName = "validExampleMeasurementEvent.json";
127         SimulatorRequest simulatorRequest = new SimulatorRequest(
128                 new SimulatorParams("", 1, 1),
129                 templateName, VALID_PATCH);
130
131         URL inDbVesUrl = new URL("http://0.0.0.0:8080/eventListener/v6");
132         doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
133         when(simulatorConfigService.getConfiguration()).thenReturn(new SimulatorConfig(SAMPLE_ID, inDbVesUrl));
134
135         simulatorService.triggerEvent(simulatorRequest);
136
137         assertEventHasExpectedStructure(inDbVesUrl.toString(), templateName, SOME_CUSTOM_SOURCE);
138     }
139
140     @Test
141     void shouldThrowJsonSyntaxWhenInvalidJson() {
142         //given
143         JsonObject patch = GSON.fromJson("{\n" +
144                 "  \"event\": {\n" +
145                 "    \"commonEventHeader\": {\n" +
146                 "      \"sourceName\": \"" + SOME_CUSTOM_SOURCE + "\"\n" +
147                 "    }\n" +
148                 "  }\n" +
149                 "}\n", JsonObject.class);
150         EventData eventData = EventData.builder().id("1").build();
151
152         SimulatorParams simulatorParams = new SimulatorParams(VES_URL, 1, 1);
153         SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
154                 "invalidJsonStructureEvent.json", patch);
155         doReturn(eventData).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
156
157         //when
158         assertThrows(JsonSyntaxException.class,
159                 () -> simulatorService.triggerEvent(simulatorRequest));
160     }
161
162     @Test
163     void shouldHandleNonExistingPatchSection() throws IOException, SchedulerException, GeneralSecurityException {
164         String templateName = "validExampleMeasurementEvent.json";
165         SimulatorRequest simulatorRequest = new SimulatorRequest(
166             new SimulatorParams("", 1, 1),
167             templateName, null);
168
169         URL inDbVesUrl = new URL("http://0.0.0.0:8080/eventListener/v6");
170         doReturn(SAMPLE_EVENT).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
171         doReturn(new SimulatorConfig(SAMPLE_ID, inDbVesUrl)).when(simulatorConfigService).getConfiguration();
172
173         simulatorService.triggerEvent(simulatorRequest);
174
175         assertEventHasExpectedStructure(inDbVesUrl.toString(), templateName, CLOSED_LOOP_VNF);
176     }
177
178     @Test
179     void shouldSuccessfullySendOneTimeEventWithVesUrlWhenPassed() throws IOException, GeneralSecurityException {
180         SimulatorService spiedTestedService = spy(new SimulatorService(templatePatcher,templateReader, eventScheduler, eventDataService, simulatorConfigService, new SSLAuthenticationHelper()));
181
182         HttpClientAdapter adapterMock = mock(HttpClientAdapter.class);
183         doNothing().when(adapterMock).send(eventContentCaptor.capture());
184         doReturn(adapterMock).when(spiedTestedService).createHttpClientAdapter(any(String.class));
185         FullEvent event = new FullEvent(VES_URL, VALID_FULL_EVENT);
186
187         spiedTestedService.triggerOneTimeEvent(event);
188
189         assertThat(eventContentCaptor.getValue()).isEqualTo(VALID_FULL_EVENT.toString());
190         verify(eventDataService, times(1)).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
191         verify(adapterMock, times(1)).send(VALID_FULL_EVENT.toString());
192     }
193
194     @Test
195     void shouldSubstituteKeywordsAndSuccessfullySendOneTimeEvent() throws IOException, GeneralSecurityException {
196         SimulatorService spiedTestedService = spy(new SimulatorService(templatePatcher,templateReader, eventScheduler, eventDataService, simulatorConfigService, new SSLAuthenticationHelper()));
197
198         HttpClientAdapter adapterMock = mock(HttpClientAdapter.class);
199         doNothing().when(adapterMock).send(eventContentCaptor.capture());
200         doReturn(adapterMock).when(spiedTestedService).createHttpClientAdapter(any(String.class));
201         FullEvent event = new FullEvent(VES_URL, FULL_EVENT_WITH_KEYWORDS);
202
203         spiedTestedService.triggerOneTimeEvent(event);
204
205         JsonObject sentContent = GSON.fromJson(eventContentCaptor.getValue(), JsonElement.class).getAsJsonObject();
206         assertThat(sentContent.getAsJsonObject("event").getAsJsonObject("commonEventHeader").get("eventOrderNo").getAsString()).isEqualTo("1");
207         assertThat(sentContent.getAsJsonObject("event").getAsJsonObject("commonEventHeader").get("eventName").getAsString()).hasSize(20);
208     }
209
210
211     private void assertEventHasExpectedStructure(String expectedVesUrl, String templateName, String sourceNameString) throws SchedulerException, IOException, GeneralSecurityException {
212         verify(eventScheduler, times(1)).scheduleEvent(vesUrlCaptor.capture(), intervalCaptor.capture(),
213                 repeatCountCaptor.capture(), templateNameCaptor.capture(), eventIdCaptor.capture(), bodyCaptor.capture());
214         assertThat(vesUrlCaptor.getValue()).isEqualTo(expectedVesUrl);
215         assertThat(intervalCaptor.getValue()).isEqualTo(1);
216         assertThat(repeatCountCaptor.getValue()).isEqualTo(1);
217         assertThat(templateNameCaptor.getValue()).isEqualTo(templateName);
218         String actualSourceName = GSON.fromJson(bodyCaptor.getValue(), JsonObject.class)
219                 .get("event").getAsJsonObject()
220                 .get("commonEventHeader").getAsJsonObject()
221                 .get("sourceName").getAsString();
222         assertThat(actualSourceName).isEqualTo(sourceNameString);
223         verify(eventDataService)
224                 .persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class),
225                         any(JsonObject.class));
226     }
227 }