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.OnboardingConfig onboardingConfig;
60 private Configuration configuration;
62 private HealthCheckService healthCheckService;
65 private HealthCheckScheduledTask healthCheckScheduledTask;
69 healthCheckScheduledTask = new HealthCheckScheduledTask(healthCheckService);
74 public void getOnboardingUrlWhenConfigurationIsNotProvided() {
75 when(configuration.getOnboarding()).thenReturn(null);
76 assertNull(healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_ON_BOARDING));
80 public void getUrlForUnknownComponent() {
81 assertNull(healthCheckScheduledTask.getExternalComponentHcUrl("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());
93 public void getCatalogFacadeMsUrlWhenConfigurationIsProvidedAndVerifyThatItIsCalculatedOnlyOnce() {
94 when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
95 assertNull(HealthCheckScheduledTask.getCatalogFacadeMsHcUrl());
97 HealthCheckScheduledTask healthCheckScheduledTaskSpy = Mockito.spy(healthCheckScheduledTask);
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));
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));
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));
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));
131 public void getMergedHCListWhenFeHcIsEmptyAndMainListIsSet() {
132 HealthCheckInfo mainHC = new HealthCheckInfo();
133 mainHC.setComponentsInfo(Collections.emptyList());
134 assertEquals(0, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(mainHC, Collections.emptyList()).getComponentsInfo().size());
138 public void getMergedHCListWhenFeHcIsEmptyAndMainListIsNotSet() {
139 assertNull(healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(), Collections.emptyList()).getComponentsInfo());
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());
151 public void getMergedHCListWhenFeHcListIsNotEmptyAndMainListIsEmpty() {
152 assertEquals(1, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(),
153 Collections.singletonList(new HealthCheckInfo())).getComponentsInfo().size());
157 private void initMocks() {
158 when(healthCheckService.getConfig()).thenReturn(configuration);
160 when(onboardingConfig.getProtocolFe()).thenReturn(PROTOCOL);
161 when(onboardingConfig.getHostFe()).thenReturn(HOST);
162 when(onboardingConfig.getPortFe()).thenReturn(PORT);
163 when(onboardingConfig.getHealthCheckUriFe()).thenReturn(URI);
165 when(catalogFacadeMsConfig.getProtocol()).thenReturn(PROTOCOL);
166 when(catalogFacadeMsConfig.getHost()).thenReturn(HOST);
167 when(catalogFacadeMsConfig.getPort()).thenReturn(PORT);
168 when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(URI);