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