9f11a00a03dc6a7bc9fadfe93b0ac347ffa23f9a
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 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
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.integration;
22
23 import static io.restassured.RestAssured.given;
24 import static io.restassured.RestAssured.when;
25 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
26 import static org.hamcrest.Matchers.equalTo;
27
28 import com.google.gson.JsonObject;
29 import java.io.IOException;
30 import java.net.Inet4Address;
31 import java.net.NetworkInterface;
32 import java.net.SocketException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.Collections;
37 import java.util.UUID;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Mockito;
44 import org.mockito.internal.util.Timer;
45 import org.mockito.internal.verification.VerificationOverTimeImpl;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
49 import org.springframework.test.context.junit4.SpringRunner;
50
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(classes = {Main.class, TestConfiguration.class}, webEnvironment = WebEnvironment.DEFINED_PORT)
53 public class BasicAvailabilityTest {
54
55     @Autowired
56     VesSimulatorController vesSimulatorController;
57
58     @Autowired
59     VesSimulatorService vesSimulatorService;
60
61     private final String ACTION_START = "start";
62
63     private String currenVesSimulatorIp;
64
65     @Before
66     public void setUp() throws Exception {
67         currenVesSimulatorIp = getCurrentIpAddress();
68     }
69
70     @After
71     public void tearDown() {
72         Mockito.reset(vesSimulatorService);
73     }
74
75     @Test
76     public void simulatorShouldFailWhenTriggeredNonexistentTemplate(){
77         //given
78         String startUrl = prepareRequestUrl(ACTION_START);
79         String body = "{\n"
80             + "\"templateName\": \"any_nonexistent_template.json\",\n"
81             + "\"patch\":{},\n"
82             + "\"simulatorParams\": {\n"
83             + "\"vesServerUrl\": \"https://" + currenVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
84             + "\"repeatInterval\": 1,\n"
85             + "\"repeatCount\": 1\n"
86             + "}\n"
87             + "}";
88
89         //when
90         given()
91             .contentType("application/json")
92             .body(body)
93             .when()
94             .post(startUrl)
95             .then()
96             .statusCode(400)
97             .body("message", equalTo("Cannot start simulator - template any_nonexistent_template.json not found."));
98     }
99
100     @Test
101     public void whenTriggeredSimulatorShouldSendSingleEventToVes() {
102         //given
103         String startUrl = prepareRequestUrl(ACTION_START);
104         String body = "{\n"
105             + "\"templateName\": \"notification.json\",\n"
106             + "\"patch\":{},\n"
107             + "\"simulatorParams\": {\n"
108             + "\"vesServerUrl\": \"https://" + currenVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
109             + "\"repeatInterval\": 1,\n"
110             + "\"repeatCount\": 1\n"
111             + "}\n"
112             + "}";
113         ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
114
115         //when
116         given()
117             .contentType("application/json")
118             .body(body)
119             .when()
120             .post(startUrl)
121             .then()
122             .statusCode(200)
123             .body("message", equalTo("Request started"));
124
125         Mockito.verify(vesSimulatorService,
126             Mockito.timeout(3000))
127             .sendEventToDmaapV5(parameterCaptor.capture());
128
129         assertThat(parameterCaptor.getValue()
130             .getAsJsonObject("event")
131             .getAsJsonObject("commonEventHeader")
132             .get("domain").getAsString()).isEqualTo("notification");
133     }
134
135     @Test
136     public void simulatorShouldCorrectlyRespondOnCancellAllEvent() {
137         //given
138         String ACTION_CANCEL_ALL = "cancel";
139         String cancelAllUrl = prepareRequestUrl(ACTION_CANCEL_ALL);
140
141         //when
142         when()
143             .post(cancelAllUrl)
144             .then()
145             .statusCode(200)
146             .body("message", equalTo("Event(s) was cancelled"));
147
148     }
149
150     @Test
151     public void simulatorBeAbleToUseNewlyAddedTemplate() throws IOException {
152         //given
153         String templateBody = "{\"fake\":\"template\"}\n";
154         String fileName = UUID.randomUUID() + ".json";
155         String requestBody = "{\n"
156             + "\"templateName\": \"" + fileName + "\",\n"
157             + "\"patch\":{},\n"
158             + "\"simulatorParams\": {\n"
159             + "\"vesServerUrl\": \"https://" + currenVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
160             + "\"repeatInterval\": 1,\n"
161             + "\"repeatCount\": 1\n"
162             + "}\n"
163             + "}";
164         ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
165
166         //when
167         Path newFile = Files.createFile(Paths.get("..", "templates", fileName));
168         Files.write(newFile, templateBody.getBytes());
169
170         given()
171             .contentType("application/json")
172             .body(requestBody)
173             .when()
174             .post(prepareRequestUrl(ACTION_START));
175
176         Files.delete(newFile);
177
178         //then
179         Mockito.verify(vesSimulatorService, Mockito.timeout(3000))
180             .sendEventToDmaapV5(parameterCaptor.capture());
181         assertThat(parameterCaptor.getValue()
182             .get("fake").getAsString()).isEqualTo("template");
183
184     }
185
186     @Test
187     public void whenTriggeredSimulatorShouldSendGivenAmountOfEventsToVes() {
188         //given
189         String startUrl = prepareRequestUrl(ACTION_START);
190         String body = "{\n"
191             + "\"templateName\": \"notification.json\",\n"
192             + "\"patch\":{},\n"
193             + "\"simulatorParams\": {\n"
194             + "\"vesServerUrl\": \"https://" + currenVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
195             + "\"repeatInterval\": 1,\n"
196             + "\"repeatCount\": 4\n"
197             + "}\n"
198             + "}";
199         ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
200
201         //when
202         given()
203             .contentType("application/json")
204             .body(body)
205             .when()
206             .post(startUrl)
207             .then()
208             .statusCode(200)
209             .body("message", equalTo("Request started"));
210
211         VerificationOverTimeImpl verificator = new VerificationOverTimeImpl(100, Mockito.times(4), false, new Timer(6000));
212         Mockito.verify(vesSimulatorService, verificator).sendEventToDmaapV5(parameterCaptor.capture());
213
214         for (JsonObject value : parameterCaptor.getAllValues()) {
215             assertThat(value
216                 .getAsJsonObject("event")
217                 .getAsJsonObject("commonEventHeader")
218                 .get("domain").getAsString()).isEqualTo("notification");
219         }
220     }
221
222     private String prepareRequestUrl(String action) {
223         return "http://0.0.0.0:5000/simulator/" + action;
224     }
225
226     private String getCurrentIpAddress() throws SocketException {
227         return Collections.list(NetworkInterface.getNetworkInterfaces()).stream()
228             .flatMap(i -> Collections.list(i.getInetAddresses()).stream())
229             .filter(ip -> ip instanceof Inet4Address)
230             .map(e -> (Inet4Address) e)
231             .findFirst()
232             .orElseThrow(RuntimeException::new)
233             .getHostAddress();
234     }
235 }