aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java
blob: 8dba750c3cda2035143dceea0117cf7d87aae719 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * ============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.rest;

import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
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.SimulatorService;
import org.onap.pnfsimulator.simulator.client.HttpResponseAdapter;
import org.onap.pnfsimulator.simulatorconfig.SimulatorConfig;
import org.quartz.SchedulerException;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class SimulatorControllerTest {

    private static final String START_ENDPOINT = "/simulator/start";
    private static final String CONFIG_ENDPOINT = "/simulator/config";
    private static final String EVENT_ENDPOINT = "/simulator/event";
    private static final String CANCEL_JOB_ENDPOINT = "/simulator/cancel/";
    private static final String ALL_EVENTS_ENDPOINT = "/simulator/all-events";
    private static final String TEST_ENDPOINT = "/simulator/test";

    private static final String JSON_MSG_EXPRESSION = "$.message";
    private static final String EVENT_WAS_CANCELLED = "Event(s) was cancelled";
    private static final String EVENT_WAS_NOT_CANCELLED = "Simulator was not able to cancel event(s)";

    private static final String NEW_URL = "http://0.0.0.0:8090/eventListener/v7";
    private static final String UPDATE_SIM_CONFIG_VALID_JSON = "{\"vesServerUrl\": \""
            + NEW_URL + "\"}";
    private static final String SAMPLE_ID = "sampleId";
    private static final Gson GSON_OBJ = new Gson();
    private static final String JOB_NAME = "testJobName";
    private static final HttpResponseAdapter TEST_HTTP_ACCEPTED_RESPONSE = new HttpResponseAdapter(202,"");
    private static String simulatorRequestBody;
    private MockMvc mockMvc;
    @InjectMocks
    private SimulatorController controller;
    @Mock
    private EventDataService eventDataService;
    @Mock
    private SimulatorService simulatorService;

    @BeforeAll
    static void beforeAll() {
        SimulatorParams simulatorParams = new SimulatorParams("http://0.0.0.0:8080", 1, 1);
        SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
                "testTemplate.json", new JsonObject(), new JsonObject());

        simulatorRequestBody = GSON_OBJ.toJson(simulatorRequest);
    }

    @BeforeEach
    void setup() throws IOException, SchedulerException, GeneralSecurityException {
        MockitoAnnotations.initMocks(this);
        when(simulatorService.triggerEvent(any())).thenReturn("jobName");
        when(simulatorService.triggerOneTimeEvent(any())).thenReturn(TEST_HTTP_ACCEPTED_RESPONSE);
        mockMvc = MockMvcBuilders
                .standaloneSetup(controller)
                .build();
    }

    @Test
    void shouldStartSimulatorProperly() throws Exception {
        startSimulator();
        SimulatorRequest simulatorRequest = new Gson().fromJson(simulatorRequestBody, SimulatorRequest.class);

        verify(simulatorService).triggerEvent(eq(simulatorRequest));
    }

    @Test
    void testShouldGetConfigurationWhenRequested() throws Exception {
        String newUrl = "http://localhost:8090/eventListener/v7";
        SimulatorConfig expectedConfig = new SimulatorConfig(SAMPLE_ID, new URL(newUrl));
        when(simulatorService.getConfiguration()).thenReturn(expectedConfig);

        MvcResult getResult = mockMvc
                .perform(get(CONFIG_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(UPDATE_SIM_CONFIG_VALID_JSON))
                .andExpect(status().isOk())
                .andReturn();

        String expectedVesUrlJsonPart = createStringReprOfJson("vesServerUrl", newUrl);
        assertThat(getResult.getResponse().getContentAsString()).contains(expectedVesUrlJsonPart);
    }

    @Test
    void testShouldSuccessfullyUpdateConfigurationWithNewVesUrl() throws Exception {
        String oldUrl = "http://localhost:8090/eventListener/v7";
        SimulatorConfig expectedConfigBeforeUpdate = new SimulatorConfig(SAMPLE_ID, new URL(oldUrl));
        SimulatorConfig expectedConfigAfterUpdate = new SimulatorConfig(SAMPLE_ID, new URL(NEW_URL));

        when(simulatorService.getConfiguration()).thenReturn(expectedConfigBeforeUpdate);
        when(simulatorService.updateConfiguration(any(SimulatorConfig.class))).thenReturn(expectedConfigAfterUpdate);

        MvcResult postResult = mockMvc
                .perform(put(CONFIG_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(UPDATE_SIM_CONFIG_VALID_JSON))
                .andExpect(status().isOk())
                .andReturn();

        String expectedVesUrlJsonPart = createStringReprOfJson("vesServerUrl", expectedConfigAfterUpdate.getVesServerUrl().toString());
        assertThat(postResult.getResponse().getContentAsString()).contains(expectedVesUrlJsonPart);
    }

    @Test
    void testShouldRaiseExceptionWhenUpdateConfigWithIncorrectPayloadWasSent() throws Exception {
        mockMvc
                .perform(put(CONFIG_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content("{\"vesUrl\": \""
                                + NEW_URL + "\"}"))
                .andExpect(status().isBadRequest());
    }

    @Test
    void testShouldRaiseExceptionWhenUrlInInvalidFormatIsSent() throws Exception {
        mockMvc
                .perform(put(CONFIG_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content("{\"vesUrl\": \"http://0.0.0.0:VES-PORT/eventListener/v7\"}"))
                .andExpect(status().isBadRequest());
    }

    @Test
    void testShouldSendEventDirectly() throws Exception {
        String contentAsString = mockMvc
                .perform(post(EVENT_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                        .content("{\"vesServerUrl\":\"http://0.0.0.0:8080/simulator/v7\",\n"
                                + "      \"event\":{  \n"
                                + "         \"commonEventHeader\":{  \n"
                                + "            \"domain\":\"notification\",\n"
                                + "            \"eventName\":\"vFirewallBroadcastPackets\"\n"
                                + "         },\n"
                                + "         \"notificationFields\":{  \n"
                                + "            \"arrayOfNamedHashMap\":[  \n"
                                + "               {  \n"
                                + "                  \"name\":\"A20161221.1031-1041.bin.gz\",\n"
                                + "                  \"hashMap\":{  \n"
                                + "                     \"fileformatType\":\"org.3GPP.32.435#measCollec\"}}]}}}"))
                .andExpect(status().isAccepted()).andReturn().getResponse().getContentAsString();
        assertThat(contentAsString).contains("One-time direct event sent successfully");
    }

    @Test
    void testShouldReplaceKeywordsAndSendEventDirectly() throws Exception {
        String contentAsString = mockMvc
                .perform(post(EVENT_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                        .content("{\"vesServerUrl\": \"http://localhost:9999/eventListener\",\n"
                                + "    \"event\": {\n"
                                + "        \"commonEventHeader\": {\n"
                                + "            \"eventId\": \"#RandomString(20)\",\n"
                                + "            \"sourceName\": \"PATCHED_sourceName\",\n"
                                + "            \"version\": 3.0\n}}}"))
                .andExpect(status().isAccepted()).andReturn().getResponse().getContentAsString();
        assertThat(contentAsString).contains("One-time direct event sent successfully");

        verify(simulatorService, Mockito.times(1)).triggerOneTimeEvent(any(FullEvent.class));
    }

    @Test
    void shouldUseTestEndpointThenReceiveProperMessage() throws Exception {
        String contentAsString = mockMvc
                .perform(post(TEST_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                        .content("{\"simulatorParams\": {\n" +
                                "        \"vesServerUrl\": \"http://localhost:9999/eventListener\"\n" +
                                "    },\n" +
                                "    \"templateName\": \"testTemplateName\"\n" +
                                "}"))
                .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
        assertThat(contentAsString).contains("message1234");
    }

    @Test
    void shouldSuccessfullyCancelJobThenReturnProperMessage() throws Exception {
        when(simulatorService.cancelEvent(JOB_NAME)).thenReturn(true);

        String contentAsString = mockMvc
                .perform(post(CANCEL_JOB_ENDPOINT + JOB_NAME)
                        .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                        .content(""))
                .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();

        assertThat(contentAsString).contains(EVENT_WAS_CANCELLED);
    }

    @Test
    void shouldFailWhileCancelingJobThenReturnProperMessage() throws Exception {
        when(simulatorService.cancelEvent(JOB_NAME)).thenReturn(false);

        String contentAsString = mockMvc
                .perform(post(CANCEL_JOB_ENDPOINT + JOB_NAME)
                        .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                        .content(""))
                .andExpect(status().isNotFound()).andReturn().getResponse().getContentAsString();

        assertThat(contentAsString).contains(EVENT_WAS_NOT_CANCELLED);
    }

    @Test
    void shouldSuccessfullyCancelAllJobsThenReturnsProperMessage() throws Exception {
        when(simulatorService.cancelAllEvents()).thenReturn(true);

        String contentAsString = mockMvc
                .perform(post(CANCEL_JOB_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                        .content(""))
                .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();

        assertThat(contentAsString).contains(EVENT_WAS_CANCELLED);
    }

    @Test
    void shouldSuccessfullyCancelJobWhenSendingJobNameWithBreakingCharactersThenReturnProperMessage() throws SchedulerException {
        final String lineBreakingJobName = "test\tJob\nName\r";
        when(simulatorService.cancelEvent(lineBreakingJobName)).thenReturn(true);

        Object actualResponseBody = Objects.requireNonNull(controller.cancelEvent(lineBreakingJobName).getBody());

        assertThat(actualResponseBody.toString()).contains(EVENT_WAS_CANCELLED);
    }

    @Test
    void shouldReturnAllEvents() throws Exception {
        List<EventData> events = getEventDatas();
        String expectedMessage = events.stream()
                .map(EventData::toString)
                .collect(Collectors.joining("\\n"));

        when(eventDataService.getAllEvents()).thenReturn(events);

        String contentAsString = mockMvc
                .perform(get(ALL_EVENTS_ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(""))
                .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();

        assertThat(contentAsString).contains(expectedMessage);
    }


    private List<EventData> getEventDatas() {
        return Arrays.asList(
                getEventData("id1", "keywords1", "input1", "patched1", "template1", 0),
                getEventData("id2", "keywords2", "input2", "patched2", "template2", 1)
        );
    }

    private EventData getEventData(String id, String keywords, String input, String patched, String template, int incrementValue) {
        return EventData.builder()
                .id(id)
                .keywords(keywords)
                .input(input)
                .patched(patched)
                .template(template)
                .incrementValue(incrementValue)
                .build();
    }

    private void startSimulator() throws Exception {
        mockMvc
                .perform(post(START_ENDPOINT)
                        .content(simulatorRequestBody)
                        .contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8"))
                .andExpect(status().isOk())
                .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Request started"));

    }

    private String createStringReprOfJson(String key, String value) {
        return GSON_OBJ.toJson(ImmutableMap.of(key, value));
    }
}