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 org.assertj.core.api.AssertionsForClassTypes.assertThat;
25 import static org.hamcrest.Matchers.equalTo;
27 import com.google.gson.JsonObject;
28 import com.mongodb.MongoClient;
29 import com.mongodb.MongoClientOptions;
30 import com.mongodb.MongoCredential;
31 import com.mongodb.ServerAddress;
32 import com.mongodb.client.FindIterable;
33 import com.mongodb.client.MongoCollection;
34 import com.mongodb.client.MongoCursor;
35 import com.mongodb.client.MongoDatabase;
36 import java.time.Instant;
37 import java.net.Inet4Address;
38 import java.net.NetworkInterface;
39 import java.net.SocketException;
40 import java.net.UnknownHostException;
41 import java.util.Collections;
42 import org.assertj.core.api.Assertions;
43 import org.bson.Document;
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.ArgumentCaptor;
49 import org.mockito.Mockito;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
53 import org.springframework.test.context.junit4.SpringRunner;
55 @RunWith(SpringRunner.class)
56 @SpringBootTest(classes = {Main.class, TestConfiguration.class}, webEnvironment = WebEnvironment.DEFINED_PORT)
57 public class OptionalTemplatesTest {
59 private static final String PNF_SIMULATOR_DB = "pnf_simulator";
60 private static final String COMMON_EVENT_HEADER = "commonEventHeader";
61 private static final String PNF_SIMULATOR_DB_PSWD = "zXcVbN123!";
62 private static final String PNF_SIMULATOR_DB_USER = "pnf_simulator_user";
63 private static final String PATCHED = "patched";
64 private static final String SINGLE_EVENT_URL = "http://0.0.0.0:5000/simulator/event";
67 VesSimulatorController vesSimulatorController;
70 private VesSimulatorService vesSimulatorService;
72 private String currentVesSimulatorIp;
75 public void setUp() throws Exception {
76 currentVesSimulatorIp = getCurrentIpAddress();
80 public void tearDown() {
81 Mockito.reset(vesSimulatorService);
85 public void whenTriggeredSimulatorWithoutTemplateShouldSendSingleEventToVes() {
87 long currentTimestamp = Instant.now().getEpochSecond();
90 + "\"vesServerUrl\": \"https://" + currentVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
92 + "\"commonEventHeader\": {\n"
93 + "\"eventId1\": \"#RandomString(20)\",\n"
94 + "\"eventId2\": \"#RandomInteger(10,10)\",\n"
95 + "\"eventId3\": \"#Increment\",\n"
96 + "\"eventId4\": \"#RandomPrimitiveInteger(10,10)\",\n"
97 + "\"eventId5\": \"#TimestampPrimitive\",\n"
98 + "\"sourceName\": \"Single_sourceName\",\n"
103 ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
107 .contentType("application/json")
110 .post(SINGLE_EVENT_URL)
113 .body("message", equalTo("One-time direct event sent successfully"));
116 long afterExecution = Instant.now().getEpochSecond();
117 Mockito.verify(vesSimulatorService,
118 Mockito.timeout(3000))
119 .sendEventToDmaapV5(parameterCaptor.capture());
121 JsonObject value = parameterCaptor.getValue();
123 .getAsJsonObject(COMMON_EVENT_HEADER)
124 .get("sourceName").getAsString()).isEqualTo("Single_sourceName");
126 .getAsJsonObject(COMMON_EVENT_HEADER)
127 .get("eventId1").getAsString().length()).isEqualTo(20);
129 .getAsJsonObject(COMMON_EVENT_HEADER)
130 .get("eventId2").getAsString()).isEqualTo("10");
132 .getAsJsonObject(COMMON_EVENT_HEADER)
133 .get("eventId3").getAsString()).isEqualTo("1");
135 .getAsJsonObject(COMMON_EVENT_HEADER)
136 .get("eventId4").getAsInt()).isEqualTo(10);
138 .getAsJsonObject(COMMON_EVENT_HEADER)
139 .get("eventId5").getAsLong()).isBetween(currentTimestamp, afterExecution);
143 public void whenTriggeredSimulatorWithoutTemplateEventShouldBeVisibleInDB() throws UnknownHostException {
146 + "\"vesServerUrl\": \"https://" + currentVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
148 + "\"commonEventHeader\": {\n"
149 + "\"sourceName\": \"HistoricalEvent\",\n"
154 ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
158 .contentType("application/json")
161 .post(SINGLE_EVENT_URL)
164 .body("message", equalTo("One-time direct event sent successfully"));
167 Mockito.verify(vesSimulatorService,
168 Mockito.timeout(3000))
169 .sendEventToDmaapV5(parameterCaptor.capture());
171 Document sourceNameInMongoDB = findSourceNameInMongoDB();
172 Assertions.assertThat(sourceNameInMongoDB.get(PATCHED))
173 .isEqualTo("{\"commonEventHeader\":{\"sourceName\":\"HistoricalEvent\",\"version\":3}}");
176 private Document findSourceNameInMongoDB() throws UnknownHostException {
177 MongoCredential credential = MongoCredential
178 .createCredential(PNF_SIMULATOR_DB_USER, PNF_SIMULATOR_DB, PNF_SIMULATOR_DB_PSWD.toCharArray());
179 MongoClient mongoClient = new MongoClient(new ServerAddress(Inet4Address.getLocalHost(), 27017),
180 credential, MongoClientOptions.builder().build());
181 MongoDatabase pnfSimulatorDb = mongoClient.getDatabase(PNF_SIMULATOR_DB);
182 MongoCollection<Document> table = pnfSimulatorDb.getCollection("eventData");
183 Document searchQuery = new Document();
184 searchQuery.put(PATCHED, new Document("$regex", ".*" + "HistoricalEvent" + ".*"));
185 FindIterable<Document> findOfPatched = table.find(searchQuery);
186 Document dbObject = null;
187 MongoCursor<Document> cursor = findOfPatched.iterator();
188 if (cursor.hasNext()) {
189 dbObject = cursor.next();
194 private String getCurrentIpAddress() throws SocketException {
195 return Collections.list(NetworkInterface.getNetworkInterfaces()).stream()
196 .flatMap(i -> Collections.list(i.getInetAddresses()).stream())
197 .filter(ip -> ip instanceof Inet4Address)
198 .map(e -> (Inet4Address) e)
200 .orElseThrow(RuntimeException::new)