2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.common.rules.test;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
32 import java.util.List;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
36 import org.onap.policy.controlloop.common.rules.test.Simulators.SimulatorBuilder;
38 class SimulatorsTest {
39 private static final String EXPECTED_EXCEPTION = "expected exception";
41 private final HttpServletServer server1 = mock(HttpServletServer.class);
42 private final HttpServletServer server2 = mock(HttpServletServer.class);
43 private final HttpServletServer server3 = mock(HttpServletServer.class);
45 private Simulators simulators;
52 simulators = new Simulators();
57 simulators.start(() -> server1, () -> server2);
58 assertEquals(List.of(server1, server2), simulators.getServers());
60 verify(server1, never()).shutdown();
61 verify(server2, never()).shutdown();
65 * Tests start() when one of the builders throws an exception.
68 void testStartException() {
69 SimulatorBuilder exbuilder = () -> {
70 throw new InterruptedException(EXPECTED_EXCEPTION);
73 assertThatThrownBy(() -> simulators.start(() -> server1, () -> server2, exbuilder, () -> server3))
74 .isInstanceOf(SimulatorException.class);
76 assertTrue(simulators.getServers().isEmpty());
78 verify(server1).shutdown();
79 verify(server2).shutdown();
81 // shouldn't have reached this builder, so nothing to shut down
82 verify(server3, never()).shutdown();
87 simulators.start(() -> server1, () -> server2, () -> server3);
89 doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(server2).shutdown();
93 verify(server1).shutdown();
94 verify(server3).shutdown();
96 assertTrue(simulators.getServers().isEmpty());