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