Search in sources :

Example 36 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class EntityV2JerseyResourceIT method testDeleteEntityByUniqAttribute.

@Test
public void testDeleteEntityByUniqAttribute() throws Exception {
    // Create database entity
    AtlasEntity hiveDB = createHiveDB(DATABASE_NAME + random());
    // Delete the database entity
    EntityMutationResponse deleteResponse = atlasClientV2.deleteEntityByAttribute(DATABASE_TYPE_V2, toMap(NAME, (String) hiveDB.getAttribute(NAME)));
    // Verify that deleteEntities() response has database entity guids
    assertNotNull(deleteResponse);
    assertNotNull(deleteResponse.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE));
    assertEquals(deleteResponse.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE).size(), 1);
// Verify entities were deleted from the repository.
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) Test(org.testng.annotations.Test)

Example 37 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class AtlasDeleteHandlerV1Test 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.
    AtlasStructDef.AtlasAttributeDef[] structTargetAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef structTargetDef = new AtlasEntityDef("StructTarget", "StructTarget_description", "1.0", Arrays.asList(structTargetAttributes), Collections.<String>emptySet());
    AtlasStructDef.AtlasAttributeDef[] traitTargetAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef traitTargetDef = new AtlasEntityDef("TraitTarget", "TraitTarget_description", "1.0", Arrays.asList(traitTargetAttributes), Collections.<String>emptySet());
    AtlasStructDef.AtlasAttributeDef[] structContainerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("struct", "TestStruct", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef structContainerDef = new AtlasEntityDef("StructContainer", "StructContainer_description", "1.0", Arrays.asList(structContainerAttributes), Collections.<String>emptySet());
    // Define struct and trait types which have a unidirectional array reference
    // to a class type.
    AtlasStructDef.AtlasAttributeDef[] structDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("target", "array<StructTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), new AtlasStructDef.AtlasAttributeDef("nestedStructs", "array<NestedStruct>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasStructDef structDef = new AtlasStructDef("TestStruct", "TestStruct_desc", "1.0", Arrays.asList(structDefAttributes));
    // Define struct and trait types which have a unidirectional array reference
    // to a class type.
    AtlasStructDef.AtlasAttributeDef[] nestedStructDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), new AtlasStructDef.AtlasAttributeDef("target", "array<TraitTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasStructDef nestedStructDef = new AtlasStructDef("NestedStruct", "NestedStruct_desc", "1.0", Arrays.asList(nestedStructDefAttributes));
    AtlasStructDef.AtlasAttributeDef[] traitDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("target", "array<TraitTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasClassificationDef traitDef = new AtlasClassificationDef("TestTrait", "TestTrait_desc", "1.0", Arrays.asList(traitDefAttributes));
    AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(structDef, nestedStructDef), ImmutableList.<AtlasClassificationDef>of(traitDef), ImmutableList.<AtlasEntityDef>of(structTargetDef, traitTargetDef, structContainerDef));
    typeDefStore.createTypesDef(typesDef);
    // Create instances of class, struct, and trait types.
    final AtlasEntity structTargetEntity = new AtlasEntity("StructTarget");
    final AtlasEntity traitTargetEntity = new AtlasEntity("TraitTarget");
    final AtlasEntity structContainerEntity = new AtlasEntity("StructContainer");
    AtlasStruct structInstance = new AtlasStruct("TestStruct");
    AtlasStruct nestedStructInstance = new AtlasStruct("NestedStruct");
    Struct traitInstance = new Struct("TestTrait");
    structContainerEntity.setAttribute("struct", structInstance);
    structInstance.setAttribute("target", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(structTargetEntity)));
    structInstance.setAttribute("nestedStructs", ImmutableList.of(nestedStructInstance));
    AtlasEntity.AtlasEntitiesWithExtInfo structCreationObj = new AtlasEntity.AtlasEntitiesWithExtInfo();
    structCreationObj.addEntity(structContainerEntity);
    structCreationObj.addEntity(traitTargetEntity);
    structCreationObj.addReferredEntity(structTargetEntity);
    init();
    AtlasEntityStream entityStream = new AtlasEntityStream(structCreationObj);
    EntityMutationResponse response = entityStore.createOrUpdate(entityStream, false);
    Assert.assertEquals(response.getCreatedEntities().size(), 3);
    final List<String> structTarget = metadataService.getEntityList("StructTarget");
    Assert.assertEquals(structTarget.size(), 1);
    final String structTargetGuid = structTarget.get(0);
    final List<String> traitTarget = metadataService.getEntityList("TraitTarget");
    Assert.assertEquals(traitTarget.size(), 1);
    final String traitTargetGuid = traitTarget.get(0);
    final List<String> structContainerTarget = metadataService.getEntityList("StructContainer");
    Assert.assertEquals(structContainerTarget.size(), 1);
    String structContainerGuid = structContainerTarget.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);
    metadataService.addTrait(structContainerGuid, convertedTrait);
    // Verify that the unidirectional references from the struct and trait instances
    // are pointing at the target entities.
    final ITypedReferenceableInstance structContainerConvertedEntity = metadataService.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);
    init();
    // Delete the entities that are targets of the struct and trait instances.
    EntityMutationResponse entityResult = entityStore.deleteByIds(new ArrayList<String>() {

        {
            add(structTargetGuid);
            add(traitTargetGuid);
        }
    });
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 2);
    Assert.assertTrue(extractGuids(entityResult.getDeletedEntities()).containsAll(Arrays.asList(structTargetGuid, traitTargetGuid)));
    assertEntityDeleted(structTargetGuid);
    assertEntityDeleted(traitTargetGuid);
    assertTestDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes(structContainerGuid);
    init();
    // Delete the entity which contains nested structs and has the TestTrait trait.
    entityResult = entityStore.deleteById(structContainerGuid);
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 1);
    Assert.assertTrue(extractGuids(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 : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) IStruct(org.apache.atlas.typesystem.IStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) Struct(org.apache.atlas.typesystem.Struct) AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 38 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class AtlasDeleteHandlerV1Test 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 {
    AtlasEntity.AtlasEntitiesWithExtInfo hrDept = TestUtilsV2.createDeptEg2();
    init();
    final EntityMutationResponse hrDeptCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(hrDept), false);
    final AtlasEntityHeader deptCreated = hrDeptCreationResponse.getFirstCreatedEntityByTypeName(DEPARTMENT_TYPE);
    final AtlasEntityHeader maxEmployee = hrDeptCreationResponse.getCreatedEntityByTypeNameAndAttribute(TestUtilsV2.EMPLOYEE_TYPE, NAME, "Max");
    final AtlasEntityHeader johnEmployee = hrDeptCreationResponse.getCreatedEntityByTypeNameAndAttribute(TestUtilsV2.EMPLOYEE_TYPE, NAME, "John");
    final AtlasEntityHeader janeEmployee = hrDeptCreationResponse.getCreatedEntityByTypeNameAndAttribute(TestUtilsV2.MANAGER_TYPE, NAME, "Jane");
    final AtlasEntityHeader juliusEmployee = hrDeptCreationResponse.getCreatedEntityByTypeNameAndAttribute(TestUtilsV2.MANAGER_TYPE, NAME, "Julius");
    ITypedReferenceableInstance hrDeptInstance = metadataService.getEntityDefinition(deptCreated.getGuid());
    Map<String, String> nameGuidMap = getEmployeeNameGuidMap(hrDeptInstance);
    // Verify that Max is one of Jane's subordinates.
    ITypedReferenceableInstance jane = metadataService.getEntityDefinition(janeEmployee.getGuid());
    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(maxEmployee.getGuid()));
    init();
    EntityMutationResponse entityResult = entityStore.deleteById(maxEmployee.getGuid());
    ITypedReferenceableInstance john = metadataService.getEntityDefinitionReference(TestUtilsV2.EMPLOYEE_TYPE, NAME, "John");
    assertEquals(entityResult.getDeletedEntities().size(), 1);
    assertEquals(entityResult.getDeletedEntities().get(0).getGuid(), maxEmployee.getGuid());
    assertEquals(entityResult.getUpdatedEntities().size(), 3);
    assertEquals(extractGuids(entityResult.getUpdatedEntities()), Arrays.asList(janeEmployee.getGuid(), deptCreated.getGuid(), johnEmployee.getGuid()));
    assertEntityDeleted(maxEmployee.getGuid());
    assertMaxForTestDisconnectBidirectionalReferences(nameGuidMap);
    // Now delete jane - this should disconnect the manager reference from her
    // subordinate.
    init();
    entityResult = entityStore.deleteById(janeEmployee.getGuid());
    assertEquals(entityResult.getDeletedEntities().size(), 1);
    assertEquals(entityResult.getDeletedEntities().get(0).getGuid(), janeEmployee.getGuid());
    assertEquals(entityResult.getUpdatedEntities().size(), 2);
    assertEquals(extractGuids(entityResult.getUpdatedEntities()), Arrays.asList(deptCreated.getGuid(), johnEmployee.getGuid()));
    assertEntityDeleted(janeEmployee.getGuid());
    final AtlasEntity.AtlasEntityWithExtInfo johnUpdated = entityStore.getById(johnEmployee.getGuid());
    assertJohnForTestDisconnectBidirectionalReferences(johnUpdated, janeEmployee.getGuid());
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ArrayList(java.util.ArrayList) 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)

Example 39 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class AtlasDeleteHandlerV1Test method testUpdateEntity_MultiplicityOneNonCompositeReference.

@Test
public void testUpdateEntity_MultiplicityOneNonCompositeReference() throws Exception {
    AtlasEntity.AtlasEntitiesWithExtInfo hrDept = TestUtilsV2.createDeptEg2();
    init();
    RequestContextV1.clear();
    final EntityMutationResponse hrDeptCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(hrDept), false);
    final AtlasEntityHeader deptCreated = hrDeptCreationResponse.getFirstUpdatedEntityByTypeName(DEPARTMENT_TYPE);
    final AtlasEntityHeader maxEmployeeCreated = hrDeptCreationResponse.getCreatedEntityByTypeNameAndAttribute(TestUtilsV2.EMPLOYEE_TYPE, NAME, "Max");
    final AtlasEntityHeader johnEmployeeCreated = hrDeptCreationResponse.getUpdatedEntityByTypeNameAndAttribute(TestUtilsV2.EMPLOYEE_TYPE, NAME, "John");
    final AtlasEntityHeader janeEmployeeCreated = hrDeptCreationResponse.getCreatedEntityByTypeNameAndAttribute(TestUtilsV2.MANAGER_TYPE, NAME, "Jane");
    final AtlasEntityHeader juliusEmployeeCreated = hrDeptCreationResponse.getUpdatedEntityByTypeNameAndAttribute(TestUtilsV2.MANAGER_TYPE, NAME, "Julius");
    ITypedReferenceableInstance max = metadataService.getEntityDefinition(maxEmployeeCreated.getGuid());
    String maxGuid = max.getId()._getId();
    AtlasVertex vertex = GraphHelper.getInstance().getVertexForGUID(maxGuid);
    Long creationTimestamp = GraphHelper.getSingleValuedProperty(vertex, Constants.TIMESTAMP_PROPERTY_KEY, Long.class);
    Assert.assertNotNull(creationTimestamp);
    Long modificationTimestampPreUpdate = GraphHelper.getSingleValuedProperty(vertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class);
    Assert.assertNotNull(modificationTimestampPreUpdate);
    AtlasEntity maxEmployee = getEmployeeByName(hrDept, "Max");
    maxEmployee.setAttribute("mentor", AtlasTypeUtil.getAtlasObjectId(johnEmployeeCreated));
    maxEmployee.setAttribute("department", AtlasTypeUtil.getAtlasObjectId(deptCreated));
    maxEmployee.setAttribute("manager", AtlasTypeUtil.getAtlasObjectId(janeEmployeeCreated));
    init();
    EntityMutationResponse entityResult = entityStore.createOrUpdate(new AtlasEntityStream(maxEmployee), false);
    assertEquals(entityResult.getUpdatedEntities().size(), 1);
    assertTrue(extractGuids(entityResult.getUpdatedEntities()).contains(maxGuid));
    // Verify the update was applied correctly - john should now be max's mentor.
    max = metadataService.getEntityDefinition(maxGuid);
    ITypedReferenceableInstance refTarget = (ITypedReferenceableInstance) max.get("mentor");
    Assert.assertEquals(refTarget.getId()._getId(), johnEmployeeCreated.getGuid());
    // Verify modification timestamp was updated.
    vertex = GraphHelper.getInstance().getVertexForGUID(maxGuid);
    Long modificationTimestampPostUpdate = GraphHelper.getSingleValuedProperty(vertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class);
    Assert.assertNotNull(modificationTimestampPostUpdate);
    Assert.assertTrue(creationTimestamp < modificationTimestampPostUpdate);
    // Update max's mentor reference to jane.
    maxEmployee.setAttribute("mentor", AtlasTypeUtil.getAtlasObjectId(janeEmployeeCreated));
    init();
    entityResult = entityStore.createOrUpdate(new AtlasEntityStream(maxEmployee), false);
    assertEquals(entityResult.getUpdatedEntities().size(), 1);
    assertTrue(extractGuids(entityResult.getUpdatedEntities()).contains(maxGuid));
    // Verify the update was applied correctly - jane should now be max's mentor.
    max = metadataService.getEntityDefinition(maxGuid);
    refTarget = (ITypedReferenceableInstance) max.get("mentor");
    Assert.assertEquals(refTarget.getId()._getId(), janeEmployeeCreated.getGuid());
    // Verify modification timestamp was updated.
    vertex = GraphHelper.getInstance().getVertexForGUID(maxGuid);
    Long modificationTimestampPost2ndUpdate = GraphHelper.getSingleValuedProperty(vertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class);
    Assert.assertNotNull(modificationTimestampPost2ndUpdate);
    Assert.assertTrue(modificationTimestampPostUpdate < modificationTimestampPost2ndUpdate);
    ITypedReferenceableInstance julius = metadataService.getEntityDefinition(juliusEmployeeCreated.getGuid());
    Id juliusId = julius.getId();
    init();
    maxEmployee.setAttribute("manager", AtlasTypeUtil.getAtlasObjectId(juliusEmployeeCreated));
    entityResult = entityStore.createOrUpdate(new AtlasEntityStream(maxEmployee), false);
    assertEquals(entityResult.getUpdatedEntities().size(), 3);
    List<String> updatedGuids = extractGuids(entityResult.getUpdatedEntities());
    assertTrue(updatedGuids.contains(maxGuid));
    assertTrue(updatedGuids.contains(janeEmployeeCreated.getGuid()));
    // Should have updated julius to add max in subordinates list.
    assertTrue(updatedGuids.contains(juliusEmployeeCreated.getGuid()));
    // Verify the update was applied correctly - julius should now be max's manager and max should be julius' subordinate.
    max = metadataService.getEntityDefinition(maxGuid);
    refTarget = (ITypedReferenceableInstance) max.get("manager");
    Assert.assertEquals(refTarget.getId()._getId(), juliusId._getId());
    julius = metadataService.getEntityDefinition(juliusId._getId());
    Object value = julius.get("subordinates");
    Assert.assertTrue(value instanceof List);
    List<ITypedReferenceableInstance> refList = (List<ITypedReferenceableInstance>) value;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), maxGuid);
    assertTestUpdateEntity_MultiplicityOneNonCompositeReference(janeEmployeeCreated.getGuid());
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) 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) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 40 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class AtlasDeleteHandlerV1Test method testDisconnectMapReferenceFromClassType.

/**
     * Verify deleting entities that are the target of class map references.
     */
@Test
public void testDisconnectMapReferenceFromClassType() throws Exception {
    // Define type for map value.
    AtlasStructDef.AtlasAttributeDef[] mapValueAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("biMapOwner", "MapOwner", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, new ArrayList<AtlasStructDef.AtlasConstraintDef>() {

        {
            add(new AtlasStructDef.AtlasConstraintDef(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {

                {
                    put(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, "biMap");
                }
            }));
        }
    }) };
    AtlasEntityDef mapValueContainerDef = new AtlasEntityDef("MapValue", "MapValue_desc", "1.0", Arrays.asList(mapValueAttributes), Collections.<String>emptySet());
    // Define type with unidirectional and bidirectional map references,
    // where the map value is a class reference to MapValue.
    AtlasStructDef.AtlasAttributeDef[] mapOwnerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("map", "map<string,MapValue>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), new AtlasStructDef.AtlasAttributeDef("biMap", "map<string,MapValue>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, new ArrayList<AtlasStructDef.AtlasConstraintDef>() {

        {
            add(new AtlasStructDef.AtlasConstraintDef(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {

                {
                    put(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, "biMapOwner");
                }
            }));
        }
    }) };
    AtlasEntityDef mapOwnerContainerDef = new AtlasEntityDef("MapOwner", "MapOwner_desc", "1.0", Arrays.asList(mapOwnerAttributes), Collections.<String>emptySet());
    AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.<AtlasEntityDef>of(mapValueContainerDef, mapOwnerContainerDef));
    typeDefStore.createTypesDef(typesDef);
    // Create instances of MapOwner and MapValue.
    // Set MapOwner.map and MapOwner.biMap with one entry that references MapValue instance.
    AtlasEntity mapOwnerInstance = new AtlasEntity("MapOwner");
    AtlasEntity mapValueInstance = new AtlasEntity("MapValue");
    mapOwnerInstance.setAttribute("map", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueInstance)));
    mapOwnerInstance.setAttribute("biMap", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueInstance)));
    // Set biMapOwner reverse reference on MapValue.
    mapValueInstance.setAttribute("biMapOwner", AtlasTypeUtil.getAtlasObjectId(mapOwnerInstance));
    AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
    entities.addReferredEntity(mapValueInstance);
    entities.addEntity(mapOwnerInstance);
    final EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entities), false);
    Assert.assertEquals(response.getCreatedEntities().size(), 2);
    final List<AtlasEntityHeader> mapOwnerCreated = response.getCreatedEntitiesByTypeName("MapOwner");
    AtlasEntity.AtlasEntityWithExtInfo mapOwnerEntity = entityStore.getById(mapOwnerCreated.get(0).getGuid());
    String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(typeRegistry.getEntityTypeByName("MapOwner"), "map");
    String mapEntryLabel = edgeLabel + "." + "value1";
    AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
    // Verify MapOwner.map attribute has expected value.
    String mapValueGuid = null;
    AtlasVertex mapOwnerVertex = null;
    for (String mapAttrName : Arrays.asList("map", "biMap")) {
        Object object = mapOwnerEntity.getEntity().getAttribute(mapAttrName);
        Assert.assertNotNull(object);
        Assert.assertTrue(object instanceof Map);
        Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>) object;
        Assert.assertEquals(map.size(), 1);
        AtlasObjectId value1Id = map.get("value1");
        Assert.assertNotNull(value1Id);
        mapValueGuid = value1Id.getGuid();
        mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerEntity.getEntity().getGuid());
        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.
    entityStore.deleteById(mapValueGuid);
    assertEntityDeleted(mapValueGuid);
    assertTestDisconnectMapReferenceFromClassType(mapOwnerEntity.getEntity().getGuid());
}
Also used : AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEdgeLabel(org.apache.atlas.repository.graph.AtlasEdgeLabel) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) Map(java.util.Map) HashMap(java.util.HashMap) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)60 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)44 Test (org.testng.annotations.Test)40 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)34 BeforeTest (org.testng.annotations.BeforeTest)22 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)21 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)17 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)17 ArrayList (java.util.ArrayList)16 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)13 HashMap (java.util.HashMap)12 List (java.util.List)8 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)7 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)7 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)7 ImmutableList (com.google.common.collect.ImmutableList)6 AtlasException (org.apache.atlas.AtlasException)6 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)6 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)6 Map (java.util.Map)5