use of org.apache.atlas.repository.graph.AtlasEdgeLabel in project incubator-atlas by apache.
the class AtlasDeleteHandlerV1Test method testDeleteTargetOfRequiredMapReference.
@Test
public void testDeleteTargetOfRequiredMapReference() throws Exception {
// Define type for map value.
AtlasEntityDef mapValueDef = new AtlasEntityDef("RequiredMapValue", "RequiredMapValue_description", "1.0", Collections.<AtlasStructDef.AtlasAttributeDef>emptyList(), Collections.<String>emptySet());
AtlasStructDef.AtlasAttributeDef[] mapOwnerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("map", "map<string,RequiredMapValue>", false, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
AtlasEntityDef mapOwnerDef = new AtlasEntityDef("RequiredMapOwner", "RequiredMapOwner_description", "1.0", Arrays.asList(mapOwnerAttributes), Collections.<String>emptySet());
AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.<AtlasEntityDef>of(mapValueDef, mapOwnerDef));
typeDefStore.createTypesDef(typesDef);
AtlasEntityType mapOwnerType = typeRegistry.getEntityTypeByName("RequiredMapOwner");
AtlasEntityType mapValueType = typeRegistry.getEntityTypeByName("RequiredMapValue");
// Create instances of RequiredMapOwner and RequiredMapValue.
// Set RequiredMapOwner.map with one entry that references RequiredMapValue instance.
AtlasEntity mapOwnerInstance = new AtlasEntity(mapOwnerType.getTypeName());
AtlasEntity mapValueInstance = new AtlasEntity(mapValueType.getTypeName());
mapOwnerInstance.setAttribute("map", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueInstance)));
AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
entities.addReferredEntity(mapValueInstance);
entities.addEntity(mapOwnerInstance);
List<AtlasEntityHeader> createEntitiesResult = entityStore.createOrUpdate(new AtlasEntityStream(entities), false).getCreatedEntities();
Assert.assertEquals(createEntitiesResult.size(), 2);
List<String> guids = metadataService.getEntityList("RequiredMapOwner");
Assert.assertEquals(guids.size(), 1);
String mapOwnerGuid = guids.get(0);
guids = metadataService.getEntityList("RequiredMapValue");
Assert.assertEquals(guids.size(), 1);
String mapValueGuid = guids.get(0);
// Verify MapOwner.map attribute has expected value.
final AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance1 = entityStore.getById(mapOwnerGuid);
Object object = mapOwnerInstance1.getEntity().getAttribute("map");
Assert.assertNotNull(object);
Assert.assertTrue(object instanceof Map);
Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>) object;
Assert.assertEquals(map.size(), 1);
AtlasObjectId mapValueInstance1 = map.get("value1");
Assert.assertNotNull(mapValueInstance1);
Assert.assertEquals(mapValueInstance1.getGuid(), mapValueGuid);
String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(mapOwnerType, "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);
// Verify deleting the target of required map attribute throws a AtlasBaseException.
try {
entityStore.deleteById(mapValueGuid);
Assert.fail(AtlasBaseException.class.getSimpleName() + " was expected but none thrown.");
} catch (Exception e) {
verifyExceptionThrown(e, AtlasBaseException.class);
}
}
use of org.apache.atlas.repository.graph.AtlasEdgeLabel in project incubator-atlas by apache.
the class DeleteHandlerV1 method getAttributeForEdge.
protected AtlasAttribute getAttributeForEdge(String edgeLabel) throws AtlasBaseException {
AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(edgeLabel);
AtlasType parentType = typeRegistry.getType(atlasEdgeLabel.getTypeName());
AtlasStructType parentStructType = (AtlasStructType) parentType;
return parentStructType.getAttribute(atlasEdgeLabel.getAttributeName());
}
use of org.apache.atlas.repository.graph.AtlasEdgeLabel in project incubator-atlas by apache.
the class DeleteHandlerV1 method deleteEdge.
protected void deleteEdge(AtlasEdge edge, boolean updateInverseAttribute, boolean force) throws AtlasBaseException {
//update inverse attribute
if (updateInverseAttribute) {
AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(edge.getLabel());
AtlasType parentType = typeRegistry.getType(atlasEdgeLabel.getTypeName());
if (parentType instanceof AtlasEntityType) {
AtlasEntityType parentEntityType = (AtlasEntityType) parentType;
AtlasStructType.AtlasAttribute attribute = parentEntityType.getAttribute(atlasEdgeLabel.getAttributeName());
if (attribute.getInverseRefAttribute() != null) {
deleteEdgeBetweenVertices(edge.getInVertex(), edge.getOutVertex(), attribute.getInverseRefAttribute());
}
}
}
deleteEdge(edge, force);
}
use of org.apache.atlas.repository.graph.AtlasEdgeLabel 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());
}
use of org.apache.atlas.repository.graph.AtlasEdgeLabel in project incubator-atlas by apache.
the class AtlasDeleteHandlerV1Test method testDeleteEntitiesWithCompositeMapReference.
@Test
public void testDeleteEntitiesWithCompositeMapReference() throws Exception {
// Create instances of MapOwner and MapValue.
// 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.
AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance = entityStore.getById(mapOwnerGuid);
Object object = mapOwnerInstance.getEntity().getAttribute("map");
Assert.assertNotNull(object);
Assert.assertTrue(object instanceof Map);
Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>) object;
Assert.assertEquals(map.size(), 1);
AtlasObjectId mapValueInstance = map.get("value1");
Assert.assertNotNull(mapValueInstance);
String mapValueGuid = mapValueInstance.getGuid();
String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(compositeMapOwnerType, "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);
RequestContextV1.clear();
List<AtlasEntityHeader> deletedEntities = entityStore.deleteById(mapOwnerGuid).getDeletedEntities();
Assert.assertEquals(deletedEntities.size(), 2);
Assert.assertTrue(extractGuids(deletedEntities).contains(mapOwnerGuid));
Assert.assertTrue(extractGuids(deletedEntities).contains(mapValueGuid));
assertEntityDeleted(mapOwnerGuid);
assertEntityDeleted(mapValueGuid);
}
Aggregations