Search in sources :

Example 6 with EntityResult

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

the class CreateUpdateEntitiesResult method fromJson.

/**
     * Deserializes the given json into an instance of
     * CreateUpdateEntitiesResult.
     *
     * @param json
     *            the (unmodified) json that comes back from Atlas.
     * @return
     * @throws AtlasServiceException
     */
public static CreateUpdateEntitiesResult fromJson(String json) throws AtlasServiceException {
    GuidMapping guidMapping = AtlasType.fromJson(json, GuidMapping.class);
    EntityResult entityResult = EntityResult.fromString(json);
    CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
    result.setEntityResult(entityResult);
    result.setGuidMapping(guidMapping);
    return result;
}
Also used : EntityResult(org.apache.atlas.model.legacy.EntityResult) GuidMapping(org.apache.atlas.model.instance.GuidMapping)

Example 7 with EntityResult

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

the class AtlasClientTest method testCreateEntity.

@Test
public void testCreateEntity() throws Exception {
    setupRetryParams();
    AtlasClient atlasClient = new AtlasClient(service, configuration);
    WebResource.Builder builder = setupBuilder(AtlasClient.API.CREATE_ENTITY, service);
    ClientResponse response = mock(ClientResponse.class);
    when(response.getStatus()).thenReturn(Response.Status.CREATED.getStatusCode());
    JSONObject jsonResponse = new JSONObject(new EntityResult(Arrays.asList("id"), null, null).toString());
    when(response.getEntity(String.class)).thenReturn(jsonResponse.toString());
    when(response.getLength()).thenReturn(jsonResponse.length());
    String entityJson = InstanceSerialization.toJson(new Referenceable("type"), true);
    when(builder.method(anyString(), Matchers.<Class>any(), anyString())).thenReturn(response);
    List<String> ids = atlasClient.createEntity(entityJson);
    assertEquals(ids.size(), 1);
    assertEquals(ids.get(0), "id");
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) JSONObject(org.codehaus.jettison.json.JSONObject) Referenceable(org.apache.atlas.typesystem.Referenceable) WebResource(com.sun.jersey.api.client.WebResource) Matchers.anyString(org.mockito.Matchers.anyString) EntityResult(org.apache.atlas.model.legacy.EntityResult) Test(org.testng.annotations.Test)

Example 8 with EntityResult

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

the class AtlasClient method deleteEntity.

/**
     * Supports Deletion of an entity identified by its unique attribute value
     * @param entityType Type of the entity being deleted
     * @param uniqueAttributeName Attribute Name that uniquely identifies the entity
     * @param uniqueAttributeValue Attribute Value that uniquely identifies the entity
     * @return List of entity ids updated/deleted(including composite references from that entity)
     */
public EntityResult deleteEntity(String entityType, String uniqueAttributeName, String uniqueAttributeValue) throws AtlasServiceException {
    LOG.debug("Deleting entity type: {}, attributeName: {}, attributeValue: {}", entityType, uniqueAttributeName, uniqueAttributeValue);
    API api = API.DELETE_ENTITY;
    WebResource resource = getResource(api);
    resource = resource.queryParam(TYPE, entityType);
    resource = resource.queryParam(ATTRIBUTE_NAME, uniqueAttributeName);
    resource = resource.queryParam(ATTRIBUTE_VALUE, uniqueAttributeValue);
    JSONObject jsonResponse = callAPIWithResource(API.DELETE_ENTITIES, resource);
    EntityResult results = extractEntityResult(jsonResponse);
    LOG.debug("Delete entities returned results: {}", results);
    return results;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) EntityResult(org.apache.atlas.model.legacy.EntityResult)

Example 9 with EntityResult

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

the class AtlasInstanceConverter method toCreateUpdateEntitiesResult.

public CreateUpdateEntitiesResult toCreateUpdateEntitiesResult(EntityMutationResponse reponse) {
    CreateUpdateEntitiesResult ret = null;
    if (reponse != null) {
        Map<EntityOperation, List<AtlasEntityHeader>> mutatedEntities = reponse.getMutatedEntities();
        Map<String, String> guidAssignments = reponse.getGuidAssignments();
        ret = new CreateUpdateEntitiesResult();
        if (MapUtils.isNotEmpty(guidAssignments)) {
            ret.setGuidMapping(new GuidMapping(guidAssignments));
        }
        if (MapUtils.isNotEmpty(mutatedEntities)) {
            EntityResult entityResult = new EntityResult();
            for (Map.Entry<EntityOperation, List<AtlasEntityHeader>> e : mutatedEntities.entrySet()) {
                switch(e.getKey()) {
                    case CREATE:
                        List<AtlasEntityHeader> createdEntities = mutatedEntities.get(EntityOperation.CREATE);
                        if (CollectionUtils.isNotEmpty(createdEntities)) {
                            Collections.reverse(createdEntities);
                            entityResult.set(EntityResult.OP_CREATED, getGuids(createdEntities));
                        }
                        break;
                    case UPDATE:
                        List<AtlasEntityHeader> updatedEntities = mutatedEntities.get(EntityOperation.UPDATE);
                        if (CollectionUtils.isNotEmpty(updatedEntities)) {
                            Collections.reverse(updatedEntities);
                            entityResult.set(EntityResult.OP_UPDATED, getGuids(updatedEntities));
                        }
                        break;
                    case PARTIAL_UPDATE:
                        List<AtlasEntityHeader> partialUpdatedEntities = mutatedEntities.get(EntityOperation.PARTIAL_UPDATE);
                        if (CollectionUtils.isNotEmpty(partialUpdatedEntities)) {
                            Collections.reverse(partialUpdatedEntities);
                            entityResult.set(EntityResult.OP_UPDATED, getGuids(partialUpdatedEntities));
                        }
                        break;
                    case DELETE:
                        List<AtlasEntityHeader> deletedEntities = mutatedEntities.get(EntityOperation.DELETE);
                        if (CollectionUtils.isNotEmpty(deletedEntities)) {
                            Collections.reverse(deletedEntities);
                            entityResult.set(EntityResult.OP_DELETED, getGuids(deletedEntities));
                        }
                        break;
                }
            }
            ret.setEntityResult(entityResult);
        }
    }
    return ret;
}
Also used : EntityOperation(org.apache.atlas.model.instance.EntityMutations.EntityOperation) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) ArrayList(java.util.ArrayList) List(java.util.List) EntityResult(org.apache.atlas.model.legacy.EntityResult) Map(java.util.Map) GuidMapping(org.apache.atlas.model.instance.GuidMapping)

Example 10 with EntityResult

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteEntityWithDuplicateReferenceListElements.

@Test
public void testDeleteEntityWithDuplicateReferenceListElements() throws Exception {
    // Create a table entity, with 2 composite column entities
    Referenceable dbEntity = createDBEntity();
    String dbGuid = createInstance(dbEntity);
    Referenceable table1Entity = createTableEntity(dbGuid);
    String tableName = TestUtils.randomString();
    table1Entity.set(NAME, tableName);
    Referenceable col1 = createColumnEntity();
    col1.set(NAME, TestUtils.randomString());
    Referenceable col2 = createColumnEntity();
    col2.set(NAME, TestUtils.randomString());
    // Populate columns reference list with duplicates.
    table1Entity.set(COLUMNS_ATTR_NAME, ImmutableList.of(col1, col2, col1, col2));
    ClassType dataType = typeSystem.getDataType(ClassType.class, table1Entity.getTypeName());
    ITypedReferenceableInstance instance = dataType.convert(table1Entity, Multiplicity.REQUIRED);
    TestUtils.resetRequestContext();
    List<String> result = repositoryService.createEntities(instance).getCreatedEntities();
    Assert.assertEquals(result.size(), 3);
    ITypedReferenceableInstance entityDefinition = repositoryService.getEntityDefinition(TABLE_TYPE, NAME, tableName);
    String tableGuid = entityDefinition.getId()._getId();
    Object attrValue = entityDefinition.get(COLUMNS_ATTR_NAME);
    assertTrue(attrValue instanceof List);
    List<ITypedReferenceableInstance> columns = (List<ITypedReferenceableInstance>) attrValue;
    Assert.assertEquals(columns.size(), 4);
    TestUtils.resetRequestContext();
    String columnGuid = columns.get(0).getId()._getId();
    // Delete one of the columns.
    EntityResult deleteResult = repositoryService.deleteEntities(Collections.singletonList(columnGuid));
    Assert.assertEquals(deleteResult.getDeletedEntities().size(), 1);
    Assert.assertTrue(deleteResult.getDeletedEntities().contains(columnGuid));
    Assert.assertEquals(deleteResult.getUpdateEntities().size(), 1);
    Assert.assertTrue(deleteResult.getUpdateEntities().contains(tableGuid));
    // Verify the duplicate edge IDs were all removed from reference property list.
    AtlasVertex tableVertex = GraphHelper.getInstance().getVertexForGUID(tableGuid);
    String columnsPropertyName = GraphHelper.getQualifiedFieldName(dataType, COLUMNS_ATTR_NAME);
    List columnsPropertyValue = tableVertex.getProperty(columnsPropertyName, List.class);
    verifyTestDeleteEntityWithDuplicateReferenceListElements(columnsPropertyValue);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) 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