Remove unused dcae-be healthcheck
[sdc.git] / catalog-fe / src / test / java / org / openecomp / sdc / fe / impl / HealthCheckScheduledTaskTest.java
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.fe.impl;
22
23 import com.google.common.collect.Lists;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.openecomp.sdc.common.api.Constants;
32 import org.openecomp.sdc.common.api.HealthCheckInfo;
33 import org.openecomp.sdc.fe.config.Configuration;
34
35 import java.util.Collections;
36
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertFalse;
39 import static org.junit.Assert.assertNull;
40 import static org.junit.Assert.assertTrue;
41 import static org.mockito.ArgumentMatchers.any;
42 import static org.mockito.Mockito.times;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.when;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class HealthCheckScheduledTaskTest {
48
49     private static final String PROTOCOL = "http";
50     private static final String HOST = "192.115.113.25";
51     private static final Integer PORT = 8090;
52     private static final String URI = "/healthCheck";
53     private static final String HC_URL = String.format("%s://%s:%s%s", PROTOCOL, HOST, PORT, URI);
54
55     @Mock
56     private Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig;
57     @Mock
58     private Configuration.OnboardingConfig onboardingConfig;
59     @Mock
60     private Configuration configuration;
61     @Mock
62     private HealthCheckService healthCheckService;
63
64     @InjectMocks
65     private HealthCheckScheduledTask healthCheckScheduledTask;
66
67     @Before
68     public void setUp() {
69         healthCheckScheduledTask = new HealthCheckScheduledTask(healthCheckService);
70         initMocks();
71     }
72
73     @Test
74     public void getOnboardingUrlWhenConfigurationIsNotProvided() {
75         when(configuration.getOnboarding()).thenReturn(null);
76         assertNull(healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_ON_BOARDING));
77     }
78
79     @Test
80     public void getUrlForUnknownComponent() {
81         assertNull(healthCheckScheduledTask.getExternalComponentHcUrl("test"));
82     }
83
84     @Test
85     public void getOnboardingUrlWhenConfigurationIsProvided() {
86         when(configuration.getOnboarding()).thenReturn(onboardingConfig);
87         assertNull(HealthCheckScheduledTask.getOnboardingHcUrl());
88         healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_ON_BOARDING);
89         assertEquals(HC_URL, HealthCheckScheduledTask.getOnboardingHcUrl());
90     }
91
92     @Test
93     public void getCatalogFacadeMsUrlWhenConfigurationIsProvidedAndVerifyThatItIsCalculatedOnlyOnce() {
94         when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
95         assertNull(HealthCheckScheduledTask.getCatalogFacadeMsHcUrl());
96
97         HealthCheckScheduledTask healthCheckScheduledTaskSpy = Mockito.spy(healthCheckScheduledTask);
98
99         healthCheckScheduledTaskSpy.getExternalComponentHcUrl(Constants.HC_COMPONENT_CATALOG_FACADE_MS);
100         assertEquals(HC_URL, HealthCheckScheduledTask.getCatalogFacadeMsHcUrl());
101         //try to run again and verify that assignment is not recalled
102         healthCheckScheduledTaskSpy.getExternalComponentHcUrl(Constants.HC_COMPONENT_CATALOG_FACADE_MS);
103         verify(healthCheckScheduledTaskSpy, times(1)).
104                 buildHealthCheckUrl(any(String.class), any(String.class), any(Integer.class), any(String.class));
105     }
106
107     @Test
108     public void getExcludedComponentListWhenCatalogFacadeMsConfigExists() {
109         when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
110         when(catalogFacadeMsConfig.getPath()).thenReturn("/uicache");
111         when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList("DMAAP", "DCAE"));
112         assertFalse(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS));
113     }
114
115     @Test
116     public void getExcludedComponentListWhenCatalogFacadeMsConfigDoesNotExist() {
117         when(configuration.getCatalogFacadeMs()).thenReturn(null);
118         when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList());
119         assertTrue(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS));
120     }
121
122     @Test
123     public void getExcludedComponentListWhenCatalogFacadeMsConfigPathIsNotSet() {
124         when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
125         when(catalogFacadeMsConfig.getPath()).thenReturn(null);
126         when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList());
127         assertTrue(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS));
128     }
129
130     @Test
131     public void getMergedHCListWhenFeHcIsEmptyAndMainListIsSet() {
132         HealthCheckInfo mainHC = new HealthCheckInfo();
133         mainHC.setComponentsInfo(Collections.emptyList());
134         assertEquals(0, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(mainHC, Collections.emptyList()).getComponentsInfo().size());
135     }
136
137     @Test
138     public void getMergedHCListWhenFeHcIsEmptyAndMainListIsNotSet() {
139         assertNull(healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(), Collections.emptyList()).getComponentsInfo());
140     }
141
142     @Test
143     public void getMergedHCListWhenFeHcListAndMainListAreNotEmpty() {
144         HealthCheckInfo mainHC = new HealthCheckInfo();
145         mainHC.setComponentsInfo(Lists.newArrayList(new HealthCheckInfo()));
146         assertEquals(2, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(mainHC,
147                 Collections.singletonList(new HealthCheckInfo())).getComponentsInfo().size());
148     }
149
150     @Test
151     public void getMergedHCListWhenFeHcListIsNotEmptyAndMainListIsEmpty() {
152         assertEquals(1, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(),
153                 Collections.singletonList(new HealthCheckInfo())).getComponentsInfo().size());
154     }
155
156
157     private void initMocks() {
158         when(healthCheckService.getConfig()).thenReturn(configuration);
159
160         when(onboardingConfig.getProtocolFe()).thenReturn(PROTOCOL);
161         when(onboardingConfig.getHostFe()).thenReturn(HOST);
162         when(onboardingConfig.getPortFe()).thenReturn(PORT);
163         when(onboardingConfig.getHealthCheckUriFe()).thenReturn(URI);
164
165         when(catalogFacadeMsConfig.getProtocol()).thenReturn(PROTOCOL);
166         when(catalogFacadeMsConfig.getHost()).thenReturn(HOST);
167         when(catalogFacadeMsConfig.getPort()).thenReturn(PORT);
168         when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(URI);
169     }
170 }