@Test
  public void ensureRequiredHeadersOrThrowTest() throws Exception {
    JSONObject headers = new JSONObject();
    Set<String> requiredHeaders = new HashSet<>(Arrays.asList("required_a", "required_b", "required_c"));
    for (String requiredHeader : requiredHeaders) {
      headers.put(requiredHeader, TestUtils.getRandomString(10));
    }
    headers.put(TestUtils.getRandomString(10), TestUtils.getRandomString(10));

    // success test
    RestRequest restRequest = createRestRequest(RestMethod.POST, "/", headers);
    RestUtils.ensureRequiredHeadersOrThrow(restRequest, requiredHeaders);

    // failure test
    // null
    headers.put(requiredHeaders.iterator().next(), JSONObject.NULL);
    restRequest = createRestRequest(RestMethod.POST, "/", headers);
    try {
      RestUtils.ensureRequiredHeadersOrThrow(restRequest, requiredHeaders);
      fail("Should have failed because one of the required headers has a bad value");
    } catch (RestServiceException e) {
      assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.InvalidArgs, e.getErrorCode());
    }
    // not present
    headers.remove(requiredHeaders.iterator().next());
    restRequest = createRestRequest(RestMethod.POST, "/", headers);
    try {
      RestUtils.ensureRequiredHeadersOrThrow(restRequest, requiredHeaders);
      fail("Should have failed because one of the required headers is not present");
    } catch (RestServiceException e) {
      assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.MissingArgs, e.getErrorCode());
    }
  }
Tests for {@link RestUtils#ensureRequiredHeadersOrThrow(RestRequest, Set)}.
@throws Exception
ambry-api/src/test/java/com/github/ambry/rest/RestUtilsTest.java