2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. 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 static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import static org.onap.pnfsimulator.simulator.TestMessages.VALID_COMMON_EVENT_HEADER_PARAMS;
32 import static org.onap.pnfsimulator.simulator.TestMessages.VALID_NOTIFICATION_PARAMS;
33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
34 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
35 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
36 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
37 import java.time.Duration;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Optional;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.pnfsimulator.FileProvider;
47 import org.onap.pnfsimulator.simulator.Simulator;
48 import org.onap.pnfsimulator.simulator.SimulatorFactory;
49 import org.onap.pnfsimulator.simulator.client.RestTemplateAdapter;
50 import org.onap.pnfsimulator.simulator.validation.JSONValidator;
51 import org.onap.pnfsimulator.simulator.validation.NoRopFilesException;
52 import org.onap.pnfsimulator.simulator.validation.ValidationException;
53 import org.springframework.test.web.servlet.MockMvc;
54 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
56 class SimulatorControllerTest {
58 private static final String START_URL = "/simulator/start";
59 private static final String STOP_URL = "/simulator/stop";
60 private static final String STATUS_URL = "/simulator/status";
61 private static final String JSON_MSG_EXPRESSION = "$.message";
62 private static final String JSON_STATUS_EXPRESSION = "$.simulatorStatus";
63 private static final String TEST_VES_URL = "http://localhost:10000/eventListener/v7";
64 private static final String TEST_XNF_URL = "sftp://onap:pano@10.11.0.68" + "/";
65 private static final String PROPER_JSON = "{\n" +
66 " \"simulatorParams\": {\n" +
67 " \"testDuration\": \"10\",\n" +
68 " \"messageInterval\": \"1\"\n" +
70 " \"commonEventHeaderParams\": {\n" +
71 " \"eventName\": \"val11\",\n" +
72 " \"nfNamingCode\": \"val12\",\n" +
73 " \"nfcNamingCode\": \"val13\",\n" +
74 " \"sourceName\": \"val14\",\n" +
75 " \"sourceId\": \"val15\",\n" +
76 " \"reportingEntityName\": \"val16\",\n" +
79 " \"pnfRegistrationParams\": {\n" +
80 " \"SerialNumber\": \"val1\",\n" +
81 " \"VendorName\": \"val2\",\n" +
82 " \"OamIpv4Address\": \"val3\",\n" +
83 " \"OamIpv6Address\": \"val4\",\n" +
84 " \"Family\": \"val5\",\n" +
85 " \"ModelNumber\": \"val6\",\n" +
86 " \"SoftwareVersion\": \"val7\",\n" +
89 private static final String WRONG_JSON = "{\n" +
91 " \"vesServerUrl\": \"http://10.154.187.70:8080/eventListener/v5\",\n" +
92 " \"testDuration\": \"10\",\n" +
93 " \"messageInterval\": \"1\"\n" +
95 " \"messageParams\": {\n" +
96 " \"sourceName\": \"val12\",\n" +
97 " \"sourceId\": \"val13\",\n" +
98 " \"reportingEntityName\": \"val14\"\n" +
102 private MockMvc mockMvc;
105 private SimulatorController controller;
108 private SimulatorFactory factory;
110 private JSONValidator validator;
112 private Simulator simulator;
114 private FileProvider fileProvider = mock(FileProvider.class);
116 private void createSampleFileList() {
117 List<String> fileList = new ArrayList<>();
118 fileList.add("A20190401.1608+0000-1622+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz");
119 fileList.add("A20190401.1623+0000-1637+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz");
122 doReturn(fileList).when(fileProvider).getFiles();
123 } catch (NoRopFilesException e) {
130 MockitoAnnotations.initMocks(this);
131 createSampleFileList();
132 simulator = createEndlessSimulator();
133 mockMvc = MockMvcBuilders
134 .standaloneSetup(controller)
138 private Simulator createEndlessSimulator() {
139 return spy(Simulator.builder()
140 .withCustomRestTemplateAdapter(mock(RestTemplateAdapter.class))
141 .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS)
142 .withPnfRegistrationParams(Optional.empty())
143 .withNotificationParams(VALID_NOTIFICATION_PARAMS)
144 .withVesUrl(TEST_VES_URL)
145 .withXnfUrl(TEST_XNF_URL)
146 .withFileProvider(fileProvider)
147 .withInterval(Duration.ofMinutes(1))
152 void wrongJSONFormatOnStart() throws Exception {
153 when(factory.create(any(),any(), any(),any())).thenReturn(simulator);
154 doThrow(new ValidationException("")).when(validator).validate(anyString(), anyString());
156 mockMvc.perform(post("/simulator/start").content(WRONG_JSON))
157 .andExpect(status().isBadRequest())
158 .andExpect(jsonPath("$.message").value("Cannot start simulator - Json format " +
159 "is not compatible with schema definitions"));
160 verify(validator).validate(anyString(), anyString());
164 void startSimulatorProperly() throws Exception {
167 verify(validator).validate(anyString(), anyString());
168 verify(factory).create(any(),any(), any(),any());
169 verify(simulator).start();
173 void notStartWhenAlreadyRunning() throws Exception {
177 .perform(post(START_URL).content(PROPER_JSON))
178 .andExpect(status().isBadRequest())
179 .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot start simulator since it's already running"));
183 void stopSimulatorWhenRunning() throws Exception {
187 .perform(post(STOP_URL))
188 .andExpect(status().isOk())
189 .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator successfully stopped"));
193 void getNotRunningMessageWhenOff() throws Exception {
195 .perform(post(STOP_URL))
196 .andExpect(status().isBadRequest())
197 .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot stop simulator, because it's not running"));
201 void getRunningStatusWhenOn() throws Exception {
205 .perform(get(STATUS_URL))
206 .andExpect(status().isOk())
207 .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("RUNNING"));
211 void getNotRunningStatusWhenOff() throws Exception {
213 .perform(get(STATUS_URL))
214 .andExpect(status().isOk())
215 .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("NOT RUNNING"));
218 private void startSimulator() throws Exception {
219 when(factory.create(any(), any(), any(),any())).thenReturn(simulator);
222 .perform(post(START_URL).content(PROPER_JSON))
223 .andExpect(status().isOk())
224 .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator started"));