summaryrefslogtreecommitdiffstats
path: root/participant
diff options
context:
space:
mode:
authorrameshiyer27 <ramesh.murugan.iyer@est.tech>2022-12-06 17:04:32 +0000
committerrameshiyer27 <ramesh.murugan.iyer@est.tech>2022-12-08 11:27:56 +0000
commitb68a2c490a0290b6c7451c28ae3528c1e9dbd3a6 (patch)
tree87c5a999a8e9e957691687e500f646391811d254 /participant
parent6f3447836b04f2fbea1fa4fca526d549bec42661 (diff)
Increase code coverage of k8s-participant module
Unit tests with static mocks are being ignored in the build causing drop in the code coverage of k8s participant module. Static implementation in the code has been changed as it is not required. Code coverage improved to 85% Issue-ID: POLICY-4477 Signed-off-by: zrrmmua <ramesh.murugan.iyer@est.tech> Change-Id: I82facafffb8da1823699f63da480951b60060888
Diffstat (limited to 'participant')
-rw-r--r--participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java2
-rw-r--r--participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java4
-rw-r--r--participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java34
-rw-r--r--participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java43
4 files changed, 38 insertions, 45 deletions
diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java
index f3f7d3427..a05dfbc5f 100644
--- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java
+++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java
@@ -143,7 +143,7 @@ public class HelmClient {
* @return string output
* @throws ServiceException incase of error.
*/
- public static String executeCommand(ProcessBuilder processBuilder) throws ServiceException {
+ public String executeCommand(ProcessBuilder processBuilder) throws ServiceException {
var commandStr = toString(processBuilder);
try {
diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java
index 67bdc0b40..89eb284eb 100644
--- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java
+++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java
@@ -46,6 +46,8 @@ public class PodStatusValidator implements Runnable {
private ChartInfo chart;
+ private HelmClient client = new HelmClient();
+
/**
* Constructor for PodStatusValidator.
* @param chart chartInfo
@@ -76,7 +78,7 @@ public class PodStatusValidator implements Runnable {
long endTime = System.currentTimeMillis() + (timeout * 1000L);
while (!isVerified && System.currentTimeMillis() < endTime) {
- var output = HelmClient.executeCommand(verifyPodStatusCommand(chart));
+ var output = client.executeCommand(verifyPodStatusCommand(chart));
var podStatusMap = mapPodStatus(output);
isVerified = !podStatusMap.isEmpty()
&& podStatusMap.values().stream().allMatch("Running"::equals);
diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java
index f5826bf3b..19106a623 100644
--- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java
+++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java
@@ -29,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import java.io.File;
@@ -42,8 +42,6 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
-import org.mockito.MockedStatic;
-import org.mockito.Mockito;
import org.mockito.Spy;
import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
@@ -74,25 +72,20 @@ class HelmClientTest {
@Mock
HelmRepository repo;
- private static MockedStatic<HelmClient> mockedClient;
@BeforeAll
static void init() throws CoderException {
charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
- //Mock static method for bash command execution
- mockedClient = mockStatic(HelmClient.class);
}
@AfterAll
public static void close() throws IOException {
- mockedClient.close();
FileSystemUtils.deleteRecursively(Path.of("target/tmp"));
}
@Test
- void test_installChart() {
- mockedClient.when(() -> HelmClient.executeCommand(any()))
- .thenReturn("success");
+ void test_installChart() throws ServiceException {
+ doReturn("success").when(helmClient).executeCommand(any());
doReturn(new File("/target/tmp/override.yaml")).when(chartStore)
.getOverrideFile(any());
var chartinfo = charts.get(0);
@@ -101,29 +94,28 @@ class HelmClientTest {
chartinfo.setNamespace("");
assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
- mockedClient.when(() -> HelmClient.executeCommand(any())).thenReturn("");
+ doReturn("").when(helmClient).executeCommand(any());
assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
}
@Test
- void test_addRepository() {
- mockedClient.when(() -> HelmClient.executeCommand(any())).thenReturn("");
+ void test_addRepository() throws ServiceException {
+ doReturn("").when(helmClient).executeCommand(any());
when(repo.getRepoName()).thenReturn("RepoName");
when(repo.getAddress()).thenReturn("http://localhost:8080");
assertDoesNotThrow(() -> helmClient.addRepository(repo));
- mockedClient.when(() -> HelmClient.executeCommand(any()))
- .thenReturn("failed");
+ doReturn("failed").when(helmClient).executeCommand(any());
assertDoesNotThrow(() -> helmClient.addRepository(repo));
}
@Test
void test_findChartRepository() throws IOException, ServiceException {
String tmpPath = "target/tmp/dummyChart/1.0/";
- mockedClient.when(() -> HelmClient.executeCommand(Mockito.any()))
- .thenReturn("nginx-stable/nginx-ingress\t0.9.3\t1.11.3"
- + " \tNGINX Ingress Controller");
+ doReturn("nginx-stable/nginx-ingress\t0.9.3\t1.11.3"
+ + " \tNGINX Ingress Controller").when(helmClient).executeCommand(any());
+
String configuredRepo = helmClient.findChartRepository(charts.get(1));
assertThat(configuredRepo).isEqualTo("nginx-stable");
@@ -143,8 +135,9 @@ class HelmClientTest {
@Test
void test_uninstallChart() throws ServiceException {
+ doReturn("success").when(helmClient).executeCommand(any());
helmClient.uninstallChart(charts.get(0));
- mockedClient.when(() -> HelmClient.executeCommand(any())).thenThrow(new ServiceException("error in execution"));
+ doThrow(ServiceException.class).when(helmClient).executeCommand(any());
assertThatThrownBy(() -> helmClient.uninstallChart(charts.get(0)))
.isInstanceOf(ServiceException.class);
@@ -152,8 +145,7 @@ class HelmClientTest {
@Test
void test_verifyConfiguredRepoForInvalidChart() throws IOException, ServiceException {
- mockedClient.when(() -> HelmClient.executeCommand(Mockito.any()))
- .thenReturn("");
+ doReturn("").when(helmClient).executeCommand(any());
String configuredRepo = helmClient.verifyConfiguredRepo(charts.get(1));
assertNull(configuredRepo);
}
diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java
index 962744db7..6cec6056d 100644
--- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java
+++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java
@@ -25,17 +25,17 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.doReturn;
import java.io.File;
import java.util.List;
import java.util.Map;
-import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.MockedStatic;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
import org.onap.policy.clamp.acm.participant.kubernetes.handler.AutomationCompositionElementHandler;
import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
@@ -54,12 +54,21 @@ class PodStatusValidatorTest {
private static int STATUS_CHECK_INTERVAL = 1;
private static List<ChartInfo> charts;
- private static MockedStatic<HelmClient> mockedClient;
+ @InjectMocks
+ private PodStatusValidator podStatusValidator = new PodStatusValidator(charts.get(0), TIMEOUT,
+ STATUS_CHECK_INTERVAL);
+
+ @InjectMocks
+ private PodStatusValidator podValidatorWithPodName = new PodStatusValidator(charts.get(2), TIMEOUT,
+ STATUS_CHECK_INTERVAL);
+
+
+ @Mock
+ private HelmClient client;
@BeforeAll
static void init() throws CoderException {
charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
- mockedClient = mockStatic(HelmClient.class);
}
@AfterEach
@@ -67,17 +76,11 @@ class PodStatusValidatorTest {
AutomationCompositionElementHandler.getPodStatusMap().clear();
}
- @AfterAll
- public static void close() {
- mockedClient.close();
- }
@Test
- void test_RunningPodState() {
+ void test_RunningPodState() throws ServiceException {
String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nHelloWorld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h";
- mockedClient.when(() -> HelmClient.executeCommand(any()))
- .thenReturn(runningPod);
- var podStatusValidator = new PodStatusValidator(charts.get(0), TIMEOUT, STATUS_CHECK_INTERVAL);
+ doReturn(runningPod).when(client).executeCommand(any());
assertDoesNotThrow(() -> podStatusValidator.run());
assertThat(AutomationCompositionElementHandler.getPodStatusMap()).hasSize(1);
assertThat(AutomationCompositionElementHandler.getPodStatusMap()).containsKey(charts.get(0).getReleaseName());
@@ -86,11 +89,9 @@ class PodStatusValidatorTest {
}
@Test
- void test_InvalidPodState() {
+ void test_InvalidPodState() throws ServiceException {
String invalidPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\nhellofromdocker-54777df9f8-qpzqr\t1/1\tInit\t0\t9h";
- mockedClient.when(() -> HelmClient.executeCommand(any()))
- .thenReturn(invalidPod);
- var podStatusValidator = new PodStatusValidator(charts.get(1), TIMEOUT, STATUS_CHECK_INTERVAL);
+ doReturn(invalidPod).when(client).executeCommand(any());
assertThatThrownBy(() -> podStatusValidator.run())
.isInstanceOf(ServiceException.class).hasMessage("Error verifying the status of the pod. Exiting");
assertThat(AutomationCompositionElementHandler.getPodStatusMap()).isEmpty();
@@ -98,12 +99,10 @@ class PodStatusValidatorTest {
// Use case scenario: Hard coded pod name
@Test
- void test_RunningPodStateWhitPodName() {
+ void test_RunningPodStateWithPodName() throws ServiceException {
String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nhelloallworld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h";
- mockedClient.when(() -> HelmClient.executeCommand(any()))
- .thenReturn(runningPod);
- var podStatusValidator = new PodStatusValidator(charts.get(2), TIMEOUT, STATUS_CHECK_INTERVAL);
- assertDoesNotThrow(() -> podStatusValidator.run());
+ doReturn(runningPod).when(client).executeCommand(any());
+ assertDoesNotThrow(() -> podValidatorWithPodName.run());
assertThat(AutomationCompositionElementHandler.getPodStatusMap()).hasSize(1);
assertThat(AutomationCompositionElementHandler.getPodStatusMap()).containsKey(charts.get(2).getReleaseName());
assertThat(AutomationCompositionElementHandler.getPodStatusMap())