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"));
}
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);
}
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);
}
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;
}
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);
}
Aggregations