2 * ============LICENSE_START=======================================================
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
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.integration;
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;
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;
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(classes = {Main.class, TestConfiguration.class}, webEnvironment = WebEnvironment.DEFINED_PORT)
53 public class BasicAvailabilityTest {
56 VesSimulatorController vesSimulatorController;
59 VesSimulatorService vesSimulatorService;
61 private final String ACTION_START = "start";
63 private String currenVesSimulatorIp;
66 public void setUp() throws Exception {
67 currenVesSimulatorIp = getCurrentIpAddress();
71 public void tearDown() {
72 Mockito.reset(vesSimulatorService);
76 public void simulatorShouldFailWhenTriggeredNonexistentTemplate(){
78 String startUrl = prepareRequestUrl(ACTION_START);
80 + "\"templateName\": \"any_nonexistent_template.json\",\n"
82 + "\"simulatorParams\": {\n"
83 + "\"vesServerUrl\": \"https://" + currenVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
84 + "\"repeatInterval\": 1,\n"
85 + "\"repeatCount\": 1\n"
91 .contentType("application/json")
97 .body("message", equalTo("Cannot start simulator - template any_nonexistent_template.json not found."));
101 public void whenTriggeredSimulatorShouldSendSingleEventToVes() {
103 String startUrl = prepareRequestUrl(ACTION_START);
105 + "\"templateName\": \"notification.json\",\n"
107 + "\"simulatorParams\": {\n"
108 + "\"vesServerUrl\": \"https://" + currenVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
109 + "\"repeatInterval\": 1,\n"
110 + "\"repeatCount\": 1\n"
113 ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
117 .contentType("application/json")
123 .body("message", equalTo("Request started"));
125 Mockito.verify(vesSimulatorService,
126 Mockito.timeout(3000))
127 .sendEventToDmaapV5(parameterCaptor.capture());
129 assertThat(parameterCaptor.getValue()
130 .getAsJsonObject("event")
131 .getAsJsonObject("commonEventHeader")
132 .get("domain").getAsString()).isEqualTo("notification");
136 public void simulatorShouldCorrectlyRespondOnCancellAllEvent() {
138 String ACTION_CANCEL_ALL = "cancel";
139 String cancelAllUrl = prepareRequestUrl(ACTION_CANCEL_ALL);
146 .body("message", equalTo("Event(s) was cancelled"));
151 public void simulatorBeAbleToUseNewlyAddedTemplate() throws IOException {
153 String templateBody = "{\"fake\":\"template\"}\n";
154 String fileName = UUID.randomUUID() + ".json";
155 String requestBody = "{\n"
156 + "\"templateName\": \"" + fileName + "\",\n"
158 + "\"simulatorParams\": {\n"
159 + "\"vesServerUrl\": \"https://" + currenVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
160 + "\"repeatInterval\": 1,\n"
161 + "\"repeatCount\": 1\n"
164 ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
167 Path newFile = Files.createFile(Paths.get("..", "templates", fileName));
168 Files.write(newFile, templateBody.getBytes());
171 .contentType("application/json")
174 .post(prepareRequestUrl(ACTION_START));
176 Files.delete(newFile);
179 Mockito.verify(vesSimulatorService, Mockito.timeout(3000))
180 .sendEventToDmaapV5(parameterCaptor.capture());
181 assertThat(parameterCaptor.getValue()
182 .get("fake").getAsString()).isEqualTo("template");
187 public void whenTriggeredSimulatorShouldSendGivenAmountOfEventsToVes() {
189 String startUrl = prepareRequestUrl(ACTION_START);
191 + "\"templateName\": \"notification.json\",\n"
193 + "\"simulatorParams\": {\n"
194 + "\"vesServerUrl\": \"https://" + currenVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
195 + "\"repeatInterval\": 1,\n"
196 + "\"repeatCount\": 4\n"
199 ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
203 .contentType("application/json")
209 .body("message", equalTo("Request started"));
211 VerificationOverTimeImpl verificator = new VerificationOverTimeImpl(100, Mockito.times(4), false, new Timer(6000));
212 Mockito.verify(vesSimulatorService, verificator).sendEventToDmaapV5(parameterCaptor.capture());
214 for (JsonObject value : parameterCaptor.getAllValues()) {
216 .getAsJsonObject("event")
217 .getAsJsonObject("commonEventHeader")
218 .get("domain").getAsString()).isEqualTo("notification");
222 private String prepareRequestUrl(String action) {
223 return "http://0.0.0.0:5000/simulator/" + action;
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)
232 .orElseThrow(RuntimeException::new)