Catalog alignment
[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.DcaeConfig dcaeConfig;
59     @Mock
60     private Configuration.OnboardingConfig onboardingConfig;
61     @Mock
62     private Configuration configuration;
63     @Mock
64     private HealthCheckService healthCheckService;
65
66     @InjectMocks
67     private HealthCheckScheduledTask healthCheckScheduledTask;
68
69     @Before
70     public void setUp() {
71         healthCheckScheduledTask = new HealthCheckScheduledTask(healthCheckService);
72         initMocks();
73     }
74
75     @Test
76     public void getOnboardingUrlWhenConfigurationIsNotProvided() {
77         when(configuration.getOnboarding()).thenReturn(null);
78         assertNull(healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_ON_BOARDING));
79     }
80
81     @Test
82     public void getUrlForUnknownComponent() {
83         assertNull(healthCheckScheduledTask.getExternalComponentHcUrl("test"));
84     }
85
86     @Test
87     public void getOnboardingUrlWhenConfigurationIsProvided() {
88         when(configuration.getOnboarding()).thenReturn(onboardingConfig);
89         assertNull(HealthCheckScheduledTask.getOnboardingHcUrl());
90         healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_ON_BOARDING);
91         assertEquals(HC_URL, HealthCheckScheduledTask.getOnboardingHcUrl());
92     }
93
94     @Test
95     public void getCatalogFacadeMsUrlWhenConfigurationIsProvidedAndVerifyThatItIsCalculatedOnlyOnce() {
96         when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
97         assertNull(HealthCheckScheduledTask.getCatalogFacadeMsHcUrl());
98
99         HealthCheckScheduledTask healthCheckScheduledTaskSpy = Mockito.spy(healthCheckScheduledTask);
100
101         healthCheckScheduledTaskSpy.getExternalComponentHcUrl(Constants.HC_COMPONENT_CATALOG_FACADE_MS);
102         assertEquals(HC_URL, HealthCheckScheduledTask.getCatalogFacadeMsHcUrl());
103         //try to run again and verify that assignment is not recalled
104         healthCheckScheduledTaskSpy.getExternalComponentHcUrl(Constants.HC_COMPONENT_CATALOG_FACADE_MS);
105         verify(healthCheckScheduledTaskSpy, times(1)).
106                 buildHealthCheckUrl(any(String.class), any(String.class), any(Integer.class), any(String.class));
107     }
108
109     @Test
110     public void getDcaeUrlWhenConfigurationIsProvided() {
111         when(configuration.getDcae()).thenReturn(dcaeConfig);
112         assertNull(HealthCheckScheduledTask.getDcaeHcUrl());
113         healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_DCAE);
114         assertEquals(HC_URL, HealthCheckScheduledTask.getDcaeHcUrl());
115     }
116
117     @Test
118     public void getExcludedComponentListWhenCatalogFacadeMsConfigExists() {
119         when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
120         when(catalogFacadeMsConfig.getPath()).thenReturn("/uicache");
121         when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList("DMAAP", "DCAE"));
122         assertFalse(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS));
123     }
124
125     @Test
126     public void getExcludedComponentListWhenCatalogFacadeMsConfigDoesNotExist() {
127         when(configuration.getCatalogFacadeMs()).thenReturn(null);
128         when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList());
129         assertTrue(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS));
130     }
131
132     @Test
133     public void getExcludedComponentListWhenCatalogFacadeMsConfigPathIsNotSet() {
134         when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
135         when(catalogFacadeMsConfig.getPath()).thenReturn(null);
136         when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList());
137         assertTrue(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS));
138     }
139
140     @Test
141     public void getMergedHCListWhenFeHcIsEmptyAndMainListIsSet() {
142         HealthCheckInfo mainHC = new HealthCheckInfo();
143         mainHC.setComponentsInfo(Collections.emptyList());
144         assertEquals(0, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(mainHC, Collections.emptyList()).getComponentsInfo().size());
145     }
146
147     @Test
148     public void getMergedHCListWhenFeHcIsEmptyAndMainListIsNotSet() {
149         assertNull(healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(), Collections.emptyList()).getComponentsInfo());
150     }
151
152     @Test
153     public void getMergedHCListWhenFeHcListAndMainListAreNotEmpty() {
154         HealthCheckInfo mainHC = new HealthCheckInfo();
155         mainHC.setComponentsInfo(Lists.newArrayList(new HealthCheckInfo()));
156         assertEquals(2, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(mainHC,
157                 Collections.singletonList(new HealthCheckInfo())).getComponentsInfo().size());
158     }
159
160     @Test
161     public void getMergedHCListWhenFeHcListIsNotEmptyAndMainListIsEmpty() {
162         assertEquals(1, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(),
163                 Collections.singletonList(new HealthCheckInfo())).getComponentsInfo().size());
164     }
165
166
167     private void initMocks() {
168         when(healthCheckService.getConfig()).thenReturn(configuration);
169
170         when(onboardingConfig.getProtocolFe()).thenReturn(PROTOCOL);
171         when(onboardingConfig.getHostFe()).thenReturn(HOST);
172         when(onboardingConfig.getPortFe()).thenReturn(PORT);
173         when(onboardingConfig.getHealthCheckUriFe()).thenReturn(URI);
174
175         when(dcaeConfig.getProtocol()).thenReturn(PROTOCOL);
176         when(dcaeConfig.getHost()).thenReturn(HOST);
177         when(dcaeConfig.getPort()).thenReturn(PORT);
178         when(dcaeConfig.getHealthCheckUri()).thenReturn(URI);
179
180         when(catalogFacadeMsConfig.getProtocol()).thenReturn(PROTOCOL);
181         when(catalogFacadeMsConfig.getHost()).thenReturn(HOST);
182         when(catalogFacadeMsConfig.getPort()).thenReturn(PORT);
183         when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(URI);
184     }
185 }