Search in sources :

Example 6 with TypesDef

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

the class TypesResource method getDefinition.

/**
     * Fetch the complete definition of a given type name which is unique.
     *
     * @param typeName name of a type which is unique.
     */
@GET
@Path("{typeName}")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getDefinition(@Context HttpServletRequest request, @PathParam("typeName") String typeName) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> TypesResource.getDefinition({})", typeName);
    }
    AtlasPerfTracer perf = null;
    if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
        perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "TypesResource.getDefinition(" + typeName + ")");
    }
    JSONObject response = new JSONObject();
    try {
        TypesDef typesDef = TypeConverterUtil.toTypesDef(typeRegistry.getType(typeName), typeRegistry);
        ;
        String typeDefinition = TypesSerialization.toJson(typesDef);
        response.put(AtlasClient.TYPENAME, typeName);
        response.put(AtlasClient.DEFINITION, new JSONObject(typeDefinition));
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get type definition for type {}", typeName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e));
    } catch (JSONException | IllegalArgumentException e) {
        LOG.error("Unable to get type definition for type {}", typeName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get type definition for type {}", typeName, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get type definition for type {}", typeName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== TypesResource.getDefinition({})", typeName);
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) TypesDef(org.apache.atlas.typesystem.TypesDef) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) JSONException(org.codehaus.jettison.json.JSONException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 7 with TypesDef

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

the class BaseRepositoryTest method setUpDefaultTypes.

private void setUpDefaultTypes() throws Exception {
    TypesDef typesDef = createDefaultTypeDefinitions();
    String typesAsJSON = TypesSerialization.toJson(typesDef);
    metadataService.createType(typesAsJSON);
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef)

Example 8 with TypesDef

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

the class TypeUpdateBaseTest method testTypeUpdateForAttributes.

protected void testTypeUpdateForAttributes() throws Exception {
    StructTypeDefinition typeDefinition = getTypeDefinition(newName(), TypesUtil.createRequiredAttrDef("a", DataTypes.INT_TYPE));
    TypeSystem ts = getTypeSystem();
    TypesDef typesDef = getTypesDef(typeDefinition);
    ts.defineTypes(typesDef);
    String typeName = typeDefinition.typeName;
    //Allow modifying required to optional attribute
    typeDefinition = getTypeDefinition(typeName, TypesUtil.createOptionalAttrDef("a", DataTypes.INT_TYPE));
    ts.updateTypes(getTypesDef(typeDefinition));
    //Allow adding new optional attribute
    typeDefinition = getTypeDefinition(typeName, TypesUtil.createOptionalAttrDef("a", DataTypes.INT_TYPE), TypesUtil.createOptionalAttrDef("b", DataTypes.INT_TYPE));
    ts.updateTypes(getTypesDef(typeDefinition));
    //Don't allow adding required attribute
    typeDefinition = getTypeDefinition(typeName, TypesUtil.createOptionalAttrDef("a", DataTypes.INT_TYPE), TypesUtil.createOptionalAttrDef("b", DataTypes.INT_TYPE), TypesUtil.createRequiredAttrDef("c", DataTypes.INT_TYPE));
    try {
        ts.updateTypes(getTypesDef(typeDefinition));
        Assert.fail("Expected TypeUpdateException");
    } catch (TypeUpdateException e) {
        //assert that type is not updated when validation fails
        Assert.assertEquals(getNumberOfFields(ts, typeDefinition.typeName), 2);
    }
    //Don't allow removing attribute
    typeDefinition = getTypeDefinition(typeName, TypesUtil.createOptionalAttrDef("a", DataTypes.INT_TYPE));
    try {
        ts.updateTypes(getTypesDef(typeDefinition));
    } catch (TypeUpdateException e) {
    //expected
    }
    //Don't allow modifying other fields of attribute definition - optional to required
    typeDefinition = getTypeDefinition(typeName, TypesUtil.createOptionalAttrDef("a", DataTypes.INT_TYPE), TypesUtil.createRequiredAttrDef("b", DataTypes.INT_TYPE));
    try {
        ts.updateTypes(getTypesDef(typeDefinition));
    } catch (TypeUpdateException e) {
    //expected
    }
    //Don't allow modifying other fields of attribute definition - attribute type change
    typeDefinition = getTypeDefinition(typeName, TypesUtil.createOptionalAttrDef("a", DataTypes.INT_TYPE), TypesUtil.createOptionalAttrDef("b", DataTypes.STRING_TYPE));
    try {
        ts.updateTypes(getTypesDef(typeDefinition));
    } catch (TypeUpdateException e) {
    //expected
    }
    //Don't allow modifying other fields of attribute definition - attribute type change
    typeDefinition = getTypeDefinition(typeName, TypesUtil.createRequiredAttrDef("a", DataTypes.INT_TYPE), new AttributeDefinition("b", DataTypes.arrayTypeName(DataTypes.STRING_TYPE.getName()), Multiplicity.COLLECTION, false, null));
    try {
        ts.updateTypes(getTypesDef(typeDefinition));
    } catch (TypeUpdateException e) {
    //expected
    }
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef)

Example 9 with TypesDef

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

the class EnumTest method testTypeUpdate.

@Test
public void testTypeUpdate() throws Exception {
    TypeSystem ts = getTypeSystem();
    EnumTypeDefinition etd = new EnumTypeDefinition(newName(), new EnumValue("A", 1));
    TypesDef typesDef = getTypesDef(etd);
    ts.defineTypes(typesDef);
    //Allow adding new enum
    etd = new EnumTypeDefinition(etd.name, new EnumValue("A", 1), new EnumValue("B", 2));
    typesDef = getTypesDef(etd);
    ts.updateTypes(typesDef);
    //Don't allow deleting enum
    etd = new EnumTypeDefinition(etd.name, new EnumValue("A", 1));
    typesDef = getTypesDef(etd);
    try {
        ts.updateTypes(typesDef);
        Assert.fail("Expected TypeUpdateException");
    } catch (TypeUpdateException e) {
        //assert that type is not updated when validation fails
        EnumType enumType = ts.getDataType(EnumType.class, etd.name);
        Assert.assertEquals(enumType.values().size(), 2);
    }
    //Don't allow changing ordinal of existing enum value
    etd = new EnumTypeDefinition(etd.name, new EnumValue("A", 2));
    typesDef = getTypesDef(etd);
    try {
        ts.updateTypes(typesDef);
        Assert.fail("Expected TypeUpdateException");
    } catch (TypeUpdateException e) {
    //expected
    }
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef) Test(org.testng.annotations.Test)

Example 10 with TypesDef

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

the class FieldMappingTest method testOutputStruct.

@Test
public void testOutputStruct() throws Exception {
    // ATLAS-645: verify that FieldMapping.output(IStruct) does not infinitely recurse
    // when an IStruct and ITypedReferenceableInstance reference each other.
    HierarchicalTypeDefinition<ClassType> valueDef = TypesUtil.createClassTypeDef("Value", ImmutableSet.<String>of(), new AttributeDefinition("owner", "Owner", Multiplicity.OPTIONAL, false, null));
    // Define struct type with reference, where the value is a class reference to Value.
    StructTypeDefinition ownerDef = TypesUtil.createStructTypeDef("Owner", new AttributeDefinition("value", "Value", Multiplicity.OPTIONAL, false, null));
    TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.of(ownerDef), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.of(valueDef));
    TypeSystem typeSystem = TypeSystem.getInstance();
    typeSystem.reset();
    typeSystem.defineTypes(typesDef);
    StructType ownerType = typeSystem.getDataType(StructType.class, "Owner");
    ClassType valueType = typeSystem.getDataType(ClassType.class, "Value");
    // Prior to fix for ATLAS-645, this call would throw a StackOverflowError
    try {
        ownerType.toString();
    } catch (StackOverflowError e) {
        Assert.fail("Infinite recursion in StructType.toString() caused StackOverflowError");
    }
    // Create instances of Owner and Value that reference each other.
    ITypedStruct ownerInstance = ownerType.createInstance();
    ITypedReferenceableInstance valueInstance = valueType.createInstance();
    // Set Owner.value reference to Value instance.
    ownerInstance.set("value", valueInstance);
    // Set Value.owner reference on Owner instance.
    valueInstance.set("owner", ownerInstance);
    // Prior to fix for ATLAS-645, this call would throw a StackOverflowError
    try {
        ownerInstance.fieldMapping().output(ownerInstance, new StringBuilder(), "", null);
    } catch (StackOverflowError e) {
        Assert.fail("Infinite recursion in FieldMapping.output() caused StackOverflowError");
    }
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) TypesDef(org.apache.atlas.typesystem.TypesDef) 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) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) StructTypeDefinition(org.apache.atlas.typesystem.types.StructTypeDefinition) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

TypesDef (org.apache.atlas.typesystem.TypesDef)43 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