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
* @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 {
private ChartInfo chart;
+ private HelmClient client = new HelmClient();
+
/**
* Constructor for PodStatusValidator.
* @param chart chartInfo
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);
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;
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;
@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);
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");
@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);
@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);
}
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;
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
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());
}
@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();
// 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())