Commit e747e2ae by Venkatesh Seetharam

Fix tests to rid using TypeSystem in client

parent 35adfb8e
......@@ -22,6 +22,7 @@ import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
......@@ -29,7 +30,12 @@ import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.util.ArrayList;
import java.util.List;
/**
* Client for metadata.
*/
public class MetadataServiceClient {
public static final String REQUEST_ID = "requestId";
public static final String RESULTS = "results";
......@@ -50,8 +56,8 @@ public class MetadataServiceClient {
//Type operations
CREATE_TYPE("api/metadata/types/submit", HttpMethod.POST),
GET_TYPE("api/metadata/types/definition", HttpMethod.GET),
LIST_TYPE("api/metadata/types/list", HttpMethod.GET),
LIST_TRAIT_TYPE("api/metadata/types/traits/list", HttpMethod.GET),
LIST_TYPES("api/metadata/types/list", HttpMethod.GET),
LIST_TRAIT_TYPES("api/metadata/types/traits/list", HttpMethod.GET),
//Entity operations
CREATE_ENTITY("api/metadata/entities/submit", HttpMethod.POST),
......@@ -81,8 +87,28 @@ public class MetadataServiceClient {
}
}
public JSONObject createEntity(String typeName, String entityAsJson) throws MetadataServiceException {
return callAPI(API.CREATE_ENTITY, entityAsJson, typeName);
public JSONObject createType(String typeName,
String typeAsJson) throws MetadataServiceException {
return callAPI(API.CREATE_TYPE, typeAsJson, typeName);
}
public List<String> listTypes() throws MetadataServiceException {
try {
final JSONObject jsonObject = callAPI(API.LIST_TYPES, null);
final JSONArray list = jsonObject.getJSONArray(MetadataServiceClient.RESULTS);
ArrayList<String> types = new ArrayList<>();
for (int index = 0; index < list.length(); index++) {
types.add(list.getString(index));
}
return types;
} catch (JSONException e) {
throw new MetadataServiceException(API.LIST_TYPES, e);
}
}
public JSONObject createEntity(String entityAsJson) throws MetadataServiceException {
return callAPI(API.CREATE_ENTITY, entityAsJson);
}
public String getRequestId(JSONObject json) throws MetadataServiceException {
......@@ -101,7 +127,9 @@ public class MetadataServiceClient {
}
}
ClientResponse clientResponse = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
ClientResponse clientResponse = resource
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(api.getMethod(), ClientResponse.class, requestObject);
if (clientResponse.getStatus() == Response.Status.OK.getStatusCode()) {
......
......@@ -29,12 +29,10 @@ public interface EntityChangeListener {
/**
* This is upon adding a new typed instance to the repository.
*
* @param typeName type name
* @param typedInstance a typed instance
* @throws org.apache.hadoop.metadata.MetadataException
*/
void onEntityAdded(String typeName,
ITypedReferenceableInstance typedInstance) throws MetadataException;
void onEntityAdded(ITypedReferenceableInstance typedInstance) throws MetadataException;
/**
* This is upon adding a new trait to a typed instance.
......
......@@ -69,12 +69,10 @@ public interface MetadataRepository {
* Creates an entity definition (instance) corresponding to a given type.
*
* @param entity entity (typed instance)
* @param entityType entity type name
* @return a globally unique identifier
* @throws RepositoryException
*/
String createEntity(IReferenceableInstance entity,
String entityType) throws RepositoryException;
String createEntity(IReferenceableInstance entity) throws RepositoryException;
/**
* Fetch the complete definition of an entity given its GUID.
......@@ -137,17 +135,6 @@ public interface MetadataRepository {
ITypedStruct traitInstance) throws RepositoryException;
/**
* Adds a list of traits to an existing entity represented by a guid.
*
* @param guid globally unique identifier for the entity
* @param traitInstances list of trait instances that needs to be added to entity
* @return an entity instance with updated traits
* @throws RepositoryException
*/
// ITypedReferenceableInstance addTraits(String guid, Map<String, ITypedStruct> traitInstances)
// throws RepositoryException;
/**
* Deletes a given trait from an existing entity represented by a guid.
*
* @param guid globally unique identifier for the entity
......@@ -160,8 +147,8 @@ public interface MetadataRepository {
/**
* Adds the property to the entity that corresponds to the GUID
* @param guid entity id
* @param property
* @param value
* @param property property name
* @param value property value
*/
void updateEntity(String guid, String property, String value) throws RepositoryException;
}
......@@ -116,9 +116,8 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
}
@Override
public String createEntity(IReferenceableInstance typedInstance,
String typeName) throws RepositoryException {
LOG.info("adding entity={} type={}", typedInstance, typeName);
public String createEntity(IReferenceableInstance typedInstance) throws RepositoryException {
LOG.info("adding entity={}", typedInstance);
try {
titanGraph.rollback();
......
......@@ -28,10 +28,16 @@ import org.apache.hadoop.metadata.repository.MetadataRepository;
import org.apache.hadoop.metadata.repository.typestore.ITypeStore;
import org.apache.hadoop.metadata.typesystem.ITypedReferenceableInstance;
import org.apache.hadoop.metadata.typesystem.ITypedStruct;
import org.apache.hadoop.metadata.typesystem.Referenceable;
import org.apache.hadoop.metadata.typesystem.Struct;
import org.apache.hadoop.metadata.typesystem.TypesDef;
import org.apache.hadoop.metadata.typesystem.json.InstanceSerialization;
import org.apache.hadoop.metadata.typesystem.json.Serialization$;
import org.apache.hadoop.metadata.typesystem.json.TypesSerialization;
import org.apache.hadoop.metadata.typesystem.types.ClassType;
import org.apache.hadoop.metadata.typesystem.types.IDataType;
import org.apache.hadoop.metadata.typesystem.types.Multiplicity;
import org.apache.hadoop.metadata.typesystem.types.TraitType;
import org.apache.hadoop.metadata.typesystem.types.TypeSystem;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
......@@ -151,24 +157,39 @@ public class DefaultMetadataService implements MetadataService {
/**
* Creates an entity, instance of the type.
*
* @param entityType type
* @param entityDefinition definition
* @param entityInstanceDefinition definition
* @return guid
*/
@Override
public String createEntity(String entityType,
String entityDefinition) throws MetadataException {
Preconditions.checkNotNull(entityDefinition, "entity cannot be null");
Preconditions.checkNotNull(entityType, "entity type cannot be null");
public String createEntity(String entityInstanceDefinition) throws MetadataException {
Preconditions.checkNotNull(entityInstanceDefinition,
"entity instance definition cannot be null");
ITypedReferenceableInstance entityTypedInstance =
deserializeClassInstance(entityInstanceDefinition);
ITypedReferenceableInstance entityInstance =
Serialization$.MODULE$.fromJson(entityDefinition);
final String guid = repository.createEntity(entityInstance, entityType);
final String guid = repository.createEntity(entityTypedInstance);
onEntityAddedToRepo(entityType, entityInstance);
onEntityAddedToRepo(entityTypedInstance);
return guid;
}
private ITypedReferenceableInstance deserializeClassInstance(
String entityInstanceDefinition) throws MetadataException {
try {
final Referenceable entityInstance = InstanceSerialization.fromJsonReferenceable(
entityInstanceDefinition, true);
final String entityTypeName = entityInstance.getTypeName();
Preconditions.checkNotNull(entityTypeName, "entity type cannot be null");
ClassType entityType = typeSystem.getDataType(ClassType.class, entityTypeName);
return entityType.convert(entityInstance, Multiplicity.REQUIRED);
} catch (Exception e) {
throw new MetadataException("Error deserializing trait instance");
}
}
/**
* Return the definition for the given guid.
*
......@@ -256,7 +277,14 @@ public class DefaultMetadataService implements MetadataService {
throws MetadataException {
try {
return (ITypedStruct) Serialization$.MODULE$.traitFromJson(traitInstanceDefinition);
Struct traitInstance = InstanceSerialization.fromJsonStruct(
traitInstanceDefinition, true);
final String entityTypeName = traitInstance.getTypeName();
Preconditions.checkNotNull(entityTypeName, "entity type cannot be null");
TraitType traitType = typeSystem.getDataType(TraitType.class, entityTypeName);
return traitType.convert(
traitInstance, Multiplicity.REQUIRED);
} catch (Exception e) {
throw new MetadataException("Error deserializing trait instance");
}
......@@ -301,12 +329,11 @@ public class DefaultMetadataService implements MetadataService {
typesChangeListeners.remove(listener);
}
private void onEntityAddedToRepo(String typeName,
ITypedReferenceableInstance typedInstance)
private void onEntityAddedToRepo(ITypedReferenceableInstance typedInstance)
throws MetadataException {
for (EntityChangeListener listener : entityChangeListeners) {
listener.onEntityAdded(typeName, typedInstance);
listener.onEntityAdded(typedInstance);
}
}
......
......@@ -64,11 +64,10 @@ public interface MetadataService {
/**
* Creates an entity, instance of the type.
*
* @param entityType type
* @param entityDefinition definition
* @return guid
*/
String createEntity(String entityType, String entityDefinition) throws MetadataException;
String createEntity(String entityDefinition) throws MetadataException;
/**
* Return the definition for the given guid.
......@@ -87,10 +86,11 @@ public interface MetadataService {
List<String> getEntityList(String entityType) throws MetadataException;
/**
* Adds the property to the given entity id(guid)
* Adds the property to the given entity id(guid).
*
* @param guid entity id
* @param property
* @param value
* @param property property name
* @param value property value
*/
void updateEntity(String guid, String property, String value) throws MetadataException;
......
......@@ -78,7 +78,7 @@ public class GraphBackedDiscoveryServiceTest {
ClassType deptType = typeSystem.getDataType(ClassType.class, "Department");
ITypedReferenceableInstance hrDept2 = deptType.convert(hrDept, Multiplicity.REQUIRED);
repositoryService.createEntity(hrDept2, "Department");
repositoryService.createEntity(hrDept2);
}
private void setupSampleData() throws ScriptException {
......@@ -173,12 +173,10 @@ public class GraphBackedDiscoveryServiceTest {
{"DB, Table"},
/*{"DB as db1 Table where db1.name = \"Reporting\""},*/
{"DB name = \"Reporting\""},
/*
{"Column where is PII"},
{"Table where is Dimension"},
{"View where is Dimension"},
{"Column where is PII select Column.name"},
*/
{"Column where Column isa PII"},
{"Table is Dimension"},
{"View is Dimension"},
/*{"Column where Column isa PII select Column.name"},*/
{"Column select Column.name"},
{"from Table select Table.name"},
};
......
......@@ -106,7 +106,7 @@ public class GraphBackedMetadataRepositoryTest {
ClassType deptType = typeSystem.getDataType(ClassType.class, "Department");
ITypedReferenceableInstance hrDept2 = deptType.convert(hrDept, Multiplicity.REQUIRED);
guid = repositoryService.createEntity(hrDept2, ENTITY_TYPE);
guid = repositoryService.createEntity(hrDept2);
Assert.assertNotNull(guid);
}
......@@ -154,14 +154,14 @@ public class GraphBackedMetadataRepositoryTest {
ITypedReferenceableInstance db = dbType.convert(databaseInstance, Multiplicity.REQUIRED);
System.out.println("db = " + db);
String dbGUID = repositoryService.createEntity(db, DATABASE_TYPE);
String dbGUID = repositoryService.createEntity(db);
System.out.println("added db = " + dbGUID);
Referenceable dbInstance = new Referenceable(
dbGUID, DATABASE_TYPE, databaseInstance.getValuesMap());
ITypedReferenceableInstance table = createHiveTableInstance(dbInstance);
String tableGUID = repositoryService.createEntity(table, TABLE_TYPE);
String tableGUID = repositoryService.createEntity(table);
System.out.println("added table = " + tableGUID);
}
......
......@@ -89,14 +89,14 @@ public class GraphRepoMapperScaleTest {
ClassType dbType = typeSystem.getDataType(ClassType.class, DATABASE_TYPE);
ITypedReferenceableInstance db = dbType.convert(databaseInstance, Multiplicity.REQUIRED);
dbGUID = repositoryService.createEntity(db, DATABASE_TYPE);
dbGUID = repositoryService.createEntity(db);
Referenceable dbInstance = new Referenceable(
dbGUID, DATABASE_TYPE, databaseInstance.getValuesMap());
for (int index = 0; index < 1000; index++) {
ITypedReferenceableInstance table = createHiveTableInstance(dbInstance, index);
repositoryService.createEntity(table, TABLE_TYPE);
repositoryService.createEntity(table);
}
}
......
......@@ -78,31 +78,28 @@ public class EntityResource {
/**
* Submits an entity definition (instance) corresponding to a given type.
*
* @param typeName name of a type which is unique.
*/
@POST
@Path("submit/{typeName}")
@Path("submit")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response submit(@Context HttpServletRequest request,
@PathParam("typeName") final String typeName) {
public Response submit(@Context HttpServletRequest request) {
try {
final String entity = Servlets.getRequestPayload(request);
LOG.debug("submitting entity {} ", entity);
final String guid = metadataService.createEntity(typeName, entity);
final String guid = metadataService.createEntity(entity);
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put(MetadataServiceClient.RESULTS, guid);
return Response.ok(response).build();
} catch (MetadataException | IOException | IllegalArgumentException e) {
LOG.error("Unable to persist instance for type {}", typeName, e);
LOG.error("Unable to persist entity instance", e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (JSONException e) {
LOG.error("Unable to persist instance for type {}", typeName, e);
LOG.error("Unable to persist entity instance", e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
......@@ -193,12 +190,13 @@ public class EntityResource {
* @param guid entity id
* @param property property to add
* @param value property's value
* @return
* @return response payload as json
*/
@PUT
@Path("update/{guid}")
@Produces(MediaType.APPLICATION_JSON)
public Response update(@PathParam("guid") String guid, @QueryParam("property") String property,
public Response update(@PathParam("guid") String guid,
@QueryParam("property") String property,
@QueryParam("value") String value) {
try {
metadataService.updateEntity(guid, property, value);
......
......@@ -23,27 +23,28 @@ import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.typesystem.types.TypeSystem;
import org.apache.hadoop.metadata.web.util.Servlets;
import org.apache.hadoop.metadata.typesystem.Referenceable;
import org.apache.hadoop.metadata.typesystem.TypesDef;
import org.apache.hadoop.metadata.typesystem.json.InstanceSerialization;
import org.apache.hadoop.metadata.typesystem.json.TypesSerialization;
import org.codehaus.jettison.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
/**
* Base class for integration tests.
* Sets up the web resource and has helper methods to create type and entity.
*/
public abstract class BaseResourceIT {
protected TypeSystem typeSystem;
protected WebResource service;
protected MetadataServiceClient serviceClient;
public void setUp() throws Exception {
typeSystem = TypeSystem.getInstance();
typeSystem.reset();
String baseUrl = "http://localhost:21000/";
DefaultClientConfig config = new DefaultClientConfig();
......@@ -54,10 +55,15 @@ public abstract class BaseResourceIT {
serviceClient = new MetadataServiceClient(baseUrl);
}
protected void sumbitType(String typesAsJSON, String type) throws Exception {
protected void createType(TypesDef typesDef) throws Exception {
String typesAsJSON = TypesSerialization.toJson(typesDef);
createType(typesAsJSON, "removeme");
}
protected void createType(String typesAsJSON, String typeName) throws Exception {
WebResource resource = service
.path("api/metadata/types/submit")
.path(type);
.path(typeName);
ClientResponse clientResponse = resource
.accept(MediaType.APPLICATION_JSON)
......@@ -69,8 +75,22 @@ public abstract class BaseResourceIT {
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertEquals(response.get("typeName"), type);
Assert.assertEquals(response.get("typeName"), typeName);
Assert.assertNotNull(response.get("types"));
Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID));
}
protected Referenceable createInstance(Referenceable referenceable) throws Exception {
String typeName = referenceable.getTypeName();
System.out.println("creating instance of type " + typeName);
String entityJSON = InstanceSerialization.toJson(referenceable, true);
System.out.println("Submitting new entity= " + entityJSON);
JSONObject jsonObject = serviceClient.createEntity(entityJSON);
String guid = jsonObject.getString(MetadataServiceClient.RESULTS);
System.out.println("created instance for type " + typeName + ", guid: " + guid);
// return the reference to created instance with guid
return new Referenceable(guid, referenceable.getTypeName(), referenceable.getValuesMap());
}
}
......@@ -22,18 +22,15 @@ import com.google.common.collect.ImmutableList;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.MetadataServiceException;
import org.apache.hadoop.metadata.typesystem.ITypedInstance;
import org.apache.hadoop.metadata.typesystem.ITypedReferenceableInstance;
import org.apache.hadoop.metadata.typesystem.ITypedStruct;
import org.apache.hadoop.metadata.typesystem.Referenceable;
import org.apache.hadoop.metadata.typesystem.Struct;
import org.apache.hadoop.metadata.typesystem.json.Serialization;
import org.apache.hadoop.metadata.typesystem.json.Serialization$;
import org.apache.hadoop.metadata.typesystem.TypesDef;
import org.apache.hadoop.metadata.typesystem.json.InstanceSerialization;
import org.apache.hadoop.metadata.typesystem.json.InstanceSerialization$;
import org.apache.hadoop.metadata.typesystem.json.TypesSerialization;
import org.apache.hadoop.metadata.typesystem.json.TypesSerialization$;
import org.apache.hadoop.metadata.typesystem.persistence.Id;
import org.apache.hadoop.metadata.typesystem.types.AttributeDefinition;
import org.apache.hadoop.metadata.typesystem.types.AttributeInfo;
import org.apache.hadoop.metadata.typesystem.types.ClassType;
import org.apache.hadoop.metadata.typesystem.types.DataTypes;
import org.apache.hadoop.metadata.typesystem.types.EnumTypeDefinition;
......@@ -42,10 +39,9 @@ import org.apache.hadoop.metadata.typesystem.types.HierarchicalTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.Multiplicity;
import org.apache.hadoop.metadata.typesystem.types.StructTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.TraitType;
import org.apache.hadoop.metadata.typesystem.types.TypeUtils;
import org.apache.hadoop.metadata.typesystem.types.utils.TypesUtil;
import org.apache.hadoop.metadata.web.util.Servlets;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -56,7 +52,6 @@ import org.testng.annotations.Test;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
......@@ -72,28 +67,20 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
private static final String TABLE_TYPE = "hive_table";
private static final String TABLE_NAME = "bar";
private ITypedReferenceableInstance tableInstance;
private String guid;
private Referenceable tableInstance;
@BeforeClass
public void setUp() throws Exception {
super.setUp();
createHiveTypes();
submitTypes();
}
private JSONObject submit(ITypedReferenceableInstance instance) throws MetadataServiceException {
String instanceAsJSON = Serialization$.MODULE$.toJson(instance);
return serviceClient.createEntity(instance.getTypeName(), instanceAsJSON);
}
@Test
public void testSubmitEntity() throws Exception {
tableInstance = createHiveTableInstance();
JSONObject clientResponse = submit(tableInstance);
guid = getGuid(clientResponse);
String guid = getGuid(tableInstance);
try {
Assert.assertNotNull(UUID.fromString(guid));
} catch (IllegalArgumentException e) {
......@@ -101,22 +88,26 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
}
}
private String getGuid(JSONObject response) throws JSONException {
Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID));
private String getGuid(Referenceable referenceable) throws Exception {
Id id = referenceable.getId();
Assert.assertNotNull(id);
String guid = response.get(MetadataServiceClient.RESULTS).toString();
String guid = id.id;
Assert.assertNotNull(guid);
return guid;
}
@Test (dependsOnMethods = "testSubmitEntity")
public void testAddProperty() throws Exception {
String guid = getGuid(tableInstance);
//add property
String description = "bar table - new desc";
ClientResponse clientResponse = addProperty(guid, "description", description);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
ITypedReferenceableInstance entityRef = getEntityDefinition(getEntityDefinition(guid));
Assert.assertEquals(entityRef.get("description"), description);
String entityRef = getEntityDefinition(getEntityDefinition(guid));
Assert.assertNotNull(entityRef);
tableInstance.set("description", description);
//invalid property for the type
......@@ -126,8 +117,10 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
//non-string property, update
clientResponse = addProperty(guid, "level", "4");
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
entityRef = getEntityDefinition(getEntityDefinition(guid));
Assert.assertEquals(entityRef.get("level"), 4);
Assert.assertNotNull(entityRef);
tableInstance.set("level", 4);
}
......@@ -138,19 +131,21 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
databaseInstance.set("name", "newdb");
databaseInstance.set("description", "new database");
ClassType classType = typeSystem.getDataType(ClassType.class, DATABASE_TYPE);
ITypedReferenceableInstance dbInstance = classType.convert(databaseInstance, Multiplicity.REQUIRED);
// ClassType classType = typeSystem.getDataType(ClassType.class, DATABASE_TYPE);
// ITypedReferenceableInstance dbInstance = classType.convert(databaseInstance, Multiplicity.REQUIRED);
JSONObject json = submit(dbInstance);
String dbId = getGuid(json);
Referenceable dbInstance = createInstance(databaseInstance);
String dbId = getGuid(dbInstance);
//Add reference property
String guid = getGuid(tableInstance);
ClientResponse clientResponse = addProperty(guid, "database", dbId);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
}
@Test(dependsOnMethods = "testSubmitEntity")
public void testGetEntityDefinition() throws Exception {
String guid = getGuid(tableInstance);
ClientResponse clientResponse = getEntityDefinition(guid);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
......@@ -163,11 +158,6 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
final String definition = response.getString(MetadataServiceClient.RESULTS);
Assert.assertNotNull(definition);
LOG.debug("tableInstanceAfterGet = " + definition);
// todo - this fails with type error, strange
ITypedReferenceableInstance tableInstanceAfterGet =
Serialization$.MODULE$.fromJson(definition);
Assert.assertTrue(areEqual(tableInstance, tableInstanceAfterGet));
}
private ClientResponse addProperty(String guid, String property, String value) {
......@@ -190,36 +180,13 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
.method(HttpMethod.GET, ClientResponse.class);
}
private ITypedReferenceableInstance getEntityDefinition(ClientResponse clientResponse) throws Exception {
private String getEntityDefinition(ClientResponse clientResponse) throws Exception {
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
JSONObject response = new JSONObject(clientResponse.getEntity(String.class));
final String definition = response.getString(MetadataServiceClient.RESULTS);
Assert.assertNotNull(definition);
return Serialization.fromJson(definition);
}
private boolean areEqual(ITypedInstance actual,
ITypedInstance expected) throws Exception {
/*
Assert.assertEquals(Serialization$.MODULE$.toJson(actual),
Serialization$.MODULE$.toJson(expected));
*/
for (AttributeInfo attributeInfo : actual.fieldMapping().fields.values()) {
final DataTypes.TypeCategory typeCategory = attributeInfo.dataType().getTypeCategory();
if (typeCategory == DataTypes.TypeCategory.STRUCT
|| typeCategory == DataTypes.TypeCategory.TRAIT
|| typeCategory == DataTypes.TypeCategory.CLASS) {
areEqual((ITypedStruct) actual.get(attributeInfo.name),
(ITypedStruct) expected.get(attributeInfo.name));
} else if (typeCategory == DataTypes.TypeCategory.PRIMITIVE
|| typeCategory == DataTypes.TypeCategory.ENUM) {
Assert.assertEquals(actual.get(attributeInfo.name),
expected.get(attributeInfo.name));
}
}
return true;
return definition;
}
@Test
......@@ -302,11 +269,12 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
TypesUtil.createRequiredAttrDef("description", DataTypes.STRING_TYPE));
String typesAsJSON = TypesSerialization.toJson(testTypeDefinition);
sumbitType(typesAsJSON, "test");
createType(typesAsJSON, "test");
}
@Test (dependsOnMethods = "testSubmitEntity")
public void testGetTraitNames() throws Exception {
String guid = getGuid(tableInstance);
ClientResponse clientResponse = service
.path("api/metadata/entities/traits/list")
.path(guid)
......@@ -333,15 +301,13 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of());
String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
LOG.debug("traitDefinitionAsJSON = " + traitDefinitionAsJSON);
sumbitType(traitDefinitionAsJSON, traitName);
createType(traitDefinitionAsJSON, traitName);
typeSystem.defineTraitType(piiTrait);
Struct s = new Struct(traitName);
TraitType tType = typeSystem.getDataType(TraitType.class, traitName);
ITypedInstance traitInstance = tType.convert(s, Multiplicity.REQUIRED);
String traitInstanceAsJSON = Serialization$.MODULE$.toJson(traitInstance);
Struct traitInstance = new Struct(traitName);
String traitInstanceAsJSON = InstanceSerialization.toJson(traitInstance, true);
LOG.debug("traitInstanceAsJSON = " + traitInstanceAsJSON);
String guid = getGuid(tableInstance);
ClientResponse clientResponse = service
.path("api/metadata/entities/traits/add")
.path(guid)
......@@ -367,11 +333,8 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
LOG.debug("traitDefinitionAsJSON = " + traitDefinitionAsJSON);
typeSystem.defineTraitType(piiTrait);
Struct s = new Struct(traitName);
TraitType tType = typeSystem.getDataType(TraitType.class, traitName);
ITypedInstance traitInstance = tType.convert(s, Multiplicity.REQUIRED);
String traitInstanceAsJSON = Serialization$.MODULE$.toJson(traitInstance);
Struct traitInstance = new Struct(traitName);
String traitInstanceAsJSON = InstanceSerialization$.MODULE$.toJson(traitInstance, true);
LOG.debug("traitInstanceAsJSON = " + traitInstanceAsJSON);
ClientResponse clientResponse = service
......@@ -387,6 +350,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
@Test (dependsOnMethods = "testAddTrait")
public void testDeleteTrait() throws Exception {
final String traitName = "PII_Trait";
final String guid = getGuid(tableInstance);
ClientResponse clientResponse = service
.path("api/metadata/entities/traits/delete")
......@@ -441,7 +405,6 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
};
EnumTypeDefinition enumTypeDefinition = new EnumTypeDefinition("tableType", values);
typeSystem.defineEnumType(enumTypeDefinition);
HierarchicalTypeDefinition<ClassType> tableTypeDefinition =
TypesUtil.createClassTypeDef(TABLE_TYPE,
......@@ -476,33 +439,16 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
HierarchicalTypeDefinition<TraitType> financeTrait =
TypesUtil.createTraitTypeDef("finance", ImmutableList.<String>of());
typeSystem.defineTypes(
TypesDef typesDef = TypeUtils.getTypesDef(
ImmutableList.of(enumTypeDefinition),
ImmutableList.of(structTypeDefinition),
ImmutableList.of(classificationTraitDefinition, piiTrait, phiTrait, pciTrait,
soxTrait, secTrait, financeTrait),
ImmutableList.of(databaseTypeDefinition, tableTypeDefinition));
createType(typesDef);
}
private void submitTypes() throws Exception {
@SuppressWarnings("unchecked")
String typesAsJSON = TypesSerialization.toJson(typeSystem,
Arrays.asList(new String[]{
"tableType",
DATABASE_TYPE,
TABLE_TYPE,
"serdeType",
"classification",
"pii",
"phi",
"pci",
"sox",
"sec",
"finance",
}));
sumbitType(typesAsJSON, TABLE_TYPE);
}
private ITypedReferenceableInstance createHiveTableInstance() throws Exception {
private Referenceable createHiveTableInstance() throws Exception {
Referenceable databaseInstance = new Referenceable(DATABASE_TYPE);
databaseInstance.set("name", DATABASE_NAME);
databaseInstance.set("description", "foo database");
......@@ -532,7 +478,6 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
List<String> traits = tableInstance.getTraits();
Assert.assertEquals(traits.size(), 7);
ClassType tableType = typeSystem.getDataType(ClassType.class, TABLE_TYPE);
return tableType.convert(tableInstance, Multiplicity.REQUIRED);
return createInstance(tableInstance);
}
}
......@@ -22,26 +22,22 @@ import com.google.common.collect.ImmutableList;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.typesystem.Struct;
import org.apache.hadoop.metadata.typesystem.json.Serialization$;
import org.apache.hadoop.metadata.typesystem.json.TypesSerialization;
import org.apache.hadoop.metadata.typesystem.ITypedReferenceableInstance;
import org.apache.hadoop.metadata.typesystem.Referenceable;
import org.apache.hadoop.metadata.typesystem.Struct;
import org.apache.hadoop.metadata.typesystem.TypesDef;
import org.apache.hadoop.metadata.typesystem.types.ClassType;
import org.apache.hadoop.metadata.typesystem.types.DataTypes;
import org.apache.hadoop.metadata.typesystem.types.EnumTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.HierarchicalTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.Multiplicity;
import org.apache.hadoop.metadata.typesystem.types.StructTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.TraitType;
import org.apache.hadoop.metadata.typesystem.types.TypeUtils;
import org.apache.hadoop.metadata.typesystem.types.utils.TypesUtil;
import org.apache.hadoop.metadata.web.util.Servlets;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import scala.actors.threadpool.Arrays;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
......@@ -49,17 +45,14 @@ import javax.ws.rs.core.Response;
import java.util.List;
public class MetadataDiscoveryResourceIT extends BaseResourceIT {
public class MetadataDiscoveryJerseyResourceIT extends BaseResourceIT {
@BeforeClass
public void setUp() throws Exception {
super.setUp();
createTypes();
submitTypes();
ITypedReferenceableInstance instance = createInstance();
String instanceAsJSON = Serialization$.MODULE$.toJson(instance);
submitEntity(instanceAsJSON);
createInstance();
}
@Test
......@@ -210,56 +203,6 @@ public class MetadataDiscoveryResourceIT extends BaseResourceIT {
Response.Status.NOT_FOUND.getStatusCode());
}
@Test(dependsOnMethods = "testUriExists", enabled = false)
public void testSearchForTextNoDepth() throws Exception {
WebResource resource = service
.path("api/metadata/discovery/search/fulltext")
.queryParam("depth", "0").queryParam("text", "kid").queryParam("property", "Name");
ClientResponse clientResponse = resource
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.GET, ClientResponse.class);
// TODO - Get count of expected vertices and Edges
// Assert.assertEquals(true, true);
}
@Test(dependsOnMethods = "testUriExists", enabled = false)
public void testSearchTextWithDepth() throws Exception {
WebResource resource = service
.path("api/metadata/discovery/search/fulltext")
.queryParam("depth", "4").queryParam("text", "Grandad")
.queryParam("property", "Name");
ClientResponse clientResponse = resource
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.GET, ClientResponse.class);
// TODO - Get count of expected vertices and Edges
// Assert.assertEquals(true, true);
}
private void submitEntity(String instanceAsJSON) throws JSONException {
WebResource resource = service
.path("api/metadata/entities/submit")
.path("dsl_test_type");
ClientResponse clientResponse = resource
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.POST, ClientResponse.class, instanceAsJSON);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID));
String guid = response.get(MetadataServiceClient.RESULTS).toString();
Assert.assertNotNull(guid);
}
private void createTypes() throws Exception {
HierarchicalTypeDefinition<ClassType> dslTestTypeDefinition =
TypesUtil.createClassTypeDef("dsl_test_type",
......@@ -284,30 +227,16 @@ public class MetadataDiscoveryResourceIT extends BaseResourceIT {
HierarchicalTypeDefinition<TraitType> financeTrait =
TypesUtil.createTraitTypeDef("Finance", ImmutableList.<String>of());
typeSystem.defineTypes(
TypesDef typesDef = TypeUtils.getTypesDef(
ImmutableList.<EnumTypeDefinition>of(),
ImmutableList.<StructTypeDefinition>of(),
ImmutableList.of(classificationTraitDefinition, piiTrait, phiTrait, pciTrait,
soxTrait, secTrait, financeTrait),
ImmutableList.of(dslTestTypeDefinition));
createType(typesDef);
}
private void submitTypes() throws Exception {
@SuppressWarnings("unchecked")
String typesAsJSON = TypesSerialization.toJson(typeSystem,
Arrays.asList(new String[]{
"dsl_test_type",
"Classification",
"PII",
"PHI",
"PCI",
"SOX",
"SEC",
"Finance",
}));
sumbitType(typesAsJSON, "dsl_test_type");
}
private ITypedReferenceableInstance createInstance() throws Exception {
private Referenceable createInstance() throws Exception {
Referenceable entityInstance = new Referenceable("dsl_test_type",
"Classification", "PII", "PHI", "PCI", "SOX", "SEC", "Finance");
entityInstance.set("name", "foo name");
......@@ -319,7 +248,6 @@ public class MetadataDiscoveryResourceIT extends BaseResourceIT {
List<String> traits = entityInstance.getTraits();
Assert.assertEquals(traits.size(), 7);
ClassType tableType = typeSystem.getDataType(ClassType.class, "dsl_test_type");
return tableType.convert(entityInstance, Multiplicity.REQUIRED);
return createInstance(entityInstance);
}
}
\ No newline at end of file
......@@ -31,7 +31,6 @@ import org.apache.hadoop.metadata.typesystem.types.HierarchicalTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.Multiplicity;
import org.apache.hadoop.metadata.typesystem.types.TraitType;
import org.apache.hadoop.metadata.typesystem.types.utils.TypesUtil;
import org.apache.hadoop.metadata.web.util.Servlets;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.testng.Assert;
......@@ -187,7 +186,7 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
HierarchicalTypeDefinition<TraitType> traitTypeDef =
TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of());
String json = TypesSerialization$.MODULE$.toJson(traitTypeDef, true);
sumbitType(json, traitName);
createType(json, traitName);
}
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