@Override
  public Response postDelegationToken(DelegationToken tokenData, HttpServletRequest hsr)
      throws AuthorizationException, IOException, InterruptedException, Exception {

    if (tokenData == null || hsr == null) {
      RouterAuditLogger.logFailure(getUser().getShortUserName(), POST_DELEGATION_TOKEN,
          UNKNOWN, TARGET_WEB_SERVICE, "Parameter error, the tokenData or hsr is null.");
      throw new IllegalArgumentException("Parameter error, the tokenData or hsr is null.");
    }

    try {
      // get Caller UserGroupInformation
      Configuration conf = federationFacade.getConf();
      UserGroupInformation callerUGI = getKerberosUserGroupInformation(conf, hsr);

      // create a delegation token
      return createDelegationToken(tokenData, callerUGI);
    } catch (YarnException e) {
      LOG.error("Create delegation token request failed.", e);
      RouterAuditLogger.logFailure(getUser().getShortUserName(), POST_DELEGATION_TOKEN,
          UNKNOWN, TARGET_WEB_SERVICE, e.getLocalizedMessage());
      return Response.status(Status.FORBIDDEN).entity(e.getMessage()).build();
    }
  }
@Test
  public void testPostDelegationTokenErrorHsr() throws Exception {
    // Prepare delegationToken data
    DelegationToken token = new DelegationToken();
    token.setRenewer(TEST_RENEWER);

    HttpServletRequest request = mock(HttpServletRequest.class);

    // If we don't set token
    LambdaTestUtils.intercept(IllegalArgumentException.class,
        "Parameter error, the tokenData or hsr is null.",
        () -> interceptor.postDelegationToken(null, request));

    // If we don't set hsr
    LambdaTestUtils.intercept(IllegalArgumentException.class,
        "Parameter error, the tokenData or hsr is null.",
        () -> interceptor.postDelegationToken(token, null));

    // If we don't set renewUser, we will get error message.
    LambdaTestUtils.intercept(AuthorizationException.class,
        "Unable to obtain user name, user not authenticated",
        () -> interceptor.postDelegationToken(token, request));

    Principal principal = mock(Principal.class);
    when(principal.getName()).thenReturn(TEST_RENEWER);
    when(request.getRemoteUser()).thenReturn(TEST_RENEWER);
    when(request.getUserPrincipal()).thenReturn(principal);

    // If we don't set the authentication type, we will get error message.
    Response response = interceptor.postDelegationToken(token, request);
    Assert.assertNotNull(response);
    Assert.assertEquals(response.getStatus(), Status.FORBIDDEN.getStatusCode());
    String errMsg = "Delegation token operations can only be carried out on a " +
        "Kerberos authenticated channel. Expected auth type is kerberos, got type null";
    Object entity = response.getEntity();
    Assert.assertNotNull(entity);
    Assert.assertTrue(entity instanceof String);
    String entityMsg = String.valueOf(entity);
    Assert.assertTrue(errMsg.contains(entityMsg));
  }