@Test
public void test_offboardServiceAccount_failure() {

    UserDetails userDetails = getMockUser(true);
    String token = userDetails.getClientToken();
    OnboardedServiceAccount onboardedServiceAccount = generateOnboardedServiceAccount("testacc02","testacc01");
    ServiceAccountUser serviceAccountUser = new ServiceAccountUser("testacc01", userDetails.getUsername(), "sudo");
    String [] policies = {"o_svcacct_testacc01"};
    when(policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails)).thenReturn(policies);
    Response userResponse = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"bound_cidrs\":[],\"max_ttl\":0,\"policies\":[\"default\", \"o_svcacct_testacc02\"],\"ttl\":0,\"groups\":\"admin\"}}");
    when(reqProcessor.process("/auth/ldap/users","{\"username\":\"user1\"}",token)).thenReturn(userResponse);
    Response userResponse2 = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"bound_cidrs\":[],\"max_ttl\":0,\"policies\":[\"default\", \"r_svcacct_testacc02\"],\"ttl\":0,\"groups\":\"\"}}");
    when(reqProcessor.process("/auth/ldap/users","{\"username\":\"user2\"}",token)).thenReturn(userResponse2);
    Response groupResponse = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"policies\":[\"default\", \"r_svcacct_testacc02\"],\"ttl\":0}}");
    when(reqProcessor.process("/auth/ldap/groups","{\"groupname\":\"group1\"}",token)).thenReturn(groupResponse);
    Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, "{\"policies\":null}");
    try {
        List<String> resList = new ArrayList<>();
        resList.add("default");
        resList.add("o_svcacct_testacc02");
        List<String> groupResList = new ArrayList<>();
        resList.add("default");
        resList.add("r_svcacct_testacc02");
        when(ControllerUtil.getPoliciesAsListFromJson(any(), any())).thenReturn(resList);
        when(ControllerUtil.getPoliciesAsListFromJson(any(), any())).thenReturn(groupResList);
    } catch (IOException e) {
        e.printStackTrace();
    }
    when(ControllerUtil.configureLDAPUser(eq("testacc01"),any(),any(),eq(token))).thenReturn(responseNoContent);
    when(ControllerUtil.configureLDAPGroup(any(),any(),any())).thenReturn(responseNoContent);
    when(ControllerUtil.updateMetadata(any(),eq(token))).thenReturn(responseNoContent);
    // Delete policies...
    ResponseEntity<String> createPolicyResponse = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Access is deleted\"]}");
    when(accessService.deletePolicyInfo(Mockito.anyString(), Mockito.any())).thenReturn(createPolicyResponse);
    // delete user/group/role associations
    String _path = TVaultConstants.SVC_ACC_ROLES_METADATA_MOUNT_PATH + "/testacc02";
    Response metaResponse = getMockResponse(HttpStatus.OK, true, "{\"data\":{\n" +
            "  \"groups\": {\n" +
            "    \"group1\": \"read\"\n" +
            "  },\n" +
            "  \"managedBy\": \"user2\",\n" +
            "  \"name\": \"testacc02\",\n" +
            "  \"users\": {\n" +
            "    \"user1\": \"read\"\n" +
            "  }\n" +
            "}}");
    when(reqProcessor.process("/sdb","{\"path\":\""+_path+"\"}",token)).thenReturn(metaResponse);
    //Delete Account Role...
    ServiceAccountTTL serviceAccountTTL = new ServiceAccountTTL();
    serviceAccountTTL.setRole_name(onboardedServiceAccount.getName());
    serviceAccountTTL.setService_account_name(onboardedServiceAccount.getName() + "@aaa.bbb.ccc.com") ;
    String svc_account_payload = getJSON(serviceAccountTTL);
    Response deleteRoleResponse = getMockResponse(HttpStatus.BAD_REQUEST, true, "");
    when(JSONUtil.getJSON(Mockito.any(ServiceAccountTTL.class))).thenReturn(svc_account_payload);
    when(reqProcessor.process("/ad/serviceaccount/offboard",svc_account_payload,token)).thenReturn(deleteRoleResponse);
    // System under test
    String expectedResponse = "{\"errors\":[\"Failed to offboard AD service account from TVault for password rotation.\"]}";
    ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.MULTI_STATUS).body(expectedResponse);
    ResponseEntity<String> responseEntity = serviceAccountsService.offboardServiceAccount(token, onboardedServiceAccount, userDetails);
    assertEquals(HttpStatus.MULTI_STATUS, responseEntity.getStatusCode());
    assertEquals(responseEntityExpected, responseEntity);
}