Certificate extends AcmeResource { public void download() throws AcmeException { if (certChain == null) { LOG.debug("download"); try (Connection conn = getSession().connect()) { conn.sendCertificateRequest(getLocation(), getLogin()); alternates = new ArrayList<>(conn.getLinks("alternate")); certChain = new ArrayList<>(conn.readCertificates()); } } } protected  Certificate(Login login, URL certUrl); void download(); X509Certificate getCertificate(); List<X509Certificate> getCertificateChain(); List<URL> getAlternates(); void writeCertificate(Writer out); void revoke(); void revoke(@Nullable RevocationReason reason); static void revoke(Login login, X509Certificate cert, @Nullable RevocationReason reason); static void revoke(Session session, KeyPair domainKeyPair, X509Certificate cert,
            @Nullable RevocationReason reason);  }
@Test public void testDownload() throws Exception { final List<X509Certificate> originalCert = TestUtils.createCertificate(); TestableConnectionProvider provider = new TestableConnectionProvider() { @Override public int sendCertificateRequest(URL url, Login login) { assertThat(url, is(locationUrl)); assertThat(login, is(notNullValue())); return HttpURLConnection.HTTP_OK; } @Override public List<X509Certificate> readCertificates() { return originalCert; } @Override public Collection<URL> getLinks(String relation) { assertThat(relation, is("alternate")); return Arrays.asList( url("https: url("https: } }; Certificate cert = new Certificate(provider.createLogin(), locationUrl); cert.download(); X509Certificate downloadedCert = cert.getCertificate(); assertThat(downloadedCert.getEncoded(), is(originalCert.get(0).getEncoded())); List<X509Certificate> downloadedChain = cert.getCertificateChain(); assertThat(downloadedChain.size(), is(originalCert.size())); for (int ix = 0; ix < downloadedChain.size(); ix++) { assertThat(downloadedChain.get(ix).getEncoded(), is(originalCert.get(ix).getEncoded())); } byte[] writtenPem; byte[] originalPem; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamWriter w = new OutputStreamWriter(baos)) { cert.writeCertificate(w); w.flush(); writtenPem = baos.toByteArray(); } try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream in = getClass().getResourceAsStream("/cert.pem")) { int len; byte[] buffer = new byte[2048]; while((len = in.read(buffer)) >= 0) { baos.write(buffer, 0, len); } originalPem = baos.toByteArray(); } assertThat(writtenPem, is(originalPem)); assertThat(cert.getAlternates(), is(notNullValue())); assertThat(cert.getAlternates().size(), is(2)); assertThat(cert.getAlternates().get(0), is(url("https: assertThat(cert.getAlternates().get(1), is(url("https: provider.close(); }