Search in sources :

Example 1 with TypeSystem

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

the class GraphHelperTest method testGetInstancesByUniqueAttributes.

@Test
public void testGetInstancesByUniqueAttributes() throws Exception {
    GraphHelper helper = GraphHelper.getInstance();
    List<ITypedReferenceableInstance> instances = new ArrayList<>();
    List<String> guids = new ArrayList<>();
    TypeSystem ts = TypeSystem.getInstance();
    ClassType dbType = ts.getDataType(ClassType.class, TestUtils.DATABASE_TYPE);
    for (int i = 0; i < 10; i++) {
        Referenceable db = TestUtils.createDBEntity();
        String guid = createInstance(db);
        ITypedReferenceableInstance instance = convert(db, dbType);
        instances.add(instance);
        guids.add(guid);
    }
    //lookup vertices via getVertexForInstanceByUniqueAttributes
    List<AtlasVertex> vertices = helper.getVerticesForInstancesByUniqueAttribute(dbType, instances);
    assertEquals(instances.size(), vertices.size());
    //assert vertex matches the vertex we get through getVertexForGUID
    for (int i = 0; i < instances.size(); i++) {
        String guid = guids.get(i);
        AtlasVertex foundVertex = vertices.get(i);
        AtlasVertex expectedVertex = helper.getVertexForGUID(guid);
        assertEquals(foundVertex, expectedVertex);
    }
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) Referenceable(org.apache.atlas.typesystem.Referenceable) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test)

Example 2 with TypeSystem

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

the class GraphBackedSearchIndexerTest method verifyUserDefinedTypeIndex.

@Test
public void verifyUserDefinedTypeIndex() throws AtlasException {
    AtlasGraph graph = TestUtils.getGraph();
    AtlasGraphManagement managementSystem = graph.getManagementSystem();
    try {
        TypeSystem typeSystem = TypeSystem.getInstance();
        String enumName = "randomEnum" + RandomStringUtils.randomAlphanumeric(10);
        EnumType managedType = typeSystem.defineEnumType(enumName, new EnumValue("randomEnumValue", 0));
        HierarchicalTypeDefinition<ClassType> databaseTypeDefinition = createClassTypeDef("Database", "Database type description", null, TypesUtil.createUniqueRequiredAttrDef("name", DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef("managedType", managedType));
        ClassType databaseType = typeSystem.defineClassType(databaseTypeDefinition);
        graphBackedSearchIndexer.onAdd(Arrays.asList(databaseType));
        verifySystemCompositeIndex(managementSystem, "Database.name" + Constants.ENTITY_TYPE_PROPERTY_KEY, false);
        verifyVertexIndexContains(managementSystem, "Database.name" + Constants.ENTITY_TYPE_PROPERTY_KEY);
        verifySystemCompositeIndex(managementSystem, "Database.name" + Constants.SUPER_TYPES_PROPERTY_KEY, false);
        verifyVertexIndexContains(managementSystem, "Database.managedType");
    } finally {
        //search indexer uses its own titan management transaction
        managementSystem.rollback();
    }
}
Also used : AtlasGraphManagement(org.apache.atlas.repository.graphdb.AtlasGraphManagement) TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) EnumType(org.apache.atlas.typesystem.types.EnumType) EnumValue(org.apache.atlas.typesystem.types.EnumValue) ClassType(org.apache.atlas.typesystem.types.ClassType) AtlasGraph(org.apache.atlas.repository.graphdb.AtlasGraph) Test(org.testng.annotations.Test)

Example 3 with TypeSystem

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

the class NotificationEntityChangeListenerTest 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);
    List<IStruct> allTraits = NotificationEntityChangeListener.getAllTraits(entity, typeSystem);
    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 4 with TypeSystem

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

the class DefaultTypeCacheTest method testGetTypesByFilter.

@Test
public void testGetTypesByFilter() throws Exception {
    // init TypeSystem
    TypeSystem ts = TypeSystem.getInstance().reset();
    ts.defineClassType(TypesUtil.createClassTypeDef("A", ImmutableSet.<String>of()));
    ts.defineClassType(TypesUtil.createClassTypeDef("A1", ImmutableSet.of("A")));
    ts.defineClassType(TypesUtil.createClassTypeDef("B", ImmutableSet.<String>of()));
    ts.defineClassType(TypesUtil.createClassTypeDef("C", ImmutableSet.of("B", "A")));
    //supertype ~ A
    ImmutableList<String> results = ts.getTypeNames(new HashMap<TypeCache.TYPE_FILTER, String>() {

        {
            put(TypeCache.TYPE_FILTER.SUPERTYPE, "A");
        }
    });
    assertTrue(results.containsAll(Arrays.asList("A1", "C")), "Results: " + results);
    //!supertype doesn't return the type itself
    results = ts.getTypeNames(new HashMap<TypeCache.TYPE_FILTER, String>() {

        {
            put(TypeCache.TYPE_FILTER.NOT_SUPERTYPE, "A");
        }
    });
    assertTrue(results.containsAll(Arrays.asList("B")), "Results: " + results);
    //supertype ~ A && supertype !~ B
    results = ts.getTypeNames(new HashMap<TypeCache.TYPE_FILTER, String>() {

        {
            put(TypeCache.TYPE_FILTER.SUPERTYPE, "A");
            put(TypeCache.TYPE_FILTER.NOT_SUPERTYPE, "B");
        }
    });
    assertTrue(results.containsAll(Arrays.asList("A1")), "Results: " + results);
    //none of category trait
    results = ts.getTypeNames(new HashMap<TypeCache.TYPE_FILTER, String>() {

        {
            put(TypeCache.TYPE_FILTER.CATEGORY, TypeCategory.TRAIT.name());
            put(TypeCache.TYPE_FILTER.SUPERTYPE, "A");
        }
    });
    assertTrue(results.isEmpty(), "Results: " + results);
    //no filter returns all types
    results = ts.getTypeNames(null);
    assertTrue(results.containsAll(Arrays.asList("A", "A1", "B", "C")), "Results: " + results);
    results = ts.getTypeNames(new HashMap<TypeCache.TYPE_FILTER, String>());
    assertTrue(results.containsAll(Arrays.asList("A", "A1", "B", "C")), "Results: " + results);
    //invalid category
    try {
        ts.getTypeNames(new HashMap<TypeCache.TYPE_FILTER, String>() {

            {
                put(TypeCache.TYPE_FILTER.CATEGORY, "A");
            }
        });
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    //expected
    }
    //invalid supertype
    results = ts.getTypeNames(new HashMap<TypeCache.TYPE_FILTER, String>() {

        {
            put(TypeCache.TYPE_FILTER.SUPERTYPE, "X");
        }
    });
    assertTrue(results.isEmpty(), "Expected empty result for non-existent type 'X'. Found: " + results);
    //invalid supertype
    results = ts.getTypeNames(new HashMap<TypeCache.TYPE_FILTER, String>() {

        {
            put(TypeCache.TYPE_FILTER.NOT_SUPERTYPE, "X");
        }
    });
    assertTrue(results.containsAll(Arrays.asList("A", "A1", "B", "C")), "Results: " + results);
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 5 with TypeSystem

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

the class DefaultTypeCacheTest method onetimeSetup.

@BeforeClass
public void onetimeSetup() throws Exception {
    // init TypeSystem
    TypeSystem ts = TypeSystem.getInstance().reset();
    // Customer ClassType
    customerType = ts.defineClassType(TypesUtil.createClassTypeDef(CLASSTYPE_CUSTOMER, ImmutableSet.<String>of(), TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef("id", DataTypes.LONG_TYPE)));
    // Address StructType
    addressType = ts.defineStructType(STRUCTTYPE_ADDRESS, true, TypesUtil.createRequiredAttrDef("first line", DataTypes.STRING_TYPE), TypesUtil.createOptionalAttrDef("second line", DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef("city", DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef("pincode", DataTypes.INT_TYPE));
    // Privileged TraitType
    privilegedTrait = ts.defineTraitType(TypesUtil.createTraitTypeDef(TRAITTYPE_PRIVILEGED, ImmutableSet.<String>of(), TypesUtil.createRequiredAttrDef("category", DataTypes.INT_TYPE)));
    // Shipping EnumType
    shippingEnum = ts.defineEnumType(TypesUtil.createEnumTypeDef(ENUMTYPE_SHIPPING, new EnumValue("Domestic", 1), new EnumValue("International", 2)));
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) EnumValue(org.apache.atlas.typesystem.types.EnumValue) BeforeClass(org.testng.annotations.BeforeClass)

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