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
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.simulator;
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;
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;
38 private static final String TEST_VES_URL = "http://test-ves-url";
41 void builder_should_create_endless_simulator_when_duration_not_specified() {
42 Simulator simulator = Simulator
44 .withDuration(Duration.ofSeconds(1))
45 .withVesUrl(TEST_VES_URL).build();
47 assertFalse(simulator.isEndless());
51 .withVesUrl(TEST_VES_URL).build();
53 assertTrue(simulator.isEndless());
57 void simulator_should_send_given_message() {
59 JSONObject messageBody = new JSONObject("{\"key\":\"val\"}");
60 HttpClientAdapter httpClientMock = Mockito.mock(HttpClientAdapter.class);
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();
71 assertTimeout(Duration.ofMillis(150), (Executable) simulator::join);
72 verify(httpClientMock, atLeast(2)).send(messageBody.toString(), TEST_VES_URL);
76 void simulator_should_stop_when_interrupted() {
78 JSONObject messageBody = new JSONObject("{\"key\":\"val\"}");
79 HttpClientAdapter httpClientMock = Mockito.mock(HttpClientAdapter.class);
81 Simulator simulator = Simulator.builder()
82 .withInterval(Duration.ofSeconds(1))
83 .withMessageBody(messageBody)
84 .withCustomHttpClientAdapter(httpClientMock)
85 .withVesUrl(TEST_VES_URL).build();
88 simulator.interrupt();
90 assertTimeout(Duration.ofSeconds(1), (Executable) simulator::join);