### Question:
TLS { public static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate) throws IOException { return createSelfSignedCertificateIfMissing(key, certificate, null); } private  TLS(); static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate); static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate, String commonName); static byte[] readPemFile(Path certificate); static byte[] certificateFingerprint(Path certificate); static String certificateHexFingerprint(Path certificate); static byte[] certificateFingerprint(Certificate certificate); static String certificateHexFingerprint(Certificate certificate);  }### Answer:
@Test void createCertificateIfFilesAreNotThere(@TempDirectory Path tempDir) throws Exception { Path certificate = tempDir.resolve("foo").resolve("server.crt"); Path key = tempDir.resolve("foo").resolve("server.key"); boolean wasCreated = TLS.createSelfSignedCertificateIfMissing(key, certificate); assertTrue(wasCreated); assertTrue(Files.exists(certificate)); assertTrue(Files.exists(key)); }
@Test void doesNotGenerateSelfSignedCertificateIfCertFileExists(@TempDirectory Path tempDir) throws Exception { Path certificate = tempDir.resolve("server1.crt"); Path key = tempDir.resolve("server1.key"); Files.createFile(certificate); boolean wasCreated = TLS.createSelfSignedCertificateIfMissing(key, certificate); assertFalse(wasCreated); assertTrue(Files.exists(certificate)); assertFalse(Files.exists(key)); }
@Test void doesNotGenerateSelfSignedCertificateIfKeyFileExists(@TempDirectory Path tempDir) throws Exception { Path certificate = tempDir.resolve("server2.crt"); Path key = tempDir.resolve("server2.key"); Files.createFile(key); boolean wasCreated = TLS.createSelfSignedCertificateIfMissing(key, certificate); assertFalse(wasCreated); assertFalse(Files.exists(certificate)); assertTrue(Files.exists(key)); }
@Test void autoGeneratedCertsAreValid(@TempDirectory Path tempDir) throws Exception { Path certificate = tempDir.resolve("server3.crt"); Path key = tempDir.resolve("server3.key"); boolean wasCreated = TLS.createSelfSignedCertificateIfMissing(key, certificate); assertTrue(wasCreated); checkKeyPair(key, certificate); }