Search in sources :

Example 11 with EntityResult

use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteMixOfExistentAndNonExistentEntities.

@Test
public void testDeleteMixOfExistentAndNonExistentEntities() throws Exception {
    ITypedReferenceableInstance entity1 = compositeMapValueType.createInstance();
    ITypedReferenceableInstance entity2 = compositeMapValueType.createInstance();
    List<String> createEntitiesResult = repositoryService.createEntities(entity1, entity2).getCreatedEntities();
    Assert.assertEquals(createEntitiesResult.size(), 2);
    List<String> guids = Arrays.asList(createEntitiesResult.get(0), "non-existent-guid1", "non-existent-guid2", createEntitiesResult.get(1));
    EntityResult deleteEntitiesResult = repositoryService.deleteEntities(guids);
    Assert.assertEquals(deleteEntitiesResult.getDeletedEntities().size(), 2);
    Assert.assertTrue(deleteEntitiesResult.getDeletedEntities().containsAll(createEntitiesResult));
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityResult(org.apache.atlas.model.legacy.EntityResult) Test(org.testng.annotations.Test)

Example 12 with EntityResult

use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteMixOfNullAndNonNullGuids.

@Test
public void testDeleteMixOfNullAndNonNullGuids() throws Exception {
    ITypedReferenceableInstance entity1 = compositeMapValueType.createInstance();
    ITypedReferenceableInstance entity2 = compositeMapValueType.createInstance();
    List<String> createEntitiesResult = repositoryService.createEntities(entity1, entity2).getCreatedEntities();
    Assert.assertEquals(createEntitiesResult.size(), 2);
    List<String> guids = Arrays.asList(createEntitiesResult.get(0), null, null, createEntitiesResult.get(1));
    EntityResult deleteEntitiesResult = repositoryService.deleteEntities(guids);
    Assert.assertEquals(deleteEntitiesResult.getDeletedEntities().size(), 2);
    Assert.assertTrue(deleteEntitiesResult.getDeletedEntities().containsAll(createEntitiesResult));
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityResult(org.apache.atlas.model.legacy.EntityResult) Test(org.testng.annotations.Test)

Example 13 with EntityResult

use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.

the class EntityResource method deleteEntities.

/**
     * Delete entities from the repository identified by their guids (including their composite references)
     * or
     * Deletes a single entity identified by its type and unique attribute value from the repository (including their composite references)
     *
     * @param guids list of deletion candidate guids
     *              or
     * @param entityType the entity type
     * @param attribute the unique attribute used to identify the entity
     * @param value the unique attribute value used to identify the entity
     * @return response payload as json - including guids of entities(including composite references from that entity) that were deleted
     */
@DELETE
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response deleteEntities(@QueryParam("guid") List<String> guids, @QueryParam("type") String entityType, @QueryParam("property") final String attribute, @QueryParam("value") final String value) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.deleteEntities({}, {}, {}, {})", guids, entityType, attribute, value);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.deleteEntities(" + guids + ", " + entityType + ", " + attribute + ", " + value + ")");
        }
        EntityResult entityResult;
        if (guids != null && !guids.isEmpty()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Deleting entities {}", guids);
            }
            EntityMutationResponse mutationResponse = entityREST.deleteByGuids(guids);
            entityResult = restAdapters.toCreateUpdateEntitiesResult(mutationResponse).getEntityResult();
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Deleting entity type={} with property {}={}", entityType, attribute, value);
            }
            Map<String, Object> attributes = new HashMap<>();
            attributes.put(attribute, value);
            EntityMutationResponse mutationResponse = entitiesStore.deleteByUniqueAttributes(getEntityType(entityType), attributes);
            entityResult = restAdapters.toCreateUpdateEntitiesResult(mutationResponse).getEntityResult();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Deleted entity result: {}", entityResult);
        }
        JSONObject response = getResponse(entityResult);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
        throw toWebApplicationException(e);
    } catch (EntityNotFoundException e) {
        if (guids != null && !guids.isEmpty()) {
            LOG.error("An entity with GUID={} does not exist ", guids, e);
        } else {
            LOG.error("An entity with qualifiedName {}-{}-{} does not exist", entityType, attribute, value, e);
        }
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.deleteEntities({}, {}, {}, {})", guids, entityType, attribute, value);
        }
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) EntityResult(org.apache.atlas.model.legacy.EntityResult) AtlasException(org.apache.atlas.AtlasException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 14 with EntityResult

use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.

the class EntityJerseyResourceIT method testSubmitSingleEntity.

@Test
public //API should accept single entity (or jsonarray of entities)
void testSubmitSingleEntity() throws Exception {
    Referenceable databaseInstance = new Referenceable(DATABASE_TYPE_BUILTIN);
    String dbName = randomString();
    databaseInstance.set("name", dbName);
    databaseInstance.set(QUALIFIED_NAME, dbName);
    databaseInstance.set("clusterName", randomString());
    databaseInstance.set("description", randomString());
    databaseInstance.set(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, dbName);
    databaseInstance.set("owner", "user1");
    databaseInstance.set("clusterName", "cl1");
    databaseInstance.set("parameters", Collections.EMPTY_MAP);
    databaseInstance.set("location", "/tmp");
    JSONObject response = atlasClientV1.callAPIWithBody(AtlasClient.API.CREATE_ENTITY, InstanceSerialization.toJson(databaseInstance, true));
    assertNotNull(response);
    Assert.assertNotNull(response.get(AtlasClient.REQUEST_ID));
    EntityResult entityResult = EntityResult.fromString(response.toString());
    assertEquals(entityResult.getCreatedEntities().size(), 1);
    assertNotNull(entityResult.getCreatedEntities().get(0));
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) JSONObject(org.codehaus.jettison.json.JSONObject) EntityResult(org.apache.atlas.model.legacy.EntityResult) Test(org.testng.annotations.Test)

Example 15 with EntityResult

use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.

the class EntityJerseyResourceIT method testCompleteUpdate.

@Test
public void testCompleteUpdate() throws Exception {
    String dbName = "db" + randomString();
    String tableName = "table" + randomString();
    Referenceable hiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    Id dbId = createInstance(hiveDBInstance);
    Referenceable hiveTableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
    Id tableId = createInstance(hiveTableInstance);
    final String guid = tableId._getId();
    try {
        Assert.assertNotNull(UUID.fromString(guid));
    } catch (IllegalArgumentException e) {
        Assert.fail("Response is not a guid, " + guid);
    }
    final List<Referenceable> columns = new ArrayList<>();
    Map<String, Object> values1 = new HashMap<>();
    values1.put(NAME, "col3");
    values1.put(QUALIFIED_NAME, "default.table.col3@cl1");
    values1.put("comment", "col3 comment");
    values1.put("type", "string");
    values1.put("owner", "user1");
    values1.put("position", 0);
    values1.put("description", "col3");
    values1.put("table", tableId);
    Map<String, Object> values2 = new HashMap<>();
    values2.put(NAME, "col4");
    values2.put(QUALIFIED_NAME, "default.table.col4@cl1");
    values2.put("comment", "col4 comment");
    values2.put("type", "string");
    values2.put("owner", "user2");
    values2.put("position", 1);
    values2.put("description", "col4");
    values2.put("table", tableId);
    Referenceable ref1 = new Referenceable(BaseResourceIT.COLUMN_TYPE_BUILTIN, values1);
    Referenceable ref2 = new Referenceable(BaseResourceIT.COLUMN_TYPE_BUILTIN, values2);
    columns.add(ref1);
    columns.add(ref2);
    hiveTableInstance.set("columns", columns);
    LOG.debug("Replacing entity= {}", hiveTableInstance);
    EntityResult updateEntity = atlasClientV1.updateEntities(hiveTableInstance);
    assertNotNull(updateEntity.getUpdateEntities());
    hiveTableInstance = atlasClientV1.getEntity(guid);
    List<Referenceable> refs = (List<Referenceable>) hiveTableInstance.get("columns");
    Assert.assertEquals(refs.size(), 2);
    Assert.assertTrue(refs.get(0).getValuesMap().equals(values1));
    Assert.assertTrue(refs.get(1).getValuesMap().equals(values2));
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONObject(org.codehaus.jettison.json.JSONObject) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Id(org.apache.atlas.typesystem.persistence.Id) EntityResult(org.apache.atlas.model.legacy.EntityResult) Test(org.testng.annotations.Test)

Aggregations

EntityResult (org.apache.atlas.model.legacy.EntityResult)33 Test (org.testng.annotations.Test)21 ArrayList (java.util.ArrayList)12 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)12 JSONObject (org.codehaus.jettison.json.JSONObject)12 List (java.util.List)11 Referenceable (org.apache.atlas.typesystem.Referenceable)11 ImmutableList (com.google.common.collect.ImmutableList)10 Id (org.apache.atlas.typesystem.persistence.Id)6 HashMap (java.util.HashMap)5 WebResource (com.sun.jersey.api.client.WebResource)4 GuidMapping (org.apache.atlas.model.instance.GuidMapping)4 Map (java.util.Map)3 AtlasException (org.apache.atlas.AtlasException)3 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)3 BaseRepositoryTest (org.apache.atlas.BaseRepositoryTest)2 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)2 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)2 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)2 RepositoryException (org.apache.atlas.repository.RepositoryException)2