### Question:
DockerUtil { public Optional<Statistics> getContainerStats(String containerId) { LoggingService.logDebug(MODULE_NAME ,"Start get Container Stats"); StatsCmd statsCmd = dockerClient.statsCmd(containerId); CountDownLatch countDownLatch = new CountDownLatch(1); StatsCallback stats = new StatsCallback(countDownLatch); try (StatsCallback statscallback = statsCmd.exec(stats)) { countDownLatch.await(1, TimeUnit.SECONDS); } catch (InterruptedException | IOException e) { LoggingService.logError(MODULE_NAME, "Error while getting Container Stats", new AgentUserException(e.getMessage(), e)); } LoggingService.logDebug(MODULE_NAME ,"Finished get Container Stats"); return Optional.ofNullable(stats.getStats()); } private  DockerUtil(); static DockerUtil getInstance(); void reInitDockerClient(); String getDockerBridgeName(); void startContainer(Microservice microservice); void stopContainer(String id); void removeContainer(String id, Boolean withRemoveVolumes); @SuppressWarnings("deprecation") String getContainerIpAddress(String id); String getContainerName(Container container); String getContainerMicroserviceUuid(Container container); static String getIoFogContainerName(String microserviceUuid); Optional<Container> getContainer(String microserviceUuid); MicroserviceStatus getMicroserviceStatus(String containerId); List<Container> getRunningContainers(); List<Container> getRunningIofogContainers(); Optional<Statistics> getContainerStats(String containerId); long getContainerStartedAt(String id); boolean areMicroserviceAndContainerEqual(String containerId, Microservice microservice); Optional<String> getContainerStatus(String containerId); boolean isContainerRunning(String containerId); List<Container> getContainers(); void removeImageById(String imageId); @SuppressWarnings("resource") void pullImage(String imageName, String microserviceUuid, Registry registry); boolean findLocalImage(String imageName); String createContainer(Microservice microservice, String host); PruneResponse dockerPrune(); void update(PullResponseItem item, Map<String, ItemStatus> statuses);  }### Answer:
@Test public void testGetContainerStatsWhenStatsArePresent() { assertEquals(Optional.of(statistics), dockerUtil.getContainerStats(containerID)); Mockito.verify(statsCmd).exec(any()); Mockito.verify(dockerClient).statsCmd(any()); Mockito.verify(statsCallback).getStats(); }
@Test public void testGetContainerStatsWhenStatsAreNotPresent() { PowerMockito.when(statsCallback.getStats()).thenReturn(null); assertEquals(Optional.empty(), dockerUtil.getContainerStats(containerID)); Mockito.verify(statsCmd).exec(any()); Mockito.verify(dockerClient).statsCmd(any()); Mockito.verify(statsCallback).getStats(); }
@Test public void throwsExceptionWhenExecIsCalledInGetContainerStatsWhenStatsAreNotPresent() { try { PowerMockito.doThrow(spy(new InterruptedException("InterruptedException"))).when(countDownLatch).await(anyLong(), any(TimeUnit.class)); assertEquals(Optional.of(statistics), dockerUtil.getContainerStats(containerID)); Mockito.verify(statsCmd).exec(any()); Mockito.verify(dockerClient).statsCmd(any()); Mockito.verify(statsCallback).getStats(); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logError(eq(MODULE_NAME), eq("Error while getting Container Stats"), any()); } catch (InterruptedException e) { fail("This should not happen"); } }