@Test
 public void testCRUDEntity() throws Exception {
   Project p = new Project();
   p.setName("Create without entity type");
   p.setEntityType(p.getClass().getName());
   Project clone = (Project) entityServletHelper.createEntity(p, TEST_USER1);
   String id = clone.getId();
   toDelete.add(id);
   assertEquals(p.getName(), clone.getName());
   // Now get the entity with the ID
   Project clone2 = (Project) entityServletHelper.getEntity(id, TEST_USER1);
   assertEquals(clone, clone2);
   // Make sure we can update it
   clone2.setName("My new name");
   Project clone3 = (Project) entityServletHelper.updateEntity(clone2, TEST_USER1);
   assertNotNull(clone3);
   assertEquals(clone2.getName(), clone3.getName());
   // Should not match the original
   assertFalse(p.getName().equals(clone3.getName()));
   // the Etag should have changed
   assertFalse(clone2.getEtag().equals(clone3.getEtag()));
   // Now delete it
   entityServletHelper.deleteEntity(id, TEST_USER1);
   // it should not be found now
   try {
     entityServletHelper.getEntity(id, TEST_USER1);
     fail("Delete failed");
   } catch (NotFoundException e) {
     // expected
   }
 }
  /** @throws java.lang.Exception */
  @Before
  public void setUp() throws Exception {
    testHelper.setUp();
    testHelper.setTestUser(TEST_USER1);

    project = new Project();
    project.setName("proj");
    project = testHelper.createEntity(project, null);

    dataset = new Study();
    dataset.setName("study");
    dataset.setParentId(project.getId());
    dataset = testHelper.createEntity(dataset, null);

    // Add a public read ACL to the project object
    AccessControlList projectAcl = testHelper.getEntityACL(project);
    ResourceAccess ac = new ResourceAccess();
    UserGroup authenticatedUsers =
        userGroupDAO.findGroup(
            AuthorizationConstants.DEFAULT_GROUPS.AUTHENTICATED_USERS.name(), false);
    assertNotNull(authenticatedUsers);
    ac.setPrincipalId(Long.parseLong(authenticatedUsers.getId()));
    ac.setAccessType(new HashSet<ACCESS_TYPE>());
    ac.getAccessType().add(ACCESS_TYPE.READ);
    projectAcl.getResourceAccess().add(ac);
    projectAcl = testHelper.updateEntityAcl(project, projectAcl);

    testUser = userGroupDAO.findGroup(TEST_USER1, true);
  }
  @Test
  public void testPLFM_1288() throws Exception {
    Project p = new Project();
    p.setName("Create without entity type");
    p.setEntityType(p.getClass().getName());
    p = (Project) entityServletHelper.createEntity(p, TEST_USER1);
    toDelete.add(p.getId());

    Study one = new Study();
    one.setName("one");
    one.setParentId(p.getId());
    one.setEntityType(Study.class.getName());
    one = (Study) entityServletHelper.createEntity(one, TEST_USER1);
    // Now try to re-use the name
    Code two = new Code();
    two.setName("code");
    two.setParentId(one.getId());
    two.setEntityType(Code.class.getName());
    try {
      two = (Code) entityServletHelper.createEntity(two, TEST_USER1);
      fail("Code cannot have a parent of type Study");
    } catch (IllegalArgumentException e) {
      System.out.println(e.getMessage());
      assertTrue(e.getMessage().indexOf(Code.class.getName()) > 0);
      assertTrue(e.getMessage().indexOf(Study.class.getName()) > 0);
    }
  }
 @Test
 public void testGetUserEntityPermissions()
     throws JSONObjectAdapterException, ServletException, IOException, NotFoundException,
         DatastoreException {
   Project p = new Project();
   p.setName("UserEntityPermissions");
   p.setEntityType(p.getClass().getName());
   Project clone = (Project) entityServletHelper.createEntity(p, TEST_USER1);
   String id = clone.getId();
   toDelete.add(id);
   UserEntityPermissions uep = entityServletHelper.getUserEntityPermissions(id, TEST_USER1);
   assertNotNull(uep);
   assertTrue(uep.getCanEdit());
 }
 @Test
 public void testEntityPath()
     throws JSONObjectAdapterException, ServletException, IOException, NotFoundException,
         DatastoreException {
   Project p = new Project();
   p.setName("EntityPath");
   p.setEntityType(p.getClass().getName());
   Project clone = (Project) entityServletHelper.createEntity(p, TEST_USER1);
   String id = clone.getId();
   toDelete.add(id);
   EntityPath path = entityServletHelper.getEntityPath(id, TEST_USER1);
   assertNotNull(path);
   assertNotNull(path.getPath());
   assertEquals(2, path.getPath().size());
   EntityHeader header = path.getPath().get(1);
   assertNotNull(header);
   assertEquals(id, header.getId());
 }
  @Test(expected = NameConflictException.class)
  public void testPLFM_449NameConflict() throws Exception {
    Project p = new Project();
    p.setName("Create without entity type");
    p.setEntityType(p.getClass().getName());
    p = (Project) entityServletHelper.createEntity(p, TEST_USER1);
    toDelete.add(p.getId());

    Study one = new Study();
    one.setName("one");
    one.setParentId(p.getId());
    one.setEntityType(Study.class.getName());
    one = (Study) entityServletHelper.createEntity(one, TEST_USER1);
    // Now try to re-use the name
    Study two = new Study();
    two.setName("one");
    two.setParentId(p.getId());
    two.setEntityType(Study.class.getName());
    two = (Study) entityServletHelper.createEntity(two, TEST_USER1);
  }
  @Test
  public void testEntityTypeBatch() throws Exception {
    List<String> ids = new ArrayList<String>();
    for (int i = 0; i < 12; i++) {
      Project p = new Project();
      p.setName("EntityTypeBatchItem" + i);
      p.setEntityType(p.getClass().getName());
      Project clone = (Project) entityServletHelper.createEntity(p, TEST_USER1);
      String id = clone.getId();
      toDelete.add(id);
      ids.add(id);
    }

    BatchResults<EntityHeader> results = entityServletHelper.getEntityTypeBatch(ids, TEST_USER1);
    assertNotNull(results);
    assertEquals(12, results.getTotalNumberOfResults());
    List<String> outputIds = new ArrayList<String>();
    for (EntityHeader header : results.getResults()) {
      outputIds.add(header.getId());
    }
    assertEquals(ids.size(), outputIds.size());
    assertTrue(ids.containsAll(outputIds));
  }
 @Test
 public void testAnnotationsCRUD() throws Exception {
   Project p = new Project();
   p.setName("AnnotCrud");
   p.setEntityType(p.getClass().getName());
   Project clone = (Project) entityServletHelper.createEntity(p, TEST_USER1);
   String id = clone.getId();
   toDelete.add(id);
   // Get the annotaions for this entity
   Annotations annos = entityServletHelper.getEntityAnnotaions(id, TEST_USER1);
   assertNotNull(annos);
   // Change the values
   annos.addAnnotation("doubleAnno", new Double(45.0001));
   annos.addAnnotation("string", "A string");
   // Updte them
   Annotations annosClone = entityServletHelper.updateAnnotations(annos, TEST_USER1);
   assertNotNull(annosClone);
   assertEquals(id, annosClone.getId());
   assertFalse(annos.getEtag().equals(annosClone.getEtag()));
   String value = (String) annosClone.getSingleValue("string");
   assertEquals("A string", value);
   assertEquals(new Double(45.0001), annosClone.getSingleValue("doubleAnno"));
 }