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.fe.impl;
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;
35 import java.util.Collections;
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;
46 @RunWith(MockitoJUnitRunner.class)
47 public class HealthCheckScheduledTaskTest {
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);
56 private Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig;
58 private Configuration.DcaeConfig dcaeConfig;
60 private Configuration.OnboardingConfig onboardingConfig;
62 private Configuration configuration;
64 private HealthCheckService healthCheckService;
67 private HealthCheckScheduledTask healthCheckScheduledTask;
71 healthCheckScheduledTask = new HealthCheckScheduledTask(healthCheckService);
76 public void getOnboardingUrlWhenConfigurationIsNotProvided() {
77 when(configuration.getOnboarding()).thenReturn(null);
78 assertNull(healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_ON_BOARDING));
82 public void getUrlForUnknownComponent() {
83 assertNull(healthCheckScheduledTask.getExternalComponentHcUrl("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());
95 public void getCatalogFacadeMsUrlWhenConfigurationIsProvidedAndVerifyThatItIsCalculatedOnlyOnce() {
96 when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
97 assertNull(HealthCheckScheduledTask.getCatalogFacadeMsHcUrl());
99 HealthCheckScheduledTask healthCheckScheduledTaskSpy = Mockito.spy(healthCheckScheduledTask);
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));
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());
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));
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));
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));
141 public void getMergedHCListWhenFeHcIsEmptyAndMainListIsSet() {
142 HealthCheckInfo mainHC = new HealthCheckInfo();
143 mainHC.setComponentsInfo(Collections.emptyList());
144 assertEquals(0, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(mainHC, Collections.emptyList()).getComponentsInfo().size());
148 public void getMergedHCListWhenFeHcIsEmptyAndMainListIsNotSet() {
149 assertNull(healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(), Collections.emptyList()).getComponentsInfo());
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());
161 public void getMergedHCListWhenFeHcListIsNotEmptyAndMainListIsEmpty() {
162 assertEquals(1, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(),
163 Collections.singletonList(new HealthCheckInfo())).getComponentsInfo().size());
167 private void initMocks() {
168 when(healthCheckService.getConfig()).thenReturn(configuration);
170 when(onboardingConfig.getProtocolFe()).thenReturn(PROTOCOL);
171 when(onboardingConfig.getHostFe()).thenReturn(HOST);
172 when(onboardingConfig.getPortFe()).thenReturn(PORT);
173 when(onboardingConfig.getHealthCheckUriFe()).thenReturn(URI);
175 when(dcaeConfig.getProtocol()).thenReturn(PROTOCOL);
176 when(dcaeConfig.getHost()).thenReturn(HOST);
177 when(dcaeConfig.getPort()).thenReturn(PORT);
178 when(dcaeConfig.getHealthCheckUri()).thenReturn(URI);
180 when(catalogFacadeMsConfig.getProtocol()).thenReturn(PROTOCOL);
181 when(catalogFacadeMsConfig.getHost()).thenReturn(HOST);
182 when(catalogFacadeMsConfig.getPort()).thenReturn(PORT);
183 when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(URI);