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.io.IOException;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Map.Entry;
39 import java.util.Properties;
40 import java.util.concurrent.LinkedBlockingQueue;
41 import java.util.concurrent.TimeUnit;
42 import javax.ws.rs.core.Response;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.BeforeClass;
46 import org.junit.Ignore;
47 import org.junit.Test;
48 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
49 import org.onap.policy.common.endpoints.http.client.HttpClient;
50 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
51 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
52 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
53 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
54 import org.onap.policy.common.utils.coder.Coder;
55 import org.onap.policy.common.utils.coder.CoderException;
56 import org.onap.policy.common.utils.network.NetworkUtil;
57 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
59 public class MainTest {
60 private static final String PARAMETER_FILE = "simParameters.json";
61 private static final String HOST = "localhost";
62 private static final String EXPECTED_EXCEPTION = "expected exception";
64 private static Map<String, String> savedValues;
67 * Saves system properties.
71 public static void setUpBeforeClass() throws IOException, InterruptedException {
72 savedValues = new HashMap<>();
74 for (String prop : List.of(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
75 JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME,
76 JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
77 JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME)) {
79 savedValues.put(prop, System.getProperty(prop));
82 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, new SelfSignedKeyStore().getKeystoreName());
83 System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
84 SelfSignedKeyStore.KEYSTORE_PASSWORD);
86 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME,
87 "src/main/resources/ssl/policy-truststore");
88 System.setProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
89 SelfSignedKeyStore.KEYSTORE_PASSWORD);
93 * Restores system properties.
97 public static void tearDownAfterClass() {
98 for (Entry<String, String> ent : savedValues.entrySet()) {
99 if (ent.getValue() == null) {
100 System.getProperties().remove(ent.getKey());
102 System.setProperty(ent.getKey(), ent.getValue());
108 * Shuts down the simulator instance.
112 public void tearDown() {
113 Main main = Main.getInstance();
114 if (main != null && main.isAlive()) {
121 public void testConstructor() throws Exception {
122 assertThatIllegalArgumentException().isThrownBy(() -> new Main("invalidDmaapProvider.json"))
123 .withMessage("invalid simulator parameters");
127 * Verifies that all of the simulators are brought up and that HTTPS works with at
132 public void testMain() throws Exception {
133 Main.main(new String[0]);
134 assertTrue(Main.getInstance() == null || !Main.getInstance().isAlive());
136 Main.main(new String[] {PARAMETER_FILE});
138 // don't need to wait long, because buildXxx() does the wait for us
139 for (int port : new int[] {6666, 6667, 6668, 6669, 6670, 6680}) {
140 assertTrue("simulator on port " + port, NetworkUtil.isTcpPortOpen(HOST, port, 1, 100));
143 // it's sufficient to verify that one of the simulators works
149 public void testMainMinimalParameters() {
150 Main.main(new String[] {"minParameters.json"});
151 assertNotNull(Main.getInstance());
152 assertTrue(Main.getInstance().isAlive());
155 private void checkAai() throws HttpClientConfigException {
156 BusTopicParams params = BusTopicParams.builder().clientName("client").hostname(HOST).port(6666).useHttps(true)
157 .allowSelfSignedCerts(true).basePath("aai").build();
158 HttpClient client = HttpClientFactoryInstance.getClientFactory().build(params);
160 Response response = client.get("/v21/network/pnfs/pnf/demo-pnf");
161 assertEquals(200, response.getStatus());
163 String result = response.readEntity(String.class);
164 assertThat(result).contains("model-123456");
168 * Tests readParameters() when the file cannot be found.
172 public void testReadParametersNoFile() {
173 assertThatIllegalArgumentException().isThrownBy(() -> new Main("missing-file.json"))
174 .withCauseInstanceOf(FileNotFoundException.class);
178 * Tests readParameters() when the json cannot be decoded.
182 public void testReadParametersInvalidJson() throws CoderException {
183 Coder coder = mock(Coder.class);
184 when(coder.decode(any(String.class), any())).thenThrow(new CoderException(EXPECTED_EXCEPTION));
186 assertThatIllegalArgumentException().isThrownBy(() -> new Main(PARAMETER_FILE) {
188 protected Coder makeCoder() {
191 }).withCauseInstanceOf(CoderException.class);
195 * Tests buildRestServer() when the server port is not open.
199 public void testBuildRestServerNotOpen() {
200 HttpServletServer server = mock(HttpServletServer.class);
202 Main main = new Main(PARAMETER_FILE) {
204 protected HttpServletServer makeServer(Properties props) {
209 protected boolean isTcpPortOpen(String hostName, int port) throws InterruptedException {
214 assertThatThrownBy(main::start).hasCauseInstanceOf(IllegalStateException.class);
218 * Tests buildRestServer() when the port checker is interrupted.
222 public void testBuildRestServerInterrupted() throws InterruptedException {
223 HttpServletServer server = mock(HttpServletServer.class);
225 Main main = new Main(PARAMETER_FILE) {
227 protected HttpServletServer makeServer(Properties props) {
232 protected boolean isTcpPortOpen(String hostName, int port) throws InterruptedException {
233 throw new InterruptedException(EXPECTED_EXCEPTION);
237 // run in another thread so we don't interrupt this thread
238 LinkedBlockingQueue<RuntimeException> queue = new LinkedBlockingQueue<>();
239 Thread thread = new Thread(() -> {
242 } catch (RuntimeException e) {
247 thread.setDaemon(true);
250 RuntimeException ex = queue.poll(5, TimeUnit.SECONDS);
252 assertTrue(ex.getCause() instanceof IllegalStateException);
253 assertThat(ex.getCause()).hasMessageStartingWith("interrupted while building");
257 * Tests buildTopicServer() when the provider class is invalid.
261 public void testBuildTopicServerInvalidProvider() {
262 assertThatThrownBy(() -> new Main("invalidTopicServer.json").start())
263 .hasCauseInstanceOf(IllegalArgumentException.class);
267 * Tests buildTopicServer() when the sink is missing.
271 public void testBuildTopicServerNoSink() {
272 assertThatThrownBy(() -> new Main("missingSink.json").start())
273 .hasCauseInstanceOf(IllegalArgumentException.class);
277 * Tests buildTopicServer() when the sink is missing.
281 public void testBuildTopicServerNoSource() {
282 assertThatThrownBy(() -> new Main("missingSource.json").start())
283 .hasCauseInstanceOf(IllegalArgumentException.class);