### Question:
KeyResolverImpl implements KeyResolver { @Override public String getKeyString(String keyId) { if (keyId.startsWith("/SmartThings")) { if (localPublicKeyStr == null) { throw new IllegalStateException("Public key file not set up" + " properly. Please see README (Configure Public Key) for" + " directions and don't forget to restart this server."); } return localPublicKeyStr; } else if (httpClient == null) { throw new RuntimeException("httpClient is required in HttpVerificationService for this application"); } else { HttpGet request = new HttpGet(apiHost + keyId); try (CloseableHttpResponse response = httpClient.execute(request)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode < 200 || statusCode > 299) { throw new RuntimeException("got status code " + statusCode + " trying to retrieve public key"); } InputStream content = response.getEntity().getContent(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(content, StandardCharsets.UTF_8))) { return reader.lines().collect(Collectors.joining("\n")); } } catch (IOException ioException) { throw new RuntimeException("IOException retrieving public key", ioException); } } } KeyResolverImpl(CloseableHttpClient httpClient, String apiHost, String publicKeyPath); @Override String getKeyString(String keyId);  }### Answer:
@Test(expected = IllegalStateException.class) public void throwsExceptionWithNoPublicKey() { KeyResolver tester = new KeyResolverImpl(null, null, null); tester.getKeyString("/SmartThings"); }
@Test public void returnsLocalKeyStr() throws IOException { KeyResolver tester = new KeyResolverImpl(null, null, "/good_public.pem"); URL url = Resources.getResource("good_public.pem"); String goodPEMContents = Resources.asCharSource(url, Charset.forName("UTF-8")).read(); String result = tester.getKeyString("/SmartThings/more:stuff"); assertEquals(goodPEMContents, result + "\n"); }
@Test(expected = RuntimeException.class) public void throwsExceptionWHenHttpClientMissing() { KeyResolver tester = new KeyResolverImpl(null, "https: tester.getKeyString("/key-to-look-up"); }
@Test public void resolvesRemoteKey() throws ClientProtocolException, IOException { KeyResolver tester = new KeyResolverImpl(httpClient, "https: when(httpClient.execute(any())).thenReturn(response); when(response.getStatusLine()).thenReturn(okStatus); when(response.getEntity()).thenReturn(entity); String responseBodyStr = "multi\nline\nfile contents"; InputStream responseBodyStream = new ByteArrayInputStream(responseBodyStr.getBytes(StandardCharsets.UTF_8)); when(entity.getContent()).thenReturn(responseBodyStream); String result = tester.getKeyString("/key-to-look-up"); ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class); verify(httpClient).execute(requestCaptor.capture()); assertEquals("https: requestCaptor.getValue().getURI().toString()); assertEquals(responseBodyStr, result); }
@Test(expected = RuntimeException.class) public void handlesBadStatusCode() throws ClientProtocolException, IOException { KeyResolver tester = new KeyResolverImpl(httpClient, "https: when(httpClient.execute(any())).thenReturn(response); when(response.getStatusLine()).thenReturn(notFoundStatus); tester.getKeyString("/key-to-look-up"); }
@Test(expected = RuntimeException.class) public void bubblesUpIOException() throws ClientProtocolException, IOException { KeyResolver tester = new KeyResolverImpl(httpClient, "https: when(httpClient.execute(any())).thenThrow(new IOException("it isn't here")); tester.getKeyString("/key-to-look-up"); }