Search in sources :

Example 21 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method getDepartmentGuid.

private String getDepartmentGuid(List<String> guids) throws RepositoryException, EntityNotFoundException {
    String hrDeptGuid = null;
    for (String guid : guids) {
        ITypedReferenceableInstance entityDefinition = repositoryService.getEntityDefinition(guid);
        Id id = entityDefinition.getId();
        if (id.getTypeName().equals("Department")) {
            hrDeptGuid = id._getId();
            break;
        }
    }
    if (hrDeptGuid == null) {
        Assert.fail("Entity for type Department not found");
    }
    return hrDeptGuid;
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id)

Example 22 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method testDisconnectBidirectionalReferences.

/**
 * Verify deleting an entity which is contained by another
 * entity through a bi-directional composite reference.
 *
 * @throws Exception
 */
@Test
public void testDisconnectBidirectionalReferences() throws Exception {
    String hrDeptGuid = createHrDeptGraph();
    ITypedReferenceableInstance hrDept = repositoryService.getEntityDefinition(hrDeptGuid);
    Map<String, String> nameGuidMap = getEmployeeNameGuidMap(hrDept);
    String maxGuid = nameGuidMap.get("Max");
    String janeGuid = nameGuidMap.get("Jane");
    String johnGuid = nameGuidMap.get("John");
    Assert.assertNotNull(maxGuid);
    Assert.assertNotNull(janeGuid);
    Assert.assertNotNull(johnGuid);
    // Verify that Max is one of Jane's subordinates.
    ITypedReferenceableInstance jane = repositoryService.getEntityDefinition(janeGuid);
    Object refValue = jane.get("subordinates");
    Assert.assertTrue(refValue instanceof List);
    List<Object> subordinates = (List<Object>) refValue;
    Assert.assertEquals(subordinates.size(), 2);
    List<String> subordinateIds = new ArrayList<>(2);
    for (Object listValue : subordinates) {
        Assert.assertTrue(listValue instanceof ITypedReferenceableInstance);
        ITypedReferenceableInstance employee = (ITypedReferenceableInstance) listValue;
        subordinateIds.add(employee.getId()._getId());
    }
    Assert.assertTrue(subordinateIds.contains(maxGuid));
    EntityResult entityResult = deleteEntities(maxGuid);
    ITypedReferenceableInstance john = repositoryService.getEntityDefinition("Person", "name", "John");
    assertEquals(entityResult.getDeletedEntities().size(), 1);
    assertTrue(entityResult.getDeletedEntities().contains(maxGuid));
    assertEquals(entityResult.getUpdateEntities().size(), 3);
    assertTrue(entityResult.getUpdateEntities().containsAll(Arrays.asList(jane.getId()._getId(), hrDeptGuid, john.getId()._getId())));
    assertEntityDeleted(maxGuid);
    assertMaxForTestDisconnectBidirectionalReferences(nameGuidMap);
    // Now delete jane - this should disconnect the manager reference from her
    // subordinate.
    entityResult = deleteEntities(janeGuid);
    assertEquals(entityResult.getDeletedEntities().size(), 1);
    assertTrue(entityResult.getDeletedEntities().contains(janeGuid));
    assertEquals(entityResult.getUpdateEntities().size(), 2);
    assertTrue(entityResult.getUpdateEntities().containsAll(Arrays.asList(hrDeptGuid, john.getId()._getId())));
    assertEntityDeleted(janeGuid);
    john = repositoryService.getEntityDefinition("Person", "name", "John");
    assertJohnForTestDisconnectBidirectionalReferences(john, janeGuid);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) 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)

Example 23 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method createDbTableGraph.

private void createDbTableGraph(String dbName, String tableName) throws Exception {
    Referenceable databaseInstance = new Referenceable(TestUtils.DATABASE_TYPE);
    databaseInstance.set("name", dbName);
    databaseInstance.set("description", "foo database");
    ClassType dbType = typeSystem.getDataType(ClassType.class, TestUtils.DATABASE_TYPE);
    ITypedReferenceableInstance db = dbType.convert(databaseInstance, Multiplicity.REQUIRED);
    Referenceable tableInstance = new Referenceable(TestUtils.TABLE_TYPE, TestUtils.CLASSIFICATION);
    tableInstance.set("name", tableName);
    tableInstance.set("description", "bar table");
    tableInstance.set("type", "managed");
    Struct traitInstance = (Struct) tableInstance.getTrait(TestUtils.CLASSIFICATION);
    traitInstance.set("tag", "foundation_etl");
    // enum
    tableInstance.set("tableType", 1);
    tableInstance.set("database", databaseInstance);
    ArrayList<Referenceable> columns = new ArrayList<>();
    for (int index = 0; index < 5; index++) {
        Referenceable columnInstance = new Referenceable("column_type");
        final String name = "column_" + index;
        columnInstance.set("name", name);
        columnInstance.set("type", "string");
        columns.add(columnInstance);
    }
    tableInstance.set("columns", columns);
    ClassType tableType = typeSystem.getDataType(ClassType.class, TestUtils.TABLE_TYPE);
    ITypedReferenceableInstance table = tableType.convert(tableInstance, Multiplicity.REQUIRED);
    repositoryService.createEntities(db, table);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Example 24 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance 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)

Example 25 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteEntitiesWithCompositeMapReference.

@Test
public void testDeleteEntitiesWithCompositeMapReference() throws Exception {
    // Create instances of MapOwner and MapValue.
    // 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();
    String edgeLabel = GraphHelper.getEdgeLabel(compositeMapOwnerType, compositeMapOwnerType.fieldMapping.fields.get("map"));
    String mapEntryLabel = edgeLabel + "." + "value1";
    AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
    AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
    object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
    Assert.assertNotNull(object);
    List<String> deletedEntities = deleteEntities(mapOwnerGuid).getDeletedEntities();
    Assert.assertEquals(deletedEntities.size(), 2);
    Assert.assertTrue(deletedEntities.contains(mapOwnerGuid));
    Assert.assertTrue(deletedEntities.contains(mapValueGuid));
    assertEntityDeleted(mapOwnerGuid);
    assertEntityDeleted(mapValueGuid);
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)142 Test (org.testng.annotations.Test)54 List (java.util.List)34 ArrayList (java.util.ArrayList)30 Id (org.apache.atlas.typesystem.persistence.Id)28 Referenceable (org.apache.atlas.typesystem.Referenceable)22 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)21 ImmutableList (com.google.common.collect.ImmutableList)20 ClassType (org.apache.atlas.typesystem.types.ClassType)19 HashMap (java.util.HashMap)16 AtlasException (org.apache.atlas.AtlasException)16 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)16 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)14 EntityResult (org.apache.atlas.model.legacy.EntityResult)12 IStruct (org.apache.atlas.typesystem.IStruct)10 Map (java.util.Map)9 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)9 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 RepositoryException (org.apache.atlas.repository.RepositoryException)9 BeforeTest (org.testng.annotations.BeforeTest)9