### Question:
AlertApiClient { public void remove(String apiKey, int conditionId) throws AlertApiException, IOException { HttpDelete delete = new HttpDelete(String.format("%s/%d.json", url, conditionId)); delete.addHeader(X_API_KEY, apiKey); delete.addHeader(USER_AGENT, NR_LAMBDA_RESOURCE_PROVIDER); HttpResponse response = this.httpClient.execute(delete); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 400 && statusCode != 404) { EntityUtils.consume(response.getEntity()); if (statusCode == 401) { throw new AlertApiException( String.format( "Unauthorized. Failed to delete alert condition with ID: %d. Probably an invalid API key.", conditionId)); } else { throw new AlertApiException( String.format("Failed to delete alert condition with ID: %d", conditionId)); } } } AlertApiClient(String url, HttpClient httpClient); JSONObject create(
            NewRelicNrqlCondition newRelicNrqlCondition, String apiKey, int policyId); JSONObject update(
            NewRelicNrqlCondition newRelicNrqlCondition, String apiKey, int conditionId); void remove(String apiKey, int conditionId); List<NewRelicNrqlCondition> list(String apiKey, int policyId, @Nullable Integer page); Iterator<NewRelicNrqlCondition> listAll(String apiKey, int policyId); NewRelicNrqlCondition get(String apiKey, int policyId, int conditionId); static final String USER_AGENT; static final String X_API_KEY; static final String CONTENT_TYPE; static final String URL_PREFIX; }### Answer:
@Test void remove() throws IOException, AlertApiException { when(mockHttpClient.execute(any(HttpDelete.class))) .thenReturn( new BasicHttpResponse( new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"))); alertApiClient.remove(TEST_API_KEY, TEST_CONDITION_ID); ArgumentCaptor<HttpDelete> requestCaptor = ArgumentCaptor.forClass(HttpDelete.class); verify(mockHttpClient).execute(requestCaptor.capture()); HttpDelete request = requestCaptor.getValue(); assertEquals("http: assertEquals(TEST_API_KEY, request.getFirstHeader(AlertApiClient.X_API_KEY).getValue()); }
@Test void removeUnauth() throws IOException { when(mockHttpClient.execute(any(HttpDelete.class))) .thenReturn( new BasicHttpResponse( new BasicStatusLine(HttpVersion.HTTP_1_1, 401, "Unauthorized"))); try { alertApiClient.remove(TEST_API_KEY, TEST_CONDITION_ID); fail("Expected AlertApiException"); } catch (AlertApiException e) { assertEquals( "Unauthorized. Failed to delete alert condition with ID: 1234. Probably an invalid API key.", e.getMessage()); } }
@Test void removeForbidden() throws IOException { when(mockHttpClient.execute(any(HttpDelete.class))) .thenReturn( new BasicHttpResponse( new BasicStatusLine(HttpVersion.HTTP_1_1, 403, "Unauthorized"))); try { alertApiClient.remove(TEST_API_KEY, TEST_CONDITION_ID); fail("Expected AlertApiException"); } catch (AlertApiException e) { assertEquals("Failed to delete alert condition with ID: 1234", e.getMessage()); } }