2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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("invalidSimParameters.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 = 6666; port <= 6670; ++port) {
129 assertTrue("simulator on port " + port, NetworkUtil.isTcpPortOpen(HOST, port, 1, 100));
132 // it's sufficient to verify that one of the simulators works
136 private void checkAai() throws HttpClientConfigException {
137 BusTopicParams params = BusTopicParams.builder().clientName("client").hostname(HOST).port(6666).useHttps(true)
138 .allowSelfSignedCerts(true).basePath("aai").build();
139 HttpClient client = HttpClientFactoryInstance.getClientFactory().build(params);
141 Response response = client.get("/v8/network/generic-vnfs/generic-vnf/my-vnf");
142 assertEquals(200, response.getStatus());
144 String result = response.readEntity(String.class);
145 assertThat(result).contains("USUCP0PCOIL0110UJZZ01-vsrx");
149 * Tests readParameters() when the file cannot be found.
152 public void testReadParametersNoFile() {
153 assertThatIllegalArgumentException().isThrownBy(() -> new Main("missing-file.json"))
154 .withCauseInstanceOf(FileNotFoundException.class);
158 * Tests readParameters() when the json cannot be decoded.
161 public void testReadParametersInvalidJson() throws CoderException {
162 Coder coder = mock(Coder.class);
163 when(coder.decode(any(String.class), any())).thenThrow(new CoderException(EXPECTED_EXCEPTION));
165 assertThatIllegalArgumentException().isThrownBy(() -> new Main(PARAMETER_FILE) {
167 protected Coder makeCoder() {
170 }).withCauseInstanceOf(CoderException.class);
174 * Tests buildRestServer() when the server port is not open.
177 public void testBuildRestServerNotOpen() {
178 HttpServletServer server = mock(HttpServletServer.class);
180 Main main = new Main(PARAMETER_FILE) {
182 protected HttpServletServer makeServer(Properties props) {
187 protected boolean isTcpPortOpen(String hostName, int port) throws InterruptedException {
192 assertThatThrownBy(main::start).hasCauseInstanceOf(IllegalStateException.class);
196 * Tests buildRestServer() when the port checker is interrupted.
199 public void testBuildRestServerInterrupted() throws InterruptedException {
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 {
210 throw new InterruptedException(EXPECTED_EXCEPTION);
214 // run in another thread so we don't interrupt this thread
215 LinkedBlockingQueue<RuntimeException> queue = new LinkedBlockingQueue<>();
216 Thread thread = new Thread(() -> {
219 } catch (RuntimeException e) {
224 thread.setDaemon(true);
227 RuntimeException ex = queue.poll(5, TimeUnit.SECONDS);
229 assertTrue(ex.getCause() instanceof IllegalStateException);
230 assertThat(ex.getCause()).hasMessageStartingWith("interrupted while building");
234 * Tests buildTopicServer() when the provider class is invalid.
237 public void testBuildTopicServerInvalidProvider() {
238 assertThatThrownBy(() -> new Main("invalidTopicServer.json").start());
242 * Tests buildTopicServer() when the sink is missing.
245 public void testBuildTopicServerNoSink() {
246 assertThatThrownBy(() -> new Main("missingSink.json").start());
250 * Tests buildTopicServer() when the sink is missing.
253 public void testBuildTopicServerNoSource() {
254 assertThatThrownBy(() -> new Main("missingSource.json").start());