Search in sources :

Example 41 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method testDisconnectMapReferenceFromClassType.

/**
     * Verify deleting entities that are the target of class map references.
     */
@Test
public void testDisconnectMapReferenceFromClassType() throws Exception {
    // Define type for map value.
    HierarchicalTypeDefinition<ClassType> mapValueDef = TypesUtil.createClassTypeDef("MapValue", ImmutableSet.<String>of(), new AttributeDefinition("biMapOwner", "MapOwner", Multiplicity.OPTIONAL, false, "biMap"));
    // Define type with unidirectional and bidirectional map references,
    // where the map value is a class reference to MapValue.
    HierarchicalTypeDefinition<ClassType> mapOwnerDef = TypesUtil.createClassTypeDef("MapOwner", ImmutableSet.<String>of(), new AttributeDefinition("map", DataTypes.mapTypeName(DataTypes.STRING_TYPE.getName(), "MapValue"), Multiplicity.OPTIONAL, false, null), new AttributeDefinition("biMap", DataTypes.mapTypeName(DataTypes.STRING_TYPE.getName(), "MapValue"), Multiplicity.OPTIONAL, false, "biMapOwner"));
    TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.of(mapOwnerDef, mapValueDef));
    typeSystem.defineTypes(typesDef);
    ClassType mapOwnerType = typeSystem.getDataType(ClassType.class, "MapOwner");
    ClassType mapValueType = typeSystem.getDataType(ClassType.class, "MapValue");
    // Create instances of MapOwner and MapValue.
    // Set MapOwner.map and MapOwner.biMap with one entry that references MapValue instance.
    ITypedReferenceableInstance mapOwnerInstance = mapOwnerType.createInstance();
    ITypedReferenceableInstance mapValueInstance = mapValueType.createInstance();
    mapOwnerInstance.set("map", Collections.singletonMap("value1", mapValueInstance));
    mapOwnerInstance.set("biMap", Collections.singletonMap("value1", mapValueInstance));
    // Set biMapOwner reverse reference on MapValue.
    mapValueInstance.set("biMapOwner", mapOwnerInstance);
    List<String> createEntitiesResult = repositoryService.createEntities(mapOwnerInstance, mapValueInstance).getCreatedEntities();
    Assert.assertEquals(createEntitiesResult.size(), 2);
    List<String> guids = repositoryService.getEntityList("MapOwner");
    Assert.assertEquals(guids.size(), 1);
    String mapOwnerGuid = guids.get(0);
    String edgeLabel = GraphHelper.getEdgeLabel(mapOwnerType, mapOwnerType.fieldMapping.fields.get("map"));
    String mapEntryLabel = edgeLabel + "." + "value1";
    AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
    // Verify MapOwner.map attribute has expected value.
    String mapValueGuid = null;
    AtlasVertex mapOwnerVertex = null;
    mapOwnerInstance = repositoryService.getEntityDefinition(mapOwnerGuid);
    for (String mapAttrName : Arrays.asList("map", "biMap")) {
        Object object = mapOwnerInstance.get(mapAttrName);
        Assert.assertNotNull(object);
        Assert.assertTrue(object instanceof Map);
        Map<String, ITypedReferenceableInstance> map = (Map<String, ITypedReferenceableInstance>) object;
        Assert.assertEquals(map.size(), 1);
        mapValueInstance = map.get("value1");
        Assert.assertNotNull(mapValueInstance);
        mapValueGuid = mapValueInstance.getId()._getId();
        mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
        object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
        Assert.assertNotNull(object);
    }
    // Delete the map value instance.
    // This should disconnect the references from the map owner instance.
    deleteEntities(mapValueGuid);
    assertEntityDeleted(mapValueGuid);
    assertTestDisconnectMapReferenceFromClassType(mapOwnerGuid);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) TypesDef(org.apache.atlas.typesystem.TypesDef) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 42 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method getVertices.

protected List<AtlasVertex> getVertices(String propertyName, Object value) {
    AtlasGraph graph = TestUtils.getGraph();
    Iterable<AtlasVertex> vertices = graph.getVertices(propertyName, value);
    List<AtlasVertex> list = new ArrayList<>();
    for (AtlasVertex vertex : vertices) {
        list.add(vertex);
    }
    return list;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ArrayList(java.util.ArrayList) AtlasGraph(org.apache.atlas.repository.graphdb.AtlasGraph)

Example 43 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryTest method assertEdge.

private boolean assertEdge(String id, String typeName) throws Exception {
    AtlasGraph graph = TestUtils.getGraph();
    Iterable<AtlasVertex> vertices = graph.query().has(Constants.GUID_PROPERTY_KEY, id).vertices();
    AtlasVertex AtlasVertex = vertices.iterator().next();
    Iterable<AtlasEdge> edges = AtlasVertex.getEdges(AtlasEdgeDirection.OUT, Constants.INTERNAL_PROPERTY_KEY_PREFIX + typeName + ".ref");
    if (!edges.iterator().hasNext()) {
        ITypedReferenceableInstance entity = repositoryService.getEntityDefinition(id);
        assertNotNull(entity.get("ref"));
        return true;
    }
    return false;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AtlasGraph(org.apache.atlas.repository.graphdb.AtlasGraph) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 44 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class GraphBackedRepositoryHardDeleteTest method assertTestDisconnectMapReferenceFromClassType.

@Override
protected void assertTestDisconnectMapReferenceFromClassType(String mapOwnerGuid) throws Exception {
    // Verify map references from mapOwner were disconnected.
    ITypedReferenceableInstance mapOwnerInstance = repositoryService.getEntityDefinition(mapOwnerGuid);
    assertNull(mapOwnerInstance.get("map"));
    assertNull(mapOwnerInstance.get("biMap"));
    AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
    Object object = mapOwnerVertex.getProperty("MapOwner.map.value1", String.class);
    assertNull(object);
    object = mapOwnerVertex.getProperty("MapOwner.biMap.value1", String.class);
    assertNull(object);
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance)

Example 45 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryTest method testGetTypeName.

@Test(dependsOnMethods = "testCreateEntity")
public void testGetTypeName() throws Exception {
    AtlasVertex tableVertex = getTableEntityVertex();
    Assert.assertEquals(GraphHelper.getTypeName(tableVertex), TestUtils.TABLE_TYPE);
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) Test(org.testng.annotations.Test)

Aggregations

AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)164 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)53 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)26 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)21 ArrayList (java.util.ArrayList)20 Test (org.testng.annotations.Test)19 HashMap (java.util.HashMap)16 Id (org.apache.atlas.typesystem.persistence.Id)14 Map (java.util.Map)13 List (java.util.List)12 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)12 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)12 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)12 AtlasException (org.apache.atlas.AtlasException)11 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)11 RepositoryException (org.apache.atlas.repository.RepositoryException)11 AtlasGraphQuery (org.apache.atlas.repository.graphdb.AtlasGraphQuery)11 AtlasType (org.apache.atlas.type.AtlasType)11 HashSet (java.util.HashSet)8 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)8