db9dbd785a2f9ad341c962a89c6e542cbccda446
[integration.git] /
1 /*
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
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.simulator;
22
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertTimeout;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.atLeast;
27 import static org.mockito.Mockito.verify;
28
29 import java.time.Duration;
30 import org.json.JSONObject;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.function.Executable;
33 import org.mockito.Mockito;
34 import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
35
36 class SimulatorTest {
37
38     private static final String TEST_VES_URL = "http://test-ves-url";
39
40     @Test
41     void builder_should_create_endless_simulator_when_duration_not_specified() {
42         Simulator simulator = Simulator
43             .builder()
44             .withDuration(Duration.ofSeconds(1))
45             .withVesUrl(TEST_VES_URL).build();
46
47         assertFalse(simulator.isEndless());
48
49         simulator = Simulator
50             .builder()
51             .withVesUrl(TEST_VES_URL).build();
52
53         assertTrue(simulator.isEndless());
54     }
55
56     @Test
57     void simulator_should_send_given_message() {
58
59         JSONObject messageBody = new JSONObject("{\"key\":\"val\"}");
60         HttpClientAdapter httpClientMock = Mockito.mock(HttpClientAdapter.class);
61
62         Simulator simulator = Simulator.builder()
63             .withDuration(Duration.ofMillis(100))
64             .withInterval(Duration.ofMillis(10))
65             .withMessageBody(messageBody)
66             .withCustomHttpClientAdapter(httpClientMock)
67             .withVesUrl(TEST_VES_URL).build();
68
69         simulator.start();
70
71         assertTimeout(Duration.ofMillis(150), (Executable) simulator::join);
72         verify(httpClientMock, atLeast(2)).send(messageBody.toString(), TEST_VES_URL);
73     }
74
75     @Test
76     void simulator_should_stop_when_interrupted() {
77
78         JSONObject messageBody = new JSONObject("{\"key\":\"val\"}");
79         HttpClientAdapter httpClientMock = Mockito.mock(HttpClientAdapter.class);
80
81         Simulator simulator = Simulator.builder()
82             .withInterval(Duration.ofSeconds(1))
83             .withMessageBody(messageBody)
84             .withCustomHttpClientAdapter(httpClientMock)
85             .withVesUrl(TEST_VES_URL).build();
86
87         simulator.start();
88         simulator.interrupt();
89
90         assertTimeout(Duration.ofSeconds(1), (Executable) simulator::join);
91     }
92 }