Search in sources :

Example 11 with AtlasObjectId

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

the class AtlasEntityStoreV1Test method validateAttribute.

private void validateAttribute(AtlasEntityExtInfo entityExtInfo, Object actual, Object expected, AtlasType attributeType, String attrName) throws AtlasBaseException, AtlasException {
    switch(attributeType.getTypeCategory()) {
        case OBJECT_ID_TYPE:
            Assert.assertTrue(actual instanceof AtlasObjectId);
            String guid = ((AtlasObjectId) actual).getGuid();
            Assert.assertTrue(AtlasTypeUtil.isAssignedGuid(guid), "expected assigned guid. found " + guid);
            break;
        case PRIMITIVE:
        case ENUM:
            Assert.assertEquals(actual, expected);
            break;
        case MAP:
            AtlasMapType mapType = (AtlasMapType) attributeType;
            AtlasType valueType = mapType.getValueType();
            Map actualMap = (Map) actual;
            Map expectedMap = (Map) expected;
            if (MapUtils.isNotEmpty(expectedMap)) {
                Assert.assertTrue(MapUtils.isNotEmpty(actualMap));
                // deleted entries are included in the attribute; hence use >=
                Assert.assertTrue(actualMap.size() >= expectedMap.size());
                for (Object key : expectedMap.keySet()) {
                    validateAttribute(entityExtInfo, actualMap.get(key), expectedMap.get(key), valueType, attrName);
                }
            }
            break;
        case ARRAY:
            AtlasArrayType arrType = (AtlasArrayType) attributeType;
            AtlasType elemType = arrType.getElementType();
            List actualList = (List) actual;
            List expectedList = (List) expected;
            if (CollectionUtils.isNotEmpty(expectedList)) {
                Assert.assertTrue(CollectionUtils.isNotEmpty(actualList));
                //actual list could have deleted entities. Hence size may not match.
                Assert.assertTrue(actualList.size() >= expectedList.size());
                for (int i = 0; i < expectedList.size(); i++) {
                    validateAttribute(entityExtInfo, actualList.get(i), expectedList.get(i), elemType, attrName);
                }
            }
            break;
        case STRUCT:
            AtlasStruct expectedStruct = (AtlasStruct) expected;
            AtlasStruct actualStruct = (AtlasStruct) actual;
            validateEntity(entityExtInfo, actualStruct, expectedStruct);
            break;
        default:
            Assert.fail("Unknown type category");
    }
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) List(java.util.List) ArrayList(java.util.ArrayList) TestUtils.randomString(org.apache.atlas.TestUtils.randomString) Map(java.util.Map) HashMap(java.util.HashMap) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 12 with AtlasObjectId

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

the class AtlasEntityStoreV1Test method testClassUpdate.

@Test(dependsOnMethods = "testCreate")
public void testClassUpdate() throws Exception {
    init();
    //Create new db instance
    final AtlasEntity databaseInstance = TestUtilsV2.createDBEntity();
    EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(databaseInstance), false);
    final AtlasEntityHeader dbCreated = response.getFirstCreatedEntityByTypeName(TestUtilsV2.DATABASE_TYPE);
    init();
    Map<String, AtlasEntity> tableCloneMap = new HashMap<>();
    AtlasEntity tableClone = new AtlasEntity(tblEntity.getEntity());
    tableClone.setAttribute("database", new AtlasObjectId(dbCreated.getGuid(), TestUtils.DATABASE_TYPE));
    tableCloneMap.put(dbCreated.getGuid(), databaseInstance);
    tableCloneMap.put(tableClone.getGuid(), tableClone);
    response = entityStore.createOrUpdate(new InMemoryMapEntityStream(tableCloneMap), false);
    final AtlasEntityHeader tableDefinition = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDefinition = getEntityFromStore(tableDefinition);
    Assert.assertNotNull(updatedTableDefinition.getAttribute("database"));
    Assert.assertEquals(((AtlasObjectId) updatedTableDefinition.getAttribute("database")).getGuid(), dbCreated.getGuid());
}
Also used : HashMap(java.util.HashMap) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) TestUtils.randomString(org.apache.atlas.TestUtils.randomString) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 13 with AtlasObjectId

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

the class InverseReferenceUpdateV1Test method testInverseReferenceAutoUpdate_NonComposite_OneToMany.

@Test
public void testInverseReferenceAutoUpdate_NonComposite_OneToMany() throws Exception {
    AtlasObjectId juliusId = nameIdMap.get("Julius");
    // Change Max's Employee.manager reference to Julius and apply the change as a partial update.
    // This should also update Julius to add Max to the inverse Manager.subordinates reference.
    AtlasEntity maxEntityForUpdate = new AtlasEntity(TestUtilsV2.EMPLOYEE_TYPE);
    maxEntityForUpdate.setAttribute("manager", juliusId);
    AtlasEntityType employeeType = typeRegistry.getEntityTypeByName(TestUtilsV2.EMPLOYEE_TYPE);
    Map<String, Object> uniqAttributes = Collections.<String, Object>singletonMap("name", "Max");
    EntityMutationResponse updateResponse = entityStore.updateByUniqueAttributes(employeeType, uniqAttributes, new AtlasEntityWithExtInfo(maxEntityForUpdate));
    List<AtlasEntityHeader> partialUpdatedEntities = updateResponse.getPartialUpdatedEntities();
    // 3 entities should have been updated:
    // * Max to change the Employee.manager reference
    // * Julius to add Max to Manager.subordinates
    // * Jane to remove Max from Manager.subordinates
    assertEquals(partialUpdatedEntities.size(), 3);
    AtlasObjectId maxId = nameIdMap.get("Max");
    String janeGuid = nameIdMap.get("Jane").getGuid();
    AtlasEntitiesWithExtInfo storedEntities = entityStore.getByIds(ImmutableList.of(maxId.getGuid(), juliusId.getGuid(), janeGuid));
    AtlasEntity storedEntity = storedEntities.getEntity(maxId.getGuid());
    verifyReferenceValue(storedEntity, "manager", juliusId.getGuid());
    storedEntity = storedEntities.getEntity(juliusId.getGuid());
    verifyReferenceList(storedEntity, "subordinates", ImmutableList.of(maxId));
    storedEntity = storedEntities.getEntity(janeGuid);
    verify_testInverseReferenceAutoUpdate_NonComposite_OneToMany(storedEntity);
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) Test(org.testng.annotations.Test)

Example 14 with AtlasObjectId

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

the class InverseReferenceUpdateV1Test method testInverseReferenceAutoUpdate_Map.

@Test
public void testInverseReferenceAutoUpdate_Map() throws Exception {
    AtlasEntity a1 = new AtlasEntity("A");
    a1.setAttribute(NAME, TestUtils.randomString());
    AtlasEntity b1 = new AtlasEntity("B");
    b1.setAttribute(NAME, TestUtils.randomString());
    AtlasEntity b2 = new AtlasEntity("B");
    b2.setAttribute(NAME, TestUtils.randomString());
    AtlasEntity b3 = new AtlasEntity("B");
    b3.setAttribute(NAME, TestUtils.randomString());
    AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo();
    atlasEntitiesWithExtInfo.addEntity(a1);
    atlasEntitiesWithExtInfo.addEntity(b1);
    atlasEntitiesWithExtInfo.addEntity(b2);
    atlasEntitiesWithExtInfo.addEntity(b3);
    AtlasEntityStream entityStream = new AtlasEntityStream(atlasEntitiesWithExtInfo);
    EntityMutationResponse response = entityStore.createOrUpdate(entityStream, false);
    AtlasEntityType aType = typeRegistry.getEntityTypeByName("A");
    AtlasEntity aForPartialUpdate = new AtlasEntity("A");
    aForPartialUpdate.setAttribute("mapToB", ImmutableMap.<String, AtlasObjectId>of("b1", AtlasTypeUtil.getAtlasObjectId(b1), "b2", AtlasTypeUtil.getAtlasObjectId(b2)));
    init();
    response = entityStore.updateByUniqueAttributes(aType, Collections.<String, Object>singletonMap(NAME, a1.getAttribute(NAME)), new AtlasEntityWithExtInfo(aForPartialUpdate));
    List<AtlasEntityHeader> partialUpdatedEntities = response.getPartialUpdatedEntities();
    // Verify 3 entities were updated:
    // * set a1.mapToB to "b1"->b1, "b2"->b2
    // * set b1.mappedFromA to a1
    // * set b2.mappedFromA to a1
    assertEquals(partialUpdatedEntities.size(), 3);
    AtlasEntitiesWithExtInfo storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), b2.getGuid(), b1.getGuid()));
    AtlasEntity storedEntity = storedEntities.getEntity(a1.getGuid());
    Object value = storedEntity.getAttribute("mapToB");
    assertTrue(value instanceof Map);
    Map<String, AtlasObjectId> refMap = (Map<String, AtlasObjectId>) value;
    assertEquals(refMap.size(), 2);
    AtlasObjectId referencedEntityId = refMap.get("b1");
    assertEquals(referencedEntityId, AtlasTypeUtil.getAtlasObjectId(b1));
    referencedEntityId = refMap.get("b2");
    assertEquals(referencedEntityId, AtlasTypeUtil.getAtlasObjectId(b2));
    storedEntity = storedEntities.getEntity(b1.getGuid());
    verifyReferenceValue(storedEntity, "mappedFromA", a1.getGuid());
    storedEntity = storedEntities.getEntity(b2.getGuid());
    verifyReferenceValue(storedEntity, "mappedFromA", a1.getGuid());
    aForPartialUpdate.setAttribute("mapToB", ImmutableMap.<String, AtlasObjectId>of("b3", AtlasTypeUtil.getAtlasObjectId(b3)));
    init();
    response = entityStore.updateByUniqueAttributes(aType, Collections.<String, Object>singletonMap(NAME, a1.getAttribute(NAME)), new AtlasEntityWithExtInfo(aForPartialUpdate));
    partialUpdatedEntities = response.getPartialUpdatedEntities();
    // Verify 4 entities were updated:
    // * set a1.mapToB to "b3"->b3
    // * set b3.mappedFromA to a1
    // * disconnect b1.mappedFromA
    // * disconnect b2.mappedFromA
    assertEquals(partialUpdatedEntities.size(), 4);
    storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), b2.getGuid(), b1.getGuid(), b3.getGuid()));
    AtlasEntity storedB3 = storedEntities.getEntity(b3.getGuid());
    verifyReferenceValue(storedB3, "mappedFromA", a1.getGuid());
    verify_testInverseReferenceAutoUpdate_Map(storedEntities.getEntity(a1.getGuid()), storedEntities.getEntity(b1.getGuid()), storedEntities.getEntity(b2.getGuid()), storedB3);
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 15 with AtlasObjectId

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

the class HardDeleteHandlerV1Test method assertTestDisconnectMapReferenceFromClassType.

protected void assertTestDisconnectMapReferenceFromClassType(final String mapOwnerGuid) throws Exception {
    // Verify map references from mapOwner were disconnected.
    AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance = entityStore.getById(mapOwnerGuid);
    Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>) mapOwnerInstance.getEntity().getAttribute("map");
    Assert.assertNull(map);
    Map<String, AtlasObjectId> biMap = (Map<String, AtlasObjectId>) mapOwnerInstance.getEntity().getAttribute("biMap");
    Assert.assertNull(biMap);
    AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
    Object object = mapOwnerVertex.getProperty("MapOwner.map.value1", String.class);
    assertNull(object);
    object = mapOwnerVertex.getProperty("MapOwner.biMap.value1", String.class);
    assertNull(object);
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Map(java.util.Map)

Aggregations

AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)46 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)27 Test (org.testng.annotations.Test)17 Map (java.util.Map)15 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)15 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)11 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)11 BeforeTest (org.testng.annotations.BeforeTest)11 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)10 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)7 List (java.util.List)6 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)5 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)5 RequestContextV1 (org.apache.atlas.RequestContextV1)4 AtlasExportRequest (org.apache.atlas.model.impexp.AtlasExportRequest)4 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)3 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)3