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