Repository: incubator-atlas
Updated Branches:
  refs/heads/master 414b7bbc9 -> ce20d6f59


http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/ce20d6f5/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
----------------------------------------------------------------------
diff --git a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java 
b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
index 1aac0f2..8ba9011 100644
--- a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
+++ b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
@@ -26,21 +26,23 @@ import org.apache.atlas.model.TypeCategory;
 import org.apache.atlas.model.instance.AtlasClassification;
 import org.apache.atlas.model.instance.AtlasEntity;
 import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
+import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo;
 import org.apache.atlas.model.instance.EntityMutationResponse;
 import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
 import org.apache.atlas.repository.store.graph.AtlasEntityStore;
+import org.apache.atlas.repository.store.graph.v1.AtlasEntityStream;
+import org.apache.atlas.repository.store.graph.v1.EntityStream;
 import org.apache.atlas.services.MetadataService;
 import org.apache.atlas.type.AtlasClassificationType;
 import org.apache.atlas.type.AtlasEntityType;
-import org.apache.atlas.type.AtlasType;
 import org.apache.atlas.type.AtlasTypeRegistry;
 import org.apache.atlas.typesystem.IStruct;
-import org.apache.atlas.typesystem.ITypedReferenceableInstance;
 import org.apache.atlas.typesystem.ITypedStruct;
 import org.apache.atlas.typesystem.Referenceable;
 import org.apache.atlas.web.adapters.AtlasFormatConverter;
 import org.apache.atlas.web.adapters.AtlasInstanceRestAdapters;
 import org.apache.atlas.web.util.Servlets;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -114,9 +116,23 @@ public class EntityREST {
         AtlasEntityType     entityType = ensureEntityType(typeName);
         Map<String, Object> attributes = getAttributes(servletRequest);
 
-        validateAttributes(entityType, attributes);
+        validateUniqueAttribute(entityType, attributes);
 
-        return entitiesStore.getByUniqueAttribute(entityType, attributes);
+        return entitiesStore.getByUniqueAttributes(entityType, attributes);
+    }
+
+    /**
+     * Create new entity or update existing entity in Atlas.
+     * Existing entity is matched using its unique guid if supplied or by its 
unique attributes eg: qualifiedName
+     * @param entity
+     * @return EntityMutationResponse
+     * @throws AtlasBaseException
+     */
+    @POST
+    @Consumes(Servlets.JSON_MEDIA_TYPE)
+    @Produces(Servlets.JSON_MEDIA_TYPE)
+    public EntityMutationResponse createOrUpdate(AtlasEntityWithExtInfo 
entity) throws AtlasBaseException {
+        return entitiesStore.createOrUpdate(new AtlasEntityStream(entity));
     }
 
     /**
@@ -152,18 +168,23 @@ public class EntityREST {
     @PUT
     @Consumes(Servlets.JSON_MEDIA_TYPE)
     @Produces(Servlets.JSON_MEDIA_TYPE)
-    @Path("/uniqueAttribute/type/{typeName}/attribute/{attrName}")
-    public EntityMutationResponse 
partialUpdateByUniqueAttribute(@PathParam("typeName") String entityType,
-                                                                 
@PathParam("attrName") String attribute,
-                                                                 
@QueryParam("value") String value, AtlasEntity entity) throws Exception {
+    @Path("/uniqueAttribute/type/{typeName}")
+    public EntityMutationResponse 
partialUpdateByUniqueAttribute(@PathParam("typeName") String typeName,
+                                                                 @Context 
HttpServletRequest servletRequest,
+                                                                 AtlasEntity 
entity) throws Exception {
+        AtlasEntityType     entityType = ensureEntityType(typeName);
+        Map<String, Object> attributes = getAttributes(servletRequest);
 
-        AtlasEntityType type = (AtlasEntityType) validateType(entityType, 
TypeCategory.ENTITY);
-        validateUniqueAttribute(type, attribute);
+        validateUniqueAttribute(entityType, attributes);
+
+        // legacy API supports only one unique attribute
+        String attribute = attributes.keySet().toArray(new String[1])[0];
+        String value     = (String)attributes.get(attribute);
 
         AtlasFormatConverter.ConverterContext ctx = new 
AtlasFormatConverter.ConverterContext();
         ctx.addEntity(entity);
         Referenceable ref = restAdapters.getReferenceable(entity, ctx);
-        CreateUpdateEntitiesResult result = 
metadataService.updateEntityByUniqueAttribute(entityType, attribute, value, 
ref);
+        CreateUpdateEntitiesResult result = 
metadataService.updateEntityByUniqueAttribute(typeName, attribute, value, ref);
         return toEntityMutationResponse(result);
     }
 
@@ -171,46 +192,67 @@ public class EntityREST {
     @DELETE
     @Consumes(Servlets.JSON_MEDIA_TYPE)
     @Produces(Servlets.JSON_MEDIA_TYPE)
-    @Path("/uniqueAttribute/type/{typeName}/attribute/{attrName}")
+    @Path("/uniqueAttribute/type/{typeName}")
     public EntityMutationResponse 
deleteByUniqueAttribute(@PathParam("typeName") String typeName,
-        @PathParam("attrName") String attribute,
-        @QueryParam("value") String value) throws Exception {
+                                                          @Context 
HttpServletRequest servletRequest) throws Exception {
+        AtlasEntityType     entityType = ensureEntityType(typeName);
+        Map<String, Object> attributes = getAttributes(servletRequest);
 
-        AtlasEntityType entityType = ensureEntityType(typeName);
+        validateUniqueAttribute(entityType, attributes);
 
-        validateUniqueAttribute(entityType, attribute);
+        // legacy API supports only one unique attribute
+        String attribute = attributes.keySet().toArray(new String[1])[0];
+        String value     = (String)attributes.get(attribute);
 
         final AtlasClient.EntityResult result = 
metadataService.deleteEntityByUniqueAttribute(typeName, attribute, value);
         return toEntityMutationResponse(result);
     }
 
+    @GET
+    @Path("/bulk")
+    @Consumes(Servlets.JSON_MEDIA_TYPE)
+    @Produces(Servlets.JSON_MEDIA_TYPE)
+    public AtlasEntitiesWithExtInfo getByGuids(@QueryParam("guid") 
List<String> guids) throws AtlasBaseException {
+
+        if (CollectionUtils.isEmpty(guids)) {
+            throw new 
AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guids);
+        }
+
+        AtlasEntitiesWithExtInfo entities = entitiesStore.getByIds(guids);
+
+        return entities;
+    }
+
     /**
-     * Fetch the complete definition of an entity
-     * which is identified by its type and unique attribute  eg: 
Referenceable.qualifiedName.
+     * Create new entity or update existing entity in Atlas.
+     * Existing entity is matched using its unique guid if supplied or by its 
unique attributes eg: qualifiedName
+     * @param entities
+     * @return EntityMutationResponse
+     * @throws AtlasBaseException
      */
-    @Deprecated
-    @GET
-    @Consumes({Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON})
+    @POST
+    @Path("/bulk")
+    @Consumes(Servlets.JSON_MEDIA_TYPE)
     @Produces(Servlets.JSON_MEDIA_TYPE)
-    @Path("/uniqueAttribute/type/{typeName}/attribute/{attrName}")
-    public List<AtlasEntity> getByUniqueAttribute(@PathParam("typeName") 
String typeName,
-        @PathParam("attrName") String attribute,
-        @QueryParam("value") String value) throws AtlasBaseException {
+    public EntityMutationResponse createOrUpdate(AtlasEntitiesWithExtInfo 
entities) throws AtlasBaseException {
+
+        EntityStream entityStream = new AtlasEntityStream(entities);
 
-        List<AtlasEntity> entityList = new ArrayList<>();
-        AtlasEntityType                   entityType = 
ensureEntityType(typeName);
+        return entitiesStore.createOrUpdate(entityStream);
+    }
 
-        validateUniqueAttribute(entityType, attribute);
+    /*******
+     * Entity Delete
+     *******/
 
-        try {
-            final ITypedReferenceableInstance entityDefinitionReference = 
metadataService.getEntityDefinitionReference(typeName, attribute, value);
-            Map<String, AtlasEntity> entityRet = 
restAdapters.getAtlasEntity(entityDefinitionReference);
-            entityList.addAll(entityRet.values());
-        } catch (AtlasException e) {
-            throw toAtlasBaseException(e);
-        }
+    @DELETE
+    @Path("/bulk")
+    @Consumes(Servlets.JSON_MEDIA_TYPE)
+    @Produces(Servlets.JSON_MEDIA_TYPE)
+    public EntityMutationResponse deleteByGuids(@QueryParam("guid") final 
List<String> guids) throws AtlasBaseException {
+        EntityMutationResponse ret = entitiesStore.deleteByIds(guids);
 
-        return entityList;
+        return ret;
     }
 
     /**
@@ -348,18 +390,6 @@ public class EntityREST {
         }
     }
 
-    private AtlasType validateType(String entityType, TypeCategory 
expectedCategory) throws AtlasBaseException {
-        if ( StringUtils.isEmpty(entityType) ) {
-            throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, 
entityType);
-        }
-
-        AtlasType type = typeRegistry.getType(entityType);
-        if (type.getTypeCategory() != expectedCategory) {
-            throw new AtlasBaseException(AtlasErrorCode.TYPE_CATEGORY_INVALID, 
type.getTypeCategory().name(), expectedCategory.name());
-        }
-
-        return type;
-    }
 
     private AtlasEntityType ensureEntityType(String typeName) throws 
AtlasBaseException {
         AtlasEntityType ret = typeRegistry.getEntityTypeByName(typeName);
@@ -381,50 +411,6 @@ public class EntityREST {
         return ret;
     }
 
-    /**
-     * Deprecated method - not used
-     * Validate that attribute is unique attribute
-     * @param entityType     the entity type
-     * @param attributeName  the name of the attribute
-     */
-    private void validateUniqueAttribute(AtlasEntityType entityType, String 
attributeName) throws AtlasBaseException {
-        AtlasAttributeDef attribute = 
entityType.getAttributeDef(attributeName);
-
-        if (attribute == null || !attribute.getIsUnique()) {
-            throw new 
AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, 
entityType.getTypeName(), attributeName);
-        }
-    }
-
-    private List<AtlasEntity> getOrderedEntityList(Map<String, AtlasEntity> 
entities, String firstItemGuid) {
-        List<AtlasEntity> ret = new ArrayList<>(entities.size());
-
-        for (AtlasEntity entity : entities.values()) {
-            if (StringUtils.equals(entity.getGuid(), firstItemGuid)) {
-                ret.add(0, entity);
-            } else {
-                ret.add(entity);
-            }
-        }
-
-        return ret;
-    }
-
-    private void validateAttributes(AtlasEntityType entityType, Map<String, 
Object> attributes)
-            throws AtlasBaseException {
-
-        if (MapUtils.isEmpty(attributes)) {
-            throw new 
AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, 
entityType.getTypeName(), "");
-        }
-
-        for (String attrName : attributes.keySet()) {
-            AtlasAttributeDef attrDef = entityType.getAttributeDef(attrName);
-
-            if (attrDef == null || !attrDef.getIsUnique()) {
-                throw new 
AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, 
entityType.getTypeName(), attrName);
-            }
-        }
-    }
-
     private Map<String, Object> getAttributes(HttpServletRequest request) {
         Map<String, Object> attributes = new HashMap<>();
 
@@ -443,4 +429,23 @@ public class EntityREST {
 
         return attributes;
     }
+
+    /**
+     * Validate that each attribute given is an unique attribute
+     * @param entityType the entity type
+     * @param attributes attributes
+     */
+    private void validateUniqueAttribute(AtlasEntityType entityType, 
Map<String, Object> attributes) throws AtlasBaseException {
+        if (MapUtils.isEmpty(attributes)) {
+            throw new 
AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, 
entityType.getTypeName(), "");
+        }
+
+        for (String attributeName : attributes.keySet()) {
+            AtlasAttributeDef attribute = 
entityType.getAttributeDef(attributeName);
+
+            if (attribute == null || !attribute.getIsUnique()) {
+                throw new 
AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, 
entityType.getTypeName(), attributeName);
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/ce20d6f5/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java
----------------------------------------------------------------------
diff --git 
a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java 
b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java
index 9957f42..948583f 100644
--- a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java
+++ b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java
@@ -25,6 +25,7 @@ import org.apache.atlas.RequestContext;
 import org.apache.atlas.TestUtilsV2;
 import org.apache.atlas.model.instance.AtlasClassification;
 import org.apache.atlas.model.instance.AtlasEntity;
+import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo;
 import org.apache.atlas.model.instance.AtlasEntityHeader;
 import org.apache.atlas.model.instance.AtlasObjectId;
 import org.apache.atlas.model.instance.AtlasStruct;
@@ -33,7 +34,9 @@ import org.apache.atlas.model.instance.EntityMutationResponse;
 import org.apache.atlas.model.instance.EntityMutations;
 import org.apache.atlas.model.typedef.AtlasTypesDef;
 import org.apache.atlas.repository.graph.AtlasGraphProvider;
+import 
org.apache.atlas.repository.store.bootstrap.AtlasTypeDefStoreInitializer;
 import org.apache.atlas.store.AtlasTypeDefStore;
+import org.apache.atlas.type.AtlasTypeRegistry;
 import org.apache.atlas.web.rest.EntitiesREST;
 
 import org.apache.atlas.web.rest.EntityREST;
@@ -49,16 +52,21 @@ import org.testng.annotations.Test;
 import javax.inject.Inject;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+
 @Guice(modules = {RepositoryMetadataModule.class})
 public class TestEntitiesREST {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(TestEntitiesREST.class);
 
     @Inject
+    AtlasTypeRegistry typeRegistry;
+
+    @Inject
     private AtlasTypeDefStore typeStore;
 
     @Inject
@@ -69,10 +77,6 @@ public class TestEntitiesREST {
 
     private List<String> createdGuids = new ArrayList<>();
 
-    private Map<String, AtlasEntity> dbEntityMap;
-
-    private Map<String, AtlasEntity> tableEntityMap;
-
     private AtlasEntity dbEntity;
 
     private AtlasEntity tableEntity;
@@ -81,17 +85,22 @@ public class TestEntitiesREST {
 
     @BeforeClass
     public void setUp() throws Exception {
-        AtlasTypesDef typesDef = TestUtilsV2.defineHiveTypes();
-        typeStore.createTypesDef(typesDef);
-        dbEntityMap = TestUtilsV2.createDBEntity();
-        dbEntity = dbEntityMap.values().iterator().next();
+        AtlasTypesDef[] testTypesDefs = new AtlasTypesDef[] {  
TestUtilsV2.defineHiveTypes() };
 
-        tableEntityMap = TestUtilsV2.createTableEntity(dbEntity.getGuid());
-        tableEntity = tableEntityMap.values().iterator().next();
+        for (AtlasTypesDef typesDef : testTypesDefs) {
+            AtlasTypesDef typesToCreate = 
AtlasTypeDefStoreInitializer.getTypesToCreate(typesDef, typeRegistry);
 
-        final AtlasEntity colEntity = 
TestUtilsV2.createColumnEntity(tableEntity.getGuid());
+            if (!typesToCreate.isEmpty()) {
+                typeStore.createTypesDef(typesToCreate);
+            }
+        }
+
+        dbEntity    = TestUtilsV2.createDBEntity();
+        tableEntity = TestUtilsV2.createTableEntity(dbEntity);
+
+        final AtlasEntity colEntity = 
TestUtilsV2.createColumnEntity(tableEntity);
         columns = new ArrayList<AtlasEntity>() {{ add(colEntity); }};
-        tableEntity.setAttribute("columns", columns);
+        tableEntity.setAttribute("columns", getObjIdList(columns));
     }
 
     @AfterMethod
@@ -106,11 +115,15 @@ public class TestEntitiesREST {
 
     @Test
     public void testCreateOrUpdateEntities() throws Exception {
-        Map<String, AtlasEntity> entities = new HashMap<>();
-        entities.put(dbEntity.getGuid(), dbEntity);
-        entities.put(tableEntity.getGuid(), tableEntity);
+        AtlasEntitiesWithExtInfo entities = new AtlasEntitiesWithExtInfo();
 
-        EntityMutationResponse response = 
entitiesREST.createOrUpdate(entities);
+        entities.addEntity(dbEntity);
+        entities.addEntity(tableEntity);
+        for (AtlasEntity column : columns) {
+            entities.addReferredEntity(column);
+        }
+
+        EntityMutationResponse response = entityREST.createOrUpdate(entities);
         List<AtlasEntityHeader> guids = 
response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE);
 
         Assert.assertNotNull(guids);
@@ -137,23 +150,24 @@ public class TestEntitiesREST {
     public void testUpdateWithSerializedEntities() throws  Exception {
         //Check with serialization and deserialization of entity attributes 
for the case
         // where attributes which are de-serialized into a map
-        Map<String, AtlasEntity> dbEntityMap = TestUtilsV2.createDBEntity();
-        AtlasEntity dbEntity = dbEntityMap.values().iterator().next();
-
-        Map<String, AtlasEntity> tableEntityMap = 
TestUtilsV2.createTableEntity(dbEntity.getGuid());
-        AtlasEntity tableEntity = tableEntityMap.values().iterator().next();
+        AtlasEntity dbEntity    = TestUtilsV2.createDBEntity();
+        AtlasEntity tableEntity = TestUtilsV2.createTableEntity(dbEntity);
 
-        final AtlasEntity colEntity = 
TestUtilsV2.createColumnEntity(tableEntity.getGuid());
+        final AtlasEntity colEntity = 
TestUtilsV2.createColumnEntity(tableEntity);
         List<AtlasEntity> columns = new ArrayList<AtlasEntity>() {{ 
add(colEntity); }};
-        tableEntity.setAttribute("columns", columns);
+        tableEntity.setAttribute("columns", getObjIdList(columns));
 
         AtlasEntity newDBEntity = serDeserEntity(dbEntity);
         AtlasEntity newTableEntity = serDeserEntity(tableEntity);
 
-        Map<String, AtlasEntity> newEntities = new HashMap<>();
-        newEntities.put(newDBEntity.getGuid(), newDBEntity);
-        newEntities.put(newTableEntity.getGuid(), newTableEntity);
-        EntityMutationResponse response2 = 
entitiesREST.createOrUpdate(newEntities);
+        AtlasEntitiesWithExtInfo newEntities = new AtlasEntitiesWithExtInfo();
+        newEntities.addEntity(newDBEntity);
+        newEntities.addEntity(newTableEntity);
+        for (AtlasEntity column : columns) {
+            newEntities.addReferredEntity(serDeserEntity(column));
+        }
+
+        EntityMutationResponse response2 = 
entityREST.createOrUpdate(newEntities);
 
         List<AtlasEntityHeader> newGuids = 
response2.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE);
         Assert.assertNotNull(newGuids);
@@ -163,8 +177,8 @@ public class TestEntitiesREST {
     @Test(dependsOnMethods = "testCreateOrUpdateEntities")
     public void testGetEntities() throws Exception {
 
-        final AtlasEntity.AtlasEntities response = 
entitiesREST.getById(createdGuids);
-        final List<AtlasEntity> entities = response.getList();
+        final AtlasEntitiesWithExtInfo response = 
entityREST.getByGuids(createdGuids);
+        final List<AtlasEntity> entities = response.getEntities();
 
         Assert.assertNotNull(entities);
         Assert.assertEquals(entities.size(), 3);
@@ -174,7 +188,7 @@ public class TestEntitiesREST {
     @Test(dependsOnMethods = "testGetEntities")
     public void testDeleteEntities() throws Exception {
 
-        final EntityMutationResponse response = 
entitiesREST.deleteById(createdGuids);
+        final EntityMutationResponse response = 
entityREST.deleteByGuids(createdGuids);
         final List<AtlasEntityHeader> entities = 
response.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE);
 
         Assert.assertNotNull(entities);
@@ -247,4 +261,14 @@ public class TestEntitiesREST {
         AtlasEntity newEntity = mapper.readValue(entityJson, 
AtlasEntity.class);
         return newEntity;
     }
+
+    private List<AtlasObjectId> getObjIdList(Collection<AtlasEntity> entities) 
{
+        List<AtlasObjectId> ret = new ArrayList<>();
+
+        for (AtlasEntity entity : entities) {
+            ret.add(entity.getAtlasObjectId());
+        }
+
+        return ret;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/ce20d6f5/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java
----------------------------------------------------------------------
diff --git 
a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java 
b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java
index fd110cd..b4fe3d7 100644
--- a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java
+++ b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java
@@ -22,6 +22,7 @@ import org.apache.atlas.RequestContext;
 import org.apache.atlas.TestUtilsV2;
 import org.apache.atlas.model.instance.AtlasClassification;
 import org.apache.atlas.model.instance.AtlasEntity;
+import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo;
 import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
 import org.apache.atlas.model.instance.AtlasEntityHeader;
 import org.apache.atlas.model.instance.EntityMutationResponse;
@@ -29,8 +30,8 @@ import org.apache.atlas.model.instance.EntityMutations;
 import org.apache.atlas.model.typedef.AtlasTypesDef;
 import org.apache.atlas.repository.graph.AtlasGraphProvider;
 import org.apache.atlas.store.AtlasTypeDefStore;
-import org.apache.atlas.web.rest.EntitiesREST;
 import org.apache.atlas.web.rest.EntityREST;
+import org.mockito.Mockito;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeClass;
@@ -39,6 +40,7 @@ import org.testng.annotations.Guice;
 import org.testng.annotations.Test;
 
 import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -53,9 +55,6 @@ public class TestEntityREST {
     @Inject
     private EntityREST entityREST;
 
-    @Inject
-    private EntitiesREST entitiesREST;
-
     private AtlasEntity dbEntity;
 
     private AtlasClassification testClassification;
@@ -77,12 +76,9 @@ public class TestEntityREST {
     }
 
     private void createTestEntity() throws Exception {
-        Map<String, AtlasEntity> dbEntityMap = TestUtilsV2.createDBEntity();
-        AtlasEntity              dbEntity    = 
dbEntityMap.values().iterator().next();
+        AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
 
-        dbEntityMap.put(dbEntity.getGuid(), dbEntity);
-
-        final EntityMutationResponse response = 
entitiesREST.createOrUpdate(dbEntityMap);
+        final EntityMutationResponse response = entityREST.createOrUpdate(new 
AtlasEntitiesWithExtInfo(dbEntity));
 
         Assert.assertNotNull(response);
         List<AtlasEntityHeader> entitiesMutated = 
response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE);
@@ -158,10 +154,9 @@ public class TestEntityREST {
 
     @Test
     public void  testUpdateGetDeleteEntityByUniqueAttribute() throws Exception 
{
-        Map<String, AtlasEntity> dbEntityMap = TestUtilsV2.createDBEntity();
-        AtlasEntity              dbEntity    = 
dbEntityMap.values().iterator().next();
-        EntityMutationResponse   response    = 
entitiesREST.createOrUpdate(dbEntityMap);
-        String                   dbGuid      = 
response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE).get(0).getGuid();
+        AtlasEntity            dbEntity = TestUtilsV2.createDBEntity();
+        EntityMutationResponse response = entityREST.createOrUpdate(new 
AtlasEntitiesWithExtInfo(dbEntity));
+        String                 dbGuid   = 
response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE).get(0).getGuid();
 
         Assert.assertTrue(AtlasEntity.isAssigned(dbGuid));
 
@@ -170,21 +165,35 @@ public class TestEntityREST {
 
         dbEntity.setAttribute(TestUtilsV2.NAME, updatedDBName);
 
-        response = 
entityREST.partialUpdateByUniqueAttribute(TestUtilsV2.DATABASE_TYPE, 
TestUtilsV2.NAME, prevDBName, dbEntity);
+        response = 
entityREST.partialUpdateByUniqueAttribute(TestUtilsV2.DATABASE_TYPE, 
toHttpServletRequest(TestUtilsV2.NAME, prevDBName), dbEntity);
         
Assert.assertEquals(response.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE).get(0).getGuid(),
 dbGuid);
 
         //Get By unique attribute
-        List<AtlasEntity> entities = 
entityREST.getByUniqueAttribute(TestUtilsV2.DATABASE_TYPE, TestUtilsV2.NAME, 
updatedDBName);
-        Assert.assertNotNull(entities);
-        Assert.assertNotNull(entities.get(0).getGuid());
-        Assert.assertEquals(entities.get(0).getGuid(), dbGuid);
-        TestEntitiesREST.verifyAttributes(entities.get(0).getAttributes(), 
dbEntity.getAttributes());
+        AtlasEntityWithExtInfo entity = 
entityREST.getByUniqueAttributes(TestUtilsV2.DATABASE_TYPE, 
toHttpServletRequest(TestUtilsV2.NAME, updatedDBName));
+        Assert.assertNotNull(entity);
+        Assert.assertNotNull(entity.getEntity().getGuid());
+        Assert.assertEquals(entity.getEntity().getGuid(), dbGuid);
+        TestEntitiesREST.verifyAttributes(entity.getEntity().getAttributes(), 
dbEntity.getAttributes());
 
-        final EntityMutationResponse deleteResponse = 
entityREST.deleteByUniqueAttribute(TestUtilsV2.DATABASE_TYPE, TestUtilsV2.NAME, 
(String) dbEntity.getAttribute(TestUtilsV2.NAME));
+        final EntityMutationResponse deleteResponse = 
entityREST.deleteByUniqueAttribute(TestUtilsV2.DATABASE_TYPE, 
toHttpServletRequest(TestUtilsV2.NAME, (String) 
dbEntity.getAttribute(TestUtilsV2.NAME)));
 
         
Assert.assertNotNull(deleteResponse.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE));
         
Assert.assertEquals(deleteResponse.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE).size(),
 1);
         
Assert.assertEquals(deleteResponse.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE).get(0).getGuid(),
 dbGuid);
     }
 
+    private HttpServletRequest toHttpServletRequest(String attrName, String 
attrValue) {
+        HttpServletRequest    request   = 
Mockito.mock(HttpServletRequest.class);
+        Map<String, String[]> paramsMap = 
toParametersMap(EntityREST.PREFIX_ATTR + attrName, attrValue);
+
+        Mockito.when(request.getParameterMap()).thenReturn(paramsMap);
+
+        return request;
+    }
+
+    private Map<String, String[]> toParametersMap(final String name, final 
String value) {
+        return new HashMap<String, String[]>() {{
+            put(name, new String[] { value });
+        }};
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/ce20d6f5/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java
----------------------------------------------------------------------
diff --git 
a/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java 
b/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java
index 64593b7..134c798 100755
--- a/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java
+++ b/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java
@@ -43,6 +43,7 @@ import org.apache.atlas.model.instance.AtlasEntityHeader;
 import org.apache.atlas.model.instance.AtlasStruct;
 import org.apache.atlas.model.instance.EntityMutationResponse;
 import org.apache.atlas.model.instance.EntityMutations;
+import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
 import org.apache.atlas.model.typedef.AtlasClassificationDef;
 import org.apache.atlas.model.typedef.AtlasEntityDef;
 import org.apache.atlas.model.typedef.AtlasEnumDef;
@@ -74,6 +75,7 @@ import org.apache.atlas.typesystem.types.TypeUtils;
 import org.apache.atlas.typesystem.types.utils.TypesUtil;
 import org.apache.atlas.utils.AuthenticationUtil;
 import org.apache.atlas.utils.ParamChecker;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.lang.RandomStringUtils;
 import org.codehaus.jettison.json.JSONArray;
@@ -286,13 +288,13 @@ public abstract class BaseResourceIT {
         EntityMutationResponse entity = null;
         try {
             if (!update) {
-                entity = entitiesClientV2.createEntity(atlasEntity);
+                entity = entitiesClientV2.createEntity(new 
AtlasEntityWithExtInfo(atlasEntity));
                 assertNotNull(entity);
                 
assertNotNull(entity.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE));
                 
assertTrue(entity.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE).size()
 > 0);
                 return 
entity.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE).get(0);
             } else {
-                entity = entitiesClientV2.updateEntity(atlasEntity);
+                entity = entitiesClientV2.updateEntity(new 
AtlasEntityWithExtInfo(atlasEntity));
                 assertNotNull(entity);
                 
assertNotNull(entity.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE));
                 
assertTrue(entity.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE).size()
 > 0);

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/ce20d6f5/webapp/src/test/java/org/apache/atlas/web/resources/EntityV2JerseyResourceIT.java
----------------------------------------------------------------------
diff --git 
a/webapp/src/test/java/org/apache/atlas/web/resources/EntityV2JerseyResourceIT.java
 
b/webapp/src/test/java/org/apache/atlas/web/resources/EntityV2JerseyResourceIT.java
index 9f03718..639d581 100755
--- 
a/webapp/src/test/java/org/apache/atlas/web/resources/EntityV2JerseyResourceIT.java
+++ 
b/webapp/src/test/java/org/apache/atlas/web/resources/EntityV2JerseyResourceIT.java
@@ -34,9 +34,11 @@ import org.apache.atlas.AtlasClient;
 import org.apache.atlas.AtlasServiceException;
 import org.apache.atlas.EntityAuditEvent;
 import org.apache.atlas.model.instance.AtlasClassification;
-import 
org.apache.atlas.model.instance.AtlasClassification.AtlasClassifications;
 import org.apache.atlas.model.instance.AtlasEntity;
 import org.apache.atlas.model.instance.AtlasEntityHeader;
+import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
+import org.apache.atlas.model.instance.AtlasObjectId;
+import 
org.apache.atlas.model.instance.AtlasClassification.AtlasClassifications;
 import org.apache.atlas.model.instance.EntityMutationResponse;
 import org.apache.atlas.model.instance.EntityMutations;
 import org.apache.atlas.model.typedef.AtlasClassificationDef;
@@ -106,54 +108,57 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
 
     @Test
     public void testCreateNestedEntities() throws Exception {
+        AtlasEntity.AtlasEntitiesWithExtInfo entities = new 
AtlasEntity.AtlasEntitiesWithExtInfo();
 
-        AtlasEntity databaseInstance = new AtlasEntity(DATABASE_TYPE_V2);
+        AtlasEntity databaseInstance = new AtlasEntity(DATABASE_TYPE_V2, 
"name", "db1");
         databaseInstance.setAttribute("name", "db1");
         databaseInstance.setAttribute("description", "foo database");
         databaseInstance.setAttribute("owner", "user1");
         databaseInstance.setAttribute("locationUri", "/tmp");
         databaseInstance.setAttribute("createTime",1000);
+        entities.addEntity(databaseInstance);
 
         int nTables = 5;
         int colsPerTable=3;
-        List<AtlasEntity> tables = new ArrayList<>();
-        List<AtlasEntity> allColumns = new ArrayList<>();
 
         for(int i = 0; i < nTables; i++) {
             String tableName = "db1-table-" + i;
 
-            AtlasEntity tableInstance =
-                    new AtlasEntity(HIVE_TABLE_TYPE_V2);
-            tableInstance.setAttribute("name", tableName);
+            AtlasEntity tableInstance = new AtlasEntity(HIVE_TABLE_TYPE_V2, 
"name", tableName);
             
tableInstance.setAttribute(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, tableName);
-            tableInstance.setAttribute("db", databaseInstance);
+            tableInstance.setAttribute("db", 
databaseInstance.getAtlasObjectId());
             tableInstance.setAttribute("description", tableName + " table");
-            tables.add(tableInstance);
+            entities.addEntity(tableInstance);
 
-            List<AtlasEntity> columns = new ArrayList<>();
+            List<AtlasObjectId> columns = new ArrayList<>();
             for(int j = 0; j < colsPerTable; j++) {
                 AtlasEntity columnInstance = new AtlasEntity(COLUMN_TYPE_V2);
                 columnInstance.setAttribute("name", tableName + "-col-" + j);
                 columnInstance.setAttribute("dataType", "String");
                 columnInstance.setAttribute("comment", "column " + j + " for 
table " + i);
-                allColumns.add(columnInstance);
-                columns.add(columnInstance);
+
+                columns.add(columnInstance.getAtlasObjectId());
+
+                entities.addReferredEntity(columnInstance);
             }
-            tableInstance.setAttribute("columns", 
ImmutableList.builder().addAll(columns).build());
+            tableInstance.setAttribute("columns", columns);
         }
 
         //Create the tables.  The database and columns should be created 
automatically, since
         //the tables reference them.
-        EntityMutationResponse response = 
entitiesClientV2.createEntities(tables);
+
+        EntityMutationResponse response = 
entitiesClientV2.createEntities(entities);
         Assert.assertNotNull(response);
 
         Map<String,String> guidsCreated = response.getGuidAssignments();
         assertEquals(guidsCreated.size(), nTables * colsPerTable + nTables + 
1);
         assertNotNull(guidsCreated.get(databaseInstance.getGuid()));
-        for(AtlasEntity r : allColumns) {
+
+        for(AtlasEntity r : entities.getEntities()) {
             assertNotNull(guidsCreated.get(r.getGuid()));
         }
-        for(AtlasEntity r : tables) {
+
+        for(AtlasEntity r : entities.getReferredEntities().values()) {
             assertNotNull(guidsCreated.get(r.getGuid()));
         }
     }
@@ -189,7 +194,7 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         AtlasEntity hiveTableInstanceV2 = 
createHiveTableInstanceV2(hiveDBInstanceV2, tableName);
         
hiveTableInstanceV2.setAttribute(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, 
tableName);
 
-        EntityMutationResponse entity = 
entitiesClientV2.createEntity(hiveTableInstanceV2);
+        EntityMutationResponse entity = entitiesClientV2.createEntity(new 
AtlasEntityWithExtInfo(hiveTableInstanceV2));
         assertNotNull(entity);
         
assertNotNull(entity.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE));
         results = searchByDSL(String.format("%s where name='%s'", 
DATABASE_TYPE_V2, DATABASE_NAME));
@@ -226,7 +231,7 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         //create entity for the type
         AtlasEntity instance = new AtlasEntity(entityDef.getName());
         instance.setAttribute("name", randomString());
-        EntityMutationResponse mutationResponse = 
entitiesClientV2.createEntity(instance);
+        EntityMutationResponse mutationResponse = 
entitiesClientV2.createEntity(new AtlasEntityWithExtInfo(instance));
         assertNotNull(mutationResponse);
         
assertNotNull(mutationResponse.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE));
         
assertEquals(mutationResponse.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE).size(),1
 );
@@ -272,9 +277,8 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         AtlasEntity hiveDB = createHiveDB();
         String qualifiedName = (String) hiveDB.getAttribute(NAME);
         //get entity by attribute
-        Map<String, String> attributes = new HashMap<>();
-        attributes.put(NAME, qualifiedName);
-        AtlasEntity byAttribute = 
entitiesClientV2.getEntityByAttribute(DATABASE_TYPE_V2, attributes).getEntity();
+
+        AtlasEntity byAttribute = 
entitiesClientV2.getEntityByAttribute(DATABASE_TYPE_V2, toMap(NAME, 
qualifiedName)).getEntity();
         assertEquals(byAttribute.getTypeName(), DATABASE_TYPE_V2);
         assertEquals(byAttribute.getAttribute(NAME), qualifiedName);
     }
@@ -407,7 +411,7 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
 
         AtlasEntity entityByGuid = getEntityByGuid(guid);
         entityByGuid.setAttribute(property, value);
-        EntityMutationResponse response = 
entitiesClientV2.updateEntity(entityByGuid);
+        EntityMutationResponse response = entitiesClientV2.updateEntity(new 
AtlasEntityWithExtInfo(entityByGuid));
         assertNotNull(response);
         
assertNotNull(response.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE));
     }
@@ -624,10 +628,10 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         AtlasEntity hiveTable = createHiveTable();
         AtlasEntity tableUpdated = hiveTable;
 
-        hiveTable.setAttribute("columns", columns);
+        hiveTable.setAttribute("columns", AtlasTypeUtil.toObjectIds(columns));
 
         LOG.debug("Updating entity= " + tableUpdated);
-        EntityMutationResponse updateResult = 
entitiesClientV2.updateEntity(tableUpdated);
+        EntityMutationResponse updateResult = 
entitiesClientV2.updateEntity(new AtlasEntityWithExtInfo(tableUpdated));
         assertNotNull(updateResult);
         
assertNotNull(updateResult.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE));
         
assertTrue(updateResult.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE).size()
 > 0);
@@ -642,11 +646,12 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         ref = new AtlasEntity(BaseResourceIT.COLUMN_TYPE_V2, values);
         columns.set(0, ref);
         tableUpdated = hiveTable;
-        tableUpdated.setAttribute("columns", columns);
+        tableUpdated.setAttribute("columns", 
AtlasTypeUtil.toObjectIds(columns));
 
         LOG.debug("Updating entity= " + tableUpdated);
-        EntityMutationResponse updateResponse = 
entitiesClientV2.updateEntityByAttribute(BaseResourceIT.HIVE_TABLE_TYPE_V2, 
AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME,
-                (String) hiveTable.getAttribute("name"), tableUpdated);
+        Map<String, String> uniqAttributes = new HashMap<>();
+        uniqAttributes.put(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, 
(String)hiveTable.getAttribute("name"));
+        EntityMutationResponse updateResponse = 
entitiesClientV2.updateEntityByAttribute(BaseResourceIT.HIVE_TABLE_TYPE_V2, 
uniqAttributes, tableUpdated);
         assertNotNull(updateResponse);
         
assertNotNull(updateResponse.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE));
         
assertTrue(updateResponse.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE).size()
 > 0);
@@ -680,8 +685,8 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         columns.add(ref1);
         columns.add(ref2);
         AtlasEntity hiveTable = createHiveTable();
-        hiveTable.setAttribute("columns", columns);
-        EntityMutationResponse updateEntityResult = 
entitiesClientV2.updateEntity(hiveTable);
+        hiveTable.setAttribute("columns", AtlasTypeUtil.toObjectIds(columns));
+        EntityMutationResponse updateEntityResult = 
entitiesClientV2.updateEntity(new AtlasEntityWithExtInfo(hiveTable));
         assertNotNull(updateEntityResult);
         
assertNotNull(updateEntityResult.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE));
         
assertNotNull(updateEntityResult.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE));
@@ -690,7 +695,7 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         
assertEquals(updateEntityResult.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE).size(),
 2);
 
         AtlasEntity entityByGuid = getEntityByGuid(hiveTable.getGuid());
-        List<AtlasEntity> refs = (List<AtlasEntity>) 
entityByGuid.getAttribute("columns");
+        List<AtlasObjectId> refs = (List<AtlasObjectId>) 
entityByGuid.getAttribute("columns");
         assertEquals(refs.size(), 2);
     }
 
@@ -713,7 +718,7 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         AtlasEntityHeader entity2Header = createEntity(db2);
 
         // Delete the database entities
-        EntityMutationResponse deleteResponse = 
entitiesClientV2.deleteEntityByGuid(ImmutableList.of(entity1Header.getGuid(), 
entity2Header.getGuid()));
+        EntityMutationResponse deleteResponse = 
entitiesClientV2.deleteEntitiesByGuids(ImmutableList.of(entity1Header.getGuid(),
 entity2Header.getGuid()));
 
         // Verify that deleteEntities() response has database entity guids
         assertNotNull(deleteResponse);
@@ -727,10 +732,9 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
     public void testDeleteEntityByUniqAttribute() throws Exception {
         // Create database entity
         AtlasEntity hiveDB = createHiveDB(DATABASE_NAME + random());
-        String qualifiedName = (String) hiveDB.getAttribute(NAME);
 
         // Delete the database entity
-        EntityMutationResponse deleteResponse = 
entitiesClientV2.deleteEntityByAttribute(DATABASE_TYPE_V2, NAME, qualifiedName);
+        EntityMutationResponse deleteResponse = 
entitiesClientV2.deleteEntityByAttribute(DATABASE_TYPE_V2, toMap(NAME, (String) 
hiveDB.getAttribute(NAME)));
 
         // Verify that deleteEntities() response has database entity guids
         assertNotNull(deleteResponse);
@@ -740,4 +744,9 @@ public class EntityV2JerseyResourceIT extends 
BaseResourceIT {
         // Verify entities were deleted from the repository.
     }
 
+    private Map<String, String> toMap(final String name, final String value) {
+        return new HashMap<String, String>() {{
+            put(name, value);
+        }};
+    }
 }


Reply via email to