### Question:
AlertApiClient { public JSONObject update( NewRelicNrqlCondition newRelicNrqlCondition, String apiKey, int conditionId) throws IOException, AlertApiException { HttpPut alertPut = new HttpPut(String.format("%s/%d.json", this.url, conditionId)); alertPut.addHeader(X_API_KEY, apiKey); alertPut.addHeader(USER_AGENT, "NRLambdaResourceProvider"); alertPut.addHeader(CONTENT_TYPE, "application/json"); alertPut.setEntity( new StringEntity( mapper.writeValueAsString( Collections.singletonMap( "nrql_condition", newRelicNrqlCondition)))); HttpResponse response = this.httpClient.execute(alertPut); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 400) { JsonNode jsonNode = mapper.readTree(response.getEntity().getContent()); String message = jsonNode.findValue("title").asText(); EntityUtils.consume(response.getEntity()); throw new AlertApiException( String.format( "Failed to update alert condition for condition ID: %d. Error message: '%s'", conditionId, message)); } else { String jsonResponse = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); JSONObject alertResponse = new JSONObject(jsonResponse); return alertResponse.getJSONObject("nrql_condition"); } } 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 update() throws IOException, AlertApiException { BasicHttpResponse okResponse = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); okResponse.setEntity( new StringEntity( "{\"nrql_condition\": {\"isTest\":true}}", ContentType.APPLICATION_JSON)); when(mockHttpClient.execute(any(HttpPut.class))).thenReturn(okResponse); JSONObject createdCondition = alertApiClient.update(nrqlCondition, TEST_API_KEY, TEST_CONDITION_ID); assertTrue(createdCondition.getBoolean("isTest")); ArgumentCaptor<HttpPut> requestCaptor = ArgumentCaptor.forClass(HttpPut.class); verify(mockHttpClient).execute(requestCaptor.capture()); HttpPut request = requestCaptor.getValue(); assertEquals("http: assertEquals( "application/json", request.getFirstHeader(AlertApiClient.CONTENT_TYPE).getValue()); assertEquals(TEST_API_KEY, request.getFirstHeader(AlertApiClient.X_API_KEY).getValue()); String requestBody = EntityUtils.toString(request.getEntity(), StandardCharsets.UTF_8); ObjectMapper objectMapper = new ObjectMapper(); String expectedRequestBody = objectMapper.writeValueAsString( Collections.singletonMap("nrql_condition", nrqlCondition)); assertEquals(expectedRequestBody, requestBody); }