/**
  * Verifies User metadata headers from output, to that sent in during input
  *
  * @param expectedHeaders the expected headers in the response.
  * @param response the {@link HttpResponse} which contains the headers of the response.
  * @param usermetadata if non-null, this is expected to come as the body.
  * @param content the content accompanying the response.
  */
 private void verifyUserMetadata(
     HttpHeaders expectedHeaders,
     HttpResponse response,
     byte[] usermetadata,
     Queue<HttpObject> content) {
   if (usermetadata == null) {
     assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
     for (Map.Entry<String, String> header : expectedHeaders) {
       String key = header.getKey();
       if (key.startsWith(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX)) {
         assertEquals(
             "Value for " + key + " does not match in user metadata",
             header.getValue(),
             HttpHeaders.getHeader(response, key));
       }
     }
     for (Map.Entry<String, String> header : response.headers()) {
       String key = header.getKey();
       if (key.startsWith(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX)) {
         assertTrue(
             "Key " + key + " does not exist in expected headers", expectedHeaders.contains(key));
       }
     }
     discardContent(content, 1);
   } else {
     assertEquals(
         "Content-Length is not as expected",
         usermetadata.length,
         HttpHeaders.getContentLength(response));
     byte[] receivedMetadata = getContent(content, HttpHeaders.getContentLength(response)).array();
     assertArrayEquals("User metadata does not match original", usermetadata, receivedMetadata);
   }
 }
 /**
  * Verifies blob properties from output, to that sent in during input
  *
  * @param expectedHeaders the expected headers in the response.
  * @param response the {@link HttpResponse} that contains the headers.
  */
 private void verifyBlobProperties(HttpHeaders expectedHeaders, HttpResponse response) {
   assertEquals(
       "Blob size does not match",
       Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE)),
       Long.parseLong(HttpHeaders.getHeader(response, RestUtils.Headers.BLOB_SIZE)));
   assertEquals(
       RestUtils.Headers.SERVICE_ID + " does not match",
       expectedHeaders.get(RestUtils.Headers.SERVICE_ID),
       HttpHeaders.getHeader(response, RestUtils.Headers.SERVICE_ID));
   assertEquals(
       RestUtils.Headers.PRIVATE + " does not match",
       expectedHeaders.get(RestUtils.Headers.PRIVATE),
       HttpHeaders.getHeader(response, RestUtils.Headers.PRIVATE));
   assertEquals(
       RestUtils.Headers.AMBRY_CONTENT_TYPE + " does not match",
       expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE),
       HttpHeaders.getHeader(response, RestUtils.Headers.AMBRY_CONTENT_TYPE));
   assertTrue(
       "No " + RestUtils.Headers.CREATION_TIME,
       HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null);
   if (Long.parseLong(expectedHeaders.get(RestUtils.Headers.TTL)) != Utils.Infinite_Time) {
     assertEquals(
         RestUtils.Headers.TTL + " does not match",
         expectedHeaders.get(RestUtils.Headers.TTL),
         HttpHeaders.getHeader(response, RestUtils.Headers.TTL));
   }
   if (expectedHeaders.contains(RestUtils.Headers.OWNER_ID)) {
     assertEquals(
         RestUtils.Headers.OWNER_ID + " does not match",
         expectedHeaders.get(RestUtils.Headers.OWNER_ID),
         HttpHeaders.getHeader(response, RestUtils.Headers.OWNER_ID));
   }
 }
  /**
   * Posts a blob with the given {@code headers} and {@code content}.
   *
   * @param headers the headers required.
   * @param content the content of the blob.
   * @return the blob ID of the blob.
   * @throws ExecutionException
   * @throws InterruptedException
   */
  private String postBlobAndVerify(HttpHeaders headers, ByteBuffer content)
      throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.POST, "/", headers, content);
    Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus());
    assertTrue(
        "No Date header",
        HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null);
    assertTrue(
        "No " + RestUtils.Headers.CREATION_TIME,
        HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null);
    assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
    String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null);

    if (blobId == null) {
      fail("postBlobAndVerify did not return a blob ID");
    }
    discardContent(responseParts, 1);
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
    return blobId;
  }
  /**
   * Posts a blob with the given {@code headers} and {@code content}.
   *
   * @param headers the headers required.
   * @param content the content of the blob.
   * @param usermetadata the {@link ByteBuffer} that represents user metadata
   * @return the blob ID of the blob.
   * @throws Exception
   */
  private String multipartPostBlobAndVerify(
      HttpHeaders headers, ByteBuffer content, ByteBuffer usermetadata) throws Exception {
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, usermetadata);
    Queue<HttpObject> responseParts =
        nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus());
    assertTrue(
        "No Date header",
        HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null);
    assertTrue(
        "No " + RestUtils.Headers.CREATION_TIME,
        HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null);
    assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
    String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null);

    if (blobId == null) {
      fail("postBlobAndVerify did not return a blob ID");
    }
    discardContent(responseParts, 1);
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
    return blobId;
  }
 /**
  * Gets the headers of the blob with blob ID {@code blobId} and verifies them against what is
  * expected.
  *
  * @param blobId the blob ID of the blob to HEAD.
  * @param range the {@link ByteRange} for the request.
  * @param expectedHeaders the expected headers in the response.
  * @throws ExecutionException
  * @throws InterruptedException
  */
 private void getHeadAndVerify(String blobId, ByteRange range, HttpHeaders expectedHeaders)
     throws ExecutionException, InterruptedException, RestServiceException {
   HttpHeaders headers = null;
   if (range != null) {
     headers =
         new DefaultHttpHeaders()
             .add(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range));
   }
   FullHttpRequest httpRequest = buildRequest(HttpMethod.HEAD, blobId, headers, null);
   Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
   HttpResponse response = (HttpResponse) responseParts.poll();
   assertEquals(
       "Unexpected response status",
       range == null ? HttpResponseStatus.OK : HttpResponseStatus.PARTIAL_CONTENT,
       response.getStatus());
   checkCommonGetHeadHeaders(response.headers());
   long contentLength = Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE));
   if (range != null) {
     Pair<String, Long> rangeAndLength =
         RestUtils.buildContentRangeAndLength(range, contentLength);
     assertEquals(
         "Content-Range header not set correctly",
         rangeAndLength.getFirst(),
         response.headers().get(RestUtils.Headers.CONTENT_RANGE));
     contentLength = rangeAndLength.getSecond();
   } else {
     assertNull(
         "Content-Range header should not be set",
         response.headers().get(RestUtils.Headers.CONTENT_RANGE));
   }
   assertEquals(
       "Accept-Ranges not set correctly",
       "bytes",
       response.headers().get(RestUtils.Headers.ACCEPT_RANGES));
   assertEquals(
       RestUtils.Headers.CONTENT_LENGTH + " does not match expected",
       contentLength,
       HttpHeaders.getContentLength(response));
   assertEquals(
       RestUtils.Headers.CONTENT_TYPE + " does not match " + RestUtils.Headers.AMBRY_CONTENT_TYPE,
       expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE),
       HttpHeaders.getHeader(response, HttpHeaders.Names.CONTENT_TYPE));
   verifyBlobProperties(expectedHeaders, response);
   discardContent(responseParts, 1);
   assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
 }