Search in sources :

Example 6 with TypeSystem

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");
    }
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) TypesDef(org.apache.atlas.typesystem.TypesDef) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) ClassType(org.apache.atlas.typesystem.types.ClassType) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) StructTypeDefinition(org.apache.atlas.typesystem.types.StructTypeDefinition) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with TypeSystem

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();
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with TypeSystem

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);
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) Referenceable(org.apache.atlas.typesystem.Referenceable) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test) BaseTest(org.apache.atlas.typesystem.types.BaseTest)

Example 9 with TypeSystem

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));
    }
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) Referenceable(org.apache.atlas.typesystem.Referenceable) TraitType(org.apache.atlas.typesystem.types.TraitType) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 10 with TypeSystem

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;
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) ClassType(org.apache.atlas.typesystem.types.ClassType)

Aggregations

TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)14 Test (org.testng.annotations.Test)9 ClassType (org.apache.atlas.typesystem.types.ClassType)8 TraitType (org.apache.atlas.typesystem.types.TraitType)6 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)5 Referenceable (org.apache.atlas.typesystem.Referenceable)4 Struct (org.apache.atlas.typesystem.Struct)3 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)3 BeforeTest (org.testng.annotations.BeforeTest)3 IStruct (org.apache.atlas.typesystem.IStruct)2 TypesDef (org.apache.atlas.typesystem.TypesDef)2 BaseTest (org.apache.atlas.typesystem.types.BaseTest)2 EnumValue (org.apache.atlas.typesystem.types.EnumValue)2 BeforeClass (org.testng.annotations.BeforeClass)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)1 AtlasGraphManagement (org.apache.atlas.repository.graphdb.AtlasGraphManagement)1 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)1 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)1