Search in sources :

Example 21 with AtlasStructDef

use of org.apache.atlas.model.typedef.AtlasStructDef in project incubator-atlas by apache.

the class GraphBackedSearchIndexer method addIndexForType.

private void addIndexForType(AtlasGraphManagement management, AtlasBaseTypeDef typeDef) {
    if (typeDef instanceof AtlasEnumDef) {
        // Only handle complex types like Struct, Classification and Entity
        return;
    }
    if (typeDef instanceof AtlasStructDef) {
        AtlasStructDef structDef = (AtlasStructDef) typeDef;
        List<AtlasAttributeDef> attributeDefs = structDef.getAttributeDefs();
        if (CollectionUtils.isNotEmpty(attributeDefs)) {
            for (AtlasAttributeDef attributeDef : attributeDefs) {
                createIndexForAttribute(management, typeDef.getName(), attributeDef);
            }
        }
    } else if (!AtlasTypeUtil.isBuiltInType(typeDef.getName())) {
        throw new IllegalArgumentException("bad data type" + typeDef.getName());
    }
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) AtlasEnumDef(org.apache.atlas.model.typedef.AtlasEnumDef)

Example 22 with AtlasStructDef

use of org.apache.atlas.model.typedef.AtlasStructDef 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 23 with AtlasStructDef

use of org.apache.atlas.model.typedef.AtlasStructDef 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)

Example 24 with AtlasStructDef

use of org.apache.atlas.model.typedef.AtlasStructDef in project incubator-atlas by apache.

the class AtlasDeleteHandlerV1Test method setUp.

@BeforeClass
public void setUp() throws Exception {
    metadataService = TestUtils.addSessionCleanupWrapper(metadataService);
    final AtlasTypesDef deptTypesDef = TestUtilsV2.defineDeptEmployeeTypes();
    typeDefStore.createTypesDef(deptTypesDef);
    final AtlasTypesDef hiveTypesDef = TestUtilsV2.defineHiveTypes();
    typeDefStore.createTypesDef(hiveTypesDef);
    // Define type for map value.
    AtlasEntityDef mapValueDef = AtlasTypeUtil.createClassTypeDef("CompositeMapValue", "CompositeMapValue" + "_description", "1.0", ImmutableSet.<String>of(), AtlasTypeUtil.createUniqueRequiredAttrDef("name", "string"));
    // Define type with map where the value is a composite class reference to MapValue.
    AtlasEntityDef mapOwnerDef = AtlasTypeUtil.createClassTypeDef("CompositeMapOwner", "CompositeMapOwner_description", ImmutableSet.<String>of(), AtlasTypeUtil.createUniqueRequiredAttrDef("name", "string"), new AtlasStructDef.AtlasAttributeDef("map", "map<string,CompositeMapValue>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, new ArrayList<AtlasStructDef.AtlasConstraintDef>() {

        {
            add(new AtlasStructDef.AtlasConstraintDef(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_OWNED_REF));
        }
    }));
    final AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.of(mapValueDef, mapOwnerDef));
    typeDefStore.createTypesDef(typesDef);
    compositeMapOwnerType = typeRegistry.getEntityTypeByName("CompositeMapOwner");
    compositeMapValueType = typeRegistry.getEntityTypeByName("CompositeMapValue");
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) ArrayList(java.util.ArrayList) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) BeforeClass(org.testng.annotations.BeforeClass)

Example 25 with AtlasStructDef

use of org.apache.atlas.model.typedef.AtlasStructDef in project incubator-atlas by apache.

the class AtlasDeleteHandlerV1Test method testLowerBoundsIgnoredWhenDeletingCompositeEntitesOwnedByMap.

@Test
public void testLowerBoundsIgnoredWhenDeletingCompositeEntitesOwnedByMap() throws Exception {
    // Define MapValueReferencer type with required reference to CompositeMapValue.
    AtlasStructDef.AtlasAttributeDef[] mapValueAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("refToMapValue", "CompositeMapValue", false, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef mapValueDef = new AtlasEntityDef("MapValueReferencer", "RequiredMapValue_description", "1.0", Arrays.asList(mapValueAttributes), Collections.<String>emptySet());
    AtlasStructDef.AtlasAttributeDef[] mapContainerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("requiredMap", "map<string,MapValueReferencer>", false, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, new ArrayList<AtlasStructDef.AtlasConstraintDef>() {

        {
            add(new AtlasStructDef.AtlasConstraintDef(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_OWNED_REF));
        }
    }) };
    AtlasEntityDef mapContainerDef = new AtlasEntityDef("MapValueReferencerContainer", "MapValueReferencerContainer_description", "1.0", Arrays.asList(mapContainerAttributes), Collections.<String>emptySet());
    AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.<AtlasEntityDef>of(mapValueDef, mapContainerDef));
    typeDefStore.createTypesDef(typesDef);
    // Create instances of CompositeMapOwner and CompositeMapValue.
    // Set MapOwner.map with one entry that references MapValue instance.
    AtlasEntity.AtlasEntityWithExtInfo entityDefinition = createMapOwnerAndValueEntities();
    String mapOwnerGuid = entityDefinition.getEntity().getGuid();
    // Verify MapOwner.map attribute has expected value.
    ITypedReferenceableInstance mapOwnerInstance = metadataService.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
    RequestContextV1.clear();
    AtlasEntity mapValueReferencer = new AtlasEntity(mapValueDef.getName());
    mapValueReferencer.setAttribute("refToMapValue", new AtlasObjectId(mapValueInstance.getId()._getId(), mapValueInstance.getTypeName()));
    AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
    entities.addEntity(mapValueReferencer);
    List<AtlasEntityHeader> createEntitiesResult = entityStore.createOrUpdate(new AtlasEntityStream(entities), false).getCreatedEntities();
    Assert.assertEquals(createEntitiesResult.size(), 1);
    // Create instance of MapValueReferencer, and update mapValueReferencerContainer
    // to reference it.
    AtlasEntity mapValueReferenceContainer = new AtlasEntity(mapContainerDef.getName());
    entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
    entities.addEntity(mapValueReferenceContainer);
    entities.addReferredEntity(mapValueReferencer);
    mapValueReferenceContainer.setAttribute("requiredMap", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueReferencer)));
    RequestContextV1.clear();
    EntityMutationResponse updateEntitiesResult = entityStore.createOrUpdate(new AtlasEntityStream(entities), false);
    String mapValueReferencerContainerGuid = updateEntitiesResult.getCreatedEntitiesByTypeName("MapValueReferencerContainer").get(0).getGuid();
    String mapValueReferencerGuid = updateEntitiesResult.getUpdatedEntitiesByTypeName("MapValueReferencer").get(0).getGuid();
    Assert.assertEquals(updateEntitiesResult.getCreatedEntities().size(), 1);
    Assert.assertEquals(updateEntitiesResult.getUpdatedEntities().size(), 1);
    Assert.assertEquals(updateEntitiesResult.getUpdatedEntities().get(0).getGuid(), mapValueReferencerGuid);
    // 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.
    EntityMutationResponse deleteEntitiesResult = entityStore.deleteByIds(Arrays.asList(mapOwnerGuid, mapValueReferencerContainerGuid));
    Assert.assertEquals(deleteEntitiesResult.getDeletedEntities().size(), 4);
    Assert.assertTrue(extractGuids(deleteEntitiesResult.getDeletedEntities()).containsAll(Arrays.asList(mapOwnerGuid, mapValueGuid, mapValueReferencerContainerGuid, mapValueReferencerGuid)));
}
Also used : AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) 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

AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)39 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)24 AtlasEnumDef (org.apache.atlas.model.typedef.AtlasEnumDef)21 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)19 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)18 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)15 ArrayList (java.util.ArrayList)11 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)11 Test (org.testng.annotations.Test)10 HashMap (java.util.HashMap)7 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)7 AtlasEnumElementDef (org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef)6 AtlasConstraintDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef)5 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)4 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)4 AtlasType (org.apache.atlas.type.AtlasType)4 BeforeTest (org.testng.annotations.BeforeTest)4 Map (java.util.Map)3 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)3 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)3