Search in sources :

Example 1 with TestUtilsV2.randomString

use of org.apache.atlas.TestUtilsV2.randomString in project atlas by apache.

the class AtlasEntityStoreV1Test method testCreateRequiredAttrNull.

@Test(expectedExceptions = AtlasBaseException.class)
public void testCreateRequiredAttrNull() throws Exception {
    // Update required attribute
    Map<String, AtlasEntity> tableCloneMap = new HashMap<>();
    AtlasEntity tableEntity = new AtlasEntity(TABLE_TYPE);
    tableEntity.setAttribute(TestUtilsV2.NAME, "table_" + TestUtilsV2.randomString());
    tableCloneMap.put(tableEntity.getGuid(), tableEntity);
    entityStore.createOrUpdate(new InMemoryMapEntityStream(tableCloneMap), false);
    Assert.fail("Expected exception while creating with required attribute null");
}
Also used : HashMap(java.util.HashMap) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) TestUtilsV2.randomString(org.apache.atlas.TestUtilsV2.randomString) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with TestUtilsV2.randomString

use of org.apache.atlas.TestUtilsV2.randomString in project atlas by apache.

the class AtlasEntityStoreV1Test method testUpdateEntityWithMap.

@Test(dependsOnMethods = "testCreate")
public void testUpdateEntityWithMap() throws Exception {
    AtlasEntity tableEntity = new AtlasEntity(tblEntity.getEntity());
    AtlasEntitiesWithExtInfo entitiesInfo = new AtlasEntitiesWithExtInfo(tableEntity);
    Map<String, AtlasStruct> partsMap = new HashMap<>();
    partsMap.put("part0", new AtlasStruct(TestUtilsV2.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "test"));
    tableEntity.setAttribute("partitionsMap", partsMap);
    init();
    EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition1 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDef1 = getEntityFromStore(tableDefinition1);
    validateEntity(entitiesInfo, updatedTableDef1);
    Assert.assertTrue(partsMap.get("part0").equals(((Map<String, AtlasStruct>) updatedTableDef1.getAttribute("partitionsMap")).get("part0")));
    // update map - add a map key
    partsMap.put("part1", new AtlasStruct(TestUtilsV2.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "test1"));
    tableEntity.setAttribute("partitionsMap", partsMap);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition2 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDef2 = getEntityFromStore(tableDefinition2);
    validateEntity(entitiesInfo, updatedTableDef2);
    assertEquals(((Map<String, AtlasStruct>) updatedTableDef2.getAttribute("partitionsMap")).size(), 2);
    Assert.assertTrue(partsMap.get("part1").equals(((Map<String, AtlasStruct>) updatedTableDef2.getAttribute("partitionsMap")).get("part1")));
    // update map - remove a key and add another key
    partsMap.remove("part0");
    partsMap.put("part2", new AtlasStruct(TestUtilsV2.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "test2"));
    tableEntity.setAttribute("partitionsMap", partsMap);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition3 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDef3 = getEntityFromStore(tableDefinition3);
    validateEntity(entitiesInfo, updatedTableDef3);
    assertEquals(((Map<String, AtlasStruct>) updatedTableDef3.getAttribute("partitionsMap")).size(), 2);
    Assert.assertNull(((Map<String, AtlasStruct>) updatedTableDef3.getAttribute("partitionsMap")).get("part0"));
    Assert.assertTrue(partsMap.get("part2").equals(((Map<String, AtlasStruct>) updatedTableDef3.getAttribute("partitionsMap")).get("part2")));
    // update struct value for existing map key
    AtlasStruct partition2 = partsMap.get("part2");
    partition2.setAttribute(TestUtilsV2.NAME, "test2Updated");
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition4 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDef4 = getEntityFromStore(tableDefinition4);
    validateEntity(entitiesInfo, updatedTableDef4);
    assertEquals(((Map<String, AtlasStruct>) updatedTableDef4.getAttribute("partitionsMap")).size(), 2);
    Assert.assertNull(((Map<String, AtlasStruct>) updatedTableDef4.getAttribute("partitionsMap")).get("part0"));
    Assert.assertTrue(partsMap.get("part2").equals(((Map<String, AtlasStruct>) updatedTableDef4.getAttribute("partitionsMap")).get("part2")));
    // Test map pointing to a class
    AtlasEntity col0 = new AtlasEntity(TestUtilsV2.COLUMN_TYPE, TestUtilsV2.NAME, "test1");
    col0.setAttribute("type", "string");
    col0.setAttribute("table", AtlasTypeUtil.getAtlasObjectId(tableEntity));
    AtlasEntityWithExtInfo col0WithExtendedInfo = new AtlasEntityWithExtInfo(col0);
    col0WithExtendedInfo.addReferredEntity(tableEntity);
    col0WithExtendedInfo.addReferredEntity(dbEntity.getEntity());
    init();
    entityStore.createOrUpdate(new AtlasEntityStream(col0WithExtendedInfo), false);
    AtlasEntity col1 = new AtlasEntity(TestUtilsV2.COLUMN_TYPE, TestUtilsV2.NAME, "test2");
    col1.setAttribute("type", "string");
    col1.setAttribute("table", AtlasTypeUtil.getAtlasObjectId(tableEntity));
    AtlasEntityWithExtInfo col1WithExtendedInfo = new AtlasEntityWithExtInfo(col1);
    col1WithExtendedInfo.addReferredEntity(tableEntity);
    col1WithExtendedInfo.addReferredEntity(dbEntity.getEntity());
    init();
    entityStore.createOrUpdate(new AtlasEntityStream(col1WithExtendedInfo), false);
    Map<String, AtlasObjectId> columnsMap = new HashMap<String, AtlasObjectId>();
    columnsMap.put("col0", AtlasTypeUtil.getAtlasObjectId(col0));
    columnsMap.put("col1", AtlasTypeUtil.getAtlasObjectId(col1));
    tableEntity.setAttribute(TestUtilsV2.COLUMNS_MAP, columnsMap);
    entitiesInfo.addReferredEntity(col0);
    entitiesInfo.addReferredEntity(col1);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition5 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(tableDefinition5));
    // Swap elements
    columnsMap.clear();
    columnsMap.put("col0", AtlasTypeUtil.getAtlasObjectId(col1));
    columnsMap.put("col1", AtlasTypeUtil.getAtlasObjectId(col0));
    tableEntity.setAttribute(TestUtilsV2.COLUMNS_MAP, columnsMap);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition6 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(tableDefinition6));
    Assert.assertEquals(entityStore.getById(col0.getGuid()).getEntity().getStatus(), AtlasEntity.Status.ACTIVE);
    Assert.assertEquals(entityStore.getById(col1.getGuid()).getEntity().getStatus(), AtlasEntity.Status.ACTIVE);
    // Drop the first key and change the class type as well to col0
    columnsMap.clear();
    columnsMap.put("col0", AtlasTypeUtil.getAtlasObjectId(col0));
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition7 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(tableDefinition7));
    // Clear state
    tableEntity.setAttribute(TestUtilsV2.COLUMNS_MAP, null);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition8 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(tableDefinition8));
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) HashMap(java.util.HashMap) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) TestUtilsV2.randomString(org.apache.atlas.TestUtilsV2.randomString) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with TestUtilsV2.randomString

use of org.apache.atlas.TestUtilsV2.randomString in project 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, e.getMessage() => " + e.getMessage());
    }
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) TestUtilsV2.randomString(org.apache.atlas.TestUtilsV2.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 4 with TestUtilsV2.randomString

use of org.apache.atlas.TestUtilsV2.randomString in project atlas by apache.

the class AtlasEntityStoreV1Test method testSpecialCharacters.

@Test(enabled = false)
public // TODO : Failing in typedef creation
void testSpecialCharacters() throws Exception {
    // Verify that type can be created with reserved characters in typename, attribute name
    final String typeName = TestUtilsV2.randomString(10);
    String strAttrName = randomStrWithReservedChars();
    String arrayAttrName = randomStrWithReservedChars();
    String mapAttrName = randomStrWithReservedChars();
    AtlasEntityDef typeDefinition = AtlasTypeUtil.createClassTypeDef(typeName, "Special chars test type", ImmutableSet.<String>of(), AtlasTypeUtil.createOptionalAttrDef(strAttrName, "string"), AtlasTypeUtil.createOptionalAttrDef(arrayAttrName, "array<string>"), AtlasTypeUtil.createOptionalAttrDef(mapAttrName, "map<string,string>"));
    AtlasTypesDef atlasTypesDef = new AtlasTypesDef(null, null, null, Arrays.asList(typeDefinition));
    typeDefStore.createTypesDef(atlasTypesDef);
    // verify that entity can be created with reserved characters in string value, array value and map key and value
    AtlasEntity entity = new AtlasEntity();
    entity.setAttribute(strAttrName, randomStrWithReservedChars());
    entity.setAttribute(arrayAttrName, new String[] { randomStrWithReservedChars() });
    entity.setAttribute(mapAttrName, new HashMap<String, String>() {

        {
            put(randomStrWithReservedChars(), randomStrWithReservedChars());
        }
    });
    AtlasEntityWithExtInfo entityWithExtInfo = new AtlasEntityWithExtInfo(entity);
    final EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entityWithExtInfo), false);
    final AtlasEntityHeader firstEntityCreated = response.getFirstEntityCreated();
    validateEntity(entityWithExtInfo, getEntityFromStore(firstEntityCreated));
// Verify that search with reserved characters works - for string attribute
// String query =
// String.format("`%s` where `%s` = '%s'", typeName, strAttrName, entity.getAttribute(strAttrName));
// String responseJson = discoveryService.searchByDSL(query, new QueryParams(1, 0));
// JSONObject response = new JSONObject(responseJson);
// assertEquals(response.getJSONArray("rows").length(), 1);
}
Also used : AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) TestUtilsV2.randomString(org.apache.atlas.TestUtilsV2.randomString) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with TestUtilsV2.randomString

use of org.apache.atlas.TestUtilsV2.randomString in project 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) TestUtilsV2.randomString(org.apache.atlas.TestUtilsV2.randomString) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

TestUtilsV2.randomString (org.apache.atlas.TestUtilsV2.randomString)6 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)6 BeforeTest (org.testng.annotations.BeforeTest)6 Test (org.testng.annotations.Test)6 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)5 HashMap (java.util.HashMap)4 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)4 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)2 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)2 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)2 BookingBeanSer (com.github.drbookings.model.ser.BookingBeanSer)1 CleaningBeanSer (com.github.drbookings.model.ser.CleaningBeanSer)1 DataStore (com.github.drbookings.ser.DataStore)1 XMLStorage (com.github.drbookings.ser.XMLStorage)1 RandomString (com.github.ktools1000.RandomString)1 BackupCreator (com.github.ktools1000.io.BackupCreator)1 File (java.io.File)1 Map (java.util.Map)1 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)1 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)1