Search in sources :

Example 6 with Struct

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

the class EntityJerseyResourceIT method testAddTraitWithAttribute.

@Test
public void testAddTraitWithAttribute() throws Exception {
    String dbName = "db" + randomString();
    String tableName = "table" + randomString();
    Referenceable hiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    Id dbId = createInstance(hiveDBInstance);
    Referenceable hiveTableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
    Id id = createInstance(hiveTableInstance);
    final String guid = id._getId();
    try {
        Assert.assertNotNull(UUID.fromString(guid));
    } catch (IllegalArgumentException e) {
        Assert.fail("Response is not a guid, " + guid);
    }
    final String traitName = "PII_Trait" + randomString();
    HierarchicalTypeDefinition<TraitType> piiTrait = TypesUtil.createTraitTypeDef(traitName, ImmutableSet.<String>of(), TypesUtil.createRequiredAttrDef("type", DataTypes.STRING_TYPE));
    String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
    LOG.debug("traitDefinitionAsJSON = {}", traitDefinitionAsJSON);
    createType(traitDefinitionAsJSON);
    Struct traitInstance = new Struct(traitName);
    traitInstance.set("type", "SSN");
    atlasClientV1.addTrait(guid, traitInstance);
    // verify the response
    Referenceable entity = atlasClientV1.getEntity(guid);
    Assert.assertNotNull(entity);
    Assert.assertEquals(entity.getId()._getId(), guid);
    assertNotNull(entity.getTrait(traitName));
    assertEquals(entity.getTrait(traitName).get("type"), traitInstance.get("type"));
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) TraitType(org.apache.atlas.typesystem.types.TraitType) Id(org.apache.atlas.typesystem.persistence.Id) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 7 with Struct

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

the class EntityJerseyResourceIT method testAddTraitWithNoRegistration.

@Test(expectedExceptions = AtlasServiceException.class)
public void testAddTraitWithNoRegistration() throws Exception {
    final String traitName = "PII_Trait" + randomString();
    HierarchicalTypeDefinition<TraitType> piiTrait = TypesUtil.createTraitTypeDef(traitName, ImmutableSet.<String>of());
    String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
    LOG.debug("traitDefinitionAsJSON = {}", traitDefinitionAsJSON);
    Struct traitInstance = new Struct(traitName);
    String traitInstanceAsJSON = InstanceSerialization$.MODULE$.toJson(traitInstance, true);
    LOG.debug("traitInstanceAsJSON = {}", traitInstanceAsJSON);
    atlasClientV1.callAPIWithBodyAndParams(AtlasClient.API.CREATE_ENTITY, traitInstanceAsJSON, "random", TRAITS);
}
Also used : TraitType(org.apache.atlas.typesystem.types.TraitType) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 8 with Struct

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

the class EntityJerseyResourceIT method testGetTraitDefinitionForEntity.

@Test
public void testGetTraitDefinitionForEntity() throws Exception {
    String dbName = "db" + randomString();
    String tableName = "table" + randomString();
    Referenceable hiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    Id dbId = createInstance(hiveDBInstance);
    Referenceable hiveTableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
    Id id = createInstance(hiveTableInstance);
    final String guid = id._getId();
    try {
        Assert.assertNotNull(UUID.fromString(guid));
    } catch (IllegalArgumentException e) {
        Assert.fail("Response is not a guid, " + guid);
    }
    String traitName = "PII_Trait" + randomString();
    HierarchicalTypeDefinition<TraitType> piiTrait = TypesUtil.createTraitTypeDef(traitName, ImmutableSet.<String>of());
    String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
    LOG.debug("traitDefinitionAsJSON = {}", traitDefinitionAsJSON);
    createType(traitDefinitionAsJSON);
    Struct traitInstance = new Struct(traitName);
    atlasClientV1.addTrait(guid, traitInstance);
    Struct traitDef = atlasClientV1.getTraitDefinition(guid, traitName);
    Assert.assertEquals(traitDef.getTypeName(), traitName);
    List<Struct> allTraitDefs = atlasClientV1.listTraitDefinitions(guid);
    System.out.println(allTraitDefs.toString());
    Assert.assertEquals(allTraitDefs.size(), 8);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) TraitType(org.apache.atlas.typesystem.types.TraitType) Id(org.apache.atlas.typesystem.persistence.Id) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 9 with Struct

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

the class AtlasStructFormatConverter method fromV2ToV1.

@Override
public Object fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext converterContext) throws AtlasBaseException {
    Struct ret = null;
    if (v2Obj != null) {
        AtlasStructType structType = (AtlasStructType) type;
        if (v2Obj instanceof Map) {
            final Map v2Map = (Map) v2Obj;
            final Map v2Attribs;
            if (v2Map.containsKey(ATTRIBUTES_PROPERTY_KEY)) {
                v2Attribs = (Map) v2Map.get(ATTRIBUTES_PROPERTY_KEY);
            } else {
                v2Attribs = v2Map;
            }
            if (MapUtils.isNotEmpty(v2Attribs)) {
                ret = new Struct(type.getTypeName(), fromV2ToV1(structType, v2Attribs, converterContext));
            } else {
                ret = new Struct(type.getTypeName());
            }
        } else if (v2Obj instanceof AtlasStruct) {
            AtlasStruct struct = (AtlasStruct) v2Obj;
            ret = new Struct(type.getTypeName(), fromV2ToV1(structType, struct.getAttributes(), converterContext));
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or AtlasStruct", v2Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasStructType(org.apache.atlas.type.AtlasStructType) HashMap(java.util.HashMap) Map(java.util.Map) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Example 10 with Struct

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

the class GraphBackedMetadataRepositoryDeleteTestBase method createDbTableGraph.

private void createDbTableGraph(String dbName, String tableName) throws Exception {
    Referenceable databaseInstance = new Referenceable(TestUtils.DATABASE_TYPE);
    databaseInstance.set("name", dbName);
    databaseInstance.set("description", "foo database");
    ClassType dbType = typeSystem.getDataType(ClassType.class, TestUtils.DATABASE_TYPE);
    ITypedReferenceableInstance db = dbType.convert(databaseInstance, Multiplicity.REQUIRED);
    Referenceable tableInstance = new Referenceable(TestUtils.TABLE_TYPE, TestUtils.CLASSIFICATION);
    tableInstance.set("name", tableName);
    tableInstance.set("description", "bar table");
    tableInstance.set("type", "managed");
    Struct traitInstance = (Struct) tableInstance.getTrait(TestUtils.CLASSIFICATION);
    traitInstance.set("tag", "foundation_etl");
    // enum
    tableInstance.set("tableType", 1);
    tableInstance.set("database", databaseInstance);
    ArrayList<Referenceable> columns = new ArrayList<>();
    for (int index = 0; index < 5; index++) {
        Referenceable columnInstance = new Referenceable("column_type");
        final String name = "column_" + index;
        columnInstance.set("name", name);
        columnInstance.set("type", "string");
        columns.add(columnInstance);
    }
    tableInstance.set("columns", columns);
    ClassType tableType = typeSystem.getDataType(ClassType.class, TestUtils.TABLE_TYPE);
    ITypedReferenceableInstance table = tableType.convert(tableInstance, Multiplicity.REQUIRED);
    repositoryService.createEntities(db, table);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

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