use of org.apache.atlas.typesystem.types.AttributeDefinition in project incubator-atlas by apache.
the class SerializationJavaTest method test1.
/*
* Class Hierarchy is:
* Department(name : String, employees : Array[Person])
* Person(name : String, department : Department, manager : Manager)
* Manager(subordinates : Array[Person]) extends Person
*
* Persons can have SecurityClearance(level : Int) clearance.
*/
@Test
public void test1() throws AtlasException {
TypeSystem ts = getTypeSystem();
HierarchicalTypeDefinition<ClassType> deptTypeDef = createClassTypeDef("Department", ImmutableSet.<String>of(), createRequiredAttrDef("name", DataTypes.STRING_TYPE), new AttributeDefinition("employees", String.format("array<%s>", "Person"), Multiplicity.COLLECTION, true, "department"));
HierarchicalTypeDefinition<ClassType> personTypeDef = createClassTypeDef("Person", ImmutableSet.<String>of(), createRequiredAttrDef("name", DataTypes.STRING_TYPE), new AttributeDefinition("department", "Department", Multiplicity.REQUIRED, false, "employees"), new AttributeDefinition("manager", "Manager", Multiplicity.OPTIONAL, false, "subordinates"));
HierarchicalTypeDefinition<ClassType> managerTypeDef = createClassTypeDef("Manager", ImmutableSet.of("Person"), new AttributeDefinition("subordinates", String.format("array<%s>", "Person"), Multiplicity.COLLECTION, false, "manager"));
HierarchicalTypeDefinition<TraitType> securityClearanceTypeDef = createTraitTypeDef("SecurityClearance", ImmutableSet.<String>of(), createRequiredAttrDef("level", DataTypes.INT_TYPE));
ts.defineTypes(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), ImmutableList.of(securityClearanceTypeDef), ImmutableList.of(deptTypeDef, personTypeDef, managerTypeDef));
Referenceable hrDept = new Referenceable("Department");
Referenceable john = new Referenceable("Person");
Referenceable jane = new Referenceable("Manager", "SecurityClearance");
hrDept.set("name", "hr");
john.set("name", "John");
john.set("department", hrDept);
jane.set("name", "Jane");
jane.set("department", hrDept);
john.set("manager", jane);
hrDept.set("employees", ImmutableList.of(john, jane));
jane.set("subordinates", ImmutableList.of(john));
jane.getTrait("SecurityClearance").set("level", 1);
ClassType deptType = ts.getDataType(ClassType.class, "Department");
ITypedReferenceableInstance hrDept2 = deptType.convert(hrDept, Multiplicity.REQUIRED);
String hrDeptStr = hrDept2.toString();
Assert.assertEquals(hrDeptStr, "{\n" + "\tid : (type: Department, id: <unassigned>)\n" + "\tname : \thr\n" + "\temployees : \t[{\n" + "\tid : (type: Person, id: <unassigned>)\n" + "\tname : \tJohn\n" + "\tdepartment : (type: Department, id: <unassigned>)\n" + "\tmanager : (type: Manager, id: <unassigned>)\n" + "}, {\n" + "\tid : (type: Manager, id: <unassigned>)\n" + "\tsubordinates : \t[{\n" + "\tid : (type: Person, id: <unassigned>)\n" + "\tname : \tJohn\n" + "\tdepartment : (type: Department, id: <unassigned>)\n" + "\tmanager : (type: Manager, id: <unassigned>)\n" + "}]\n" + "\tname : \tJane\n" + "\tdepartment : (type: Department, id: <unassigned>)\n" + "\tmanager : <null>\n" + "\n" + "\tSecurityClearance : \t{\n" + "\t\tlevel : \t\t1\n" + "\t}}]\n" + "}");
String jsonStr = Serialization$.MODULE$.toJson(hrDept2);
//System.out.println(jsonStr);
hrDept2 = Serialization$.MODULE$.fromJson(jsonStr);
Assert.assertEquals(hrDept2.toString(), hrDeptStr);
}
use of org.apache.atlas.typesystem.types.AttributeDefinition in project incubator-atlas by apache.
the class TypesJerseyResourceIT method createHiveTypes.
private List<HierarchicalTypeDefinition> createHiveTypes() throws Exception {
ArrayList<HierarchicalTypeDefinition> typeDefinitions = new ArrayList<>();
HierarchicalTypeDefinition<ClassType> databaseTypeDefinition = TypesUtil.createClassTypeDef("database", ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef(NAME, DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef(DESCRIPTION, DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef(QUALIFIED_NAME, DataTypes.STRING_TYPE));
typeDefinitions.add(databaseTypeDefinition);
HierarchicalTypeDefinition<ClassType> tableTypeDefinition = TypesUtil.createClassTypeDef("table", ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef(NAME, DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef(DESCRIPTION, DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef(QUALIFIED_NAME, DataTypes.STRING_TYPE), createOptionalAttrDef("columnNames", DataTypes.arrayTypeName(DataTypes.STRING_TYPE)), createOptionalAttrDef("created", DataTypes.DATE_TYPE), createOptionalAttrDef("parameters", DataTypes.mapTypeName(DataTypes.STRING_TYPE, DataTypes.STRING_TYPE)), TypesUtil.createRequiredAttrDef("type", DataTypes.STRING_TYPE), new AttributeDefinition("database", "database", Multiplicity.REQUIRED, false, "database"));
typeDefinitions.add(tableTypeDefinition);
HierarchicalTypeDefinition<TraitType> fetlTypeDefinition = TypesUtil.createTraitTypeDef("fetl", ImmutableSet.<String>of(), TypesUtil.createRequiredAttrDef("level", DataTypes.INT_TYPE));
typeDefinitions.add(fetlTypeDefinition);
return typeDefinitions;
}
use of org.apache.atlas.typesystem.types.AttributeDefinition in project incubator-atlas by apache.
the class TypesJerseyResourceIT method testGetDefinition.
@Test(dependsOnMethods = "testSubmit")
public void testGetDefinition() throws Exception {
for (HierarchicalTypeDefinition typeDefinition : typeDefinitions) {
System.out.println("typeName = " + typeDefinition.typeName);
JSONObject response = atlasClientV1.callAPIWithBodyAndParams(AtlasClient.API.LIST_TYPES, null, typeDefinition.typeName);
Assert.assertNotNull(response);
Assert.assertNotNull(response.get(AtlasClient.DEFINITION));
Assert.assertNotNull(response.get(AtlasClient.REQUEST_ID));
String typesJson = response.getString(AtlasClient.DEFINITION);
final TypesDef typesDef = TypesSerialization.fromJson(typesJson);
List<HierarchicalTypeDefinition<ClassType>> hierarchicalTypeDefinitions = typesDef.classTypesAsJavaList();
for (HierarchicalTypeDefinition<ClassType> classType : hierarchicalTypeDefinitions) {
for (AttributeDefinition attrDef : classType.attributeDefinitions) {
if (NAME.equals(attrDef.name)) {
assertEquals(attrDef.isIndexable, true);
assertEquals(attrDef.isUnique, true);
}
}
}
}
}
use of org.apache.atlas.typesystem.types.AttributeDefinition in project incubator-atlas by apache.
the class DefaultTypeSystem method createType.
private <T extends HierarchicalType> void createType(Collection<AttributeDefinition> attributes, Class<T> type, String name, String description, boolean isTrait) throws ResourceAlreadyExistsException {
try {
List<AtlasStructDef.AtlasAttributeDef> attrDefs = new ArrayList<>();
for (AttributeDefinition attrDefinition : attributes) {
attrDefs.add(TypeConverterUtil.toAtlasAttributeDef(attrDefinition));
}
if (isTrait) {
AtlasClassificationDef classificationDef = new AtlasClassificationDef(name, description, "1.0", attrDefs, ImmutableSet.of(TaxonomyResourceProvider.TAXONOMY_TERM_TYPE));
AtlasTypesDef typesDef = new AtlasTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.of(classificationDef), ImmutableList.<AtlasEntityDef>of());
typeDefStore.createTypesDef(typesDef);
} else {
AtlasEntityDef entityDef = new AtlasEntityDef(name, description, "1.0", attrDefs);
AtlasTypesDef typesDef = new AtlasTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.of(entityDef));
typeDefStore.createTypesDef(typesDef);
}
} catch (AtlasBaseException e) {
if (e.getAtlasErrorCode() == AtlasErrorCode.TYPE_ALREADY_EXISTS) {
throw new ResourceAlreadyExistsException(String.format("Type '%s' already exists", name));
} else {
throw new CatalogRuntimeException(String.format("Unable to create type '%s' in type system: %s", name, e), e);
}
}
}
use of org.apache.atlas.typesystem.types.AttributeDefinition in project incubator-atlas by apache.
the class TermResourceDefinitionTest method testGetPropertyDefinitions.
@Test
public void testGetPropertyDefinitions() {
ResourceDefinition termDefinition = new TermResourceDefinition();
Collection<AttributeDefinition> propertyDefinitions = termDefinition.getPropertyDefinitions();
assertEquals(propertyDefinitions.size(), 4);
Set<String> defNames = new HashSet<>();
for (AttributeDefinition def : propertyDefinitions) {
defNames.add(def.name);
}
assertTrue(defNames.contains("name"));
assertTrue(defNames.contains("description"));
assertTrue(defNames.contains("available_as_tag"));
assertTrue(defNames.contains("acceptable_use"));
}
Aggregations