Repository: incubator-atlas Updated Branches: refs/heads/master 02cf8c488 -> d8c2a10e0
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d8c2a10e/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 aa209f9..d82e5ba 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 @@ -35,6 +35,7 @@ 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.lang3.StringUtils; @@ -47,6 +48,7 @@ import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import java.util.ArrayList; import java.util.List; +import java.util.Map; import static org.apache.atlas.web.adapters.AtlasInstanceRestAdapters.toAtlasBaseException; import static org.apache.atlas.web.adapters.AtlasInstanceRestAdapters.toEntityMutationResponse; @@ -74,45 +76,6 @@ public class EntityREST { } /** - * Create or Update an entity if it already exists - * - * @param entity The updated entity - * @return - */ - @POST - @Consumes({Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON}) - @Produces(Servlets.JSON_MEDIA_TYPE) - public EntityMutationResponse createOrUpdate(final AtlasEntity entity) throws AtlasBaseException { - EntityMutationResponse response = null; - ITypedReferenceableInstance[] entitiesInOldFormat = restAdapters.getITypedReferenceables(new ArrayList<AtlasEntity>() {{ add(entity); }}); - - try { - final AtlasClient.EntityResult result = metadataService.updateEntities(entitiesInOldFormat); - response = toEntityMutationResponse(result); - } catch (AtlasException e) { - LOG.error("Exception while getting a typed reference for the entity ", e); - throw AtlasInstanceRestAdapters.toAtlasBaseException(e); - } - return response; - } - - /** - * Complete Update of an entity identified by its GUID - * - * @param guid - * @param entity The updated entity - * @return - */ - @PUT - @Path("guid/{guid}") - @Consumes({Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON}) - @Produces(Servlets.JSON_MEDIA_TYPE) - public EntityMutationResponse updateByGuid(@PathParam("guid") String guid, AtlasEntity entity, @DefaultValue("false") @QueryParam("partialUpdate") boolean partialUpdate) throws AtlasBaseException { - return createOrUpdate(entity); - } - - - /** * Fetch the complete definition of an entity given its GUID. * * @param guid GUID for the entity @@ -120,10 +83,14 @@ public class EntityREST { @GET @Path("/guid/{guid}") @Produces(Servlets.JSON_MEDIA_TYPE) - public AtlasEntity getById(@PathParam("guid") String guid) throws AtlasBaseException { + public List<AtlasEntityWithAssociations> getById(@PathParam("guid") String guid) throws AtlasBaseException { + List<AtlasEntityWithAssociations> entityList = new ArrayList<>(); + try { ITypedReferenceableInstance ref = metadataService.getEntityDefinition(guid); - return restAdapters.getAtlasEntity(ref); + Map<String, AtlasEntityWithAssociations> entityRet = restAdapters.getAtlasEntity(ref); + entityList.addAll(entityRet.values()); + return entityList; } catch (AtlasException e) { throw toAtlasBaseException(e); } @@ -138,10 +105,14 @@ public class EntityREST { @GET @Path("/guid/{guid}/associations") @Produces(Servlets.JSON_MEDIA_TYPE) - public AtlasEntityWithAssociations getWithAssociationsByGuid(@PathParam("guid") String guid) throws AtlasBaseException { + public List<AtlasEntityWithAssociations> getWithAssociationsByGuid(@PathParam("guid") String guid) throws AtlasBaseException { + + List<AtlasEntityWithAssociations> entityList = new ArrayList<>(); try { ITypedReferenceableInstance ref = metadataService.getEntityDefinition(guid); - return restAdapters.getAtlasEntity(ref); + Map<String, AtlasEntityWithAssociations> entityRet = restAdapters.getAtlasEntity(ref); + entityList.addAll(entityRet.values()); + return entityList; } catch (AtlasException e) { throw toAtlasBaseException(e); } @@ -190,7 +161,9 @@ public class EntityREST { AtlasEntityType type = (AtlasEntityType) validateType(entityType, TypeCategory.ENTITY); validateUniqueAttribute(type, attribute); - Referenceable ref = restAdapters.getReferenceable(entity); + AtlasFormatConverter.ConverterContext ctx = new AtlasFormatConverter.ConverterContext(); + ctx.addEntity(entity); + Referenceable ref = restAdapters.getReferenceable(entity, ctx); AtlasClient.EntityResult result = metadataService.updateEntityByUniqueAttribute(entityType, attribute, value, ref); return toEntityMutationResponse(result); } @@ -220,19 +193,23 @@ public class EntityREST { @Consumes({Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON}) @Produces(Servlets.JSON_MEDIA_TYPE) @Path("/uniqueAttribute/type/{typeName}/attribute/{attrName}") - public AtlasEntity getByUniqueAttribute(@PathParam("typeName") String entityType, + public List<AtlasEntityWithAssociations> getByUniqueAttribute(@PathParam("typeName") String entityType, @PathParam("attrName") String attribute, @QueryParam("value") String value) throws AtlasBaseException { + List<AtlasEntityWithAssociations> entityList = new ArrayList<>(); AtlasEntityType type = (AtlasEntityType) validateType(entityType, TypeCategory.ENTITY); validateUniqueAttribute(type, attribute); try { final ITypedReferenceableInstance entityDefinitionReference = metadataService.getEntityDefinitionReference(entityType, attribute, value); - return restAdapters.getAtlasEntity(entityDefinitionReference); + Map<String, AtlasEntityWithAssociations> entityRet = restAdapters.getAtlasEntity(entityDefinitionReference); + entityList.addAll(entityRet.values()); } catch (AtlasException e) { throw toAtlasBaseException(e); } + + return entityList; } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d8c2a10e/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 cfe9909..9957f42 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 @@ -26,6 +26,7 @@ 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.AtlasEntityHeader; +import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasStruct; import org.apache.atlas.model.instance.ClassificationAssociateRequest; import org.apache.atlas.model.instance.EntityMutationResponse; @@ -68,6 +69,10 @@ 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; @@ -78,9 +83,12 @@ public class TestEntitiesREST { public void setUp() throws Exception { AtlasTypesDef typesDef = TestUtilsV2.defineHiveTypes(); typeStore.createTypesDef(typesDef); - dbEntity = TestUtilsV2.createDBEntity(); + dbEntityMap = TestUtilsV2.createDBEntity(); + dbEntity = dbEntityMap.values().iterator().next(); + + tableEntityMap = TestUtilsV2.createTableEntity(dbEntity.getGuid()); + tableEntity = tableEntityMap.values().iterator().next(); - tableEntity = TestUtilsV2.createTableEntity(dbEntity.getGuid()); final AtlasEntity colEntity = TestUtilsV2.createColumnEntity(tableEntity.getGuid()); columns = new ArrayList<AtlasEntity>() {{ add(colEntity); }}; tableEntity.setAttribute("columns", columns); @@ -98,9 +106,9 @@ public class TestEntitiesREST { @Test public void testCreateOrUpdateEntities() throws Exception { - List<AtlasEntity> entities = new ArrayList<AtlasEntity>(); - entities.add(dbEntity); - entities.add(tableEntity); + Map<String, AtlasEntity> entities = new HashMap<>(); + entities.put(dbEntity.getGuid(), dbEntity); + entities.put(tableEntity.getGuid(), tableEntity); EntityMutationResponse response = entitiesREST.createOrUpdate(entities); List<AtlasEntityHeader> guids = response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE); @@ -129,9 +137,12 @@ 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 - AtlasEntity dbEntity = TestUtilsV2.createDBEntity(); + 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 tableEntity = TestUtilsV2.createTableEntity(dbEntity.getGuid()); final AtlasEntity colEntity = TestUtilsV2.createColumnEntity(tableEntity.getGuid()); List<AtlasEntity> columns = new ArrayList<AtlasEntity>() {{ add(colEntity); }}; tableEntity.setAttribute("columns", columns); @@ -139,9 +150,9 @@ public class TestEntitiesREST { AtlasEntity newDBEntity = serDeserEntity(dbEntity); AtlasEntity newTableEntity = serDeserEntity(tableEntity); - List<AtlasEntity> newEntities = new ArrayList<AtlasEntity>(); - newEntities.add(newDBEntity); - newEntities.add(newTableEntity); + Map<String, AtlasEntity> newEntities = new HashMap<>(); + newEntities.put(newDBEntity.getGuid(), newDBEntity); + newEntities.put(newTableEntity.getGuid(), newTableEntity); EntityMutationResponse response2 = entitiesREST.createOrUpdate(newEntities); List<AtlasEntityHeader> newGuids = response2.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE); @@ -211,7 +222,7 @@ public class TestEntitiesREST { //date Assert.assertEquals(tableEntity.getAttribute("created"), retrievedTableEntity.getAttribute("created")); //array of Ids - Assert.assertEquals(((List<AtlasEntity>) retrievedTableEntity.getAttribute("columns")).get(0).getGuid(), retrievedColumnEntity.getGuid()); + Assert.assertEquals(((List<AtlasObjectId>) retrievedTableEntity.getAttribute("columns")).get(0).getGuid(), retrievedColumnEntity.getGuid()); //array of structs Assert.assertEquals(((List<AtlasStruct>) retrievedTableEntity.getAttribute("partitions")), tableEntity.getAttribute("partitions")); } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d8c2a10e/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 ee36fdf..2d49c47 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 @@ -29,6 +29,7 @@ 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.testng.Assert; import org.testng.annotations.AfterMethod; @@ -41,6 +42,7 @@ import javax.inject.Inject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; @Guice(modules = {RepositoryMetadataModule.class}) public class TestEntityREST { @@ -51,6 +53,9 @@ public class TestEntityREST { @Inject private EntityREST entityREST; + @Inject + private EntitiesREST entitiesREST; + private AtlasEntity dbEntity; private String dbGuid; @@ -61,7 +66,8 @@ public class TestEntityREST { public void setUp() throws Exception { AtlasTypesDef typesDef = TestUtilsV2.defineHiveTypes(); typeStore.createTypesDef(typesDef); - dbEntity = TestUtilsV2.createDBEntity(); + Map<String, AtlasEntity> dbEntityMap = TestUtilsV2.createDBEntity(); + dbEntity = dbEntityMap.values().iterator().next(); } @AfterClass @@ -74,9 +80,10 @@ public class TestEntityREST { RequestContext.clear(); } - @Test - public void testCreateOrUpdateEntity() throws Exception { - final EntityMutationResponse response = entityREST.createOrUpdate(dbEntity); + public void createOrUpdateEntity() throws Exception { + Map<String, AtlasEntity> dbEntityMap = new HashMap<>(); + dbEntityMap.put(dbEntity.getGuid(), dbEntity); + final EntityMutationResponse response = entitiesREST.createOrUpdate(dbEntityMap); Assert.assertNotNull(response); List<AtlasEntityHeader> entitiesMutated = response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE); @@ -88,18 +95,19 @@ public class TestEntityREST { Assert.assertEquals(entitiesMutated.size(), 1); } - @Test(dependsOnMethods = "testCreateOrUpdateEntity") + @Test public void testGetEntityById() throws Exception { - - final AtlasEntity response = entityREST.getById(dbGuid); + createOrUpdateEntity(); + final List<AtlasEntityWithAssociations> response = entityREST.getById(dbGuid); Assert.assertNotNull(response); - TestEntitiesREST.verifyAttributes(response.getAttributes(), dbEntity.getAttributes()); + TestEntitiesREST.verifyAttributes(response.get(0).getAttributes(), dbEntity.getAttributes()); } - @Test(dependsOnMethods = "testCreateOrUpdateEntity") + @Test public void testAddAndGetClassification() throws Exception { + createOrUpdateEntity(); List<AtlasClassification> classifications = new ArrayList<>(); testClassification = new AtlasClassification(TestUtilsV2.CLASSIFICATION, new HashMap<String, Object>() {{ put("tag", "tagName"); }}); classifications.add(testClassification); @@ -122,8 +130,8 @@ public class TestEntityREST { @Test(dependsOnMethods = "testAddAndGetClassification") public void testGetEntityWithAssociations() throws Exception { - AtlasEntityWithAssociations entity = entityREST.getWithAssociationsByGuid(dbGuid); - final List<AtlasClassification> retrievedClassifications = entity.getClassifications(); + List<AtlasEntityWithAssociations> entity = entityREST.getWithAssociationsByGuid(dbGuid); + final List<AtlasClassification> retrievedClassifications = entity.get(0).getClassifications(); Assert.assertNotNull(retrievedClassifications); Assert.assertEquals(new ArrayList<AtlasClassification>() {{ add(testClassification); }}, retrievedClassifications); @@ -151,8 +159,8 @@ public class TestEntityREST { @Test public void testUpdateGetDeleteEntityByUniqueAttribute() throws Exception { - AtlasEntity dbEntity = TestUtilsV2.createDBEntity(); - entityREST.createOrUpdate(dbEntity); + Map<String, AtlasEntity> dbEntityMap = TestUtilsV2.createDBEntity(); + entitiesREST.createOrUpdate(dbEntityMap); final String prevDBName = (String) dbEntity.getAttribute(TestUtilsV2.NAME); final String updatedDBName = "updatedDBName"; @@ -164,11 +172,11 @@ public class TestEntityREST { Assert.assertTrue(AtlasEntity.isAssigned(dbGuid)); //Get By unique attribute - AtlasEntity entity = entityREST.getByUniqueAttribute(TestUtilsV2.DATABASE_TYPE, TestUtilsV2.NAME, updatedDBName); - Assert.assertNotNull(entity); - Assert.assertNotNull(entity.getGuid()); - Assert.assertEquals(entity.getGuid(), dbGuid); - TestEntitiesREST.verifyAttributes(entity.getAttributes(), dbEntity.getAttributes()); + List<AtlasEntityWithAssociations> 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()); final EntityMutationResponse deleteResponse = entityREST.deleteByUniqueAttribute(TestUtilsV2.DATABASE_TYPE, TestUtilsV2.NAME, (String) dbEntity.getAttribute(TestUtilsV2.NAME)); http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d8c2a10e/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 d9bffe9..29a6d49 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 @@ -564,7 +564,7 @@ public class EntityV2JerseyResourceIT extends BaseResourceIT { hiveTable.setAttribute("columns", columns); LOG.debug("Updating entity= " + tableUpdated); - EntityMutationResponse updateResult = entitiesClientV2.updateEntity(hiveTable.getGuid(), tableUpdated); + EntityMutationResponse updateResult = entitiesClientV2.updateEntity(tableUpdated); assertNotNull(updateResult); assertNotNull(updateResult.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE)); assertTrue(updateResult.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE).size() > 0);
