### Question:
DockerHelper { @SuppressWarnings("DefaultCharset") public String getContainerId() { try (BufferedReader br = new BufferedReader(new FileReader(cgroupPath))) { String line; while ((line = br.readLine()) != null) { if (line.length() > CONTAINER_ID_LENGTH) { return line.substring(line.length() - CONTAINER_ID_LENGTH); } } } catch (FileNotFoundException e) { logger.log(Level.WARNING, "Failed to read container id, cgroup file does not exist."); } catch (IOException e) { logger.log(Level.WARNING, "Unable to read container id: " + e.getMessage()); } return ""; } DockerHelper(); @VisibleForTesting  DockerHelper(String cgroupPath); @SuppressWarnings("DefaultCharset") String getContainerId();  }### Answer:
@Test void testCgroupFileMissing() { DockerHelper dockerHelper = new DockerHelper("a_file_never_existing"); assertThat(dockerHelper.getContainerId()).isEmpty(); }
@Test void testContainerIdMissing(@TempDir File tempFolder) throws IOException { File file = new File(tempFolder, "no_container_id"); String content = "13:pids:/\n" + "12:hugetlb:/\n" + "11:net_prio:/"; Files.write(content.getBytes(Charsets.UTF_8), file); DockerHelper dockerHelper = new DockerHelper(file.getPath()); assertThat(dockerHelper.getContainerId()).isEmpty(); }
@Test void testGetContainerId(@TempDir File tempFolder) throws IOException { File file = new File(tempFolder, "cgroup"); String expected = "386a1920640799b5bf5a39bd94e489e5159a88677d96ca822ce7c433ff350163"; String content = "dummy\n11:devices:/ecs/bbc36dd0-5ee0-4007-ba96-c590e0b278d2/" + expected; Files.write(content.getBytes(Charsets.UTF_8), file); DockerHelper dockerHelper = new DockerHelper(file.getPath()); assertThat(dockerHelper.getContainerId()).isEqualTo(expected); }