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.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;
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;
49 class SimulatorControllerTest {
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" +
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" +
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" +
81 private static final String WRONG_JSON = "{\n" +
83 " \"vesServerUrl\": \"http://10.154.187.70:8080/eventListener/v5\",\n" +
84 " \"testDuration\": \"10\",\n" +
85 " \"messageInterval\": \"1\"\n" +
87 " \"messageParams\": {\n" +
88 " \"sourceName\": \"val12\",\n" +
89 " \"sourceId\": \"val13\",\n" +
90 " \"reportingEntityName\": \"val14\"\n" +
94 private MockMvc mockMvc;
97 private SimulatorController controller;
100 private SimulatorFactory factory;
102 private JSONValidator validator;
104 private Simulator simulator;
108 MockitoAnnotations.initMocks(this);
109 simulator = createEndlessSimulator();
110 mockMvc = MockMvcBuilders
111 .standaloneSetup(controller)
115 private Simulator createEndlessSimulator() {
116 return spy(Simulator.builder()
117 .withCustomHttpClientAdapter(mock(HttpClientAdapter.class))
118 .withInterval(Duration.ofMinutes(1))
123 void wrongJSONFormatOnStart() throws Exception {
124 when(factory.create(any(),any(), any(),any())).thenReturn(simulator);
125 doThrow(new ValidationException("")).when(validator).validate(anyString(), anyString());
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());
135 void startSimulatorProperly() throws Exception {
138 verify(validator).validate(anyString(), anyString());
139 verify(factory).create(any(),any(), any(),any());
140 verify(simulator).start();
144 void notStartWhenAlreadyRunning() throws Exception {
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"));
154 void stopSimulatorWhenRunning() throws Exception {
158 .perform(post(STOP_URL))
159 .andExpect(status().isOk())
160 .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator successfully stopped"));
164 void getNotRunningMessageWhenOff() throws Exception {
166 .perform(post(STOP_URL))
167 .andExpect(status().isBadRequest())
168 .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot stop simulator, because it's not running"));
172 void getRunningStatusWhenOn() throws Exception {
176 .perform(get(STATUS_URL))
177 .andExpect(status().isOk())
178 .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("RUNNING"));
182 void getNotRunningStatusWhenOff() throws Exception {
184 .perform(get(STATUS_URL))
185 .andExpect(status().isOk())
186 .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("NOT RUNNING"));
189 private void startSimulator() throws Exception {
190 when(factory.create(any(), any(), any(),any())).thenReturn(simulator);
193 .perform(post(START_URL).content(PROPER_JSON))
194 .andExpect(status().isOk())
195 .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator started"));