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.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.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
36 import org.onap.policy.controlloop.common.rules.test.SimulatorException;
37 import org.onap.policy.controlloop.common.rules.test.Simulators;
38 import org.onap.policy.controlloop.common.rules.test.Simulators.SimulatorBuilder;
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 MockitoAnnotations.initMocks(this);
59 simulators = new Simulators();
63 public void testStart() {
64 simulators.start(() -> server1, () -> server2);
65 assertEquals(List.of(server1, server2), simulators.getServers());
67 verify(server1, never()).shutdown();
68 verify(server2, never()).shutdown();
72 * Tests start() when one of the builders throws an exception.
75 public void testStartException() {
76 SimulatorBuilder exbuilder = () -> {
77 throw new InterruptedException(EXPECTED_EXCEPTION);
80 assertThatThrownBy(() -> simulators.start(() -> server1, () -> server2, exbuilder, () -> server3))
81 .isInstanceOf(SimulatorException.class);
83 assertTrue(simulators.getServers().isEmpty());
85 verify(server1).shutdown();
86 verify(server2).shutdown();
88 // shouldn't have reached this builder, so nothing to shut down
89 verify(server3, never()).shutdown();
93 public void testDestroy() {
94 simulators.start(() -> server1, () -> server2, () -> server3);
96 doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(server2).shutdown();
100 verify(server1).shutdown();
101 verify(server3).shutdown();
103 assertTrue(simulators.getServers().isEmpty());