Upgrade drools-applications to JUnit 5
[policy/drools-applications.git] / controlloop / common / rules-test / src / test / java / org / onap / policy / controlloop / common / rules / test / SimulatorsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.common.rules.test;
23
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;
31
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;
37
38 class SimulatorsTest {
39     private static final String EXPECTED_EXCEPTION = "expected exception";
40
41     private final HttpServletServer server1 = mock(HttpServletServer.class);
42     private final HttpServletServer server2 = mock(HttpServletServer.class);
43     private final HttpServletServer server3 = mock(HttpServletServer.class);
44
45     private Simulators simulators;
46
47     /**
48      * Sets up.
49      */
50     @BeforeEach
51     public void setUp() {
52         simulators = new Simulators();
53     }
54
55     @Test
56     void testStart() {
57         simulators.start(() -> server1, () -> server2);
58         assertEquals(List.of(server1, server2), simulators.getServers());
59
60         verify(server1, never()).shutdown();
61         verify(server2, never()).shutdown();
62     }
63
64     /**
65      * Tests start() when one of the builders throws an exception.
66      */
67     @Test
68     void testStartException() {
69         SimulatorBuilder exbuilder = () -> {
70             throw new InterruptedException(EXPECTED_EXCEPTION);
71         };
72
73         assertThatThrownBy(() -> simulators.start(() -> server1, () -> server2, exbuilder, () -> server3))
74                         .isInstanceOf(SimulatorException.class);
75
76         assertTrue(simulators.getServers().isEmpty());
77
78         verify(server1).shutdown();
79         verify(server2).shutdown();
80
81         // shouldn't have reached this builder, so nothing to shut down
82         verify(server3, never()).shutdown();
83     }
84
85     @Test
86     void testDestroy() {
87         simulators.start(() -> server1, () -> server2, () -> server3);
88
89         doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(server2).shutdown();
90
91         simulators.destroy();
92
93         verify(server1).shutdown();
94         verify(server3).shutdown();
95
96         assertTrue(simulators.getServers().isEmpty());
97     }
98 }