summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/health/HealthCheckBusinessLogicHealthTest.java
blob: 41ff6c0e361c5b8234e70c5a65a173d59df5d384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.health;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.LinkedList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
import org.openecomp.sdc.be.catalog.impl.DmaapProducerHealth;
import org.openecomp.sdc.be.components.BeConfDependentTest;
import org.openecomp.sdc.be.components.distribution.engine.DistributionEngineClusterHealth;
import org.openecomp.sdc.be.components.distribution.engine.DmaapHealth;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.switchover.detector.SwitchoverDetector;
import org.openecomp.sdc.common.api.HealthCheckInfo;
import org.openecomp.sdc.common.http.client.api.HttpExecuteException;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import org.springframework.test.util.ReflectionTestUtils;

@ExtendWith(MockitoExtension.class)
class HealthCheckBusinessLogicHealthTest extends BeConfDependentTest {

    // TODO - remove this setup after migration to Junit5 BeConfDependentTest
    @BeforeAll
    static void setup() {
        configurationManager =
            new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
    }

    private final DmaapProducerHealth dmaapProducerHealth = mock(DmaapProducerHealth.class);
    private final HealthCheckInfo dmaapProducerHealthCheckInfo = mock(HealthCheckInfo.class);

    private HealthCheckBusinessLogic createTestSubject() {

        HealthCheckBusinessLogic healthCheckBusinessLogic = new HealthCheckBusinessLogic();
        DmaapHealth dmaapHealth = new DmaapHealth();
        ReflectionTestUtils.setField(healthCheckBusinessLogic, "dmaapHealth", dmaapHealth);
        PortalHealthCheckBuilder portalHealthCheckBuilder = new PortalHealthCheckBuilder();
        ReflectionTestUtils.setField(healthCheckBusinessLogic, "portalHealthCheck", portalHealthCheckBuilder);
        DistributionEngineClusterHealth distributionEngineClusterHealth = new DistributionEngineClusterHealth();
        ReflectionTestUtils.setField(healthCheckBusinessLogic, "distributionEngineClusterHealth",
            distributionEngineClusterHealth);
        SwitchoverDetector switchoverDetector = new SwitchoverDetector();
        ReflectionTestUtils.setField(healthCheckBusinessLogic, "switchoverDetector", switchoverDetector);
        List<HealthCheckInfo> prevBeHealthCheckInfos = new LinkedList<>();
        ReflectionTestUtils.setField(healthCheckBusinessLogic, "prevBeHealthCheckInfos", prevBeHealthCheckInfos);
        ReflectionTestUtils.setField(healthCheckBusinessLogic, "dmaapProducerHealth", dmaapProducerHealth);
        return healthCheckBusinessLogic;
    }

    @BeforeEach
    void beforeTest() {
        when(dmaapProducerHealth.getHealthCheckInfo())
            .thenReturn(dmaapProducerHealthCheckInfo);
    }

    @Test
    void testInit() throws Exception {
        HealthCheckBusinessLogic testSubject = createTestSubject();
        testSubject.init();
    }

    @Test
    void testIsDistributionEngineUp() throws Exception {
        HealthCheckBusinessLogic testSubject;
        // default test
        testSubject = createTestSubject();
        testSubject.isDistributionEngineUp();
    }

    @Test
    void testGetBeHealthCheckInfosStatus() throws Exception {
        HealthCheckBusinessLogic testSubject;

        // default test
        testSubject = createTestSubject();
        testSubject.getBeHealthCheckInfosStatus();
    }

    @Test
    void testGetJanusGraphHealthCheck() throws Exception {
        HealthCheckBusinessLogic testSubject;
        List<HealthCheckInfo> healthCheckInfos = new LinkedList<>();

        // default test
        testSubject = createTestSubject();
        healthCheckInfos.add(testSubject.getJanusGraphHealthCheck());
    }

    @Test
    void testGetPortalHealthCheckSuccess() throws Exception {
        PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
        String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
        int timeout = 3000;
        doReturn(200).when(testSubject).getStatusCode(healthCheckURL, timeout);
        testSubject.init();
        testSubject.runTask();
        HealthCheckInfo hci = testSubject.getHealthCheckInfo();
        Assertions.assertEquals("PORTAL", hci.getHealthCheckComponent());
        Assertions.assertEquals(HealthCheckInfo.HealthCheckStatus.UP, hci.getHealthCheckStatus());
        Assertions.assertEquals("OK", hci.getDescription());
    }

    @Test
    void testGetPortalHealthCheckFailureMissingConfig() throws Exception {
        PortalHealthCheckBuilder testSubject = new PortalHealthCheckBuilder();
        testSubject.init(null);
        HealthCheckInfo hci = testSubject.getHealthCheckInfo();
        Assertions.assertEquals("PORTAL", hci.getHealthCheckComponent());
        Assertions.assertEquals(HealthCheckInfo.HealthCheckStatus.DOWN, hci.getHealthCheckStatus());
        Assertions.assertEquals("PORTAL health check configuration is missing", hci.getDescription());
    }

    @Test
    void testGetPortalHealthCheckFailureErrorResponse() throws HttpExecuteException, CipherUtilException {
        PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
        String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
        int timeout = 3000;
        doReturn(404).when(testSubject).getStatusCode(healthCheckURL, timeout);
        testSubject.init(testSubject.getConfiguration());
        testSubject.runTask();
        HealthCheckInfo hci = testSubject.getHealthCheckInfo();
        Assertions.assertEquals("PORTAL", hci.getHealthCheckComponent());
        Assertions.assertEquals(HealthCheckInfo.HealthCheckStatus.DOWN, hci.getHealthCheckStatus());
        Assertions.assertEquals("PORTAL responded with 404 status code", hci.getDescription());
    }

    @Test
    void testGetPortalHealthCheckFailureNoResponse() throws HttpExecuteException, CipherUtilException {
        PortalHealthCheckBuilder testSubject = spy(PortalHealthCheckBuilder.class);
        String healthCheckURL = testSubject.buildPortalHealthCheckUrl();
        int timeout = 3000;
        doThrow(HttpExecuteException.class).when(testSubject).getStatusCode(healthCheckURL, timeout);
        testSubject.init(testSubject.getConfiguration());
        testSubject.runTask();
        HealthCheckInfo hci = testSubject.getHealthCheckInfo();
        Assertions.assertEquals("PORTAL", hci.getHealthCheckComponent());
        Assertions.assertEquals(HealthCheckInfo.HealthCheckStatus.DOWN, hci.getHealthCheckStatus());
        Assertions.assertEquals("PORTAL is not available", hci.getDescription());
    }

    @Test
    void testDestroy() throws Exception {
        HealthCheckBusinessLogic testSubject;

        // default test
        testSubject = createTestSubject();
        testSubject.init();
        testSubject.destroy();
    }

    @Test
    void testGetSiteMode() throws Exception {
        HealthCheckBusinessLogic testSubject;

        // default test
        testSubject = createTestSubject();
        testSubject.getSiteMode();
    }

    @Test
    void testAnyStatusChanged() throws Exception {
        HealthCheckBusinessLogic testSubject;
        List<HealthCheckInfo> beHealthCheckInfos = null;
        List<HealthCheckInfo> prevBeHealthCheckInfos = null;
        boolean result;

        // test 1
        testSubject = createTestSubject();
        beHealthCheckInfos = null;
        prevBeHealthCheckInfos = null;
        result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
        Assertions.assertEquals(false, result);

        // test 2
        testSubject = createTestSubject();
        prevBeHealthCheckInfos = null;
        beHealthCheckInfos = null;
        result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
        Assertions.assertEquals(false, result);

        // test 3
        testSubject = createTestSubject();
        beHealthCheckInfos = null;
        prevBeHealthCheckInfos = null;
        result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
        Assertions.assertEquals(false, result);

        // test 4
        testSubject = createTestSubject();
        prevBeHealthCheckInfos = null;
        beHealthCheckInfos = null;
        result = testSubject.anyStatusChanged(beHealthCheckInfos, prevBeHealthCheckInfos);
        Assertions.assertEquals(false, result);
    }

}