Search in sources :

Example 26 with EntityResult

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

the class EntityResource method getResponse.

private JSONObject getResponse(CreateUpdateEntitiesResult result) throws AtlasException, JSONException {
    JSONObject response = new JSONObject();
    EntityResult entityResult = result.getEntityResult();
    GuidMapping mapping = result.getGuidMapping();
    response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
    if (entityResult != null) {
        response.put(AtlasClient.ENTITIES, new JSONObject(entityResult.toString()).get(AtlasClient.ENTITIES));
        String sampleEntityId = getSample(result.getEntityResult());
        if (sampleEntityId != null) {
            String entityDefinition = metadataService.getEntityDefinitionJson(sampleEntityId);
            response.put(AtlasClient.DEFINITION, new JSONObject(entityDefinition));
        }
    }
    if (mapping != null) {
        response.put(AtlasClient.GUID_ASSIGNMENTS, new JSONObject(AtlasType.toJson(mapping)).get(AtlasClient.GUID_ASSIGNMENTS));
    }
    return response;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) EntityResult(org.apache.atlas.model.legacy.EntityResult) GuidMapping(org.apache.atlas.model.instance.GuidMapping)

Example 27 with EntityResult

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes.

/**
     * Verify deleting entities that are the target of a unidirectional class array reference
     * from a struct or trait instance.
     */
@Test
public void testDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes() throws Exception {
    // Define class types.
    HierarchicalTypeDefinition<ClassType> structTargetDef = TypesUtil.createClassTypeDef("StructTarget", ImmutableSet.<String>of(), TypesUtil.createOptionalAttrDef("attr1", DataTypes.STRING_TYPE));
    HierarchicalTypeDefinition<ClassType> traitTargetDef = TypesUtil.createClassTypeDef("TraitTarget", ImmutableSet.<String>of(), TypesUtil.createOptionalAttrDef("attr1", DataTypes.STRING_TYPE));
    HierarchicalTypeDefinition<ClassType> structContainerDef = TypesUtil.createClassTypeDef("StructContainer", ImmutableSet.<String>of(), TypesUtil.createOptionalAttrDef("struct", "TestStruct"));
    // Define struct and trait types which have a unidirectional array reference
    // to a class type.
    StructTypeDefinition structDef = TypesUtil.createStructTypeDef("TestStruct", new AttributeDefinition("target", DataTypes.arrayTypeName("StructTarget"), Multiplicity.OPTIONAL, false, null), new AttributeDefinition("nestedStructs", DataTypes.arrayTypeName("NestedStruct"), Multiplicity.OPTIONAL, false, null));
    StructTypeDefinition nestedStructDef = TypesUtil.createStructTypeDef("NestedStruct", TypesUtil.createOptionalAttrDef("attr1", DataTypes.STRING_TYPE));
    HierarchicalTypeDefinition<TraitType> traitDef = TypesUtil.createTraitTypeDef("TestTrait", ImmutableSet.<String>of(), new AttributeDefinition("target", DataTypes.arrayTypeName("TraitTarget"), Multiplicity.OPTIONAL, false, null));
    TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.of(structDef, nestedStructDef), ImmutableList.of(traitDef), ImmutableList.of(structTargetDef, traitTargetDef, structContainerDef));
    typeSystem.defineTypes(typesDef);
    // Create instances of class, struct, and trait types.
    Referenceable structTargetEntity = new Referenceable("StructTarget");
    Referenceable traitTargetEntity = new Referenceable("TraitTarget");
    Referenceable structContainerEntity = new Referenceable("StructContainer");
    Struct structInstance = new Struct("TestStruct");
    Struct nestedStructInstance = new Struct("NestedStruct");
    Referenceable traitInstance = new Referenceable("TestTrait");
    structContainerEntity.set("struct", structInstance);
    structInstance.set("target", ImmutableList.of(structTargetEntity));
    structInstance.set("nestedStructs", ImmutableList.of(nestedStructInstance));
    ClassType structTargetType = typeSystem.getDataType(ClassType.class, "StructTarget");
    ClassType traitTargetType = typeSystem.getDataType(ClassType.class, "TraitTarget");
    ClassType structContainerType = typeSystem.getDataType(ClassType.class, "StructContainer");
    ITypedReferenceableInstance structTargetConvertedEntity = structTargetType.convert(structTargetEntity, Multiplicity.REQUIRED);
    ITypedReferenceableInstance traitTargetConvertedEntity = traitTargetType.convert(traitTargetEntity, Multiplicity.REQUIRED);
    ITypedReferenceableInstance structContainerConvertedEntity = structContainerType.convert(structContainerEntity, Multiplicity.REQUIRED);
    List<String> guids = repositoryService.createEntities(structTargetConvertedEntity, traitTargetConvertedEntity, structContainerConvertedEntity).getCreatedEntities();
    Assert.assertEquals(guids.size(), 3);
    guids = repositoryService.getEntityList("StructTarget");
    Assert.assertEquals(guids.size(), 1);
    String structTargetGuid = guids.get(0);
    guids = repositoryService.getEntityList("TraitTarget");
    Assert.assertEquals(guids.size(), 1);
    String traitTargetGuid = guids.get(0);
    guids = repositoryService.getEntityList("StructContainer");
    Assert.assertEquals(guids.size(), 1);
    String structContainerGuid = guids.get(0);
    // Add TestTrait to StructContainer instance
    traitInstance.set("target", ImmutableList.of(new Id(traitTargetGuid, 0, "TraitTarget")));
    TraitType traitType = typeSystem.getDataType(TraitType.class, "TestTrait");
    ITypedStruct convertedTrait = traitType.convert(traitInstance, Multiplicity.REQUIRED);
    repositoryService.addTrait(structContainerGuid, convertedTrait);
    // Verify that the unidirectional references from the struct and trait instances
    // are pointing at the target entities.
    structContainerConvertedEntity = repositoryService.getEntityDefinition(structContainerGuid);
    Object object = structContainerConvertedEntity.get("struct");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof ITypedStruct);
    ITypedStruct struct = (ITypedStruct) object;
    object = struct.get("target");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof List);
    List<ITypedReferenceableInstance> refList = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), structTargetGuid);
    IStruct trait = structContainerConvertedEntity.getTrait("TestTrait");
    Assert.assertNotNull(trait);
    object = trait.get("target");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof List);
    refList = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), traitTargetGuid);
    // Delete the entities that are targets of the struct and trait instances.
    EntityResult entityResult = deleteEntities(structTargetGuid, traitTargetGuid);
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 2);
    Assert.assertTrue(entityResult.getDeletedEntities().containsAll(Arrays.asList(structTargetGuid, traitTargetGuid)));
    assertEntityDeleted(structTargetGuid);
    assertEntityDeleted(traitTargetGuid);
    assertTestDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes(structContainerGuid);
    // Delete the entity which contains nested structs and has the TestTrait trait.
    entityResult = deleteEntities(structContainerGuid);
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 1);
    Assert.assertTrue(entityResult.getDeletedEntities().contains(structContainerGuid));
    assertEntityDeleted(structContainerGuid);
    // Verify all TestStruct struct vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "TestStruct"));
    // Verify all NestedStruct struct vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "NestedStruct"));
    // Verify all TestTrait trait vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "TestTrait"));
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) EntityResult(org.apache.atlas.model.legacy.EntityResult) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) TypesDef(org.apache.atlas.typesystem.TypesDef) Referenceable(org.apache.atlas.typesystem.Referenceable) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Id(org.apache.atlas.typesystem.persistence.Id) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test)

Example 28 with EntityResult

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteReference.

@Test
public void testDeleteReference() throws Exception {
    //Deleting column should update table
    Referenceable db = createDBEntity();
    String dbId = createInstance(db);
    Referenceable column = createColumnEntity();
    String colId = createInstance(column);
    Referenceable table = createTableEntity(dbId);
    table.set(COLUMNS_ATTR_NAME, Arrays.asList(new Id(colId, 0, COLUMN_TYPE)));
    String tableId = createInstance(table);
    EntityResult entityResult = deleteEntities(colId);
    assertEquals(entityResult.getDeletedEntities().size(), 1);
    assertEquals(entityResult.getDeletedEntities().get(0), colId);
    assertEquals(entityResult.getUpdateEntities().size(), 1);
    assertEquals(entityResult.getUpdateEntities().get(0), tableId);
    assertEntityDeleted(colId);
    ITypedReferenceableInstance tableInstance = repositoryService.getEntityDefinition(tableId);
    assertColumnForTestDeleteReference(tableInstance);
    //Deleting table should update process
    Referenceable process = new Referenceable(PROCESS_TYPE);
    process.set(AtlasClient.PROCESS_ATTRIBUTE_OUTPUTS, Arrays.asList(new Id(tableId, 0, TABLE_TYPE)));
    String processId = createInstance(process);
    ITypedReferenceableInstance processInstance = repositoryService.getEntityDefinition(processId);
    deleteEntities(tableId);
    assertEntityDeleted(tableId);
    assertTableForTestDeleteReference(tableId);
    assertProcessForTestDeleteReference(processInstance);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id) EntityResult(org.apache.atlas.model.legacy.EntityResult) Test(org.testng.annotations.Test)

Example 29 with EntityResult

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testLowerBoundsIgnoredWhenDeletingCompositeEntitesOwnedByMap.

@Test
public void testLowerBoundsIgnoredWhenDeletingCompositeEntitesOwnedByMap() throws Exception {
    // Define MapValueReferencer type with required reference to CompositeMapValue.
    HierarchicalTypeDefinition<ClassType> mapValueReferencerTypeDef = TypesUtil.createClassTypeDef("MapValueReferencer", ImmutableSet.<String>of(), new AttributeDefinition("refToMapValue", "CompositeMapValue", Multiplicity.REQUIRED, false, null));
    // Define MapValueReferencerContainer type with required composite map reference to MapValueReferencer.
    HierarchicalTypeDefinition<ClassType> mapValueReferencerContainerTypeDef = TypesUtil.createClassTypeDef("MapValueReferencerContainer", ImmutableSet.<String>of(), new AttributeDefinition("requiredMap", DataTypes.mapTypeName(DataTypes.STRING_TYPE.getName(), "MapValueReferencer"), Multiplicity.REQUIRED, true, null));
    Map<String, IDataType> definedClassTypes = typeSystem.defineClassTypes(mapValueReferencerTypeDef, mapValueReferencerContainerTypeDef);
    ClassType mapValueReferencerClassType = (ClassType) definedClassTypes.get("MapValueReferencer");
    ClassType mapValueReferencerContainerType = (ClassType) definedClassTypes.get("MapValueReferencerContainer");
    // Create instances of CompositeMapOwner and CompositeMapValue.
    // Set MapOwner.map with one entry that references MapValue instance.
    ITypedReferenceableInstance entityDefinition = createMapOwnerAndValueEntities();
    String mapOwnerGuid = entityDefinition.getId()._getId();
    // Verify MapOwner.map attribute has expected value.
    ITypedReferenceableInstance mapOwnerInstance = repositoryService.getEntityDefinition(mapOwnerGuid);
    Object object = mapOwnerInstance.get("map");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof Map);
    Map<String, ITypedReferenceableInstance> map = (Map<String, ITypedReferenceableInstance>) object;
    Assert.assertEquals(map.size(), 1);
    ITypedReferenceableInstance mapValueInstance = map.get("value1");
    Assert.assertNotNull(mapValueInstance);
    String mapValueGuid = mapValueInstance.getId()._getId();
    // Create instance of MapValueReferencerContainer
    RequestContext.createContext();
    ITypedReferenceableInstance mapValueReferencerContainer = mapValueReferencerContainerType.createInstance();
    List<String> createdEntities = repositoryService.createEntities(mapValueReferencerContainer).getCreatedEntities();
    Assert.assertEquals(createdEntities.size(), 1);
    String mapValueReferencerContainerGuid = createdEntities.get(0);
    mapValueReferencerContainer = repositoryService.getEntityDefinition(createdEntities.get(0));
    // Create instance of MapValueReferencer, and update mapValueReferencerContainer
    // to reference it.
    ITypedReferenceableInstance mapValueReferencer = mapValueReferencerClassType.createInstance();
    mapValueReferencerContainer.set("requiredMap", Collections.singletonMap("value1", mapValueReferencer));
    mapValueReferencer.set("refToMapValue", mapValueInstance.getId());
    RequestContext.createContext();
    EntityResult updateEntitiesResult = repositoryService.updateEntities(mapValueReferencerContainer).getEntityResult();
    Assert.assertEquals(updateEntitiesResult.getCreatedEntities().size(), 1);
    Assert.assertEquals(updateEntitiesResult.getUpdateEntities().size(), 1);
    Assert.assertEquals(updateEntitiesResult.getUpdateEntities().get(0), mapValueReferencerContainerGuid);
    String mapValueReferencerGuid = updateEntitiesResult.getCreatedEntities().get(0);
    // Delete map owner and map referencer container.  A total of 4 entities should be deleted,
    // including the composite entities.  The lower bound constraint on MapValueReferencer.refToMapValue
    // should not be enforced on the composite MapValueReferencer since it is being deleted.
    EntityResult deleteEntitiesResult = repositoryService.deleteEntities(Arrays.asList(mapOwnerGuid, mapValueReferencerContainerGuid));
    Assert.assertEquals(deleteEntitiesResult.getDeletedEntities().size(), 4);
    Assert.assertTrue(deleteEntitiesResult.getDeletedEntities().containsAll(Arrays.asList(mapOwnerGuid, mapValueGuid, mapValueReferencerContainerGuid, mapValueReferencerGuid)));
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityResult(org.apache.atlas.model.legacy.EntityResult) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 30 with EntityResult

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteEntities.

@Test
public void testDeleteEntities() throws Exception {
    // Create a table entity, with 3 composite column entities
    Referenceable dbEntity = createDBEntity();
    String dbGuid = createInstance(dbEntity);
    Referenceable table1Entity = createTableEntity(dbGuid);
    Referenceable col1 = createColumnEntity();
    Referenceable col2 = createColumnEntity();
    Referenceable col3 = createColumnEntity();
    table1Entity.set(COLUMNS_ATTR_NAME, ImmutableList.of(col1, col2, col3));
    createInstance(table1Entity);
    // Retrieve the table entities from the Repository, to get their guids and the composite column guids.
    ITypedReferenceableInstance tableInstance = repositoryService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, table1Entity.get(NAME));
    List<IReferenceableInstance> columns = (List<IReferenceableInstance>) tableInstance.get(COLUMNS_ATTR_NAME);
    //Delete column
    String colId = columns.get(0).getId()._getId();
    String tableId = tableInstance.getId()._getId();
    EntityResult entityResult = deleteEntities(colId);
    assertEquals(entityResult.getDeletedEntities().size(), 1);
    assertEquals(entityResult.getDeletedEntities().get(0), colId);
    assertEquals(entityResult.getUpdateEntities().size(), 1);
    assertEquals(entityResult.getUpdateEntities().get(0), tableId);
    assertEntityDeleted(colId);
    tableInstance = repositoryService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, table1Entity.get(NAME));
    assertDeletedColumn(tableInstance);
    //update by removing a column
    tableInstance.set(COLUMNS_ATTR_NAME, ImmutableList.of(col3));
    entityResult = updatePartial(tableInstance);
    colId = columns.get(1).getId()._getId();
    assertEquals(entityResult.getDeletedEntities().size(), 1);
    assertEquals(entityResult.getDeletedEntities().get(0), colId);
    assertEntityDeleted(colId);
    // Delete the table entities.  The deletion should cascade to their composite columns.
    tableInstance = repositoryService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, table1Entity.get(NAME));
    List<String> deletedGuids = deleteEntities(tableInstance.getId()._getId()).getDeletedEntities();
    assertEquals(deletedGuids.size(), 2);
    // Verify that deleteEntities() response has guids for tables and their composite columns.
    Assert.assertTrue(deletedGuids.contains(tableInstance.getId()._getId()));
    Assert.assertTrue(deletedGuids.contains(columns.get(2).getId()._getId()));
    // Verify that tables and their composite columns have been deleted from the graph Repository.
    for (String guid : deletedGuids) {
        assertEntityDeleted(guid);
    }
    assertTestDeleteEntities(tableInstance);
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) Referenceable(org.apache.atlas.typesystem.Referenceable) 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