aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/pnfsimulator/simulator/SimulatorServiceTest.java
blob: d5426ec90781420a1396c870aad66caf6cbdf629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * ============LICENSE_START=======================================================
 * PNF-REGISTRATION-HANDLER
 * ================================================================================
 * Copyright (C) 2018 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 com.google.gson.JsonSyntaxException;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.onap.pnfsimulator.event.EventData;
import org.onap.pnfsimulator.event.EventDataService;
import org.onap.pnfsimulator.rest.model.FullEvent;
import org.onap.pnfsimulator.rest.model.SimulatorParams;
import org.onap.pnfsimulator.rest.model.SimulatorRequest;
import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
import org.onap.pnfsimulator.simulator.client.HttpResponseAdapter;
import org.onap.pnfsimulator.simulator.client.HttpTestUtils;
import org.onap.pnfsimulator.simulator.client.utils.ssl.SslAuthenticationHelper;
import org.onap.pnfsimulator.simulator.scheduler.EventScheduler;
import org.onap.pnfsimulator.simulatorconfig.SimulatorConfig;
import org.onap.pnfsimulator.simulatorconfig.SimulatorConfigService;
import org.quartz.SchedulerException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.internal.verification.VerificationModeFactory.times;

class SimulatorServiceTest {

    private static final String VES_URL = "http://0.0.0.0:8080";
    private static final String IN_DB_VES_URL = "http://0.0.0.0:8080/eventListener/v6";
    private static final Gson GSON = new Gson();
    private static final JsonObject VALID_PATCH = GSON.fromJson("{\"event\": {\n"
        + "    \"commonEventHeader\": {\n"
        + "      \"sourceName\": \"SomeCustomSource\"}}}\n", JsonObject.class);
    private static final JsonObject VALID_FULL_EVENT = GSON.fromJson("{\"event\": {\n"
        + "    \"commonEventHeader\": {\n"
        + "      \"domain\": \"notification\",\n"
        + "      \"eventName\": \"vFirewallBroadcastPackets\"\n"
        + "    },\n"
        + "    \"notificationFields\": {\n"
        + "      \"arrayOfNamedHashMap\": [{\n"
        + "        \"name\": \"A20161221.1031-1041.bin.gz\",\n"
        + "        \"hashMap\": {\n"
        + "          \"fileformatType\": \"org.3GPP.32.435#measCollec\"}}]}}}", JsonObject.class);
    private static final JsonObject FULL_EVENT_WITH_KEYWORDS = GSON.fromJson("{\"event\":{  \n"
        + "      \"commonEventHeader\":{  \n"
        + "         \"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";
    private static final EventData SAMPLE_EVENT = EventData.builder().id("1").build();
    private static URL inDbVesUrl;
    private final ArgumentCaptor<JsonObject> bodyCaptor = ArgumentCaptor.forClass(JsonObject.class);
    private final ArgumentCaptor<Integer> intervalCaptor = ArgumentCaptor.forClass(Integer.class);
    private final ArgumentCaptor<Integer> repeatCountCaptor = ArgumentCaptor
        .forClass(Integer.class);
    private final ArgumentCaptor<String> templateNameCaptor = ArgumentCaptor.forClass(String.class);
    private final ArgumentCaptor<String> eventIdCaptor = ArgumentCaptor.forClass(String.class);
    private final ArgumentCaptor<String> vesUrlCaptor = ArgumentCaptor.forClass(String.class);
    private final ArgumentCaptor<String> eventContentCaptor = ArgumentCaptor.forClass(String.class);
    private final SslAuthenticationHelper sslAuthenticationHelper = new SslAuthenticationHelper();
    private final TemplatePatcher templatePatcher = new TemplatePatcher();
    private final TemplateReader templateReader = new FilesystemTemplateReader(
        "src/test/resources/org/onap/pnfsimulator/simulator/", GSON);

    private SimulatorService simulatorService;
    private EventDataService eventDataService;
    private EventScheduler eventScheduler;
    private SimulatorConfigService simulatorConfigService;

    @BeforeEach
    void setUp() throws MalformedURLException {
        inDbVesUrl = new URL(IN_DB_VES_URL);
        eventDataService = mock(EventDataService.class);
        eventScheduler = mock(EventScheduler.class);
        simulatorConfigService = mock(SimulatorConfigService.class);

        simulatorService = new SimulatorService(templatePatcher, templateReader,
            eventScheduler, eventDataService, simulatorConfigService,
            new TemplateVariablesReplacer(),new SslAuthenticationHelper());
    }

    @Test
    void shouldTriggerEventWithGivenParams() throws IOException, SchedulerException, GeneralSecurityException {
        String templateName = "validExampleMeasurementEvent.json";
        SimulatorParams simulatorParams = new SimulatorParams(VES_URL, 1, 1);
        SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
            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
    void shouldTriggerEventWithDefaultVesUrlWhenNotProvidedInRequest() throws IOException, SchedulerException, GeneralSecurityException {
        String templateName = "validExampleMeasurementEvent.json";
        SimulatorRequest simulatorRequest = new SimulatorRequest(
            new SimulatorParams("", 1, 1),
            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));

        simulatorService.triggerEvent(simulatorRequest);

        assertEventHasExpectedStructure(inDbVesUrl.toString(), templateName, SOME_CUSTOM_SOURCE);
    }

    @Test
    void shouldThrowJsonSyntaxWhenInvalidJson() {
        //given
        JsonObject patch = GSON.fromJson("{\n"
            + "  \"event\": {\n"
            + "    \"commonEventHeader\": {\n"
            + "      \"sourceName\": \""
            + SOME_CUSTOM_SOURCE + "\"\n"
            + "    }\n"
            + "  }\n"
            + "}\n", JsonObject.class);
        EventData eventData = EventData.builder().id("1").build();

        SimulatorParams simulatorParams = new SimulatorParams(VES_URL, 1, 1);
        SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
            "invalidJsonStructureEvent.json", patch, new JsonObject());
        doReturn(eventData).when(eventDataService).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));

        //when
        assertThrows(JsonSyntaxException.class,
            () -> simulatorService.triggerEvent(simulatorRequest));
    }

    @Test
    void shouldHandleNonExistingPatchSection() throws IOException, SchedulerException, GeneralSecurityException {
        String templateName = "validExampleMeasurementEvent.json";
        SimulatorRequest simulatorRequest = new SimulatorRequest(
            new SimulatorParams("", 1, 1),
            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();

        simulatorService.triggerEvent(simulatorRequest);

        assertEventHasExpectedStructure(inDbVesUrl.toString(), templateName, CLOSED_LOOP_VNF);
    }

    @Test
    void shouldSuccessfullySendOneTimeEventWithVesUrlWhenPassed() throws IOException, GeneralSecurityException {
        SimulatorService spiedTestedService = spy(new SimulatorService(
            templatePatcher, templateReader, eventScheduler,
            eventDataService, simulatorConfigService,
            new TemplateVariablesReplacer(),
            new SslAuthenticationHelper()));

        HttpClientAdapter adapterMock = mock(HttpClientAdapter.class);
        prepareMocksWithAcceptedResponse(spiedTestedService, adapterMock);
        FullEvent event = new FullEvent(VES_URL, VALID_FULL_EVENT);

        spiedTestedService.triggerOneTimeEvent(event);

        assertThat(eventContentCaptor.getValue()).isEqualTo(VALID_FULL_EVENT.toString());
        verify(eventDataService, times(1)).persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class), any(JsonObject.class));
        verify(adapterMock, times(1)).send(VALID_FULL_EVENT.toString());
    }

    @Test
    void shouldSubstituteKeywordsAndSuccessfullySendOneTimeEvent() throws IOException, GeneralSecurityException {
        SimulatorService spiedTestedService = spy(new SimulatorService(
            templatePatcher, templateReader, eventScheduler,
            eventDataService, simulatorConfigService,
            new TemplateVariablesReplacer(),
            new SslAuthenticationHelper())
        );

        HttpClientAdapter adapterMock = mock(HttpClientAdapter.class);
        prepareMocksWithAcceptedResponse(spiedTestedService, adapterMock);
        FullEvent event = new FullEvent(VES_URL, FULL_EVENT_WITH_KEYWORDS);

        spiedTestedService.triggerOneTimeEvent(event);

        JsonObject sentContent = GSON.fromJson(eventContentCaptor.getValue(), JsonElement.class).getAsJsonObject();
        assertThat(sentContent.getAsJsonObject("event").getAsJsonObject("commonEventHeader").get("eventOrderNo").getAsString()).isEqualTo("1");
        assertThat(sentContent.getAsJsonObject("event").getAsJsonObject("commonEventHeader").get("eventName").getAsString()).hasSize(20);
    }

    @Test
    void shouldGetSimulatorConfiguration() {
        SimulatorConfig simulatorConfig = getSimulatorConfig();

        when(simulatorConfigService.getConfiguration()).thenReturn(simulatorConfig);

        assertEquals(simulatorService.getConfiguration(), simulatorConfig);
    }

    @Test
    void shouldUpdateSimulatorConfiguration() {
        SimulatorConfig simulatorConfig = getSimulatorConfig();

        when(simulatorConfigService.updateConfiguration(simulatorConfig)).thenReturn(simulatorConfig);

        assertEquals(simulatorService.updateConfiguration(simulatorConfig), simulatorConfig);
    }

    @Test
    void shouldCancelAllEvents() throws SchedulerException {
        when(eventScheduler.cancelAllEvents()).thenReturn(true);

        assertTrue(simulatorService.cancelAllEvents());
    }

    @Test
    void shouldCancelSingleEvent() throws SchedulerException {
        final String jobName = "testJobName";
        when(eventScheduler.cancelEvent(jobName)).thenReturn(true);

        assertTrue(simulatorService.cancelEvent(jobName));
    }

    private void prepareMocksWithAcceptedResponse(SimulatorService spiedTestedService, HttpClientAdapter adapterMock) throws IOException, GeneralSecurityException {
        HttpResponseAdapter response = new HttpResponseAdapter(HttpStatus.SC_ACCEPTED, HttpTestUtils.HTTP_MESSAGE_ACCEPTER);
        doReturn(response).when(adapterMock).send(eventContentCaptor.capture());
        doReturn(adapterMock).when(spiedTestedService).createHttpClientAdapter(any(String.class));
    }

    private void assertEventHasExpectedStructure(String expectedVesUrl, String templateName, String sourceNameString) throws SchedulerException, IOException, GeneralSecurityException {
        verify(eventScheduler, times(1)).scheduleEvent(vesUrlCaptor.capture(), intervalCaptor.capture(),
            repeatCountCaptor.capture(), templateNameCaptor.capture(), eventIdCaptor.capture(), bodyCaptor.capture());
        assertThat(vesUrlCaptor.getValue()).isEqualTo(expectedVesUrl);
        assertThat(intervalCaptor.getValue()).isEqualTo(1);
        assertThat(repeatCountCaptor.getValue()).isEqualTo(1);
        assertThat(templateNameCaptor.getValue()).isEqualTo(templateName);
        String actualSourceName = GSON.fromJson(bodyCaptor.getValue(), JsonObject.class)
            .get("event").getAsJsonObject()
            .get("commonEventHeader").getAsJsonObject()
            .get("sourceName").getAsString();
        assertThat(actualSourceName).isEqualTo(sourceNameString);
        verify(eventDataService)
            .persistEventData(any(JsonObject.class), any(JsonObject.class), any(JsonObject.class),
                any(JsonObject.class));
    }

    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");
    }

}