a5ffe4d47b21a4f26b73e2b1e444cdb951c65d7a
[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 org.assertj.core.api.AssertionsForClassTypes.assertThat;
25 import static org.hamcrest.Matchers.equalTo;
26
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;
54
55 @RunWith(SpringRunner.class)
56 @SpringBootTest(classes = {Main.class, TestConfiguration.class}, webEnvironment = WebEnvironment.DEFINED_PORT)
57 public class OptionalTemplatesTest {
58
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";
65
66     @Autowired
67     VesSimulatorController vesSimulatorController;
68
69     @Autowired
70     private VesSimulatorService vesSimulatorService;
71
72     private String currentVesSimulatorIp;
73
74     @Before
75     public void setUp() throws Exception {
76         currentVesSimulatorIp = getCurrentIpAddress();
77     }
78
79     @After
80     public void tearDown() {
81         Mockito.reset(vesSimulatorService);
82     }
83
84     @Test
85     public void whenTriggeredSimulatorWithoutTemplateShouldSendSingleEventToVes() {
86         //given
87         long currentTimestamp = Instant.now().getEpochSecond();
88
89         String body = "{\n"
90             + "\"vesServerUrl\": \"https://" + currentVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
91             + "\"event\": { \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"
99             + "\"version\": 3"
100             + "}\n"
101             + "}\n"
102             + "}";
103         ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
104
105         //when
106         given()
107             .contentType("application/json")
108             .body(body)
109             .when()
110             .post(SINGLE_EVENT_URL)
111             .then()
112             .statusCode(202)
113             .body("message", equalTo("One-time direct event sent successfully"));
114
115         //then
116         long afterExecution = Instant.now().getEpochSecond();
117         Mockito.verify(vesSimulatorService,
118             Mockito.timeout(3000))
119             .sendEventToDmaapV5(parameterCaptor.capture());
120
121         JsonObject value = parameterCaptor.getValue();
122         assertThat(value
123             .getAsJsonObject(COMMON_EVENT_HEADER)
124             .get("sourceName").getAsString()).isEqualTo("Single_sourceName");
125         assertThat(value
126             .getAsJsonObject(COMMON_EVENT_HEADER)
127             .get("eventId1").getAsString().length()).isEqualTo(20);
128         assertThat(value
129             .getAsJsonObject(COMMON_EVENT_HEADER)
130             .get("eventId2").getAsString()).isEqualTo("10");
131         assertThat(value
132             .getAsJsonObject(COMMON_EVENT_HEADER)
133             .get("eventId3").getAsString()).isEqualTo("1");
134         assertThat(value
135             .getAsJsonObject(COMMON_EVENT_HEADER)
136             .get("eventId4").getAsInt()).isEqualTo(10);
137         assertThat(value
138             .getAsJsonObject(COMMON_EVENT_HEADER)
139             .get("eventId5").getAsLong()).isBetween(currentTimestamp, afterExecution);
140     }
141
142     @Test
143     public void whenTriggeredSimulatorWithoutTemplateEventShouldBeVisibleInDB() throws UnknownHostException {
144         //given
145         String body = "{\n"
146             + "\"vesServerUrl\": \"https://" + currentVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n"
147             + "\"event\": { \n"
148             + "\"commonEventHeader\": {\n"
149             + "\"sourceName\": \"HistoricalEvent\",\n"
150             + "\"version\": 3"
151             + "}\n"
152             + "}\n"
153             + "}";
154         ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class);
155
156         //when
157         given()
158             .contentType("application/json")
159             .body(body)
160             .when()
161             .post(SINGLE_EVENT_URL)
162             .then()
163             .statusCode(202)
164             .body("message", equalTo("One-time direct event sent successfully"));
165
166         //then
167         Mockito.verify(vesSimulatorService,
168             Mockito.timeout(3000))
169             .sendEventToDmaapV5(parameterCaptor.capture());
170
171         Document sourceNameInMongoDB = findSourceNameInMongoDB();
172         Assertions.assertThat(sourceNameInMongoDB.get(PATCHED))
173             .isEqualTo("{\"commonEventHeader\":{\"sourceName\":\"HistoricalEvent\",\"version\":3}}");
174     }
175
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();
190         }
191         return dbObject;
192     }
193
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)
199             .findFirst()
200             .orElseThrow(RuntimeException::new)
201             .getHostAddress();
202     }
203
204 }