Search in sources :

Example 16 with IReferenceableInstance

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

Example 17 with IReferenceableInstance

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

the class EntityNotificationIT method testAddTrait.

public void testAddTrait() throws Exception {
    String superSuperTraitName = "SuperTrait" + randomString();
    createTrait(superSuperTraitName);
    String superTraitName = "SuperTrait" + randomString();
    createTrait(superTraitName, superSuperTraitName);
    traitName = "Trait" + randomString();
    createTrait(traitName, superTraitName);
    Struct traitInstance = new Struct(traitName);
    String traitInstanceJSON = InstanceSerialization.toJson(traitInstance, true);
    LOG.debug("Trait instance = {}", traitInstanceJSON);
    final String guid = tableId._getId();
    atlasClientV1.addTrait(guid, traitInstance);
    EntityNotification entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(EntityNotification.OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
    IReferenceableInstance entity = entityNotification.getEntity();
    assertTrue(entity.getTraits().contains(traitName));
    List<IStruct> allTraits = entityNotification.getAllTraits();
    List<String> allTraitNames = new LinkedList<>();
    for (IStruct struct : allTraits) {
        allTraitNames.add(struct.getTypeName());
    }
    assertTrue(allTraitNames.contains(traitName));
    assertTrue(allTraitNames.contains(superTraitName));
    assertTrue(allTraitNames.contains(superSuperTraitName));
    String anotherTraitName = "Trait" + randomString();
    createTrait(anotherTraitName, superTraitName);
    traitInstance = new Struct(anotherTraitName);
    traitInstanceJSON = InstanceSerialization.toJson(traitInstance, true);
    LOG.debug("Trait instance = {}", traitInstanceJSON);
    atlasClientV1.addTrait(guid, traitInstance);
    entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(EntityNotification.OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
    allTraits = entityNotification.getAllTraits();
    allTraitNames = new LinkedList<>();
    for (IStruct struct : allTraits) {
        allTraitNames.add(struct.getTypeName());
    }
    assertTrue(allTraitNames.contains(traitName));
    assertTrue(allTraitNames.contains(anotherTraitName));
    // verify that the super type shows up twice in all traits
    assertEquals(2, Collections.frequency(allTraitNames, superTraitName));
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) EntityNotification(org.apache.atlas.notification.entity.EntityNotification) LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) IStruct(org.apache.atlas.typesystem.IStruct)

Example 18 with IReferenceableInstance

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

the class EntityProcessor method processNode.

@Override
public void processNode(ObjectGraphWalker.Node nd) throws AtlasException {
    IReferenceableInstance ref = null;
    Id id = null;
    if (nd.attributeName == null) {
        ref = (IReferenceableInstance) nd.instance;
        id = ref.getId();
    } else if (nd.aInfo.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) {
        if (nd.value != null && (nd.value instanceof Id)) {
            id = (Id) nd.value;
        }
    }
    if (id != null) {
        if (id.isUnassigned()) {
            if (ref != null) {
                if (idToInstanceMap.containsKey(id)) {
                    // Oops
                    throw new RepositoryException(String.format("Unexpected internal error: Id %s processed again", id));
                }
                idToInstanceMap.put(id, ref);
            }
        }
    }
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) RepositoryException(org.apache.atlas.repository.RepositoryException) Id(org.apache.atlas.typesystem.persistence.Id)

Example 19 with IReferenceableInstance

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

the class EnumTest method testClass.

@Test
public void testClass() throws AtlasException {
    TypeSystem ts = getTypeSystem();
    defineEnums(ts);
    ClassType clsType = defineClassTypeWithEnum(ts);
    IReferenceableInstance r = createInstanceWithEnum("t4");
    ITypedReferenceableInstance typedR = clsType.convert(r, Multiplicity.REQUIRED);
    Assert.assertEquals(typedR.toString(), "{\n" + "\tid : (type: t4, id: <unassigned>)\n" + "\ta : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\td : \t2\n" + "\tenum1 : \tGLOBAL\n" + "\te : \t1\n" + "\tf : \t1\n" + "\tg : \t1\n" + "\tenum2 : \tUSER\n" + "\th : \t1.0\n" + "\ti : \t1.0\n" + "\tj : \t1\n" + "\tk : \t1\n" + "\tenum3 : \tCOMMITTED\n" + "\tl : \t" + TEST_DATE + "\n" + "\tm : \t[1, 1]\n" + "\tn : \t[1.1, 1.1]\n" + "\to : \t{a=1.0, b=2.0}\n" + "\tenum4 : \tPARTITION\n" + "}");
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Test(org.testng.annotations.Test)

Example 20 with IReferenceableInstance

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

the class AtlasDeleteHandlerV1Test method testDeleteEntities.

@Test
public void testDeleteEntities() throws Exception {
    // Create a table entity, with 3 composite column entities
    init();
    final AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    EntityMutationResponse dbCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(dbEntity), false);
    final AtlasEntity tableEntity = TestUtilsV2.createTableEntity(dbEntity);
    AtlasEntity.AtlasEntitiesWithExtInfo entitiesInfo = new AtlasEntity.AtlasEntitiesWithExtInfo(tableEntity);
    final AtlasEntity columnEntity1 = TestUtilsV2.createColumnEntity(tableEntity);
    entitiesInfo.addReferredEntity(columnEntity1);
    final AtlasEntity columnEntity2 = TestUtilsV2.createColumnEntity(tableEntity);
    entitiesInfo.addReferredEntity(columnEntity2);
    final AtlasEntity columnEntity3 = TestUtilsV2.createColumnEntity(tableEntity);
    entitiesInfo.addReferredEntity(columnEntity3);
    tableEntity.setAttribute(COLUMNS_ATTR_NAME, Arrays.asList(AtlasTypeUtil.getAtlasObjectId(columnEntity1), AtlasTypeUtil.getAtlasObjectId(columnEntity2), AtlasTypeUtil.getAtlasObjectId(columnEntity3)));
    init();
    final EntityMutationResponse tblCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    final AtlasEntityHeader column1Created = tblCreationResponse.getCreatedEntityByTypeNameAndAttribute(COLUMN_TYPE, NAME, (String) columnEntity1.getAttribute(NAME));
    final AtlasEntityHeader column2Created = tblCreationResponse.getCreatedEntityByTypeNameAndAttribute(COLUMN_TYPE, NAME, (String) columnEntity2.getAttribute(NAME));
    final AtlasEntityHeader column3Created = tblCreationResponse.getCreatedEntityByTypeNameAndAttribute(COLUMN_TYPE, NAME, (String) columnEntity3.getAttribute(NAME));
    // Retrieve the table entities from the Repository, to get their guids and the composite column guids.
    ITypedReferenceableInstance tableInstance = metadataService.getEntityDefinitionReference(TestUtils.TABLE_TYPE, NAME, (String) tableEntity.getAttribute(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();
    init();
    EntityMutationResponse deletionResponse = entityStore.deleteById(colId);
    assertEquals(deletionResponse.getDeletedEntities().size(), 1);
    assertEquals(deletionResponse.getDeletedEntities().get(0).getGuid(), colId);
    assertEquals(deletionResponse.getUpdatedEntities().size(), 1);
    assertEquals(deletionResponse.getUpdatedEntities().get(0).getGuid(), tableId);
    assertEntityDeleted(colId);
    final AtlasEntity.AtlasEntityWithExtInfo tableEntityCreated = entityStore.getById(tableId);
    assertDeletedColumn(tableEntityCreated);
    assertTestDisconnectUnidirectionalArrayReferenceFromClassType((List<AtlasObjectId>) tableEntityCreated.getEntity().getAttribute(COLUMNS_ATTR_NAME), colId);
    // update by removing a column - col1
    final AtlasEntity tableEntity1 = TestUtilsV2.createTableEntity(dbEntity, (String) tableEntity.getAttribute(NAME));
    AtlasEntity.AtlasEntitiesWithExtInfo entitiesInfo1 = new AtlasEntity.AtlasEntitiesWithExtInfo(tableEntity1);
    final AtlasEntity columnEntity3New = TestUtilsV2.createColumnEntity(tableEntity1, (String) column3Created.getAttribute(NAME));
    tableEntity1.setAttribute(COLUMNS_ATTR_NAME, Arrays.asList(AtlasTypeUtil.getAtlasObjectId(columnEntity3New)));
    entitiesInfo1.addReferredEntity(columnEntity3New);
    init();
    deletionResponse = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo1), false);
    assertEquals(deletionResponse.getDeletedEntities().size(), 1);
    assertEquals(deletionResponse.getDeletedEntities().get(0).getGuid(), column2Created.getGuid());
    assertEntityDeleted(colId);
    // Delete the table entities.  The deletion should cascade to their composite columns.
    tableInstance = metadataService.getEntityDefinitionReference(TestUtils.TABLE_TYPE, NAME, (String) tableEntity.getAttribute(NAME));
    init();
    EntityMutationResponse tblDeletionResponse = entityStore.deleteById(tableInstance.getId()._getId());
    assertEquals(tblDeletionResponse.getDeletedEntities().size(), 2);
    final AtlasEntityHeader tableDeleted = tblDeletionResponse.getFirstDeletedEntityByTypeName(TABLE_TYPE);
    final AtlasEntityHeader colDeleted = tblDeletionResponse.getFirstDeletedEntityByTypeName(COLUMN_TYPE);
    // Verify that deleteEntities() response has guids for tables and their composite columns.
    Assert.assertTrue(tableDeleted.getGuid().equals(tableInstance.getId()._getId()));
    Assert.assertTrue(colDeleted.getGuid().equals(column3Created.getGuid()));
    // Verify that tables and their composite columns have been deleted from the graph Repository.
    assertEntityDeleted(tableDeleted.getGuid());
    assertEntityDeleted(colDeleted.getGuid());
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)21 ArrayList (java.util.ArrayList)11 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)9 List (java.util.List)7 Id (org.apache.atlas.typesystem.persistence.Id)7 Test (org.testng.annotations.Test)7 ImmutableList (com.google.common.collect.ImmutableList)5 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)5 BeforeTest (org.testng.annotations.BeforeTest)5 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)4 Referenceable (org.apache.atlas.typesystem.Referenceable)4 HashMap (java.util.HashMap)3 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 AtlasException (org.apache.atlas.AtlasException)2 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)2 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)2 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)2