eee5e3cf560e574d55de1ec7604464be56e2136d
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.be.components.health;
22
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28
29 import java.util.LinkedList;
30 import java.util.List;
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
38 import org.openecomp.sdc.be.catalog.impl.DmaapProducerHealth;
39 import org.openecomp.sdc.be.components.BeConfDependentTest;
40 import org.openecomp.sdc.be.components.distribution.engine.DistributionEngineClusterHealth;
41 import org.openecomp.sdc.be.components.distribution.engine.DmaapHealth;
42 import org.openecomp.sdc.be.config.ConfigurationManager;
43 import org.openecomp.sdc.be.switchover.detector.SwitchoverDetector;
44 import org.openecomp.sdc.common.api.HealthCheckInfo;
45 import org.openecomp.sdc.common.http.client.api.HttpExecuteException;
46 import org.openecomp.sdc.common.impl.ExternalConfiguration;
47 import org.openecomp.sdc.common.impl.FSConfigurationSource;
48 import org.springframework.test.util.ReflectionTestUtils;
49
50 @ExtendWith(MockitoExtension.class)
51 class HealthCheckBusinessLogicHealthTest extends BeConfDependentTest {
52
53     // TODO - remove this setup after migration to Junit5 BeConfDependentTest
54     @BeforeAll
55     private static void setup() {
56         configurationManager =
57             new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
58     }
59
60     private final DmaapProducerHealth dmaapProducerHealth = mock(DmaapProducerHealth.class);
61     private final HealthCheckInfo dmaapProducerHealthCheckInfo = mock(HealthCheckInfo.class);
62
63     private HealthCheckBusinessLogic createTestSubject() {
64
65         HealthCheckBusinessLogic healthCheckBusinessLogic = new HealthCheckBusinessLogic();
66         DmaapHealth dmaapHealth = new DmaapHealth();
67         ReflectionTestUtils.setField(healthCheckBusinessLogic, "dmaapHealth", dmaapHealth);
68         PortalHealthCheckBuilder portalHealthCheckBuilder = new PortalHealthCheckBuilder();
69         ReflectionTestUtils.setField(healthCheckBusinessLogic, "portalHealthCheck", portalHealthCheckBuilder);
70         DistributionEngineClusterHealth distributionEngineClusterHealth = new DistributionEngineClusterHealth();
71         ReflectionTestUtils.setField(healthCheckBusinessLogic, "distributionEngineClusterHealth",
72             distributionEngineClusterHealth);
73         SwitchoverDetector switchoverDetector = new SwitchoverDetector();
74         ReflectionTestUtils.setField(healthCheckBusinessLogic, "switchoverDetector", switchoverDetector);
75         List<HealthCheckInfo> prevBeHealthCheckInfos = new LinkedList<>();
76         ReflectionTestUtils.setField(healthCheckBusinessLogic, "prevBeHealthCheckInfos", prevBeHealthCheckInfos);
77         ReflectionTestUtils.setField(healthCheckBusinessLogic, "dmaapProducerHealth", dmaapProducerHealth);
78         return healthCheckBusinessLogic;
79     }
80
81     @BeforeEach
82     private void beforeTest() {
83         when(dmaapProducerHealth.getHealthCheckInfo())
84             .thenReturn(dmaapProducerHealthCheckInfo);
85     }
86
87     @Test
88     void testInit() throws Exception {
89         HealthCheckBusinessLogic testSubject = createTestSubject();
90         testSubject.init();
91     }
92
93     @Test
94     void testIsDistributionEngineUp() throws Exception {
95         HealthCheckBusinessLogic testSubject;
96         // default test
97         testSubject = createTestSubject();
98         testSubject.isDistributionEngineUp();
99     }
100
101     @Test
102     void testGetBeHealthCheckInfosStatus() throws Exception {
103         HealthCheckBusinessLogic testSubject;
104
105         // default test
106         testSubject = createTestSubject();
107         testSubject.getBeHealthCheckInfosStatus();
108     }
109
110     @Test
111     void testGetJanusGraphHealthCheck() throws Exception {
112         HealthCheckBusinessLogic testSubject;
113         List<HealthCheckInfo> healthCheckInfos = new LinkedList<>();
114
115         // default test
116         testSubject = createTestSubject();
117         healthCheckInfos.add(testSubject.getJanusGraphHealthCheck());
118     }
119
120     @Test
121     void testGetPortalHealthCheckSuccess() throws Exception {
122         PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
123         String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
124         int timeout = 3000;
125         doReturn(200).when(testSubject).getStatusCode(healthCheckURL, timeout);
126         testSubject.init();
127         testSubject.runTask();
128         HealthCheckInfo hci = testSubject.getHealthCheckInfo();
129         Assertions.assertEquals("PORTAL", hci.getHealthCheckComponent());
130         Assertions.assertEquals(HealthCheckInfo.HealthCheckStatus.UP, hci.getHealthCheckStatus());
131         Assertions.assertEquals("OK", hci.getDescription());
132     }
133
134     @Test
135     void testGetPortalHealthCheckFailureMissingConfig() throws Exception {
136         PortalHealthCheckBuilder testSubject = new PortalHealthCheckBuilder();
137         testSubject.init(null);
138         HealthCheckInfo hci = testSubject.getHealthCheckInfo();
139         Assertions.assertEquals("PORTAL", hci.getHealthCheckComponent());
140         Assertions.assertEquals(HealthCheckInfo.HealthCheckStatus.DOWN, hci.getHealthCheckStatus());
141         Assertions.assertEquals("PORTAL health check configuration is missing", hci.getDescription());
142     }
143
144     @Test
145     void testGetPortalHealthCheckFailureErrorResponse() throws HttpExecuteException, CipherUtilException {
146         PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
147         String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
148         int timeout = 3000;
149         doReturn(404).when(testSubject).getStatusCode(healthCheckURL, timeout);
150         testSubject.init(testSubject.getConfiguration());
151         testSubject.runTask();
152         HealthCheckInfo hci = testSubject.getHealthCheckInfo();
153         Assertions.assertEquals("PORTAL", hci.getHealthCheckComponent());
154         Assertions.assertEquals(HealthCheckInfo.HealthCheckStatus.DOWN, hci.getHealthCheckStatus());
155         Assertions.assertEquals("PORTAL responded with 404 status code", hci.getDescription());
156     }
157
158     @Test
159     void testGetPortalHealthCheckFailureNoResponse() throws HttpExecuteException, CipherUtilException {
160         PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
161         String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
162         int timeout = 3000;
163         doThrow(HttpExecuteException.class).when(testSubject).getStatusCode(healthCheckURL, timeout);
164         testSubject.init(testSubject.getConfiguration());
165         testSubject.runTask();
166         HealthCheckInfo hci = testSubject.getHealthCheckInfo();
167         Assertions.assertEquals("PORTAL", hci.getHealthCheckComponent());
168         Assertions.assertEquals(HealthCheckInfo.HealthCheckStatus.DOWN, hci.getHealthCheckStatus());
169         Assertions.assertEquals("PORTAL is not available", hci.getDescription());
170     }
171
172     @Test
173     void testDestroy() throws Exception {
174         HealthCheckBusinessLogic testSubject;
175
176         // default test
177         testSubject = createTestSubject();
178         testSubject.init();
179         testSubject.destroy();
180     }
181
182     @Test
183     void testGetSiteMode() throws Exception {
184         HealthCheckBusinessLogic testSubject;
185
186         // default test
187         testSubject = createTestSubject();
188         testSubject.getSiteMode();
189     }
190
191     @Test
192     void testAnyStatusChanged() throws Exception {
193         HealthCheckBusinessLogic testSubject;
194         List<HealthCheckInfo> beHealthCheckInfos = null;
195         List<HealthCheckInfo> prevBeHealthCheckInfos = null;
196         boolean result;
197
198         // test 1
199         testSubject = createTestSubject();
200         beHealthCheckInfos = null;
201         prevBeHealthCheckInfos = null;
202         result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
203         Assertions.assertEquals(false, result);
204
205         // test 2
206         testSubject = createTestSubject();
207         prevBeHealthCheckInfos = null;
208         beHealthCheckInfos = null;
209         result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
210         Assertions.assertEquals(false, result);
211
212         // test 3
213         testSubject = createTestSubject();
214         beHealthCheckInfos = null;
215         prevBeHealthCheckInfos = null;
216         result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
217         Assertions.assertEquals(false, result);
218
219         // test 4
220         testSubject = createTestSubject();
221         prevBeHealthCheckInfos = null;
222         beHealthCheckInfos = null;
223         result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
224         Assertions.assertEquals(false, result);
225     }
226
227 }