Commit 4f928a8d by Venkatesh Seetharam

Remove type name from create type API

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