HttpCertSigner implements CertSigner { @Override public String generateX509Certificate(String csr, String keyUsage, int expireMins) { List<Integer> extKeyUsage = null; if (InstanceProvider.ZTS_CERT_USAGE_CLIENT.equals(keyUsage)) { extKeyUsage = new ArrayList<>(); extKeyUsage.add(2); } ContentResponse response = null; for (int i = 0; i < requestRetryCount; i++) { response = processX509CertRequest(csr, extKeyUsage, expireMins, i + 1); if (response != null) { break; } } if (response == null) { return null; } if (response.getStatus() != HttpStatus.CREATED_201) { LOGGER.error("unable to fetch requested uri '" + x509CertUri + "' status: " + response.getStatus()); return null; } String data = response.getContentAsString(); if (data == null || data.isEmpty()) { LOGGER.error("received empty response from uri '" + x509CertUri + "' status: " + response.getStatus()); return null; } X509CertSignObject pemCert = JSON.fromString(data, X509CertSignObject.class); return (pemCert != null) ? pemCert.getPem() : null; } HttpCertSigner(); @Override void close(); @Override int getMaxCertExpiryTimeMins(); @Override String generateX509Certificate(String csr, String keyUsage, int expireMins); @Override String getCACertificate();  }
@Test public void testGenerateX509Certificate() throws Exception { HttpClient httpClient = Mockito.mock(HttpClient.class); HttpCertSignerFactory certFactory = new HttpCertSignerFactory(); HttpCertSigner certSigner = (HttpCertSigner) certFactory.create(); certSigner.setHttpClient(httpClient); Request request = Mockito.mock(Request.class); Mockito.when(httpClient.POST("https: ContentResponse response = Mockito.mock(ContentResponse.class); Mockito.when(request.send()).thenReturn(response); Mockito.when(response.getStatus()).thenReturn(201); Mockito.when(response.getContentAsString()).thenReturn("{\"pem\": \"pem-value\"}"); String pem = certSigner.generateX509Certificate("csr", null, 0); assertEquals(pem, "pem-value"); pem = certSigner.generateX509Certificate("csr", InstanceProvider.ZTS_CERT_USAGE_CLIENT, 0); assertEquals(pem, "pem-value"); pem = certSigner.generateX509Certificate("csr", InstanceProvider.ZTS_CERT_USAGE_CLIENT, 30); assertEquals(pem, "pem-value"); certSigner.requestTimeout = 120; pem = certSigner.generateX509Certificate("csr", InstanceProvider.ZTS_CERT_USAGE_CLIENT, 30); assertEquals(pem, "pem-value"); certSigner.close(); }