Merge "switch drools pdp image to new one"
[integration.git] / test / mocks / mass-pnf-sim / 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.anyString;
24 import static org.mockito.Mockito.any;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
32 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
33 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34
35 import java.time.Duration;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.pnfsimulator.simulator.Simulator;
42 import org.onap.pnfsimulator.simulator.SimulatorFactory;
43 import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
44 import org.onap.pnfsimulator.simulator.validation.JSONValidator;
45 import org.onap.pnfsimulator.simulator.validation.ValidationException;
46 import org.springframework.test.web.servlet.MockMvc;
47 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
48
49 class SimulatorControllerTest {
50
51     private static final String START_URL = "/simulator/start";
52     private static final String STOP_URL = "/simulator/stop";
53     private static final String STATUS_URL = "/simulator/status";
54     private static final String JSON_MSG_EXPRESSION = "$.message";
55     private static final String JSON_STATUS_EXPRESSION = "$.simulatorStatus";
56     private static final String PROPER_JSON = "{\n" +
57         "  \"simulatorParams\": {\n" +
58         "    \"vesServerUrl\": \"http://10.154.187.70:8080/eventListener/v7\",\n" +
59         "    \"testDuration\": \"10\",\n" +
60         "    \"messageInterval\": \"1\"\n" +
61         "  },\n" +
62         "  \"commonEventHeaderParams\": {\n" +
63         "    \"eventName\": \"val11\",\n" +
64         "    \"nfNamingCode\": \"val12\",\n" +
65         "    \"nfcNamingCode\": \"val13\",\n" +
66         "    \"sourceName\": \"val14\",\n" +
67         "    \"sourceId\": \"val15\",\n" +
68         "    \"reportingEntityName\": \"val16\",\n" +
69         "  },\n" +
70
71         "  \"pnfRegistrationParams\": {\n" +
72         "    \"SerialNumber\": \"val1\",\n" +
73         "    \"VendorName\": \"val2\",\n" +
74         "    \"OamIpv4Address\": \"val3\",\n" +
75         "    \"OamIpv6Address\": \"val4\",\n" +
76         "    \"Family\": \"val5\",\n" +
77         "    \"ModelNumber\": \"val6\",\n" +
78         "    \"SoftwareVersion\": \"val7\",\n" +
79         "  }\n" +
80         "}";
81     private static final String WRONG_JSON = "{\n" +
82         "  \"mes\": {\n" +
83         "    \"vesServerUrl\": \"http://10.154.187.70:8080/eventListener/v5\",\n" +
84         "    \"testDuration\": \"10\",\n" +
85         "    \"messageInterval\": \"1\"\n" +
86         "  },\n" +
87         "  \"messageParams\": {\n" +
88         "    \"sourceName\": \"val12\",\n" +
89         "    \"sourceId\": \"val13\",\n" +
90         "    \"reportingEntityName\": \"val14\"\n" +
91         "  }\n" +
92         "}\n";
93
94     private MockMvc mockMvc;
95
96     @InjectMocks
97     private SimulatorController controller;
98
99     @Mock
100     private SimulatorFactory factory;
101     @Mock
102     private JSONValidator validator;
103
104     private Simulator simulator;
105
106     @BeforeEach
107     void setup() {
108         MockitoAnnotations.initMocks(this);
109         simulator = createEndlessSimulator();
110         mockMvc = MockMvcBuilders
111             .standaloneSetup(controller)
112             .build();
113     }
114
115     private Simulator createEndlessSimulator() {
116         return spy(Simulator.builder()
117             .withCustomHttpClientAdapter(mock(HttpClientAdapter.class))
118             .withInterval(Duration.ofMinutes(1))
119             .build());
120     }
121
122     @Test
123     void wrongJSONFormatOnStart() throws Exception {
124         when(factory.create(any(),any(), any(),any())).thenReturn(simulator);
125         doThrow(new ValidationException("")).when(validator).validate(anyString(), anyString());
126
127         mockMvc.perform(post("/simulator/start").content(WRONG_JSON))
128             .andExpect(status().isBadRequest())
129             .andExpect(jsonPath("$.message").value("Cannot start simulator - Json format " +
130                 "is not compatible with schema definitions"));
131         verify(validator).validate(anyString(), anyString());
132     }
133
134     @Test
135     void startSimulatorProperly() throws Exception {
136         startSimulator();
137
138         verify(validator).validate(anyString(), anyString());
139         verify(factory).create(any(),any(), any(),any());
140         verify(simulator).start();
141     }
142
143     @Test
144     void notStartWhenAlreadyRunning() throws Exception {
145         startSimulator();
146
147         mockMvc
148             .perform(post(START_URL).content(PROPER_JSON))
149             .andExpect(status().isBadRequest())
150             .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot start simulator since it's already running"));
151     }
152
153     @Test
154     void stopSimulatorWhenRunning() throws Exception {
155         startSimulator();
156
157         mockMvc
158             .perform(post(STOP_URL))
159             .andExpect(status().isOk())
160             .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator successfully stopped"));
161     }
162
163     @Test
164     void getNotRunningMessageWhenOff() throws Exception {
165         mockMvc
166             .perform(post(STOP_URL))
167             .andExpect(status().isBadRequest())
168             .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot stop simulator, because it's not running"));
169     }
170
171     @Test
172     void getRunningStatusWhenOn() throws Exception {
173         startSimulator();
174
175         mockMvc
176             .perform(get(STATUS_URL))
177             .andExpect(status().isOk())
178             .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("RUNNING"));
179     }
180
181     @Test
182     void getNotRunningStatusWhenOff() throws Exception {
183         mockMvc
184             .perform(get(STATUS_URL))
185             .andExpect(status().isOk())
186             .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("NOT RUNNING"));
187     }
188
189     private void startSimulator() throws Exception {
190         when(factory.create(any(), any(), any(),any())).thenReturn(simulator);
191
192         mockMvc
193             .perform(post(START_URL).content(PROPER_JSON))
194             .andExpect(status().isOk())
195             .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator started"));
196     }
197 }