Remove outdated doc for A1 Adaptor
[integration.git] / test / mocks / masspnfsim / pnf-sim-lightweight / src / test / java / org / onap / pnfsimulator / rest / SimulatorControllerTest.java
1 /*
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
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.rest;
22
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;
55
56 class SimulatorControllerTest {
57
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" +
69         "  },\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" +
77         "  },\n" +
78
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" +
87         "  }\n" +
88         "}";
89     private static final String WRONG_JSON = "{\n" +
90         "  \"mes\": {\n" +
91         "    \"vesServerUrl\": \"http://10.154.187.70:8080/eventListener/v5\",\n" +
92         "    \"testDuration\": \"10\",\n" +
93         "    \"messageInterval\": \"1\"\n" +
94         "  },\n" +
95         "  \"messageParams\": {\n" +
96         "    \"sourceName\": \"val12\",\n" +
97         "    \"sourceId\": \"val13\",\n" +
98         "    \"reportingEntityName\": \"val14\"\n" +
99         "  }\n" +
100         "}\n";
101
102     private MockMvc mockMvc;
103
104     @InjectMocks
105     private SimulatorController controller;
106
107     @Mock
108     private SimulatorFactory factory;
109     @Mock
110     private JSONValidator validator;
111
112     private Simulator simulator;
113
114     private FileProvider fileProvider = mock(FileProvider.class);
115
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");
120
121         try {
122             doReturn(fileList).when(fileProvider).getFiles();
123         } catch (NoRopFilesException e) {
124             e.printStackTrace();
125         }
126     }
127
128     @BeforeEach
129     void setup() {
130         MockitoAnnotations.initMocks(this);
131         createSampleFileList();
132         simulator = createEndlessSimulator();
133         mockMvc = MockMvcBuilders
134             .standaloneSetup(controller)
135             .build();
136     }
137
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))
148             .build());
149     }
150
151     @Test
152     void wrongJSONFormatOnStart() throws Exception {
153         when(factory.create(any(),any(), any(),any())).thenReturn(simulator);
154         doThrow(new ValidationException("")).when(validator).validate(anyString(), anyString());
155
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());
161     }
162
163     @Test
164     void startSimulatorProperly() throws Exception {
165         startSimulator();
166
167         verify(validator).validate(anyString(), anyString());
168         verify(factory).create(any(),any(), any(),any());
169         verify(simulator).start();
170     }
171
172     @Test
173     void notStartWhenAlreadyRunning() throws Exception {
174         startSimulator();
175
176         mockMvc
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"));
180     }
181
182     @Test
183     void stopSimulatorWhenRunning() throws Exception {
184         startSimulator();
185
186         mockMvc
187             .perform(post(STOP_URL))
188             .andExpect(status().isOk())
189             .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator successfully stopped"));
190     }
191
192     @Test
193     void getNotRunningMessageWhenOff() throws Exception {
194         mockMvc
195             .perform(post(STOP_URL))
196             .andExpect(status().isBadRequest())
197             .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot stop simulator, because it's not running"));
198     }
199
200     @Test
201     void getRunningStatusWhenOn() throws Exception {
202         startSimulator();
203
204         mockMvc
205             .perform(get(STATUS_URL))
206             .andExpect(status().isOk())
207             .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("RUNNING"));
208     }
209
210     @Test
211     void getNotRunningStatusWhenOff() throws Exception {
212         mockMvc
213             .perform(get(STATUS_URL))
214             .andExpect(status().isOk())
215             .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("NOT RUNNING"));
216     }
217
218     private void startSimulator() throws Exception {
219         when(factory.create(any(), any(), any(),any())).thenReturn(simulator);
220
221         mockMvc
222             .perform(post(START_URL).content(PROPER_JSON))
223             .andExpect(status().isOk())
224             .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator started"));
225     }
226 }