Search in sources :

Example 1 with TypesDef

use of org.apache.atlas.typesystem.TypesDef 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 2 with TypesDef

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

the class GraphBackedTypeStoreTest method testRestoreType.

@Test(dependsOnMethods = "testStore")
public void testRestoreType() throws Exception {
    TypesDef typesDef = typeStore.restoreType("Manager");
    verifyRestoredClassType(typesDef, "Manager");
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef) Test(org.testng.annotations.Test)

Example 3 with TypesDef

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

the class GraphBackedTypeStoreTest method testRestore.

@Test(dependsOnMethods = "testStore")
public void testRestore() throws Exception {
    TypesDef types = typeStore.restore();
    // validate enum
    List<EnumTypeDefinition> enumTypes = types.enumTypesAsJavaList();
    Assert.assertEquals(1, enumTypes.size());
    EnumTypeDefinition orgLevel = enumTypes.get(0);
    Assert.assertEquals(orgLevel.name, "OrgLevel");
    Assert.assertEquals(orgLevel.description, "OrgLevel" + DESCRIPTION);
    Assert.assertEquals(orgLevel.enumValues.length, 2);
    EnumValue enumValue = orgLevel.enumValues[0];
    Assert.assertEquals(enumValue.value, "L1");
    Assert.assertEquals(enumValue.ordinal, 1);
    // validate class
    List<StructTypeDefinition> structTypes = types.structTypesAsJavaList();
    Assert.assertEquals(1, structTypes.size());
    verifyRestoredClassType(types, "Manager");
    // validate trait
    List<HierarchicalTypeDefinition<TraitType>> traitTypes = types.traitTypesAsJavaList();
    Assert.assertEquals(1, traitTypes.size());
    HierarchicalTypeDefinition<TraitType> trait = traitTypes.get(0);
    Assert.assertEquals("SecurityClearance", trait.typeName);
    Assert.assertEquals(trait.typeName + DESCRIPTION, trait.typeDescription);
    Assert.assertEquals(1, trait.attributeDefinitions.length);
    AttributeDefinition attribute = trait.attributeDefinitions[0];
    Assert.assertEquals("level", attribute.name);
    Assert.assertEquals(DataTypes.INT_TYPE.getName(), attribute.dataTypeName);
    // validate the new types
    ts.reset();
    ts.defineTypes(types);
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef) Test(org.testng.annotations.Test)

Example 4 with TypesDef

use of org.apache.atlas.typesystem.TypesDef 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)

Example 5 with TypesDef

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testDisconnectMapReferenceFromClassType.

/**
 * Verify deleting entities that are the target of class map references.
 */
@Test
public void testDisconnectMapReferenceFromClassType() throws Exception {
    // Define type for map value.
    HierarchicalTypeDefinition<ClassType> mapValueDef = TypesUtil.createClassTypeDef("MapValue", ImmutableSet.<String>of(), new AttributeDefinition("biMapOwner", "MapOwner", Multiplicity.OPTIONAL, false, "biMap"));
    // Define type with unidirectional and bidirectional map references,
    // where the map value is a class reference to MapValue.
    HierarchicalTypeDefinition<ClassType> mapOwnerDef = TypesUtil.createClassTypeDef("MapOwner", ImmutableSet.<String>of(), new AttributeDefinition("map", DataTypes.mapTypeName(DataTypes.STRING_TYPE.getName(), "MapValue"), Multiplicity.OPTIONAL, false, null), new AttributeDefinition("biMap", DataTypes.mapTypeName(DataTypes.STRING_TYPE.getName(), "MapValue"), Multiplicity.OPTIONAL, false, "biMapOwner"));
    TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.of(mapOwnerDef, mapValueDef));
    typeSystem.defineTypes(typesDef);
    ClassType mapOwnerType = typeSystem.getDataType(ClassType.class, "MapOwner");
    ClassType mapValueType = typeSystem.getDataType(ClassType.class, "MapValue");
    // Create instances of MapOwner and MapValue.
    // Set MapOwner.map and MapOwner.biMap with one entry that references MapValue instance.
    ITypedReferenceableInstance mapOwnerInstance = mapOwnerType.createInstance();
    ITypedReferenceableInstance mapValueInstance = mapValueType.createInstance();
    mapOwnerInstance.set("map", Collections.singletonMap("value1", mapValueInstance));
    mapOwnerInstance.set("biMap", Collections.singletonMap("value1", mapValueInstance));
    // Set biMapOwner reverse reference on MapValue.
    mapValueInstance.set("biMapOwner", mapOwnerInstance);
    List<String> createEntitiesResult = repositoryService.createEntities(mapOwnerInstance, mapValueInstance).getCreatedEntities();
    Assert.assertEquals(createEntitiesResult.size(), 2);
    List<String> guids = repositoryService.getEntityList("MapOwner");
    Assert.assertEquals(guids.size(), 1);
    String mapOwnerGuid = guids.get(0);
    String edgeLabel = GraphHelper.getEdgeLabel(mapOwnerType, mapOwnerType.fieldMapping.fields.get("map"));
    String mapEntryLabel = edgeLabel + "." + "value1";
    AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
    // Verify MapOwner.map attribute has expected value.
    String mapValueGuid = null;
    AtlasVertex mapOwnerVertex = null;
    mapOwnerInstance = repositoryService.getEntityDefinition(mapOwnerGuid);
    for (String mapAttrName : Arrays.asList("map", "biMap")) {
        Object object = mapOwnerInstance.get(mapAttrName);
        Assert.assertNotNull(object);
        Assert.assertTrue(object instanceof Map);
        Map<String, ITypedReferenceableInstance> map = (Map<String, ITypedReferenceableInstance>) object;
        Assert.assertEquals(map.size(), 1);
        mapValueInstance = map.get("value1");
        Assert.assertNotNull(mapValueInstance);
        mapValueGuid = mapValueInstance.getId()._getId();
        mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
        object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
        Assert.assertNotNull(object);
    }
    // Delete the map value instance.
    // This should disconnect the references from the map owner instance.
    deleteEntities(mapValueGuid);
    assertEntityDeleted(mapValueGuid);
    assertTestDisconnectMapReferenceFromClassType(mapOwnerGuid);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) TypesDef(org.apache.atlas.typesystem.TypesDef) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

TypesDef (org.apache.atlas.typesystem.TypesDef)44 Test (org.testng.annotations.Test)20 ClassType (org.apache.atlas.typesystem.types.ClassType)15 TraitType (org.apache.atlas.typesystem.types.TraitType)13 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)8 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)7 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 BeforeTest (org.testng.annotations.BeforeTest)5 AtlasException (org.apache.atlas.AtlasException)4 HierarchicalTypeDefinition (org.apache.atlas.typesystem.types.HierarchicalTypeDefinition)4 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 Referenceable (org.apache.atlas.typesystem.Referenceable)3 Id (org.apache.atlas.typesystem.persistence.Id)3 StructTypeDefinition (org.apache.atlas.typesystem.types.StructTypeDefinition)3 BeforeClass (org.testng.annotations.BeforeClass)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2