use of org.apache.atlas.typesystem.types.TypeSystem in project incubator-atlas by apache.
the class FieldMappingTest method testOutputStruct.
@Test
public void testOutputStruct() throws Exception {
// ATLAS-645: verify that FieldMapping.output(IStruct) does not infinitely recurse
// when an IStruct and ITypedReferenceableInstance reference each other.
HierarchicalTypeDefinition<ClassType> valueDef = TypesUtil.createClassTypeDef("Value", ImmutableSet.<String>of(), new AttributeDefinition("owner", "Owner", Multiplicity.OPTIONAL, false, null));
// Define struct type with reference, where the value is a class reference to Value.
StructTypeDefinition ownerDef = TypesUtil.createStructTypeDef("Owner", new AttributeDefinition("value", "Value", Multiplicity.OPTIONAL, false, null));
TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.of(ownerDef), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.of(valueDef));
TypeSystem typeSystem = TypeSystem.getInstance();
typeSystem.reset();
typeSystem.defineTypes(typesDef);
StructType ownerType = typeSystem.getDataType(StructType.class, "Owner");
ClassType valueType = typeSystem.getDataType(ClassType.class, "Value");
// Prior to fix for ATLAS-645, this call would throw a StackOverflowError
try {
ownerType.toString();
} catch (StackOverflowError e) {
Assert.fail("Infinite recursion in StructType.toString() caused StackOverflowError");
}
// Create instances of Owner and Value that reference each other.
ITypedStruct ownerInstance = ownerType.createInstance();
ITypedReferenceableInstance valueInstance = valueType.createInstance();
// Set Owner.value reference to Value instance.
ownerInstance.set("value", valueInstance);
// Set Value.owner reference on Owner instance.
valueInstance.set("owner", ownerInstance);
// Prior to fix for ATLAS-645, this call would throw a StackOverflowError
try {
ownerInstance.fieldMapping().output(ownerInstance, new StringBuilder(), "", null);
} catch (StackOverflowError e) {
Assert.fail("Infinite recursion in FieldMapping.output() caused StackOverflowError");
}
}
use of org.apache.atlas.typesystem.types.TypeSystem in project incubator-atlas by apache.
the class FieldMappingTest method beforeTest.
@BeforeTest
public void beforeTest() throws Exception {
TypeSystem typeSystem = TypeSystem.getInstance();
typeSystem.reset();
}
use of org.apache.atlas.typesystem.types.TypeSystem 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.TypeSystem in project incubator-atlas by apache.
the class EntityNotificationImplTest method testGetAllTraitsSuperTraits.
@Test
public void testGetAllTraitsSuperTraits() throws Exception {
TypeSystem typeSystem = mock(TypeSystem.class);
String traitName = "MyTrait";
IStruct myTrait = new Struct(traitName);
String superTraitName = "MySuperTrait";
TraitType traitDef = mock(TraitType.class);
Set<String> superTypeNames = Collections.singleton(superTraitName);
TraitType superTraitDef = mock(TraitType.class);
Set<String> superSuperTypeNames = Collections.emptySet();
Referenceable entity = getEntity("id", myTrait);
when(typeSystem.getDataType(TraitType.class, traitName)).thenReturn(traitDef);
when(typeSystem.getDataType(TraitType.class, superTraitName)).thenReturn(superTraitDef);
when(traitDef.getAllSuperTypeNames()).thenReturn(superTypeNames);
when(superTraitDef.getAllSuperTypeNames()).thenReturn(superSuperTypeNames);
EntityNotificationImpl entityNotification = new EntityNotificationImpl(entity, EntityNotification.OperationType.TRAIT_ADD, typeSystem);
List<IStruct> allTraits = entityNotification.getAllTraits();
assertEquals(2, allTraits.size());
for (IStruct trait : allTraits) {
String typeName = trait.getTypeName();
assertTrue(typeName.equals(traitName) || typeName.equals(superTraitName));
}
}
use of org.apache.atlas.typesystem.types.TypeSystem in project incubator-atlas by apache.
the class QueryProcessorTest method findTypeLookupsDuringQueryParsing.
private ValidatingTypeCache findTypeLookupsDuringQueryParsing(String query) throws AtlasException {
TypeSystem typeSystem = TypeSystem.getInstance();
ValidatingTypeCache result = new ValidatingTypeCache();
typeSystem.setTypeCache(result);
typeSystem.reset();
HierarchicalTypeDefinition<ClassType> hiveTypeDef = createClassTypeDef("hive_db", "", ImmutableSet.<String>of(), createRequiredAttrDef("name", DataTypes.STRING_TYPE), createRequiredAttrDef("tableCount", DataTypes.INT_TYPE));
typeSystem.defineClassType(hiveTypeDef);
Either<Parsers.NoSuccess, Expressions.Expression> either = QueryParser.apply(query, null);
Expressions.Expression expression = either.right().get();
QueryProcessor.validate(expression);
return result;
}
Aggregations