@Test
public void testPostEntitiesWithYarnACLsEnabled() throws Exception {

  AdminACLsManager oldAdminACLsManager =
      timelineACLsManager.setAdminACLsManager(adminACLsManager);
  try {
    TimelineEntities entities = new TimelineEntities();
    TimelineEntity entity = new TimelineEntity();
    entity.setEntityId("test id 2");
    entity.setEntityType("test type 2");
    entity.setStartTime(System.currentTimeMillis());
    entity.setDomainId("domain_id_1");
    entities.addEntity(entity);
    WebResource r = resource();
    ClientResponse response = r.path("ws").path("v1").path("timeline")
        .queryParam("user.name", "writer_user_1")
        .accept(MediaType.APPLICATION_JSON)
        .type(MediaType.APPLICATION_JSON)
        .post(ClientResponse.class, entities);
    assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8,
        response.getType().toString());
    TimelinePutResponse putResponse =
        response.getEntity(TimelinePutResponse.class);
    Assert.assertNotNull(putResponse);
    Assert.assertEquals(0, putResponse.getErrors().size());
    // override/append timeline data in the same entity with different user
    response = r.path("ws").path("v1").path("timeline")
        .queryParam("user.name", "writer_user_3")
        .accept(MediaType.APPLICATION_JSON)
        .type(MediaType.APPLICATION_JSON)
        .post(ClientResponse.class, entities);
    assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8,
        response.getType().toString());
    putResponse = response.getEntity(TimelinePutResponse.class);
    Assert.assertNotNull(putResponse);
    Assert.assertEquals(1, putResponse.getErrors().size());
    Assert.assertEquals(TimelinePutResponse.TimelinePutError.ACCESS_DENIED,
        putResponse.getErrors().get(0).getErrorCode());
    // Cross domain relationship will be rejected
    entities = new TimelineEntities();
    entity = new TimelineEntity();
    entity.setEntityId("test id 3");
    entity.setEntityType("test type 2");
    entity.setStartTime(System.currentTimeMillis());
    entity.setDomainId("domain_id_2");
    entity.setRelatedEntities(Collections.singletonMap(
        "test type 2", Collections.singleton("test id 2")));
    entities.addEntity(entity);
    r = resource();
    response = r.path("ws").path("v1").path("timeline")
        .queryParam("user.name", "writer_user_3")
        .accept(MediaType.APPLICATION_JSON)
        .type(MediaType.APPLICATION_JSON)
        .post(ClientResponse.class, entities);
    assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8,
        response.getType().toString());
    putResponse = response.getEntity(TimelinePutResponse.class);
    Assert.assertNotNull(putResponse);
    Assert.assertEquals(1, putResponse.getErrors().size());
    Assert.assertEquals(TimelinePutError.FORBIDDEN_RELATION,
        putResponse.getErrors().get(0).getErrorCode());
    // Make sure the entity has been added anyway even though the
    // relationship is been excluded
    response = r.path("ws").path("v1").path("timeline")
        .path("test type 2").path("test id 3")
        .queryParam("user.name", "reader_user_3")
        .accept(MediaType.APPLICATION_JSON)
        .get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8,
        response.getType().toString());
    entity = response.getEntity(TimelineEntity.class);
    Assert.assertNotNull(entity);
    Assert.assertEquals("test id 3", entity.getEntityId());
    Assert.assertEquals("test type 2", entity.getEntityType());
  } finally {
    timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  }
}