Search in sources :

Example 56 with Struct

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

the class DefaultMetadataServiceTest method testUpdateEntityWithMap.

@Test
public void testUpdateEntityWithMap() throws Exception {
    final Map<String, Struct> partsMap = new HashMap<>();
    partsMap.put("part0", new Struct(TestUtils.PARTITION_STRUCT_TYPE, new HashMap<String, Object>() {

        {
            put(NAME, "test");
        }
    }));
    table.set("partitionsMap", partsMap);
    updateInstance(table);
    String tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    Referenceable tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertTrue(partsMap.get("part0").equalsContents(((Map<String, Struct>) tableDefinition.get("partitionsMap")).get("part0")));
    //update map - add a map key
    partsMap.put("part1", new Struct(TestUtils.PARTITION_STRUCT_TYPE, new HashMap<String, Object>() {

        {
            put(NAME, "test1");
        }
    }));
    table.set("partitionsMap", partsMap);
    updateInstance(table);
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    assertEquals(((Map<String, Struct>) tableDefinition.get("partitionsMap")).size(), 2);
    Assert.assertTrue(partsMap.get("part1").equalsContents(((Map<String, Struct>) tableDefinition.get("partitionsMap")).get("part1")));
    //update map - remove a key and add another key
    partsMap.remove("part0");
    partsMap.put("part2", new Struct(TestUtils.PARTITION_STRUCT_TYPE, new HashMap<String, Object>() {

        {
            put(NAME, "test2");
        }
    }));
    table.set("partitionsMap", partsMap);
    updateInstance(table);
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    assertEquals(((Map<String, Struct>) tableDefinition.get("partitionsMap")).size(), 2);
    Assert.assertNull(((Map<String, Struct>) tableDefinition.get("partitionsMap")).get("part0"));
    Assert.assertTrue(partsMap.get("part2").equalsContents(((Map<String, Struct>) tableDefinition.get("partitionsMap")).get("part2")));
    //update struct value for existing map key
    Struct partition2 = partsMap.get("part2");
    partition2.set(NAME, "test2Updated");
    updateInstance(table);
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    assertEquals(((Map<String, Struct>) tableDefinition.get("partitionsMap")).size(), 2);
    Assert.assertNull(((Map<String, Struct>) tableDefinition.get("partitionsMap")).get("part0"));
    Assert.assertTrue(partsMap.get("part2").equalsContents(((Map<String, Struct>) tableDefinition.get("partitionsMap")).get("part2")));
    //Test map pointing to a class
    final Map<String, Referenceable> columnsMap = new HashMap<>();
    Referenceable col0Type = new Referenceable(TestUtils.COLUMN_TYPE, new HashMap<String, Object>() {

        {
            put(NAME, "test1");
            put("type", "string");
        }
    });
    columnsMap.put("col0", col0Type);
    Referenceable col1Type = new Referenceable(TestUtils.COLUMN_TYPE, new HashMap<String, Object>() {

        {
            put(NAME, "test2");
            put("type", "string");
        }
    });
    columnsMap.put("col1", col1Type);
    table.set(TestUtils.COLUMNS_MAP, columnsMap);
    updateInstance(table);
    verifyMapUpdates(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME), columnsMap, TestUtils.COLUMNS_MAP);
    //Swap elements
    columnsMap.clear();
    columnsMap.put("col0", col1Type);
    columnsMap.put("col1", col0Type);
    table.set(TestUtils.COLUMNS_MAP, columnsMap);
    updateInstance(table);
    verifyMapUpdates(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME), columnsMap, TestUtils.COLUMNS_MAP);
    //Drop the first key and change the class type as well to col0
    columnsMap.clear();
    columnsMap.put("col0", col0Type);
    table.set(TestUtils.COLUMNS_MAP, columnsMap);
    updateInstance(table);
    verifyMapUpdates(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME), columnsMap, TestUtils.COLUMNS_MAP);
    //Clear state
    table.setNull(TestUtils.COLUMNS_MAP);
    updateInstance(table);
    verifyMapUpdates(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME), null, TestUtils.COLUMNS_MAP);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) HashMap(java.util.HashMap) JSONObject(org.codehaus.jettison.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) 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)

Aggregations

Struct (org.apache.atlas.typesystem.Struct)56 Test (org.testng.annotations.Test)36 IStruct (org.apache.atlas.typesystem.IStruct)29 Referenceable (org.apache.atlas.typesystem.Referenceable)25 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)17 TraitType (org.apache.atlas.typesystem.types.TraitType)12 ArrayList (java.util.ArrayList)9 LinkedList (java.util.LinkedList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Id (org.apache.atlas.typesystem.persistence.Id)7 JSONObject (org.codehaus.jettison.json.JSONObject)7 BeforeTest (org.testng.annotations.BeforeTest)6 AfterTest (org.testng.annotations.AfterTest)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ITypedInstance (org.apache.atlas.typesystem.ITypedInstance)4 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)4 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)4 Date (java.util.Date)3