Search in sources :

Example 96 with Referenceable

use of org.apache.atlas.typesystem.Referenceable in project incubator-atlas by apache.

the class DefaultMetadataServiceTest method testArrayOfStructs.

@Test
public void testArrayOfStructs() throws Exception {
    //Add array of structs
    TestUtils.dumpGraph(TestUtils.getGraph());
    final Struct partition1 = new Struct(TestUtils.PARTITION_STRUCT_TYPE);
    partition1.set(NAME, "part1");
    final Struct partition2 = new Struct(TestUtils.PARTITION_STRUCT_TYPE);
    partition2.set(NAME, "part2");
    List<Struct> partitions = new ArrayList<Struct>() {

        {
            add(partition1);
            add(partition2);
        }
    };
    table.set("partitions", partitions);
    String newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    String tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    Referenceable tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    List<Struct> partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    //add a new element to array of struct
    final Struct partition3 = new Struct(TestUtils.PARTITION_STRUCT_TYPE);
    partition3.set(NAME, "part3");
    partitions.add(partition3);
    table.set("partitions", partitions);
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    //remove one of the struct values
    partitions.remove(1);
    table.set("partitions", partitions);
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    //Update struct value within array of struct
    partitions.get(0).set(NAME, "part4");
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    //add a repeated element to array of struct
    final Struct partition4 = new Struct(TestUtils.PARTITION_STRUCT_TYPE);
    partition4.set(NAME, "part4");
    partitions.add(partition4);
    table.set("partitions", partitions);
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    // Remove all elements. Should set array attribute to null
    partitions.clear();
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNull(tableDefinition.get("partitions"));
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 97 with Referenceable

use of org.apache.atlas.typesystem.Referenceable in project incubator-atlas by apache.

the class DefaultMetadataServiceTest method testCreateEntityWithUniqueAttributeWithReference.

@Test
public void testCreateEntityWithUniqueAttributeWithReference() throws Exception {
    Referenceable db = createDBEntity();
    String dbId = TestUtils.createInstance(metadataService, db);
    //Assert that there is just 1 audit events and thats for entity create
    assertAuditEvents(dbId, 1);
    assertAuditEvents(dbId, EntityAuditEvent.EntityAuditAction.ENTITY_CREATE);
    Referenceable table = new Referenceable(TestUtils.TABLE_TYPE);
    table.set(NAME, TestUtils.randomString());
    table.set("description", "random table");
    table.set("type", "type");
    table.set("tableType", "MANAGED");
    table.set("database", new Id(dbId, 0, TestUtils.DATABASE_TYPE));
    table.set("databaseComposite", db);
    TestUtils.createInstance(metadataService, table);
    //table create should re-use the db instance created earlier
    String tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    Referenceable tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Referenceable actualDb = (Referenceable) tableDefinition.get("databaseComposite");
    assertEquals(actualDb.getId().id, dbId);
    //Assert that as part table create, db is not created and audit event is not added to db
    assertAuditEvents(dbId, 1);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) Id(org.apache.atlas.typesystem.persistence.Id) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 98 with Referenceable

use of org.apache.atlas.typesystem.Referenceable in project incubator-atlas by apache.

the class DefaultMetadataServiceTest method testSpecialCharacters.

@Test
public //See GraphHelper.encodePropertyKey()
void testSpecialCharacters() throws Exception {
    //Verify that type can be created with reserved characters in typename, attribute name
    String strAttrName = randomStrWithReservedChars();
    String arrayAttrName = randomStrWithReservedChars();
    String mapAttrName = randomStrWithReservedChars();
    HierarchicalTypeDefinition<ClassType> typeDefinition = createClassTypeDef("test_type_" + RandomStringUtils.randomAlphanumeric(10), ImmutableSet.<String>of(), createOptionalAttrDef(strAttrName, DataTypes.STRING_TYPE), new AttributeDefinition(arrayAttrName, DataTypes.arrayTypeName(DataTypes.STRING_TYPE.getName()), Multiplicity.OPTIONAL, false, null), new AttributeDefinition(mapAttrName, DataTypes.mapTypeName(DataTypes.STRING_TYPE.getName(), DataTypes.STRING_TYPE.getName()), Multiplicity.OPTIONAL, false, null));
    metadataService.createType(TypesSerialization.toJson(typeDefinition, false));
    //verify that entity can be created with reserved characters in string value, array value and map key and value
    Referenceable entity = new Referenceable(typeDefinition.typeName);
    entity.set(strAttrName, randomStrWithReservedChars());
    entity.set(arrayAttrName, new ArrayList<String>() {

        {
            add(randomStrWithReservedChars());
        }
    });
    entity.set(mapAttrName, new HashMap<String, String>() {

        {
            put(randomStrWithReservedChars(), randomStrWithReservedChars());
        }
    });
    String id = createInstance(metadataService, entity);
    //Verify that get entity definition returns actual values with reserved characters
    Referenceable instance = InstanceSerialization.fromJsonReferenceable(metadataService.getEntityDefinitionJson(id), true);
    assertReferenceableEquals(instance, entity);
    //Verify that search with reserved characters works - for string attribute
    String query = String.format("`%s` where `%s` = '%s'", typeDefinition.typeName, strAttrName, entity.get(strAttrName));
    String responseJson = discoveryService.searchByDSL(query, new QueryParams(1, 0));
    JSONObject response = new JSONObject(responseJson);
    assertEquals(response.getJSONArray("rows").length(), 1);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) JSONObject(org.codehaus.jettison.json.JSONObject) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) QueryParams(org.apache.atlas.query.QueryParams) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 99 with Referenceable

use of org.apache.atlas.typesystem.Referenceable in project incubator-atlas by apache.

the class DefaultMetadataServiceTest method testStructs.

@Test
public void testStructs() throws Exception {
    Struct serdeInstance = new Struct(TestUtils.SERDE_TYPE);
    serdeInstance.set(NAME, "serde1Name");
    serdeInstance.set("serde", "test");
    serdeInstance.set("description", "testDesc");
    table.set("serde1", serdeInstance);
    String newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    String tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    Referenceable tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("serde1"));
    Assert.assertTrue(serdeInstance.equalsContents(tableDefinition.get("serde1")));
    //update struct attribute
    serdeInstance.set("serde", "testUpdated");
    updateInstance(table);
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertTrue(serdeInstance.equalsContents(tableDefinition.get("serde1")));
    //set to null
    serdeInstance.setNull("description");
    updateInstance(table);
    tableDefinitionJson = metadataService.getEntityDefinitionJson(tableId._getId());
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNull(((Struct) tableDefinition.get("serde1")).get("description"));
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 100 with Referenceable

use of org.apache.atlas.typesystem.Referenceable in project incubator-atlas by apache.

the class DefaultMetadataServiceTest method verifyArrayUpdates.

private void verifyArrayUpdates(String typeName, String uniqAttrName, String uniqAttrValue, List<Referenceable> expectedArray, String arrAttrName) throws AtlasException {
    String json = metadataService.getEntityDefinition(typeName, uniqAttrName, uniqAttrValue);
    Referenceable entityDefinition = InstanceSerialization.fromJsonReferenceable(json, true);
    List<Referenceable> actualArray = (List<Referenceable>) entityDefinition.get(arrAttrName);
    if (expectedArray == null && actualArray != null) {
        //all are marked as deleted in case of soft delete
        for (Referenceable referenceable : actualArray) {
            assertEquals(referenceable.getId().state, Id.EntityState.DELETED);
        }
    } else if (expectedArray == null) {
        //hard delete case
        assertNull(actualArray);
    } else {
        int index;
        for (index = 0; index < expectedArray.size(); index++) {
            Assert.assertTrue(actualArray.get(index).equalsContents(expectedArray.get(index)));
        }
        //Rest of the entities in the list are marked as deleted
        for (; index < actualArray.size(); index++) {
            assertEquals(actualArray.get(index).getId().state, Id.EntityState.DELETED);
        }
    }
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

Referenceable (org.apache.atlas.typesystem.Referenceable)235 Test (org.testng.annotations.Test)114 Id (org.apache.atlas.typesystem.persistence.Id)50 ArrayList (java.util.ArrayList)45 List (java.util.List)25 Struct (org.apache.atlas.typesystem.Struct)25 HashMap (java.util.HashMap)24 BeforeTest (org.testng.annotations.BeforeTest)24 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)22 AfterTest (org.testng.annotations.AfterTest)22 HookNotification (org.apache.atlas.notification.hook.HookNotification)20 IStruct (org.apache.atlas.typesystem.IStruct)18 ClassType (org.apache.atlas.typesystem.types.ClassType)16 JSONObject (org.codehaus.jettison.json.JSONObject)16 ImmutableList (com.google.common.collect.ImmutableList)15 AtlasServiceException (org.apache.atlas.AtlasServiceException)14 TraitType (org.apache.atlas.typesystem.types.TraitType)12 WriteEntity (org.apache.hadoop.hive.ql.hooks.WriteEntity)12 Date (java.util.Date)11 AtlasException (org.apache.atlas.AtlasException)11