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);
}
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);
}
}
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;
}
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());
}
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");
}
}
Aggregations