2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.health;
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;
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;
50 @ExtendWith(MockitoExtension.class)
51 class HealthCheckBusinessLogicHealthTest extends BeConfDependentTest {
53 // TODO - remove this setup after migration to Junit5 BeConfDependentTest
55 private static void setup() {
56 configurationManager =
57 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
60 private final DmaapProducerHealth dmaapProducerHealth = mock(DmaapProducerHealth.class);
61 private final HealthCheckInfo dmaapProducerHealthCheckInfo = mock(HealthCheckInfo.class);
63 private HealthCheckBusinessLogic createTestSubject() {
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;
82 private void beforeTest() {
83 when(dmaapProducerHealth.getHealthCheckInfo())
84 .thenReturn(dmaapProducerHealthCheckInfo);
88 void testInit() throws Exception {
89 HealthCheckBusinessLogic testSubject = createTestSubject();
94 void testIsDistributionEngineUp() throws Exception {
95 HealthCheckBusinessLogic testSubject;
97 testSubject = createTestSubject();
98 testSubject.isDistributionEngineUp();
102 void testGetBeHealthCheckInfosStatus() throws Exception {
103 HealthCheckBusinessLogic testSubject;
106 testSubject = createTestSubject();
107 testSubject.getBeHealthCheckInfosStatus();
111 void testGetJanusGraphHealthCheck() throws Exception {
112 HealthCheckBusinessLogic testSubject;
113 List<HealthCheckInfo> healthCheckInfos = new LinkedList<>();
116 testSubject = createTestSubject();
117 healthCheckInfos.add(testSubject.getJanusGraphHealthCheck());
121 void testGetPortalHealthCheckSuccess() throws Exception {
122 PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
123 String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
125 doReturn(200).when(testSubject).getStatusCode(healthCheckURL, timeout);
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());
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());
145 void testGetPortalHealthCheckFailureErrorResponse() throws HttpExecuteException, CipherUtilException {
146 PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
147 String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
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());
159 void testGetPortalHealthCheckFailureNoResponse() throws HttpExecuteException, CipherUtilException {
160 PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
161 String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
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());
173 void testDestroy() throws Exception {
174 HealthCheckBusinessLogic testSubject;
177 testSubject = createTestSubject();
179 testSubject.destroy();
183 void testGetSiteMode() throws Exception {
184 HealthCheckBusinessLogic testSubject;
187 testSubject = createTestSubject();
188 testSubject.getSiteMode();
192 void testAnyStatusChanged() throws Exception {
193 HealthCheckBusinessLogic testSubject;
194 List<HealthCheckInfo> beHealthCheckInfos = null;
195 List<HealthCheckInfo> prevBeHealthCheckInfos = null;
199 testSubject = createTestSubject();
200 beHealthCheckInfos = null;
201 prevBeHealthCheckInfos = null;
202 result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
203 Assertions.assertEquals(false, result);
206 testSubject = createTestSubject();
207 prevBeHealthCheckInfos = null;
208 beHealthCheckInfos = null;
209 result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
210 Assertions.assertEquals(false, result);
213 testSubject = createTestSubject();
214 beHealthCheckInfos = null;
215 prevBeHealthCheckInfos = null;
216 result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
217 Assertions.assertEquals(false, result);
220 testSubject = createTestSubject();
221 prevBeHealthCheckInfos = null;
222 beHealthCheckInfos = null;
223 result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
224 Assertions.assertEquals(false, result);