8a87c4f265a68dba268ddb52fc79c9d8e7cf6da2
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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;
39
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         MockitoAnnotations.initMocks(this);
58
59         simulators = new Simulators();
60     }
61
62     @Test
63     public void testStart() {
64         simulators.start(() -> server1, () -> server2);
65         assertEquals(List.of(server1, server2), simulators.getServers());
66
67         verify(server1, never()).shutdown();
68         verify(server2, never()).shutdown();
69     }
70
71     /**
72      * Tests start() when one of the builders throws an exception.
73      */
74     @Test
75     public void testStartException() {
76         SimulatorBuilder exbuilder = () -> {
77             throw new InterruptedException(EXPECTED_EXCEPTION);
78         };
79
80         assertThatThrownBy(() -> simulators.start(() -> server1, () -> server2, exbuilder, () -> server3))
81                         .isInstanceOf(SimulatorException.class);
82
83         assertTrue(simulators.getServers().isEmpty());
84
85         verify(server1).shutdown();
86         verify(server2).shutdown();
87
88         // shouldn't have reached this builder, so nothing to shut down
89         verify(server3, never()).shutdown();
90     }
91
92     @Test
93     public void testDestroy() {
94         simulators.start(() -> server1, () -> server2, () -> server3);
95
96         doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(server2).shutdown();
97
98         simulators.destroy();
99
100         verify(server1).shutdown();
101         verify(server3).shutdown();
102
103         assertTrue(simulators.getServers().isEmpty());
104     }
105 }