Search in sources :

Example 6 with ITypedStruct

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

the class GraphBackedMetadataRepositoryTest method testAddTraitWithAttribute.

@Test(dependsOnMethods = "testAddTrait")
public void testAddTraitWithAttribute() throws Exception {
    final String aGUID = getGUID();
    final String traitName = "P_I_I";
    HierarchicalTypeDefinition<TraitType> piiTrait = TypesUtil.createTraitTypeDef(traitName, ImmutableSet.<String>of(), TypesUtil.createRequiredAttrDef("type", DataTypes.STRING_TYPE));
    TraitType traitType = typeSystem.defineTraitType(piiTrait);
    ITypedStruct traitInstance = traitType.createInstance();
    traitInstance.set("type", "SSN");
    repositoryService.addTrait(aGUID, traitInstance);
    TestUtils.dumpGraph(TestUtils.getGraph());
    // refresh trait names
    List<String> traitNames = repositoryService.getTraitNames(aGUID);
    Assert.assertEquals(traitNames.size(), 3);
    Assert.assertTrue(traitNames.contains(traitName));
    ITypedReferenceableInstance instance = repositoryService.getEntityDefinition(aGUID);
    IStruct traitInstanceRef = instance.getTrait(traitName);
    String type = (String) traitInstanceRef.get("type");
    Assert.assertEquals(type, "SSN");
}
Also used : TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test)

Example 7 with ITypedStruct

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteEntityWithTraits.

@Test
public void testDeleteEntityWithTraits() throws Exception {
    Referenceable entity = createDBEntity();
    String id = createInstance(entity);
    TraitType dataType = typeSystem.getDataType(TraitType.class, PII);
    ITypedStruct trait = dataType.convert(new Struct(TestUtils.PII), Multiplicity.REQUIRED);
    repositoryService.addTrait(id, trait);
    ITypedReferenceableInstance instance = repositoryService.getEntityDefinition(id);
    assertTrue(instance.getTraits().contains(PII));
    deleteEntities(id);
    assertEntityDeleted(id);
    assertTestDeleteEntityWithTraits(id);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 8 with ITypedStruct

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

the class TypeInheritanceTest method testDiamondInheritance.

/*
     * Type Hierarchy is:
     *   A(a,b,c,d)
     *   B(b) extends A
     *   C(c) extends A
     *   D(d) extends B,C
     *
     * - There are a total of 11 fields in an instance of D
     * - an attribute that is hidden by a SubType can referenced by prefixing it with the
     * complete Path.
     *   For e.g. the 'b' attribute in A (that is a superType for B) is hidden the 'b' attribute
     *   in B.
     *   So it is availabel by the name 'A.B.D.b'
     *
     * - Another way to set attributes is to cast. Casting a 'D' instance of 'B' makes the 'A.B.D
     * .b' attribute
     *   available as 'A.B.b'. Casting one more time to an 'A' makes the 'A.B.b' attribute
     *   available as 'b'.
     */
@Test
public void testDiamondInheritance() throws AtlasException {
    HierarchicalTypeDefinition A = createTraitTypeDef("A", null, createRequiredAttrDef("a", DataTypes.INT_TYPE), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), createOptionalAttrDef("c", DataTypes.BYTE_TYPE), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
    HierarchicalTypeDefinition B = createTraitTypeDef("B", ImmutableSet.of("A"), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE));
    HierarchicalTypeDefinition C = createTraitTypeDef("C", ImmutableSet.of("A"), createOptionalAttrDef("c", DataTypes.BYTE_TYPE));
    HierarchicalTypeDefinition D = createTraitTypeDef("D", ImmutableSet.of("B", "C"), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
    defineTraits(A, B, C, D);
    TraitType DType = getTypeSystem().getDataType(TraitType.class, "D");
    Struct s1 = new Struct("D");
    s1.set("d", 1);
    s1.set("c", 1);
    s1.set("b", true);
    s1.set("a", 1);
    s1.set("A.B.D.b", true);
    s1.set("A.B.D.c", 2);
    s1.set("A.B.D.d", 2);
    s1.set("A.C.D.a", 3);
    s1.set("A.C.D.b", false);
    s1.set("A.C.D.c", 3);
    s1.set("A.C.D.d", 3);
    ITypedStruct ts = DType.convert(s1, Multiplicity.REQUIRED);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
    /*
         * cast to B and set the 'b' attribute on A.
         */
    TraitType BType = getTypeSystem().getDataType(TraitType.class, "B");
    IStruct s2 = DType.castAs(ts, "B");
    s2.set("A.B.b", false);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \tfalse\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
    /*
         * cast again to A and set the 'b' attribute on A.
         */
    IStruct s3 = BType.castAs(s2, "A");
    s3.set("b", true);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
}
Also used : ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test)

Example 9 with ITypedStruct

use of org.apache.atlas.typesystem.ITypedStruct 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 10 with ITypedStruct

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

the class StructTest method test1.

@Test
public void test1() throws AtlasException {
    Struct s = createStruct();
    ITypedStruct ts = structType.convert(s, Multiplicity.REQUIRED);
    Assert.assertEquals(ts.toString(), "{\n" + "\ta : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\td : \t2\n" + "\te : \t1\n" + "\tf : \t1\n" + "\tg : \t1\n" + "\th : \t1.0\n" + "\ti : \t1.0\n" + "\tj : \t1\n" + "\tk : \t1\n" + "\tl : \t" + TEST_DATE + "\n" + "\tm : \t[1, 1]\n" + "\tn : \t[1.1, 1.1]\n" + "\to : \t{a=1.0, b=2.0}\n" + "\tp : \t\n" + "\tq : \t<null>\n" + "\tr : \t{a=}\n" + "}");
}
Also used : ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Aggregations

ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)29 Test (org.testng.annotations.Test)14 IStruct (org.apache.atlas.typesystem.IStruct)13 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)13 Struct (org.apache.atlas.typesystem.Struct)11 TraitType (org.apache.atlas.typesystem.types.TraitType)6 AtlasException (org.apache.atlas.AtlasException)5 ImmutableList (com.google.common.collect.ImmutableList)4 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)4 List (java.util.List)3 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)3 Id (org.apache.atlas.typesystem.persistence.Id)3 ArrayList (java.util.ArrayList)2 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 EntityChangeListener (org.apache.atlas.listener.EntityChangeListener)2 Referenceable (org.apache.atlas.typesystem.Referenceable)2 TypesDef (org.apache.atlas.typesystem.TypesDef)2 StructType (org.apache.atlas.typesystem.types.StructType)2 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)2 BeforeTest (org.testng.annotations.BeforeTest)2