Commit 4f928a8d by Venkatesh Seetharam

Remove type name from create type API

parent a86f0f3e
......@@ -87,9 +87,8 @@ public class MetadataServiceClient {
}
}
public JSONObject createType(String typeName,
String typeAsJson) throws MetadataServiceException {
return callAPI(API.CREATE_TYPE, typeAsJson, typeName);
public JSONObject createType(String typeAsJson) throws MetadataServiceException {
return callAPI(API.CREATE_TYPE, typeAsJson);
}
public List<String> listTypes() throws MetadataServiceException {
......
......@@ -81,15 +81,13 @@ public class DefaultMetadataService implements MetadataService {
* Creates a new type based on the type system to enable adding
* entities (instances for types).
*
* @param typeName name for this type, must be unique
* @param typeDefinition definition as json
* @return a unique id for this type
*/
@Override
public JSONObject createType(String typeName,
String typeDefinition) throws MetadataException {
public JSONObject createType(String typeDefinition) throws MetadataException {
try {
validateTypeDoesNotExist(typeName, typeDefinition);
Preconditions.checkNotNull(typeDefinition, "type definition cannot be null");
TypesDef typesDef = TypesSerialization.fromJson(typeDefinition);
Map<String, IDataType> typesAdded = typeSystem.defineTypes(typesDef);
......@@ -105,20 +103,8 @@ public class DefaultMetadataService implements MetadataService {
return response;
} catch (JSONException e) {
LOG.error("Unable to persist type {}", typeName, e);
throw new MetadataException("Unable to create response for: " + typeName);
}
}
private void validateTypeDoesNotExist(String typeName,
String typeDefinition) throws MetadataException {
Preconditions.checkNotNull(typeName, "type name cannot be null");
Preconditions.checkNotNull(typeDefinition, "type definition cannot be null");
// verify if the type already exists
if (typeSystem.isRegistered(typeName)) {
LOG.error("type is already defined for {}", typeName);
throw new MetadataException("type is already defined for : " + typeName);
LOG.error("Unable to create response for types={}", typeDefinition, e);
throw new MetadataException("Unable to create response");
}
}
......@@ -186,7 +172,7 @@ public class DefaultMetadataService implements MetadataService {
ClassType entityType = typeSystem.getDataType(ClassType.class, entityTypeName);
return entityType.convert(entityInstance, Multiplicity.REQUIRED);
} catch (Exception e) {
throw new MetadataException("Error deserializing trait instance");
throw new MetadataException("Error deserializing class instance", e);
}
}
......@@ -286,7 +272,7 @@ public class DefaultMetadataService implements MetadataService {
return traitType.convert(
traitInstance, Multiplicity.REQUIRED);
} catch (Exception e) {
throw new MetadataException("Error deserializing trait instance");
throw new MetadataException("Error deserializing trait instance", e);
}
}
......
......@@ -32,12 +32,10 @@ public interface MetadataService {
* Creates a new type based on the type system to enable adding
* entities (instances for types).
*
* @param typeName name for this type, must be unique
* @param typeDefinition definition as json
* @return a unique id for this type
*/
JSONObject createType(String typeName,
String typeDefinition) throws MetadataException;
JSONObject createType(String typeDefinition) throws MetadataException;
/**
* Return the definition for the given type.
......
......@@ -67,29 +67,25 @@ public class TypesResource {
/**
* Submits a type definition corresponding to a given type representing a meta model of a
* domain. Could represent things like Hive Database, Hive Table, etc.
*
* @param typeName name of a type, should be unique.
*/
@POST
@Path("submit/{typeName}")
@Path("submit")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response submit(@Context HttpServletRequest request,
@PathParam("typeName") String typeName) {
public Response submit(@Context HttpServletRequest request) {
try {
final String typeDefinition = Servlets.getRequestPayload(request);
LOG.debug("creating type {} with definition {} ", typeName, typeDefinition);
LOG.debug("creating type with definition {} ", typeDefinition);
JSONObject typesAdded = metadataService.createType(typeName, typeDefinition);
JSONObject typesAdded = metadataService.createType(typeDefinition);
JSONObject response = new JSONObject();
response.put("typeName", typeName);
response.put("types", typesAdded);
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
return Response.ok(response).build();
} catch (Exception e) {
LOG.error("Unable to persist type {}", typeName, e);
LOG.error("Unable to persist types", e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
}
......
......@@ -57,13 +57,12 @@ public abstract class BaseResourceIT {
protected void createType(TypesDef typesDef) throws Exception {
String typesAsJSON = TypesSerialization.toJson(typesDef);
createType(typesAsJSON, "removeme");
createType(typesAsJSON);
}
protected void createType(String typesAsJSON, String typeName) throws Exception {
protected void createType(String typesAsJSON) throws Exception {
WebResource resource = service
.path("api/metadata/types/submit")
.path(typeName);
.path("api/metadata/types/submit");
ClientResponse clientResponse = resource
.accept(MediaType.APPLICATION_JSON)
......@@ -75,7 +74,6 @@ public abstract class BaseResourceIT {
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertEquals(response.get("typeName"), typeName);
Assert.assertNotNull(response.get("types"));
Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID));
}
......
......@@ -269,7 +269,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
TypesUtil.createRequiredAttrDef("description", DataTypes.STRING_TYPE));
String typesAsJSON = TypesSerialization.toJson(testTypeDefinition);
createType(typesAsJSON, "test");
createType(typesAsJSON);
}
@Test (dependsOnMethods = "testSubmitEntity")
......@@ -301,7 +301,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of());
String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
LOG.debug("traitDefinitionAsJSON = " + traitDefinitionAsJSON);
createType(traitDefinitionAsJSON, traitName);
createType(traitDefinitionAsJSON);
Struct traitInstance = new Struct(traitName);
String traitInstanceAsJSON = InstanceSerialization.toJson(traitInstance, true);
......
......@@ -70,8 +70,7 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
System.out.println("typesAsJSON = " + typesAsJSON);
WebResource resource = service
.path("api/metadata/types/submit")
.path(typeDefinition.typeName);
.path("api/metadata/types/submit");
ClientResponse clientResponse = resource
.accept(MediaType.APPLICATION_JSON)
......@@ -83,7 +82,6 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertEquals(response.get("typeName"), typeDefinition.typeName);
Assert.assertNotNull(response.get("types"));
Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID));
}
......@@ -108,7 +106,6 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertEquals(response.get("typeName"), typeDefinition.typeName);
Assert.assertNotNull(response.get("definition"));
Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID));
}
......@@ -186,7 +183,7 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
HierarchicalTypeDefinition<TraitType> traitTypeDef =
TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of());
String json = TypesSerialization$.MODULE$.toJson(traitTypeDef, true);
createType(json, traitName);
createType(json);
}
return traitNames;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment