From f845ac23cdd262b7624785283531952e38557deb Mon Sep 17 00:00:00 2001 From: vasraz Date: Wed, 14 Jun 2023 20:56:59 +0100 Subject: Improve test coverage Signed-off-by: Vasyl Razinkov Change-Id: Ib5c265567973369a9061ce611ab018857016bff1 Issue-ID: SDC-4536 --- .../engine/DistributionEngineClusterHealth.java | 62 ++--- .../DistributionEngineClusterHealthTest.java | 289 +++++++++++---------- .../openecomp/sdc/common/api/HealthCheckInfo.java | 74 +----- .../sdc/common/api/HealthCheckInfoTest.java | 101 +------ 4 files changed, 196 insertions(+), 330 deletions(-) diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealth.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealth.java index 0851f22c0a..9eb0141209 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealth.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealth.java @@ -32,6 +32,8 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import javax.annotation.PreDestroy; +import lombok.AllArgsConstructor; +import lombok.Getter; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.config.DistributionEngineConfiguration; @@ -65,7 +67,7 @@ public class DistributionEngineClusterHealth { private HealthCheckInfo healthCheckInfo = HealthCheckInfoResult.UNKNOWN.getHealthCheckInfo(); private Map envNamePerStatus = null; private ScheduledFuture scheduledFuture = null; - + protected void init(final String publicApiKey) { logger.trace("Enter init method of DistributionEngineClusterHealth"); Long reconnectIntervalConfig = ConfigurationManager.getConfigurationManager().getConfiguration() @@ -153,40 +155,28 @@ public class DistributionEngineClusterHealth { } } - public enum HealthCheckInfoResult { - OK(new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.UP, null, - ClusterStatusDescription.OK.getDescription())), UNAVAILABLE( - new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, - ClusterStatusDescription.UNAVAILABLE.getDescription())), NOT_CONFIGURED( - new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, - ClusterStatusDescription.NOT_CONFIGURED.getDescription())), DISABLED( - new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, - ClusterStatusDescription.DISABLED.getDescription())), UNKNOWN( - new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.UNKNOWN, null, - ClusterStatusDescription.UNKNOWN.getDescription())); - private HealthCheckInfo healthCheckInfo; - - HealthCheckInfoResult(HealthCheckInfo healthCheckInfo) { - this.healthCheckInfo = healthCheckInfo; - } - - public HealthCheckInfo getHealthCheckInfo() { - return healthCheckInfo; - } + @AllArgsConstructor + @Getter + private enum HealthCheckInfoResult { + // @formatter:off + OK (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.UP, null, ClusterStatusDescription.OK.getDescription())), + UNAVAILABLE (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.UNAVAILABLE.getDescription())), + NOT_CONFIGURED (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.NOT_CONFIGURED.getDescription())), + DISABLED (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.DISABLED.getDescription())), + UNKNOWN (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.UNKNOWN, null, ClusterStatusDescription.UNKNOWN.getDescription())); + // @formatter:on + private final HealthCheckInfo healthCheckInfo; } - public enum ClusterStatusDescription { - OK("OK"), UNAVAILABLE("U-EB cluster is not available"), NOT_CONFIGURED("U-EB cluster is not configured"), DISABLED( - "DE is disabled in configuration"), UNKNOWN("U-EB cluster is currently unknown (try again in few minutes)"); - private String desc; - - ClusterStatusDescription(String desc) { - this.desc = desc; - } - - public String getDescription() { - return desc; - } + @AllArgsConstructor + @Getter + private enum ClusterStatusDescription { + OK("OK"), + UNAVAILABLE("U-EB cluster is not available"), + NOT_CONFIGURED("U-EB cluster is not configured"), + DISABLED("DE is disabled in configuration"), + UNKNOWN("U-EB cluster is currently unknown (try again in few minutes)"); + private final String description; } /** @@ -199,11 +189,12 @@ public class DistributionEngineClusterHealth { */ public class HealthCheckScheduledTask implements Runnable { + @Getter List healthCheckCalls = new ArrayList<>(); /** * executor for the query itself */ - ExecutorService healthCheckExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { + private final ExecutorService healthCheckExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, "UEB-Health-Check-Thread"); @@ -298,8 +289,5 @@ public class DistributionEngineClusterHealth { return result; } - public List getHealthCheckCalls() { - return healthCheckCalls; - } } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealthTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealthTest.java index 5f2b412743..7997814a95 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealthTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealthTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,150 +20,153 @@ package org.openecomp.sdc.be.components.distribution.engine; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import mockit.Deencapsulation; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.openecomp.sdc.be.components.BeConfDependentTest; import org.openecomp.sdc.be.components.distribution.engine.DistributionEngineClusterHealth.HealthCheckScheduledTask; import org.openecomp.sdc.common.api.HealthCheckInfo; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; -import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry; - -public class DistributionEngineClusterHealthTest extends BeConfDependentTest{ - - private DistributionEngineClusterHealth createTestSubject() { - return new DistributionEngineClusterHealth(); - } - - @Test - public void testDestroy() throws Exception { - DistributionEngineClusterHealth testSubject; - - // default test - testSubject = createTestSubject(); - Deencapsulation.invoke(testSubject, "destroy"); - } - - @Test - public void testStartHealthCheckTask() throws Exception { - DistributionEngineClusterHealth testSubject; - Map envNamePerStatus = null; - boolean startTask = false; - - // default test - testSubject = createTestSubject(); - testSubject.startHealthCheckTask(envNamePerStatus, startTask); - } - - @Test - public void testStartHealthCheckTask_1() { - final DistributionEngineClusterHealth distributionEngineClusterHealth = new DistributionEngineClusterHealth(); - final Map envNamePerStatus = new HashMap<>(); - distributionEngineClusterHealth.init("myKey"); - distributionEngineClusterHealth.startHealthCheckTask(envNamePerStatus); - } - - @Test - public void testHealthCheckScheduledTask() throws Exception { - DistributionEngineClusterHealth testSubject; - Map envNamePerStatus = new HashMap<>(); - - // default test - testSubject = createTestSubject(); - HealthCheckScheduledTask healthCheckScheduledTask = testSubject. new HealthCheckScheduledTask(new LinkedList<>()); - LinkedList healthCheckCalls = new LinkedList<>(); - UebHealthCheckCall hcc = new UebHealthCheckCall("mock", "mock"); - healthCheckCalls.add(hcc); - healthCheckScheduledTask.healthCheckCalls = healthCheckCalls; - - Deencapsulation.invoke(healthCheckScheduledTask, "queryUeb"); - } - - @Test - public void testHealthCheckScheduledTaskRun() throws Exception { - DistributionEngineClusterHealth testSubject; - Map envNamePerStatus = new HashMap<>(); - envNamePerStatus.put("mock", new AtomicBoolean(true)); - // default test - testSubject = createTestSubject(); - testSubject.startHealthCheckTask(envNamePerStatus, false); - HealthCheckScheduledTask healthCheckScheduledTask = testSubject. new HealthCheckScheduledTask(new LinkedList<>()); - LinkedList healthCheckCalls = new LinkedList<>(); - UebHealthCheckCall hcc = new UebHealthCheckCall("mock", "mock"); - healthCheckCalls.add(hcc); - healthCheckScheduledTask.healthCheckCalls = healthCheckCalls; - - Deencapsulation.invoke(healthCheckScheduledTask, "run"); - } - - @Test - public void testHealthCheckScheduledTaskRun_2() throws Exception { - DistributionEngineClusterHealth testSubject; - Map envNamePerStatus = new HashMap<>(); - envNamePerStatus.put("mock", new AtomicBoolean(false)); - // default test - testSubject = createTestSubject(); - testSubject.startHealthCheckTask(envNamePerStatus, false); - HealthCheckScheduledTask healthCheckScheduledTask = testSubject. new HealthCheckScheduledTask(new LinkedList<>()); - LinkedList healthCheckCalls = new LinkedList<>(); - UebHealthCheckCall hcc = new UebHealthCheckCall("mock", "mock"); - healthCheckCalls.add(hcc); - healthCheckScheduledTask.healthCheckCalls = healthCheckCalls; - - Deencapsulation.invoke(healthCheckScheduledTask, "run"); - } - - @Test - public void testLogAlarm() throws Exception { - DistributionEngineClusterHealth testSubject; - boolean lastHealthState = false; - - // default test - testSubject = createTestSubject(); - Deencapsulation.invoke(testSubject, "logAlarm", new Object[] { lastHealthState }); - } - - @Test - public void testGetHealthCheckInfo() throws Exception { - DistributionEngineClusterHealth testSubject; - HealthCheckInfo result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getHealthCheckInfo(); - } - - @Test - public void testSetHealthCheckUebIsDisabled() throws Exception { - DistributionEngineClusterHealth testSubject; - - // default test - testSubject = createTestSubject(); - testSubject.setHealthCheckUebIsDisabled(); - } - - @Test - public void testSetHealthCheckUebConfigurationError() throws Exception { - DistributionEngineClusterHealth testSubject; - - // default test - testSubject = createTestSubject(); - testSubject.setHealthCheckUebConfigurationError(); - } - - @Test - public void testSetHealthCheckOkAndReportInCaseLastStateIsDown() throws Exception { - DistributionEngineClusterHealth testSubject; - - // default test - testSubject = createTestSubject(); - testSubject.setHealthCheckOkAndReportInCaseLastStateIsDown(); - } +class DistributionEngineClusterHealthTest extends BeConfDependentTest { + + private DistributionEngineClusterHealth createTestSubject() { + return new DistributionEngineClusterHealth(); + } + + // TODO - move to BeConfDependentTest after migration to Junit 5 + @BeforeAll + public static void setupBeforeClass() { + componentName = "catalog-be"; + confPath = "src/test/resources/config"; + setUp(); + } + + @Test + void testDestroy() throws Exception { + DistributionEngineClusterHealth testSubject; + + // default test + testSubject = createTestSubject(); + Deencapsulation.invoke(testSubject, "destroy"); + } + + @Test + void testStartHealthCheckTask() throws Exception { + DistributionEngineClusterHealth testSubject; + Map envNamePerStatus = null; + boolean startTask = false; + + // default test + testSubject = createTestSubject(); + testSubject.startHealthCheckTask(envNamePerStatus, startTask); + } + + @Test + void testStartHealthCheckTask_1() { + final DistributionEngineClusterHealth distributionEngineClusterHealth = new DistributionEngineClusterHealth(); + final Map envNamePerStatus = new HashMap<>(); + distributionEngineClusterHealth.init("myKey"); + distributionEngineClusterHealth.startHealthCheckTask(envNamePerStatus); + } + + @Test + void testHealthCheckScheduledTask() throws Exception { + DistributionEngineClusterHealth testSubject; + Map envNamePerStatus = new HashMap<>(); + + // default test + testSubject = createTestSubject(); + HealthCheckScheduledTask healthCheckScheduledTask = testSubject.new HealthCheckScheduledTask(new LinkedList<>()); + LinkedList healthCheckCalls = new LinkedList<>(); + UebHealthCheckCall hcc = new UebHealthCheckCall("mock", "mock"); + healthCheckCalls.add(hcc); + healthCheckScheduledTask.healthCheckCalls = healthCheckCalls; + + Deencapsulation.invoke(healthCheckScheduledTask, "queryUeb"); + } + + @Test + void testHealthCheckScheduledTaskRun() throws Exception { + DistributionEngineClusterHealth testSubject; + Map envNamePerStatus = new HashMap<>(); + envNamePerStatus.put("mock", new AtomicBoolean(true)); + // default test + testSubject = createTestSubject(); + testSubject.startHealthCheckTask(envNamePerStatus, false); + HealthCheckScheduledTask healthCheckScheduledTask = testSubject.new HealthCheckScheduledTask(new LinkedList<>()); + LinkedList healthCheckCalls = new LinkedList<>(); + UebHealthCheckCall hcc = new UebHealthCheckCall("mock", "mock"); + healthCheckCalls.add(hcc); + healthCheckScheduledTask.healthCheckCalls = healthCheckCalls; + + Deencapsulation.invoke(healthCheckScheduledTask, "run"); + } + + @Test + void testHealthCheckScheduledTaskRun_2() throws Exception { + DistributionEngineClusterHealth testSubject; + Map envNamePerStatus = new HashMap<>(); + envNamePerStatus.put("mock", new AtomicBoolean(false)); + // default test + testSubject = createTestSubject(); + testSubject.startHealthCheckTask(envNamePerStatus, false); + HealthCheckScheduledTask healthCheckScheduledTask = testSubject.new HealthCheckScheduledTask(new LinkedList<>()); + LinkedList healthCheckCalls = new LinkedList<>(); + UebHealthCheckCall hcc = new UebHealthCheckCall("mock", "mock"); + healthCheckCalls.add(hcc); + healthCheckScheduledTask.healthCheckCalls = healthCheckCalls; + + Deencapsulation.invoke(healthCheckScheduledTask, "run"); + } + + @Test + void testLogAlarm() throws Exception { + DistributionEngineClusterHealth testSubject; + boolean lastHealthState = false; + + // default test + testSubject = createTestSubject(); + Deencapsulation.invoke(testSubject, "logAlarm", new Object[]{lastHealthState}); + } + + @Test + void testGetHealthCheckInfo() throws Exception { + DistributionEngineClusterHealth testSubject; + HealthCheckInfo result; + + // default test + testSubject = createTestSubject(); + result = testSubject.getHealthCheckInfo(); + } + + @Test + void testSetHealthCheckUebIsDisabled() throws Exception { + DistributionEngineClusterHealth testSubject; + + // default test + testSubject = createTestSubject(); + testSubject.setHealthCheckUebIsDisabled(); + } + + @Test + void testSetHealthCheckUebConfigurationError() throws Exception { + DistributionEngineClusterHealth testSubject; + + // default test + testSubject = createTestSubject(); + testSubject.setHealthCheckUebConfigurationError(); + } + + @Test + void testSetHealthCheckOkAndReportInCaseLastStateIsDown() throws Exception { + DistributionEngineClusterHealth testSubject; + + // default test + testSubject = createTestSubject(); + testSubject.setHealthCheckOkAndReportInCaseLastStateIsDown(); + } } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/api/HealthCheckInfo.java b/common-app-api/src/main/java/org/openecomp/sdc/common/api/HealthCheckInfo.java index 361701a9d7..009e532174 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/api/HealthCheckInfo.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/api/HealthCheckInfo.java @@ -20,7 +20,17 @@ package org.openecomp.sdc.common.api; import java.util.List; - +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter +@ToString public class HealthCheckInfo { private String healthCheckComponent; @@ -37,65 +47,5 @@ public class HealthCheckInfo { this.description = description; } - public HealthCheckInfo(String healthCheckComponent, HealthCheckStatus healthCheckStatus, String version, String description, - List componentsInfo) { - super(); - this.healthCheckComponent = healthCheckComponent; - this.healthCheckStatus = healthCheckStatus; - this.version = version; - this.description = description; - this.componentsInfo = componentsInfo; - } - - public HealthCheckInfo() { - super(); - } - - public String getHealthCheckComponent() { - return healthCheckComponent; - } - - public void setHealthCheckComponent(String healthCheckComponent) { - this.healthCheckComponent = healthCheckComponent; - } - - public HealthCheckStatus getHealthCheckStatus() { - return healthCheckStatus; - } - - public void setHealthCheckStatus(HealthCheckStatus healthCheckStatus) { - this.healthCheckStatus = healthCheckStatus; - } - - public List getComponentsInfo() { - return componentsInfo; - } - - public void setComponentsInfo(List componentsInfo) { - this.componentsInfo = componentsInfo; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - @Override - public String toString() { - return "HealthCheckInfo [healthCheckComponent=" + healthCheckComponent + ", healthCheckStatus=" + healthCheckStatus + ", version=" + version - + ", description=" + description + ", componentsInfo=" + componentsInfo + "]"; - } - - public enum HealthCheckStatus {UP, DOWN, UNKNOWN;} + public enum HealthCheckStatus {UP, DOWN, UNKNOWN} } diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/api/HealthCheckInfoTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/api/HealthCheckInfoTest.java index 9e0757d4de..2c8e3cb80d 100644 --- a/common-app-api/src/test/java/org/openecomp/sdc/common/api/HealthCheckInfoTest.java +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/api/HealthCheckInfoTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,95 +20,20 @@ package org.openecomp.sdc.common.api; -import org.junit.Test; -import org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus; - -import java.util.List; - -public class HealthCheckInfoTest { - - private HealthCheckInfo createTestSubject() { - return new HealthCheckInfo("", HealthCheckStatus.UP, "", ""); - } - - @Test - public void testGetHealthCheckComponent() throws Exception { - HealthCheckInfo testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getHealthCheckComponent(); - } - - @Test - public void testGetHealthCheckStatus() throws Exception { - HealthCheckInfo testSubject; - HealthCheckStatus result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getHealthCheckStatus(); - } - - @Test - public void testGetComponentsInfo() throws Exception { - HealthCheckInfo testSubject; - List result; +import static org.junit.jupiter.api.Assertions.assertNotNull; - // default test - testSubject = createTestSubject(); - result = testSubject.getComponentsInfo(); - } - - @Test - public void testSetComponentsInfo() throws Exception { - HealthCheckInfo testSubject; - List componentsInfo = null; - - // default test - testSubject = createTestSubject(); - testSubject.setComponentsInfo(componentsInfo); - } - - @Test - public void testGetVersion() throws Exception { - HealthCheckInfo testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getVersion(); - } - - @Test - public void testSetVersion() throws Exception { - HealthCheckInfo testSubject; - String version = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setVersion(version); - } - - @Test - public void testGetDescription() throws Exception { - HealthCheckInfo testSubject; - String result; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus; - // default test - testSubject = createTestSubject(); - result = testSubject.getDescription(); - } +class HealthCheckInfoTest { - @Test - public void testToString() throws Exception { - HealthCheckInfo testSubject; - String result; + private HealthCheckInfo createTestSubject() { + return new HealthCheckInfo("", HealthCheckStatus.UP, "", ""); + } - // default test - testSubject = createTestSubject(); - result = testSubject.toString(); - } + @Test + void testCTOR() { + assertNotNull(createTestSubject()); + } } -- cgit 1.2.3-korg