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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.rest;
23 import com.google.common.collect.ImmutableMap;
24 import com.google.gson.Gson;
25 import com.google.gson.JsonObject;
26 import org.junit.jupiter.api.BeforeAll;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.pnfsimulator.rest.model.FullEvent;
34 import org.onap.pnfsimulator.rest.model.SimulatorParams;
35 import org.onap.pnfsimulator.rest.model.SimulatorRequest;
36 import org.onap.pnfsimulator.rest.util.JsonObjectDeserializer;
37 import org.onap.pnfsimulator.simulator.SimulatorService;
38 import org.onap.pnfsimulator.simulatorconfig.SimulatorConfig;
39 import org.quartz.SchedulerException;
40 import org.springframework.http.MediaType;
41 import org.springframework.test.web.servlet.MockMvc;
42 import org.springframework.test.web.servlet.MvcResult;
43 import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
44 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
46 import java.io.IOException;
49 import static org.assertj.core.api.Java6Assertions.assertThat;
50 import static org.mockito.ArgumentMatchers.any;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.verify;
53 import static org.mockito.Mockito.when;
54 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
55 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
56 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
57 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
58 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
60 class SimulatorControllerTest {
62 private static final String START_ENDPOINT = "/simulator/start";
63 private static final String CONFIG_ENDPOINT = "/simulator/config";
64 private static final String EVENT_ENDPOINT = "/simulator/event";
65 private static final String JSON_MSG_EXPRESSION = "$.message";
67 private static final String NEW_URL = "http://0.0.0.0:8090/eventListener/v7";
68 private static final String UPDATE_SIM_CONFIG_VALID_JSON = "{\"vesServerUrl\": \"" + NEW_URL + "\"}";
69 private static final String SAMPLE_ID = "sampleId";
70 private static final Gson GSON_OBJ = new Gson();
71 private static String simulatorRequestBody;
72 private MockMvc mockMvc;
74 private SimulatorController controller;
76 private SimulatorService simulatorService;
79 static void beforeAll() {
80 SimulatorParams simulatorParams = new SimulatorParams("http://0.0.0.0:8080", 1, 1);
81 SimulatorRequest simulatorRequest = new SimulatorRequest(simulatorParams,
82 "testTemplate.json", new JsonObject());
84 simulatorRequestBody = GSON_OBJ.toJson(simulatorRequest);
88 void setup() throws IOException, SchedulerException {
89 MockitoAnnotations.initMocks(this);
90 when(simulatorService.triggerEvent(any())).thenReturn("jobName");
91 mockMvc = MockMvcBuilders
92 .standaloneSetup(controller)
97 void shouldStartSimulatorProperly() throws Exception {
99 SimulatorRequest simulatorRequest = new Gson().fromJson(simulatorRequestBody, SimulatorRequest.class);
101 verify(simulatorService).triggerEvent(eq(simulatorRequest));
105 void testShouldGetConfigurationWhenRequested() throws Exception {
106 String newUrl = "http://localhost:8090/eventListener/v7";
107 SimulatorConfig expectedConfig = new SimulatorConfig(SAMPLE_ID, new URL(newUrl));
108 when(simulatorService.getConfiguration()).thenReturn(expectedConfig);
110 MvcResult getResult = mockMvc
111 .perform(get(CONFIG_ENDPOINT)
112 .contentType(MediaType.APPLICATION_JSON)
113 .content(UPDATE_SIM_CONFIG_VALID_JSON))
114 .andExpect(status().isOk())
117 String expectedVesUrlJsonPart = createStringReprOfJson("vesServerUrl", newUrl);
118 assertThat(getResult.getResponse().getContentAsString()).contains(expectedVesUrlJsonPart);
122 void testShouldSuccessfullyUpdateConfigurationWithNewVesUrl() throws Exception {
123 String oldUrl = "http://localhost:8090/eventListener/v7";
124 SimulatorConfig expectedConfigBeforeUpdate = new SimulatorConfig(SAMPLE_ID, new URL(oldUrl));
125 SimulatorConfig expectedConfigAfterUpdate = new SimulatorConfig(SAMPLE_ID, new URL(NEW_URL));
127 when(simulatorService.getConfiguration()).thenReturn(expectedConfigBeforeUpdate);
128 when(simulatorService.updateConfiguration(any(SimulatorConfig.class))).thenReturn(expectedConfigAfterUpdate);
130 MvcResult postResult = mockMvc
131 .perform(put(CONFIG_ENDPOINT)
132 .contentType(MediaType.APPLICATION_JSON)
133 .content(UPDATE_SIM_CONFIG_VALID_JSON))
134 .andExpect(status().isOk())
137 String expectedVesUrlJsonPart = createStringReprOfJson("vesServerUrl", expectedConfigAfterUpdate.getVesServerUrl().toString());
138 assertThat(postResult.getResponse().getContentAsString()).contains(expectedVesUrlJsonPart);
142 void testShouldRaiseExceptionWhenUpdateConfigWithIncorrectPayloadWasSent() throws Exception {
144 .perform(put(CONFIG_ENDPOINT)
145 .contentType(MediaType.APPLICATION_JSON)
146 .content("{\"vesUrl\": \"" + NEW_URL + "\"}"))
147 .andExpect(status().isBadRequest());
151 void testShouldRaiseExceptionWhenUrlInInvalidFormatIsSent() throws Exception {
153 .perform(put(CONFIG_ENDPOINT)
154 .contentType(MediaType.APPLICATION_JSON)
155 .content("{\"vesUrl\": \"http://0.0.0.0:VES-PORT/eventListener/v7\"}"))
156 .andExpect(status().isBadRequest());
160 void testShouldSendEventDirectly() throws Exception {
161 String contentAsString = mockMvc
162 .perform(post(EVENT_ENDPOINT)
163 .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
164 .content("{\"vesServerUrl\":\"http://0.0.0.0:8080/simulator/v7\",\n" +
166 " \"commonEventHeader\":{ \n" +
167 " \"domain\":\"notification\",\n" +
168 " \"eventName\":\"vFirewallBroadcastPackets\"\n" +
170 " \"notificationFields\":{ \n" +
171 " \"arrayOfNamedHashMap\":[ \n" +
173 " \"name\":\"A20161221.1031-1041.bin.gz\",\n" +
174 " \"hashMap\":{ \n" +
175 " \"fileformatType\":\"org.3GPP.32.435#measCollec\"}}]}}}"))
176 .andExpect(status().isAccepted()).andReturn().getResponse().getContentAsString();
177 assertThat(contentAsString).contains("One-time direct event sent successfully");
181 void testShouldReplaceKeywordsAndSendEventDirectly() throws Exception {
182 String contentAsString = mockMvc
183 .perform(post(EVENT_ENDPOINT)
184 .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
185 .content("{\"vesServerUrl\": \"http://localhost:9999/eventListener\",\n" +
187 " \"commonEventHeader\": {\n" +
188 " \"eventId\": \"#RandomString(20)\",\n" +
189 " \"sourceName\": \"PATCHED_sourceName\",\n" +
190 " \"version\": 3.0\n}}}"))
191 .andExpect(status().isAccepted()).andReturn().getResponse().getContentAsString();
192 assertThat(contentAsString).contains("One-time direct event sent successfully");
194 verify(simulatorService, Mockito.times(1)).triggerOneTimeEvent(any(FullEvent.class));
198 private void startSimulator() throws Exception {
200 .perform(post(START_ENDPOINT)
201 .content(simulatorRequestBody)
202 .contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8"))
203 .andExpect(status().isOk())
204 .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Request started"));
208 private String createStringReprOfJson(String key, String value) {
209 return GSON_OBJ.toJson(ImmutableMap.of(key, value));