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.controlloop.common.rules.test;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
30 import java.util.List;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
37 import org.onap.policy.controlloop.common.rules.test.Simulators.SimulatorBuilder;
39 @RunWith(MockitoJUnitRunner.class)
40 public class SimulatorsTest {
41 private static final String EXPECTED_EXCEPTION = "expected exception";
44 private HttpServletServer server1;
46 private HttpServletServer server2;
48 private HttpServletServer server3;
50 private Simulators simulators;
57 simulators = new Simulators();
61 public void testStart() {
62 simulators.start(() -> server1, () -> server2);
63 assertEquals(List.of(server1, server2), simulators.getServers());
65 verify(server1, never()).shutdown();
66 verify(server2, never()).shutdown();
70 * Tests start() when one of the builders throws an exception.
73 public void testStartException() {
74 SimulatorBuilder exbuilder = () -> {
75 throw new InterruptedException(EXPECTED_EXCEPTION);
78 assertThatThrownBy(() -> simulators.start(() -> server1, () -> server2, exbuilder, () -> server3))
79 .isInstanceOf(SimulatorException.class);
81 assertTrue(simulators.getServers().isEmpty());
83 verify(server1).shutdown();
84 verify(server2).shutdown();
86 // shouldn't have reached this builder, so nothing to shut down
87 verify(server3, never()).shutdown();
91 public void testDestroy() {
92 simulators.start(() -> server1, () -> server2, () -> server3);
94 doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(server2).shutdown();
98 verify(server1).shutdown();
99 verify(server3).shutdown();
101 assertTrue(simulators.getServers().isEmpty());