Search in sources :

Example 11 with AttributeDefinition

use of org.apache.atlas.typesystem.types.AttributeDefinition 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 12 with AttributeDefinition

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

the class TypesJerseyResourceIT method createHiveTypes.

private List<HierarchicalTypeDefinition> createHiveTypes() throws Exception {
    ArrayList<HierarchicalTypeDefinition> typeDefinitions = new ArrayList<>();
    HierarchicalTypeDefinition<ClassType> databaseTypeDefinition = TypesUtil.createClassTypeDef("database", ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef(NAME, DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef(DESCRIPTION, DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef(QUALIFIED_NAME, DataTypes.STRING_TYPE));
    typeDefinitions.add(databaseTypeDefinition);
    HierarchicalTypeDefinition<ClassType> tableTypeDefinition = TypesUtil.createClassTypeDef("table", ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef(NAME, DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef(DESCRIPTION, DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef(QUALIFIED_NAME, DataTypes.STRING_TYPE), createOptionalAttrDef("columnNames", DataTypes.arrayTypeName(DataTypes.STRING_TYPE)), createOptionalAttrDef("created", DataTypes.DATE_TYPE), createOptionalAttrDef("parameters", DataTypes.mapTypeName(DataTypes.STRING_TYPE, DataTypes.STRING_TYPE)), TypesUtil.createRequiredAttrDef("type", DataTypes.STRING_TYPE), new AttributeDefinition("database", "database", Multiplicity.REQUIRED, false, "database"));
    typeDefinitions.add(tableTypeDefinition);
    HierarchicalTypeDefinition<TraitType> fetlTypeDefinition = TypesUtil.createTraitTypeDef("fetl", ImmutableSet.<String>of(), TypesUtil.createRequiredAttrDef("level", DataTypes.INT_TYPE));
    typeDefinitions.add(fetlTypeDefinition);
    return typeDefinitions;
}
Also used : HierarchicalTypeDefinition(org.apache.atlas.typesystem.types.HierarchicalTypeDefinition) TraitType(org.apache.atlas.typesystem.types.TraitType) ArrayList(java.util.ArrayList) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) ClassType(org.apache.atlas.typesystem.types.ClassType)

Example 13 with AttributeDefinition

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

the class TypesJerseyResourceIT method testGetDefinition.

@Test(dependsOnMethods = "testSubmit")
public void testGetDefinition() throws Exception {
    for (HierarchicalTypeDefinition typeDefinition : typeDefinitions) {
        System.out.println("typeName = " + typeDefinition.typeName);
        JSONObject response = atlasClientV1.callAPIWithBodyAndParams(AtlasClient.API.LIST_TYPES, null, typeDefinition.typeName);
        Assert.assertNotNull(response);
        Assert.assertNotNull(response.get(AtlasClient.DEFINITION));
        Assert.assertNotNull(response.get(AtlasClient.REQUEST_ID));
        String typesJson = response.getString(AtlasClient.DEFINITION);
        final TypesDef typesDef = TypesSerialization.fromJson(typesJson);
        List<HierarchicalTypeDefinition<ClassType>> hierarchicalTypeDefinitions = typesDef.classTypesAsJavaList();
        for (HierarchicalTypeDefinition<ClassType> classType : hierarchicalTypeDefinitions) {
            for (AttributeDefinition attrDef : classType.attributeDefinitions) {
                if (NAME.equals(attrDef.name)) {
                    assertEquals(attrDef.isIndexable, true);
                    assertEquals(attrDef.isUnique, true);
                }
            }
        }
    }
}
Also used : HierarchicalTypeDefinition(org.apache.atlas.typesystem.types.HierarchicalTypeDefinition) JSONObject(org.codehaus.jettison.json.JSONObject) TypesDef(org.apache.atlas.typesystem.TypesDef) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test)

Example 14 with AttributeDefinition

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

the class DefaultTypeSystem method createType.

private <T extends HierarchicalType> void createType(Collection<AttributeDefinition> attributes, Class<T> type, String name, String description, boolean isTrait) throws ResourceAlreadyExistsException {
    try {
        List<AtlasStructDef.AtlasAttributeDef> attrDefs = new ArrayList<>();
        for (AttributeDefinition attrDefinition : attributes) {
            attrDefs.add(TypeConverterUtil.toAtlasAttributeDef(attrDefinition));
        }
        if (isTrait) {
            AtlasClassificationDef classificationDef = new AtlasClassificationDef(name, description, "1.0", attrDefs, ImmutableSet.of(TaxonomyResourceProvider.TAXONOMY_TERM_TYPE));
            AtlasTypesDef typesDef = new AtlasTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.of(classificationDef), ImmutableList.<AtlasEntityDef>of());
            typeDefStore.createTypesDef(typesDef);
        } else {
            AtlasEntityDef entityDef = new AtlasEntityDef(name, description, "1.0", attrDefs);
            AtlasTypesDef typesDef = new AtlasTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.of(entityDef));
            typeDefStore.createTypesDef(typesDef);
        }
    } catch (AtlasBaseException e) {
        if (e.getAtlasErrorCode() == AtlasErrorCode.TYPE_ALREADY_EXISTS) {
            throw new ResourceAlreadyExistsException(String.format("Type '%s' already exists", name));
        } else {
            throw new CatalogRuntimeException(String.format("Unable to create type '%s' in type system: %s", name, e), e);
        }
    }
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) CatalogRuntimeException(org.apache.atlas.catalog.exception.CatalogRuntimeException) ArrayList(java.util.ArrayList) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef)

Example 15 with AttributeDefinition

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

the class TermResourceDefinitionTest method testGetPropertyDefinitions.

@Test
public void testGetPropertyDefinitions() {
    ResourceDefinition termDefinition = new TermResourceDefinition();
    Collection<AttributeDefinition> propertyDefinitions = termDefinition.getPropertyDefinitions();
    assertEquals(propertyDefinitions.size(), 4);
    Set<String> defNames = new HashSet<>();
    for (AttributeDefinition def : propertyDefinitions) {
        defNames.add(def.name);
    }
    assertTrue(defNames.contains("name"));
    assertTrue(defNames.contains("description"));
    assertTrue(defNames.contains("available_as_tag"));
    assertTrue(defNames.contains("acceptable_use"));
}
Also used : AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) Test(org.testng.annotations.Test)

Aggregations

AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)27 ClassType (org.apache.atlas.typesystem.types.ClassType)17 Test (org.testng.annotations.Test)13 TraitType (org.apache.atlas.typesystem.types.TraitType)10 ArrayList (java.util.ArrayList)7 TypesDef (org.apache.atlas.typesystem.TypesDef)7 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)5 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)4 HierarchicalTypeDefinition (org.apache.atlas.typesystem.types.HierarchicalTypeDefinition)4 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)3 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)3 Referenceable (org.apache.atlas.typesystem.Referenceable)3 StructTypeDefinition (org.apache.atlas.typesystem.types.StructTypeDefinition)3 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)3 BeforeTest (org.testng.annotations.BeforeTest)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)2 QueryParams (org.apache.atlas.query.QueryParams)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 Callable (java.util.concurrent.Callable)1