Search in sources :

Example 96 with AtlasEntity

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

the class AtlasEntityStoreV1Test method testPartialUpdateAttr.

@Test
public void testPartialUpdateAttr() throws Exception {
    //Update optional attribute
    init();
    AtlasEntity dbEntity = new AtlasEntity(TestUtilsV2.DATABASE_TYPE);
    dbEntity.setAttribute("name", RandomStringUtils.randomAlphanumeric(10));
    dbEntity.setAttribute("description", "us db");
    dbEntity.setAttribute("isReplicated", false);
    dbEntity.setAttribute("created", "09081988");
    dbEntity.setAttribute("namespace", "db namespace");
    dbEntity.setAttribute("cluster", "Fenton_Cluster");
    dbEntity.setAttribute("colo", "10001");
    EntityStream dbStream = new AtlasEntityStream(new AtlasEntitiesWithExtInfo(dbEntity));
    EntityMutationResponse response = entityStore.createOrUpdate(dbStream, false);
    AtlasEntityHeader dbHeader = response.getFirstEntityCreated();
    AtlasEntity createdDbEntity = getEntityFromStore(dbHeader);
    // update the db entity
    dbEntity = new AtlasEntity(TestUtilsV2.DATABASE_TYPE);
    dbEntity.setGuid(createdDbEntity.getGuid());
    // dbEntity.setAttribute("name", createdDbEntity.getAttribute("name"));
    // dbEntity.setAttribute("description", "another db"); // required attr
    // optional attr
    dbEntity.setAttribute("created", "08151947");
    // optional attr
    dbEntity.setAttribute("isReplicated", true);
    dbStream = new AtlasEntityStream(new AtlasEntitiesWithExtInfo(dbEntity));
    // fail full update if required attributes are not specified.
    try {
        entityStore.createOrUpdate(dbStream, false);
    } catch (AtlasBaseException ex) {
        Assert.assertEquals(ex.getAtlasErrorCode(), AtlasErrorCode.INSTANCE_CRUD_INVALID_PARAMS);
    }
    // do partial update without providing required attributes
    dbStream.reset();
    response = entityStore.createOrUpdate(dbStream, true);
    dbHeader = response.getFirstEntityPartialUpdated();
    AtlasEntity updatedDbEntity = getEntityFromStore(dbHeader);
    assertEquals(updatedDbEntity.getAttribute("name"), createdDbEntity.getAttribute("name"));
    assertEquals(updatedDbEntity.getAttribute("description"), createdDbEntity.getAttribute("description"));
    assertEquals(updatedDbEntity.getAttribute("isReplicated"), true);
    assertEquals(updatedDbEntity.getAttribute("created"), "08151947");
    assertEquals(updatedDbEntity.getAttribute("namespace"), createdDbEntity.getAttribute("namespace"));
    assertEquals(updatedDbEntity.getAttribute("cluster"), createdDbEntity.getAttribute("cluster"));
    assertEquals(updatedDbEntity.getAttribute("colo"), createdDbEntity.getAttribute("colo"));
    // create a new table type
    AtlasEntity tblEntity = new AtlasEntity(TABLE_TYPE);
    tblEntity.setAttribute("name", RandomStringUtils.randomAlphanumeric(10));
    tblEntity.setAttribute("type", "type");
    tblEntity.setAttribute("tableType", "MANAGED");
    tblEntity.setAttribute("database", AtlasTypeUtil.getAtlasObjectId(updatedDbEntity));
    // create new column entity
    AtlasEntity col1 = TestUtilsV2.createColumnEntity(tblEntity);
    AtlasEntity col2 = TestUtilsV2.createColumnEntity(tblEntity);
    col1.setAttribute(TestUtilsV2.NAME, "col1");
    col2.setAttribute(TestUtilsV2.NAME, "col2");
    List<AtlasObjectId> columns = new ArrayList<>();
    columns.add(AtlasTypeUtil.getAtlasObjectId(col1));
    columns.add(AtlasTypeUtil.getAtlasObjectId(col2));
    tblEntity.setAttribute(TestUtilsV2.COLUMNS_ATTR_NAME, columns);
    AtlasEntitiesWithExtInfo tableEntityInfo = new AtlasEntitiesWithExtInfo(tblEntity);
    tableEntityInfo.addReferredEntity(col1.getGuid(), col1);
    tableEntityInfo.addReferredEntity(col2.getGuid(), col2);
    EntityStream tblStream = new AtlasEntityStream(tableEntityInfo);
    response = entityStore.createOrUpdate(tblStream, false);
    AtlasEntityHeader tblHeader = response.getFirstEntityCreated();
    AtlasEntity createdTblEntity = getEntityFromStore(tblHeader);
    columns = (List<AtlasObjectId>) createdTblEntity.getAttribute(TestUtilsV2.COLUMNS_ATTR_NAME);
    assertEquals(columns.size(), 2);
    // update - add 2 more columns to table
    AtlasEntity col3 = TestUtilsV2.createColumnEntity(createdTblEntity);
    col3.setAttribute(TestUtilsV2.NAME, "col3");
    col3.setAttribute("description", "description col3");
    AtlasEntity col4 = TestUtilsV2.createColumnEntity(createdTblEntity);
    col4.setAttribute(TestUtilsV2.NAME, "col4");
    col4.setAttribute("description", "description col4");
    columns.clear();
    columns.add(AtlasTypeUtil.getAtlasObjectId(col3));
    columns.add(AtlasTypeUtil.getAtlasObjectId(col4));
    tblEntity = new AtlasEntity(TABLE_TYPE);
    tblEntity.setGuid(createdTblEntity.getGuid());
    tblEntity.setAttribute(TestUtilsV2.COLUMNS_ATTR_NAME, columns);
    tableEntityInfo = new AtlasEntitiesWithExtInfo(tblEntity);
    tableEntityInfo.addReferredEntity(col3.getGuid(), col3);
    tableEntityInfo.addReferredEntity(col4.getGuid(), col4);
    tblStream = new AtlasEntityStream(tableEntityInfo);
    response = entityStore.createOrUpdate(tblStream, true);
    tblHeader = response.getFirstEntityPartialUpdated();
    AtlasEntity updatedTblEntity = getEntityFromStore(tblHeader);
    columns = (List<AtlasObjectId>) updatedTblEntity.getAttribute(TestUtilsV2.COLUMNS_ATTR_NAME);
    // deleted columns are included in the attribute; hence use >=
    assertTrue(columns.size() >= 2);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) ArrayList(java.util.ArrayList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 97 with AtlasEntity

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

Example 98 with AtlasEntity

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

the class AtlasDeleteHandlerV1Test method testDeleteReference.

@Test
public void testDeleteReference() throws Exception {
    //Deleting column should update table
    final AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    init();
    EntityMutationResponse dbCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(dbEntity), false);
    final AtlasEntity tableEntity = TestUtilsV2.createTableEntity(dbEntity);
    final AtlasEntity columnEntity = TestUtilsV2.createColumnEntity(tableEntity);
    tableEntity.setAttribute(COLUMNS_ATTR_NAME, Arrays.asList(AtlasTypeUtil.getAtlasObjectId(columnEntity)));
    AtlasEntity.AtlasEntityWithExtInfo input = new AtlasEntity.AtlasEntityWithExtInfo(tableEntity);
    input.addReferredEntity(columnEntity);
    init();
    EntityMutationResponse tblCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(input), false);
    final AtlasEntityHeader columnCreated = tblCreationResponse.getFirstCreatedEntityByTypeName(COLUMN_TYPE);
    final AtlasEntityHeader tableCreated = tblCreationResponse.getFirstCreatedEntityByTypeName(TABLE_TYPE);
    init();
    EntityMutationResponse deletionResponse = entityStore.deleteById(columnCreated.getGuid());
    assertEquals(deletionResponse.getDeletedEntities().size(), 1);
    assertEquals(deletionResponse.getDeletedEntities().get(0).getGuid(), columnCreated.getGuid());
    assertEquals(deletionResponse.getUpdatedEntities().size(), 1);
    assertEquals(deletionResponse.getUpdatedEntities().get(0).getGuid(), tableCreated.getGuid());
    assertEntityDeleted(columnCreated.getGuid());
    assertColumnForTestDeleteReference(entityStore.getById(tableCreated.getGuid()));
    //Deleting table should update process
    AtlasEntity process = TestUtilsV2.createProcessEntity(null, Arrays.asList(AtlasTypeUtil.getAtlasObjectId(tableCreated)));
    init();
    final EntityMutationResponse processCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(process), false);
    init();
    entityStore.deleteById(tableCreated.getGuid());
    assertEntityDeleted(tableCreated.getGuid());
    assertTableForTestDeleteReference(tableCreated.getGuid());
    assertProcessForTestDeleteReference(processCreationResponse.getFirstEntityCreated());
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 99 with AtlasEntity

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

the class AtlasEntityStoreV1Test method testTagAssociationAfterRedefinition.

@Test
public void testTagAssociationAfterRedefinition() {
    AtlasClassificationDef aTag = new AtlasClassificationDef("testTag");
    AtlasAttributeDef attributeDef = new AtlasAttributeDef("testAttribute", "int", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, true, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList());
    aTag.addAttribute(attributeDef);
    AtlasTypesDef typesDef = new AtlasTypesDef();
    typesDef.setClassificationDefs(Arrays.asList(aTag));
    try {
        typeDefStore.createTypesDef(typesDef);
    } catch (AtlasBaseException e) {
        fail("Tag creation should've succeeded");
    }
    try {
        typeDefStore.deleteTypesDef(typesDef);
    } catch (AtlasBaseException e) {
        fail("Tag deletion should've succeeded");
    }
    aTag = new AtlasClassificationDef("testTag");
    attributeDef = new AtlasAttributeDef("testAttribute", "string", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, true, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList());
    aTag.addAttribute(attributeDef);
    typesDef.setClassificationDefs(Arrays.asList(aTag));
    try {
        typeDefStore.createTypesDef(typesDef);
    } catch (AtlasBaseException e) {
        fail("Tag re-creation should've succeeded");
    }
    final AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    try {
        EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(dbEntity), false);
        List<AtlasEntityHeader> createdEntity = response.getCreatedEntities();
        assertTrue(CollectionUtils.isNotEmpty(createdEntity));
        String guid = createdEntity.get(0).getGuid();
        entityStore.addClassification(Arrays.asList(guid), new AtlasClassification(aTag.getName(), "testAttribute", "test-string"));
    } catch (AtlasBaseException e) {
        fail("DB entity creation should've succeeded");
    }
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) TestUtils.randomString(org.apache.atlas.TestUtils.randomString) AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 100 with AtlasEntity

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

the class AtlasEntityStoreV1Test method testCheckOptionalAttrValueRetention.

@Test
public void testCheckOptionalAttrValueRetention() throws Exception {
    AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    init();
    EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(dbEntity), false);
    AtlasEntity firstEntityCreated = getEntityFromStore(response.getFirstCreatedEntityByTypeName(TestUtilsV2.DATABASE_TYPE));
    //The optional boolean attribute should have a non-null value
    final String isReplicatedAttr = "isReplicated";
    final String paramsAttr = "parameters";
    Assert.assertNotNull(firstEntityCreated.getAttribute(isReplicatedAttr));
    Assert.assertEquals(firstEntityCreated.getAttribute(isReplicatedAttr), Boolean.FALSE);
    Assert.assertNull(firstEntityCreated.getAttribute(paramsAttr));
    //Update to true
    dbEntity.setAttribute(isReplicatedAttr, Boolean.TRUE);
    //Update array
    final HashMap<String, String> params = new HashMap<String, String>() {

        {
            put("param1", "val1");
            put("param2", "val2");
        }
    };
    dbEntity.setAttribute(paramsAttr, params);
    //Complete update
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(dbEntity), false);
    AtlasEntity firstEntityUpdated = getEntityFromStore(response.getFirstUpdatedEntityByTypeName(TestUtilsV2.DATABASE_TYPE));
    Assert.assertNotNull(firstEntityUpdated);
    Assert.assertNotNull(firstEntityUpdated.getAttribute(isReplicatedAttr));
    Assert.assertEquals(firstEntityUpdated.getAttribute(isReplicatedAttr), Boolean.TRUE);
    Assert.assertEquals(firstEntityUpdated.getAttribute(paramsAttr), params);
//TODO - enable test after GET API is ready
//        init();
//        //Complete update without setting the attribute
//        AtlasEntity newEntity = TestUtilsV2.createDBEntity();
//        //Reset name to the current DB name
//        newEntity.setAttribute(AtlasClient.NAME, firstEntityCreated.getAttribute(AtlasClient.NAME));
//        response = entityStore.createOrUpdate(newEntity);
//
//        firstEntityUpdated = response.getFirstEntityUpdated();
//        Assert.assertNotNull(firstEntityUpdated.getAttribute(isReplicatedAttr));
//        Assert.assertEquals(firstEntityUpdated.getAttribute(isReplicatedAttr), Boolean.TRUE);
//        Assert.assertEquals(firstEntityUpdated.getAttribute(paramsAttr), params);
}
Also used : HashMap(java.util.HashMap) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) TestUtils.randomString(org.apache.atlas.TestUtils.randomString) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)124 Test (org.testng.annotations.Test)58 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)43 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)36 HashMap (java.util.HashMap)25 BeforeTest (org.testng.annotations.BeforeTest)25 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)24 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)24 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)21 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)17 ArrayList (java.util.ArrayList)16 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)14 Map (java.util.Map)12 List (java.util.List)11 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)11 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)9 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)9 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)7 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)7 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)7