Search in sources :

Example 26 with TraitType

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

the class TypesJerseyResourceIT method testUpdate.

@Test
public void testUpdate() throws Exception {
    HierarchicalTypeDefinition<ClassType> typeDefinition = TypesUtil.createClassTypeDef(randomString(), ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef(NAME, DataTypes.STRING_TYPE));
    List<String> typesCreated = atlasClientV1.createType(TypesSerialization.toJson(typeDefinition, false));
    assertEquals(typesCreated.size(), 1);
    assertEquals(typesCreated.get(0), typeDefinition.typeName);
    // Add attribute description
    typeDefinition = TypesUtil.createClassTypeDef(typeDefinition.typeName, ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef(NAME, DataTypes.STRING_TYPE), createOptionalAttrDef(DESCRIPTION, DataTypes.STRING_TYPE));
    TypesDef typeDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.of(typeDefinition));
    List<String> typesUpdated = atlasClientV1.updateType(typeDef);
    assertEquals(typesUpdated.size(), 1);
    Assert.assertTrue(typesUpdated.contains(typeDefinition.typeName));
    TypesDef updatedTypeDef = atlasClientV1.getType(typeDefinition.typeName);
    assertNotNull(updatedTypeDef);
    HierarchicalTypeDefinition<ClassType> updatedType = updatedTypeDef.classTypesAsJavaList().get(0);
    assertEquals(updatedType.attributeDefinitions.length, 2);
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef) TraitType(org.apache.atlas.typesystem.types.TraitType) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test)

Example 27 with TraitType

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

the class GraphToTypedInstanceMapper method mapVertexToInstanceTraits.

private void mapVertexToInstanceTraits(AtlasVertex instanceVertex, ITypedReferenceableInstance typedInstance, List<String> traits) throws AtlasException {
    for (String traitName : traits) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("mapping trait {} to instance", traitName);
        }
        TraitType traitType = typeSystem.getDataType(TraitType.class, traitName);
        mapVertexToTraitInstance(instanceVertex, typedInstance, traitName, traitType);
    }
}
Also used : TraitType(org.apache.atlas.typesystem.types.TraitType)

Example 28 with TraitType

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

the class NotificationEntityChangeListener method getSuperTraits.

private static List<IStruct> getSuperTraits(String typeName, Map<String, Object> values, TypeSystem typeSystem) throws AtlasException {
    List<IStruct> superTypes = new LinkedList<>();
    TraitType traitDef = typeSystem.getDataType(TraitType.class, typeName);
    Set<String> superTypeNames = traitDef.getAllSuperTypeNames();
    for (String superTypeName : superTypeNames) {
        TraitType superTraitDef = typeSystem.getDataType(TraitType.class, superTypeName);
        Map<String, Object> superTypeValues = new HashMap<>();
        FieldMapping fieldMapping = superTraitDef.fieldMapping();
        if (fieldMapping != null) {
            Set<String> superTypeAttributeNames = fieldMapping.fields.keySet();
            for (String superTypeAttributeName : superTypeAttributeNames) {
                if (values.containsKey(superTypeAttributeName)) {
                    superTypeValues.put(superTypeAttributeName, values.get(superTypeAttributeName));
                }
            }
        }
        IStruct superTrait = new Struct(superTypeName, superTypeValues);
        superTypes.add(superTrait);
        superTypes.addAll(getSuperTraits(superTypeName, values, typeSystem));
    }
    return superTypes;
}
Also used : HashMap(java.util.HashMap) TraitType(org.apache.atlas.typesystem.types.TraitType) FieldMapping(org.apache.atlas.typesystem.types.FieldMapping) LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Example 29 with TraitType

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

the class SerializationJavaTest method testTrait.

@Test
public void testTrait() throws AtlasException {
    TypeSystem ts = getTypeSystem();
    HierarchicalTypeDefinition<TraitType> securityClearanceTypeDef = createTraitTypeDef("SecurityClearance2", ImmutableSet.<String>of(), createRequiredAttrDef("level", DataTypes.INT_TYPE));
    ts.defineTypes(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), ImmutableList.of(securityClearanceTypeDef), ImmutableList.<HierarchicalTypeDefinition<ClassType>>of());
    Struct s = new Struct("SecurityClearance2");
    s.set("level", 1);
    TraitType tType = ts.getDataType(TraitType.class, "SecurityClearance2");
    ITypedInstance t = tType.convert(s, Multiplicity.REQUIRED);
    String jsonStr = Serialization$.MODULE$.toJson(t);
    ITypedInstance t2 = Serialization$.MODULE$.traitFromJson(jsonStr);
    Assert.assertEquals(t.toString(), t2.toString());
}
Also used : ITypedInstance(org.apache.atlas.typesystem.ITypedInstance) TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) TraitType(org.apache.atlas.typesystem.types.TraitType) ClassType(org.apache.atlas.typesystem.types.ClassType) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test) BaseTest(org.apache.atlas.typesystem.types.BaseTest)

Example 30 with TraitType

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

the class FieldMappingTest method testOutputReferenceableInstance.

@Test
public void testOutputReferenceableInstance() throws Exception {
    // ATLAS-645: verify that FieldMapping.output(IReferenceableInstance)
    // does not infinitely recurse when ITypedReferenceableInstance's reference each other.
    HierarchicalTypeDefinition<ClassType> valueDef = TypesUtil.createClassTypeDef("Value", ImmutableSet.<String>of(), new AttributeDefinition("owner", "Owner", Multiplicity.OPTIONAL, false, null));
    // Define class type with reference, where the value is a class reference to Value.
    HierarchicalTypeDefinition<ClassType> ownerDef = TypesUtil.createClassTypeDef("Owner", ImmutableSet.<String>of(), new AttributeDefinition("value", "Value", Multiplicity.OPTIONAL, false, null));
    TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.of(ownerDef, valueDef));
    TypeSystem typeSystem = TypeSystem.getInstance();
    typeSystem.defineTypes(typesDef);
    ClassType ownerType = typeSystem.getDataType(ClassType.class, "Owner");
    // Prior to fix for ATLAS-645, this call would throw a StackOverflowError
    try {
        ownerType.toString();
    } catch (StackOverflowError e) {
        Assert.fail("Infinite recursion in ClassType.toString() caused StackOverflowError");
    }
    ClassType valueType = typeSystem.getDataType(ClassType.class, "Value");
    // Create instances of Owner and Value that reference each other.
    ITypedReferenceableInstance 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(), "", new HashSet<IReferenceableInstance>());
    } catch (StackOverflowError e) {
        Assert.fail("Infinite recursion in FieldMapping.output() caused StackOverflowError");
    }
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) 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) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

TraitType (org.apache.atlas.typesystem.types.TraitType)40 Test (org.testng.annotations.Test)22 ClassType (org.apache.atlas.typesystem.types.ClassType)19 TypesDef (org.apache.atlas.typesystem.TypesDef)13 Referenceable (org.apache.atlas.typesystem.Referenceable)12 Struct (org.apache.atlas.typesystem.Struct)12 Id (org.apache.atlas.typesystem.persistence.Id)10 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)10 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)7 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)7 IStruct (org.apache.atlas.typesystem.IStruct)6 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)6 ArrayList (java.util.ArrayList)5 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)5 AtlasServiceException (org.apache.atlas.AtlasServiceException)4 HierarchicalTypeDefinition (org.apache.atlas.typesystem.types.HierarchicalTypeDefinition)3 BeforeTest (org.testng.annotations.BeforeTest)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 HashMap (java.util.HashMap)2