2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T 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.policy.models.simulators;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 import java.io.FileNotFoundException;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Map.Entry;
38 import java.util.Properties;
39 import java.util.concurrent.LinkedBlockingQueue;
40 import java.util.concurrent.TimeUnit;
41 import javax.ws.rs.core.Response;
42 import org.junit.After;
43 import org.junit.AfterClass;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
47 import org.onap.policy.common.endpoints.http.client.HttpClient;
48 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
49 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
50 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
51 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
52 import org.onap.policy.common.utils.coder.Coder;
53 import org.onap.policy.common.utils.coder.CoderException;
54 import org.onap.policy.common.utils.network.NetworkUtil;
56 public class MainTest {
57 private static final String PARAMETER_FILE = "simParameters.json";
58 private static final String HOST = "localhost";
59 private static final String EXPECTED_EXCEPTION = "expected exception";
61 private static Map<String, String> savedValues;
64 * Saves system properties.
67 public static void setUpBeforeClass() {
68 savedValues = new HashMap<>();
70 for (String prop : List.of(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
71 JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME,
72 JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
73 JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME)) {
75 savedValues.put(prop, System.getProperty(prop));
78 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
79 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, "kstest");
81 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
82 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, "kstest");
86 * Restores system properties.
89 public static void tearDownAfterClass() {
90 for (Entry<String, String> ent : savedValues.entrySet()) {
91 if (ent.getValue() == null) {
92 System.getProperties().remove(ent.getKey());
94 System.setProperty(ent.getKey(), ent.getValue());
100 * Shuts down the simulator instance.
103 public void tearDown() {
104 Main main = Main.getInstance();
105 if (main != null && main.isAlive()) {
111 public void testConstructor() throws Exception {
112 assertThatIllegalArgumentException().isThrownBy(() -> new Main("invalidDmaapProvider.json"))
113 .withMessage("invalid simulator parameters");
117 * Verifies that all of the simulators are brought up and that HTTPS works with at
121 public void testMain() throws Exception {
122 Main.main(new String[0]);
123 assertTrue(Main.getInstance() == null || !Main.getInstance().isAlive());
125 Main.main(new String[] {PARAMETER_FILE});
127 // don't need to wait long, because buildXxx() does the wait for us
128 for (int port : new int[] {6666, 6667, 6668, 6669, 6670, 6680}) {
129 assertTrue("simulator on port " + port, NetworkUtil.isTcpPortOpen(HOST, port, 1, 100));
132 // it's sufficient to verify that one of the simulators works
137 public void testMainMinimalParameters() {
138 Main.main(new String[] {"minParameters.json"});
139 assertNotNull(Main.getInstance());
140 assertTrue(Main.getInstance().isAlive());
143 private void checkAai() throws HttpClientConfigException {
144 BusTopicParams params = BusTopicParams.builder().clientName("client").hostname(HOST).port(6666).useHttps(true)
145 .allowSelfSignedCerts(true).basePath("aai").build();
146 HttpClient client = HttpClientFactoryInstance.getClientFactory().build(params);
148 Response response = client.get("/v21/network/pnfs/pnf/demo-pnf");
149 assertEquals(200, response.getStatus());
151 String result = response.readEntity(String.class);
152 assertThat(result).contains("model-123456");
156 * Tests readParameters() when the file cannot be found.
159 public void testReadParametersNoFile() {
160 assertThatIllegalArgumentException().isThrownBy(() -> new Main("missing-file.json"))
161 .withCauseInstanceOf(FileNotFoundException.class);
165 * Tests readParameters() when the json cannot be decoded.
168 public void testReadParametersInvalidJson() throws CoderException {
169 Coder coder = mock(Coder.class);
170 when(coder.decode(any(String.class), any())).thenThrow(new CoderException(EXPECTED_EXCEPTION));
172 assertThatIllegalArgumentException().isThrownBy(() -> new Main(PARAMETER_FILE) {
174 protected Coder makeCoder() {
177 }).withCauseInstanceOf(CoderException.class);
181 * Tests buildRestServer() when the server port is not open.
184 public void testBuildRestServerNotOpen() {
185 HttpServletServer server = mock(HttpServletServer.class);
187 Main main = new Main(PARAMETER_FILE) {
189 protected HttpServletServer makeServer(Properties props) {
194 protected boolean isTcpPortOpen(String hostName, int port) throws InterruptedException {
199 assertThatThrownBy(main::start).hasCauseInstanceOf(IllegalStateException.class);
203 * Tests buildRestServer() when the port checker is interrupted.
206 public void testBuildRestServerInterrupted() throws InterruptedException {
207 HttpServletServer server = mock(HttpServletServer.class);
209 Main main = new Main(PARAMETER_FILE) {
211 protected HttpServletServer makeServer(Properties props) {
216 protected boolean isTcpPortOpen(String hostName, int port) throws InterruptedException {
217 throw new InterruptedException(EXPECTED_EXCEPTION);
221 // run in another thread so we don't interrupt this thread
222 LinkedBlockingQueue<RuntimeException> queue = new LinkedBlockingQueue<>();
223 Thread thread = new Thread(() -> {
226 } catch (RuntimeException e) {
231 thread.setDaemon(true);
234 RuntimeException ex = queue.poll(5, TimeUnit.SECONDS);
236 assertTrue(ex.getCause() instanceof IllegalStateException);
237 assertThat(ex.getCause()).hasMessageStartingWith("interrupted while building");
241 * Tests buildTopicServer() when the provider class is invalid.
244 public void testBuildTopicServerInvalidProvider() {
245 assertThatThrownBy(() -> new Main("invalidTopicServer.json").start())
246 .hasCauseInstanceOf(IllegalArgumentException.class);
250 * Tests buildTopicServer() when the sink is missing.
253 public void testBuildTopicServerNoSink() {
254 assertThatThrownBy(() -> new Main("missingSink.json").start())
255 .hasCauseInstanceOf(IllegalArgumentException.class);
259 * Tests buildTopicServer() when the sink is missing.
262 public void testBuildTopicServerNoSource() {
263 assertThatThrownBy(() -> new Main("missingSource.json").start())
264 .hasCauseInstanceOf(IllegalArgumentException.class);