Search in sources :

Example 1 with ClassType

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

the class DefaultMetadataServiceTest method testSpecialCharacters.

@Test
public // See GraphHelper.encodePropertyKey()
void testSpecialCharacters() throws Exception {
    // Verify that type can be created with reserved characters in typename, attribute name
    String strAttrName = randomStrWithReservedChars();
    String arrayAttrName = randomStrWithReservedChars();
    String mapAttrName = randomStrWithReservedChars();
    HierarchicalTypeDefinition<ClassType> typeDefinition = createClassTypeDef("test_type_" + RandomStringUtils.randomAlphanumeric(10), ImmutableSet.<String>of(), createOptionalAttrDef(strAttrName, DataTypes.STRING_TYPE), new AttributeDefinition(arrayAttrName, DataTypes.arrayTypeName(DataTypes.STRING_TYPE.getName()), Multiplicity.OPTIONAL, false, null), new AttributeDefinition(mapAttrName, DataTypes.mapTypeName(DataTypes.STRING_TYPE.getName(), DataTypes.STRING_TYPE.getName()), Multiplicity.OPTIONAL, false, null));
    metadataService.createType(TypesSerialization.toJson(typeDefinition, false));
    // verify that entity can be created with reserved characters in string value, array value and map key and value
    Referenceable entity = new Referenceable(typeDefinition.typeName);
    entity.set(strAttrName, randomStrWithReservedChars());
    entity.set(arrayAttrName, new ArrayList<String>() {

        {
            add(randomStrWithReservedChars());
        }
    });
    entity.set(mapAttrName, new HashMap<String, String>() {

        {
            put(randomStrWithReservedChars(), randomStrWithReservedChars());
        }
    });
    String id = createInstance(metadataService, entity);
    // Verify that get entity definition returns actual values with reserved characters
    Referenceable instance = InstanceSerialization.fromJsonReferenceable(metadataService.getEntityDefinitionJson(id), true);
    assertReferenceableEquals(instance, entity);
    // Verify that search with reserved characters works - for string attribute
    String query = String.format("`%s` where `%s` = '%s'", typeDefinition.typeName, strAttrName, entity.get(strAttrName));
    String responseJson = discoveryService.searchByDSL(query, new QueryParams(1, 0));
    JSONObject response = new JSONObject(responseJson);
    assertEquals(response.getJSONArray("rows").length(), 1);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) JSONObject(org.codehaus.jettison.json.JSONObject) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) QueryParams(org.apache.atlas.query.QueryParams) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with ClassType

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

the class DefaultMetadataServiceTest method testTypeUpdateFailureShouldRollBack.

@Test
public void testTypeUpdateFailureShouldRollBack() throws AtlasException, JSONException {
    String typeName = "test_type_" + RandomStringUtils.randomAlphanumeric(10);
    HierarchicalTypeDefinition<ClassType> typeDef = TypesUtil.createClassTypeDef(typeName, ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef("test_type_attribute", DataTypes.STRING_TYPE));
    TypesDef typesDef = new TypesDef(typeDef, false);
    JSONObject type = metadataService.createType(TypesSerialization.toJson(typesDef));
    Assert.assertNotNull(type.get(AtlasClient.TYPES));
    HierarchicalTypeDefinition<ClassType> updatedTypeDef = TypesUtil.createClassTypeDef(typeName, ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef("test_type_attribute", DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef("test_type_invalid_attribute$", DataTypes.STRING_TYPE));
    TypesDef updatedTypesDef = new TypesDef(updatedTypeDef, false);
    try {
        metadataService.updateType(TypesSerialization.toJson(updatedTypesDef));
        fail("Expected AtlasException");
    } catch (AtlasException e) {
    // expected
    }
    // type definition should reflect old type
    String typeDefinition = metadataService.getTypeDefinition(typeName);
    typesDef = TypesSerialization.fromJson(typeDefinition);
    assertEquals(typesDef.classTypes().head().attributeDefinitions.length, 1);
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef) JSONObject(org.codehaus.jettison.json.JSONObject) ClassType(org.apache.atlas.typesystem.types.ClassType) AtlasException(org.apache.atlas.AtlasException) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with ClassType

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

the class StoreBackedTypeCacheMetadataServiceTest method testInvalidUpdateType.

@Test
public void testInvalidUpdateType() throws Exception {
    // Cache should be empty
    Assert.assertFalse(storeBackedTypeCache.isCachedInMemory(TestUtils.TABLE_TYPE));
    HierarchicalTypeDefinition<ClassType> classTypeDef = TypesUtil.createClassTypeDef(TestUtils.TABLE_TYPE, ImmutableSet.<String>of(), new AttributeDefinition("attr1", DataTypes.STRING_TYPE.getName(), Multiplicity.OPTIONAL, false, null));
    String json = TypesSerialization.toJson(classTypeDef, false);
    // Try to update the type with disallowed changes.  Should fail with TypeUpdateException.
    try {
        metadataService.updateType(json);
        Assert.fail(TypeUpdateException.class.getSimpleName() + " was expected but none thrown");
    } catch (TypeUpdateException e) {
    // good
    }
    // hive_table type should now be cached.
    Assert.assertTrue(storeBackedTypeCache.isCachedInMemory(TestUtils.TABLE_TYPE));
}
Also used : AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) TypeUpdateException(org.apache.atlas.typesystem.types.TypeUpdateException) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test)

Example 4 with ClassType

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

the class StoreBackedTypeCacheTest method testGetClassType.

@Test
public void testGetClassType() throws Exception {
    for (Map.Entry<String, ClassType> typeEntry : classTypesToTest.entrySet()) {
        // Not cached yet
        Assert.assertFalse(typeCache.isCachedInMemory(typeEntry.getKey()));
        IDataType dataType = ts.getDataType(IDataType.class, typeEntry.getKey());
        // Verify the type is now cached.
        Assert.assertTrue(typeCache.isCachedInMemory(typeEntry.getKey()));
        Assert.assertTrue(dataType instanceof ClassType);
        ClassType cachedType = (ClassType) dataType;
        // Verify that get() also loaded and cached any dependencies of this type from the type store.
        verifyHierarchicalType(cachedType, typeEntry.getValue());
    }
}
Also used : ClassType(org.apache.atlas.typesystem.types.ClassType) IDataType(org.apache.atlas.typesystem.types.IDataType) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 5 with ClassType

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

the class DefaultMetadataServiceTest method testTypeWithDotsCreationShouldNotBeCreated.

@Test
public void testTypeWithDotsCreationShouldNotBeCreated() throws AtlasException, JSONException {
    String typeName = "test_.v1_type_XXXX";
    HierarchicalTypeDefinition<ClassType> typeDef = TypesUtil.createClassTypeDef(typeName, ImmutableSet.<String>of(), TypesUtil.createUniqueRequiredAttrDef("test_type_attribute", DataTypes.STRING_TYPE));
    TypesDef typesDef = new TypesDef(typeDef, false);
    try {
        metadataService.createType(TypesSerialization.toJson(typesDef));
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getCause().getMessage().contains(AtlasTypeUtil.getInvalidTypeNameErrorMessage()), e.getCause().getMessage());
    }
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

ClassType (org.apache.atlas.typesystem.types.ClassType)54 Test (org.testng.annotations.Test)26 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)19 TraitType (org.apache.atlas.typesystem.types.TraitType)19 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)17 Referenceable (org.apache.atlas.typesystem.Referenceable)16 TypesDef (org.apache.atlas.typesystem.TypesDef)15 ArrayList (java.util.ArrayList)12 Id (org.apache.atlas.typesystem.persistence.Id)8 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)8 Date (java.util.Date)5 HashMap (java.util.HashMap)5 HierarchicalTypeDefinition (org.apache.atlas.typesystem.types.HierarchicalTypeDefinition)5 BeforeTest (org.testng.annotations.BeforeTest)5 AtlasException (org.apache.atlas.AtlasException)4 JSONObject (org.codehaus.jettison.json.JSONObject)4 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)3 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)3 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)3 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)3