Refs #10247: Create Resource Registry Context Client

Task-Url: https://support.d4science.org/issues/10247


git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@158786 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-11-23 14:10:08 +00:00
parent 4f42387f85
commit bdf17da9aa
6 changed files with 21 additions and 911 deletions

View File

@ -1,29 +0,0 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.context;
import java.util.UUID;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextCreationException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public interface OLDContextManagement {
public String create(UUID parentContext, String name) throws ContextCreationException, ResourceRegistryException;
public String read(UUID context) throws ContextNotFoundException, ContextException;
public String rename(UUID context, String newName) throws ContextNotFoundException, ContextException;
public String move(UUID newParent, UUID contextToMove) throws ContextNotFoundException, ContextException;
public boolean delete(UUID context) throws ContextNotFoundException, ContextException;
}

View File

@ -1,324 +0,0 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.context;
import java.util.Iterator;
import java.util.UUID;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.model.relation.IsParentOf;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextCreationException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class OLDContextManagementImpl implements OLDContextManagement {
private static Logger logger = LoggerFactory.getLogger(OLDContextManagementImpl.class);
protected Vertex checkContext(OrientGraph orientGraph, UUID parentContext, String contextName)
throws ContextNotFoundException, ContextException {
Vertex parent = null;
if (parentContext != null) {
parent = getContext(orientGraph, parentContext);
// TODO Rewrite using Gremlin
String select = "SELECT FROM (TRAVERSE out(" + IsParentOf.NAME + ") FROM " + parent.getId()
+ " MAXDEPTH 1) WHERE " + Context.NAME_PROPERTY + "=\"" + contextName + "\" AND "
+ Context.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + "<>\"" + parentContext.toString() + "\"";
logger.trace(select);
String message = "A context with the same name (" + contextName + ") has been already created as child of "
+ parentContext.toString() + "(name=" + parent.getProperty(Context.NAME_PROPERTY).toString() + ")";
logger.trace("Checking if {} -> {}", message, select);
OSQLSynchQuery<Vertex> osqlSynchQuery = new OSQLSynchQuery<Vertex>(select);
Iterable<Vertex> vertexes = orientGraph.command(osqlSynchQuery).execute();
if (vertexes != null && vertexes.iterator().hasNext()) {
throw new ContextException(message);
}
} else {
// TODO Rewrite using Gremlin
String select = "SELECT FROM " + org.gcube.informationsystem.model.entity.Context.NAME + " WHERE "
+ Context.NAME_PROPERTY + " = \"" + contextName + "\"" + " AND in(\"" + IsParentOf.NAME
+ "\").size() = 0";
OSQLSynchQuery<Vertex> osqlSynchQuery = new OSQLSynchQuery<Vertex>(select);
Iterable<Vertex> vertexes = orientGraph.command(osqlSynchQuery).execute();
if (vertexes != null && vertexes.iterator().hasNext()) {
throw new ContextException("A root context with the same name (" + contextName + ") already exist");
}
}
return parent;
}
public Vertex getContext(OrientGraph orientGraph, UUID context) throws ContextNotFoundException {
try {
return Utility.getElementByUUID(orientGraph, Context.NAME, context, Vertex.class);
} catch (ResourceRegistryException e) {
throw new ContextNotFoundException(e.getMessage());
}
}
@Override
public String create(UUID parentContext, String name) throws ContextCreationException, ResourceRegistryException {
OrientGraph orientGraph = null;
UUID uuid = UUID.randomUUID();
try {
logger.info("Trying to create {} with name {} and parent {} UUID {}", Context.NAME, name, Context.NAME,
parentContext);
orientGraph = SecurityContextMapper
.getSecurityContextGraph(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER);
Vertex parent;
try {
parent = checkContext(orientGraph, parentContext, name);
} catch (ContextException e) {
throw new ContextCreationException(e.getMessage());
}
SecurityContext.createSecurityContext(orientGraph, uuid);
OrientVertex context = orientGraph.addVertex("class:" + Context.NAME);
context.setProperty(Context.NAME_PROPERTY, name);
context.save();
HeaderUtility.addHeader(context, uuid);
if (parentContext != null) {
OrientEdge edge = orientGraph.addEdge(null, parent, context, IsParentOf.NAME);
HeaderUtility.addHeader(edge, null);
edge.save();
}
SecurityContext.addToSecurityContext(orientGraph, context, uuid);
logger.trace("Creating {}", Utility.toJsonString(context, true));
orientGraph.commit();
Vertex readContext = getContext(orientGraph, uuid);
logger.info("Context created {}", Utility.toJsonString((OrientVertex) readContext, true));
return Utility.toJsonString((OrientVertex) readContext, false);
} catch (ContextCreationException e) {
throw e;
} catch (Exception e) {
if (orientGraph != null) {
orientGraph.rollback();
SecurityContext.deleteSecurityContext(orientGraph, uuid, true);
}
throw new ResourceRegistryException(e.getMessage());
} finally {
if (orientGraph != null) {
orientGraph.shutdown();
}
}
}
@Override
public String read(UUID contextUUID) throws ContextNotFoundException, ContextException {
OrientGraph orientGraph = SecurityContextMapper
.getSecurityContextGraph(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.READER);
Vertex context = getContext(orientGraph, contextUUID);
return Utility.toJsonString((OrientVertex) context, false);
}
@Override
public String rename(UUID contextUUID, String newName) throws ContextNotFoundException, ContextException {
OrientGraph orientGraph = null;
try {
logger.info("Trying to rename {} with UUID {} to {}", Context.NAME, contextUUID, newName);
orientGraph = SecurityContextMapper
.getSecurityContextGraph(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER);
Vertex context = getContext(orientGraph, contextUUID);
UUID parentUUID = null;
Iterable<Edge> edges = context.getEdges(Direction.IN, IsParentOf.NAME);
if (edges != null && edges.iterator().hasNext()) {
Iterator<Edge> iteratorEdge = edges.iterator();
Edge edge = iteratorEdge.next();
if (iteratorEdge.hasNext()) {
throw new ContextException("Seems that the Context has more than one Parent. "
+ Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
Vertex parent = edge.getVertex(Direction.OUT);
parentUUID = UUID
.fromString((String) parent.getProperty(Context.HEADER_PROPERTY + "." + Header.UUID_PROPERTY));
}
checkContext(orientGraph, parentUUID, newName);
context.setProperty(Context.NAME_PROPERTY, newName);
orientGraph.commit();
ContextUtility.invalidContextUUIDCache(contextUUID);
String contextJsonString = Utility.toJsonString(context, true);
logger.info("Context renamed {}", contextJsonString);
return Utility.toJsonString((OrientVertex) context, false);
} catch (ContextException ce) {
if (orientGraph != null) {
orientGraph.rollback();
}
throw ce;
} catch (Exception e) {
if (orientGraph != null) {
orientGraph.rollback();
}
throw new ContextException(e.getMessage());
} finally {
if (orientGraph != null) {
orientGraph.shutdown();
}
}
}
@Override
public String move(UUID newParentUUID, UUID contextToMoveUUID) throws ContextNotFoundException, ContextException {
OrientGraph orientGraph = null;
try {
logger.info("Trying to move {} with UUID {} as child of {} with UUID {}", Context.NAME, contextToMoveUUID,
Context.NAME, newParentUUID);
orientGraph = SecurityContextMapper
.getSecurityContextGraph(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER);
Vertex context = getContext(orientGraph, contextToMoveUUID);
logger.trace("Context to move {}", Utility.toJsonString(context, true));
checkContext(orientGraph, newParentUUID, context.getProperty(Context.NAME_PROPERTY).toString());
// Removing the old parent relationship if any
Iterable<Edge> edges = context.getEdges(Direction.IN, IsParentOf.NAME);
if (edges != null && edges.iterator().hasNext()) {
Iterator<Edge> edgeIterator = edges.iterator();
Edge edge = edgeIterator.next();
logger.trace("Removing {} {}", Edge.class.getSimpleName(), edge);
edge.remove();
}
if (newParentUUID != null) {
Vertex parent = getContext(orientGraph, newParentUUID);
logger.trace("New Parent Context {}", Utility.toJsonString(parent, true));
OrientEdge edge = orientGraph.addEdge(null, parent, context, IsParentOf.NAME);
HeaderUtility.addHeader(edge, null);
edge.save();
}
orientGraph.commit();
ContextUtility.invalidContextUUIDCache(contextToMoveUUID);
context = getContext(orientGraph, contextToMoveUUID);
String contextJsonString = Utility.toJsonString(context, true);
logger.info("Context moved {}", contextJsonString);
return Utility.toJsonString((OrientVertex) context, false);
} catch (ContextException ce) {
if (orientGraph != null) {
orientGraph.rollback();
}
throw ce;
} catch (Exception e) {
if (orientGraph != null) {
orientGraph.rollback();
}
throw new ContextException(e.getMessage());
} finally {
if (orientGraph != null) {
orientGraph.shutdown();
}
}
}
@Override
public boolean delete(UUID uuid) throws ContextNotFoundException, ContextException {
OrientGraph orientGraph = null;
try {
logger.info("Trying to remove {} with UUID {}", Context.NAME, uuid);
orientGraph = SecurityContextMapper
.getSecurityContextGraph(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER);
Vertex context = getContext(orientGraph, uuid);
logger.trace("Context to be delete {}", Utility.toJsonString(context, true));
Iterable<Edge> edges = context.getEdges(Direction.OUT, IsParentOf.NAME);
if (edges != null && edges.iterator().hasNext()) {
throw new ContextException("Only context with no children can be deleted");
}
SecurityContext.deleteSecurityContext(orientGraph, uuid, false);
context.remove();
orientGraph.commit();
ContextUtility.invalidContextUUIDCache(uuid);
logger.info("{} with UUID {} successfully removed", Context.NAME, uuid);
return true;
} catch (ContextException | ContextNotFoundException ce) {
if (orientGraph != null) {
orientGraph.rollback();
}
throw ce;
} catch (Exception e) {
if (orientGraph != null) {
orientGraph.rollback();
}
throw new ContextException(e.getMessage());
} finally {
if (orientGraph != null) {
orientGraph.shutdown();
}
}
}
}

View File

@ -23,7 +23,6 @@ import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.ResourceInitializer;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
@ -31,8 +30,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.query.Invalid
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD;
import org.gcube.informationsystem.resourceregistry.context.OLDContextManagementImpl;
import org.gcube.informationsystem.resourceregistry.context.OLDContextManagement;
import org.gcube.informationsystem.resourceregistry.context.ContextManagement;
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
@ -297,10 +295,11 @@ public class Access {
@Path(AccessPath.CONTEXT_PATH_PART + "{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String getContext(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ContextException {
throws ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to read {} with id {} ", org.gcube.informationsystem.model.entity.Context.NAME, uuid);
OLDContextManagement contextManager = new OLDContextManagementImpl();
return contextManager.read(UUID.fromString(uuid));
ContextManagement contextManagement = new ContextManagement();
contextManagement.setUUID(UUID.fromString(uuid));
return contextManagement.read();
}
}

View File

@ -74,6 +74,22 @@ public class ContextManager {
return contextManagement.read();
}
/**
* e.g. POST /resource-registry/context
*
* BODY: {...}
*
*/
@POST
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String update(String json)
throws ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to update {} with json {} ", Context.NAME, json);
ContextManagement contextManagement = new ContextManagement();
contextManagement.setJSON(json);
return contextManagement.update();
}
/**
* e.g. DELETE /resource-registry/context/c0f314e7-2807-4241-a792-2a6c79ed4fd0
* @param uuid
@ -82,7 +98,6 @@ public class ContextManager {
*/
@DELETE
@Path("{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public boolean delete(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to delete {} with id {} ", Context.NAME, uuid);
@ -91,20 +106,4 @@ public class ContextManager {
return contextManagement.delete();
}
/**
* e.g. POST /resource-registry/context
*
* BODY: {...}
*
*/
@POST
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String rename(String json)
throws ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to update {} with json {} ", Context.NAME, json);
ContextManagement contextManagement = new ContextManagement();
contextManagement.setJSON(json);
return contextManagement.update();
}
}

View File

@ -1,140 +0,0 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.rest;
import java.util.UUID;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.resourceregistry.ResourceInitializer;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextCreationException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
import org.gcube.informationsystem.resourceregistry.context.OLDContextManagement;
import org.gcube.informationsystem.resourceregistry.context.OLDContextManagementImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@Path("OLD"+ContextPath.CONTEXT_PATH_PART)
public class OLDContextManager {
/**
* Logger
*/
private static Logger logger = LoggerFactory
.getLogger(OLDContextManager.class);
public static final String ID_PATH_PARAM = "id";
protected OLDContextManagement contextManager = new OLDContextManagementImpl();
/**
* e.g. PUT /resource-registry/context?name=myVRE&parentContextId=a2fe0030-7b3d-4617-ba37-532c0e4b778d
* @param parentUUID
* @param name
* @return
* @throws InternalException
* @throws Exception
*/
@PUT
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public Response create(
@QueryParam(ContextPath.PARENT_CONTEXT_ID_PARAM) @DefaultValue("") String parentUUID,
@QueryParam(ContextPath.NAME_PARAM) String name)
throws ContextCreationException, ResourceRegistryException {
logger.info("Requested to create {} with name : {} ", Context.NAME, name);
UUID parent = null;
if(parentUUID!=null && parentUUID.compareTo("")!=0){
parent = UUID.fromString(parentUUID);
}
String ret = contextManager.create(parent, name);
return Response.status(Status.CREATED).entity(ret).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
}
/**
* e.g. GET /resource-registry/context/c0f314e7-2807-4241-a792-2a6c79ed4fd0
* @param uuid
* @return
* @throws ContextException
*/
@GET
@Path("{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String read(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ContextException {
logger.info("Requested to read {} with id {} ", Context.NAME, uuid);
return contextManager.read(UUID.fromString(uuid));
}
/**
* e.g. DELETE /resource-registry/context/c0f314e7-2807-4241-a792-2a6c79ed4fd0
* @param uuid
* @return
* @throws ContextException
*/
@DELETE
@Path("{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public boolean delete(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ContextException {
logger.info("Requested to delete {} with id {} ", Context.NAME, uuid);
return contextManager.delete(UUID.fromString(uuid));
}
/**
* e.g. POST /resource-registry/context/rename/c0f314e7-2807-4241-a792-2a6c79ed4fd0?name=newNameVRE
* @param uuid
* @param name
* @return
* @throws ContextNotFoundException
* @throws ContextException
*/
@POST
@Path(ContextPath.RENAME_PATH_PART + "/{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String rename(@PathParam(ID_PATH_PARAM) String uuid,
@QueryParam(ContextPath.NAME_PARAM) String name)
throws ContextNotFoundException, ContextException {
logger.info("Requested to rename as {} {} with id {} ", name, Context.NAME, uuid);
return contextManager.rename(UUID.fromString(uuid), name);
}
/**
* e.g. POST /resource-registry/context/move/c0f314e7-2807-4241-a792-2a6c79ed4fd0?parentContextId=68cf247a-b1ed-44cd-9d2e-c16d865bade7
* @param uuid
* @param newParentUUID
* @return
* @throws ContextNotFoundException
* @throws ContextException
*/
@POST
@Path(ContextPath.MOVE_PATH_PART + "/{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String move(
@PathParam(ID_PATH_PARAM) String uuid,
@QueryParam(ContextPath.PARENT_CONTEXT_ID_PARAM) String newParentUUID)
throws ContextNotFoundException, ContextException {
logger.info("Requested to move {} with id {} as child of {} having id {} ",
Context.NAME, uuid, Context.NAME, newParentUUID);
return contextManager.move(UUID.fromString(newParentUUID),
UUID.fromString(uuid));
}
}

View File

@ -1,395 +0,0 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.context;
import java.util.UUID;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextCreationException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public class ContextManagementImplTest {
private static Logger logger = LoggerFactory
.getLogger(ContextManagementImplTest.class);
protected OLDContextManagementImpl contextManagementImpl;
public ContextManagementImplTest() {
contextManagementImpl = new OLDContextManagementImpl();
}
public static final String CTX_NAME_A = "A";
public static final String CTX_NAME_B = "B";
public static final String CTX_NAME_C = "C";
protected void invalidCreation(UUID parentUUID, String name)
throws ResourceRegistryException {
try {
contextManagementImpl.create(parentUUID, name);
throw new RuntimeException("The context " + name + " with parent "
+ parentUUID + " was already created and MUST throw "
+ ContextCreationException.class.getSimpleName()
+ ". This is a bug in your code.");
} catch (ContextCreationException e) {
logger.debug(
"As expected the context {} with parent {} was already created and a {} has been thrown",
name, parentUUID, e.getClass().getSimpleName());
}
}
protected void invalidMoving(UUID parentUUID, UUID contextToMove) {
try {
contextManagementImpl.move(parentUUID, contextToMove);
throw new RuntimeException("The context " + contextToMove
+ " with parent " + parentUUID
+ " was already created and MUST throw "
+ ContextException.class.getSimpleName()
+ ". This is a bug in your code.");
} catch (ContextNotFoundException e) {
throw new RuntimeException(e);
} catch (ContextException e) {
logger.debug(
"As expected the context {} with parent {} was already created and a {} has been thrown",
contextToMove, parentUUID, e.getClass().getSimpleName());
}
}
protected void invalidRename(UUID uuid, String newName) {
try {
contextManagementImpl.rename(uuid, newName);
throw new RuntimeException("The context with uuid " + uuid
+ " cannot be renamed to " + newName + " and MUST throw "
+ ContextException.class.getSimpleName()
+ ". This is a bug in your code.");
} catch (ContextNotFoundException e) {
throw new RuntimeException(e);
} catch (ContextException e) {
logger.debug(
"As expected the context with uuid {} cannot be renamed to {} and a {} has been thrown",
uuid, newName, e.getClass().getSimpleName());
}
}
protected void invalidDelete(UUID uuid) {
try {
contextManagementImpl.delete(uuid);
throw new RuntimeException("The context with uuid " + uuid
+ " cannot be deleted and MUST throw "
+ ContextException.class.getSimpleName()
+ ". This is a bug in your code.");
} catch (ContextNotFoundException e) {
throw new RuntimeException(e);
} catch (ContextException e) {
logger.debug(
"As expected the context with uuid {} cannot be deleted and a {} has been thrown",
uuid, e.getClass().getSimpleName());
}
}
// @Test
public void simpleTest() throws Exception {
String contextJsonA1 = contextManagementImpl.create(null, CTX_NAME_A);
Context createdContexA1 = ISMapper.unmarshal(Context.class,
contextJsonA1);
UUID A_1 = createdContexA1.getHeader().getUUID();
logger.info("{}", contextJsonA1);
/*
* A(1)
*/
String contextJsonA2 = contextManagementImpl.create(A_1, CTX_NAME_A);
Context createdContexA2 = ISMapper.unmarshal(Context.class,
contextJsonA2);
logger.info("{}", contextJsonA2);
UUID A_2 = createdContexA2.getHeader().getUUID();
/*
* A_1 A_2
*/
String contextJsonB3 = contextManagementImpl.create(A_2, CTX_NAME_B);
Context createdContexB3 = ISMapper.unmarshal(Context.class,
contextJsonB3);
logger.info("{}", contextJsonB3);
UUID B_3 = createdContexB3.getHeader().getUUID();
/*
* A_1 A_2 B_3
*/
contextJsonB3 = contextManagementImpl.move(A_1, B_3);
createdContexB3 = ISMapper.unmarshal(Context.class, contextJsonB3);
logger.info("{}", contextJsonB3);
/*
* A_1 A_2 B_3
*/
contextJsonB3 = contextManagementImpl.rename(B_3, CTX_NAME_C);
createdContexB3 = ISMapper.unmarshal(Context.class, contextJsonB3);
logger.info("{}", contextJsonB3);
/*
* A_1 A_2 C_3
*/
contextJsonB3 = contextManagementImpl.rename(B_3, CTX_NAME_B);
createdContexB3 = ISMapper.unmarshal(Context.class, contextJsonB3);
logger.info("{}", contextJsonB3);
/*
* A_1 A_2 B_3
*/
contextJsonB3 = contextManagementImpl.move(A_2, B_3);
createdContexB3 = ISMapper.unmarshal(Context.class, contextJsonB3);
logger.info("{}", contextJsonB3);
/*
* A_1 A_2 B_3
*/
boolean deleted = contextManagementImpl.delete(B_3);
Assert.assertTrue(deleted);
/*
* A_1 A_2
*/
deleted = contextManagementImpl.delete(A_2);
Assert.assertTrue(deleted);
/*
* A_1
*/
deleted = contextManagementImpl.delete(A_1);
Assert.assertTrue(deleted);
/*
*
*/
logger.debug("The DB should be now clean");
}
// @Test
public void readTest() throws Exception {
String name = "LLL";
String contextJson = contextManagementImpl.create(null, name);
Context createdContex = ISMapper.unmarshal(Context.class,
contextJson);
UUID uuid = createdContex.getHeader().getUUID();
logger.info("{}", contextJson);
Assert.assertTrue(createdContex.getName().compareTo(name) == 0);
String readContextJson = contextManagementImpl.read(uuid);
Context readContex = ISMapper.unmarshal(Context.class,
readContextJson);
Assert.assertTrue(readContex.getName().compareTo(name) == 0);
Assert.assertTrue(uuid.compareTo(readContex.getHeader().getUUID()) == 0);
logger.trace("{}", createdContex);
boolean deleted = contextManagementImpl.delete(uuid);
Assert.assertTrue(deleted);
}
// @Test
public void completeTest() throws Exception {
String contextJsonA1 = contextManagementImpl.create(null, CTX_NAME_A);
Context createdContexA1 = ISMapper.unmarshal(Context.class,
contextJsonA1);
UUID A_1 = createdContexA1.getHeader().getUUID();
logger.info("{}", contextJsonA1);
/*
* A(1)
*/
String contextJsonA2 = contextManagementImpl.create(A_1, CTX_NAME_A);
Context createdContexA2 = ISMapper.unmarshal(Context.class,
contextJsonA2);
logger.info("{}", contextJsonA2);
UUID A_2 = createdContexA2.getHeader().getUUID();
/*
* A_1 A_2
*/
String contextJsonB3 = contextManagementImpl.create(A_2, CTX_NAME_B);
Context createdContexB3 = ISMapper.unmarshal(Context.class,
contextJsonB3);
logger.info("{}", contextJsonB3);
UUID B_3 = createdContexB3.getHeader().getUUID();
/*
* A_1 A_2 B_3
*/
String contextJsonB4 = contextManagementImpl.create(A_1, CTX_NAME_B);
Context createdContexB4 = ISMapper.unmarshal(Context.class,
contextJsonB4);
logger.info("{}", contextJsonB4);
UUID B_4 = createdContexB4.getHeader().getUUID();
/*
* A_1 A_2 B_4 B_3
*/
String contextJsonA5 = contextManagementImpl.create(B_4, CTX_NAME_A);
Context createdContexA5 = ISMapper.unmarshal(Context.class,
contextJsonA5);
logger.info("{}", contextJsonA5);
UUID A_5 = createdContexA5.getHeader().getUUID();
/*
* A_1 A_2 B_4 B_3 A_5
*/
invalidCreation(null, CTX_NAME_A); // Trying to recreate A_1. Fails
invalidCreation(A_1, CTX_NAME_A); // Trying to recreate A_2. Fails
invalidCreation(A_2, CTX_NAME_B); // Trying to recreate B_3. Fails
invalidCreation(A_1, CTX_NAME_B); // Trying to recreate B_4. Fails
invalidCreation(B_4, CTX_NAME_A); // Trying to recreate A_5. Fails
// Trying to move B_3 as child of A_1. It fails due to B_4. Fails
invalidMoving(A_1, B_3);
// Trying to move A_5 as child of A_1. It fails due to A_2. Fails
invalidMoving(A_1, A_5);
// Moving B_3 as child of B_4. OK
String movedContextJsonB3 = contextManagementImpl.move(B_4, B_3);
Context movedContexB3 = ISMapper.unmarshal(Context.class,
movedContextJsonB3);
Assert.assertTrue(B_3.compareTo(movedContexB3.getHeader().getUUID()) == 0);
logger.info("{}", contextJsonB3);
/*
* A_1 A_2 B_4 B_3 A_5
*/
// Restoring the initial situation by moving B_3 as child of A_2
String movedAgainJsonB3 = contextManagementImpl.move(A_2, B_3);
Context movedAgainContexB3 = ISMapper.unmarshal(Context.class,
movedAgainJsonB3);
Assert.assertTrue(B_3.compareTo(movedAgainContexB3.getHeader()
.getUUID()) == 0);
logger.info("{}", contextJsonB3);
/*
* A_1 A_2 B_4 B_3 A_5
*/
// Trying to move B_3 as child of A_1. It fails due to B_4. Fails
invalidMoving(A_1, B_3);
// Renaming B_3 as C_3
String contextJsonC3 = contextManagementImpl.rename(B_3, CTX_NAME_C);
Context createdContexC3 = ISMapper.unmarshal(Context.class,
contextJsonC3);
logger.info("{}", contextJsonC3);
UUID C_3 = createdContexC3.getHeader().getUUID();
Assert.assertTrue(C_3.compareTo(B_3) == 0);
/*
* A_1 A_2 B_4 C_3 A_5
*/
// Moving C_3 (was B_3) as child of A_1. Now it is possible
contextJsonC3 = contextManagementImpl.move(A_1, C_3);
createdContexC3 = ISMapper.unmarshal(Context.class, contextJsonC3);
logger.info("{}", contextJsonC3);
/*
* A_1 C_3 A_2 B_4 A_5
*/
// Trying to rename C_3 (was B_3) newly to B_3. Fails due to B_4
invalidRename(C_3, CTX_NAME_B);
// Moving back C_3 (was B_3) as child of A_2.
contextJsonB3 = contextManagementImpl.move(A_2, C_3);
createdContexB3 = ISMapper.unmarshal(Context.class, contextJsonB3);
logger.info("{}", contextJsonB3);
String contextJsonBC3 = contextManagementImpl.rename(C_3, CTX_NAME_B);
Context createdContexBC3 = ISMapper.unmarshal(Context.class,
contextJsonBC3);
logger.info("{}", contextJsonBC3);
UUID BC_3 = createdContexBC3.getHeader().getUUID();
Assert.assertTrue(BC_3.compareTo(B_3) == 0);
Assert.assertTrue(BC_3.compareTo(C_3) == 0);
/*
* A_1 A_2 B_4 B_3 A_5
*/
invalidDelete(A_1);
invalidDelete(A_2);
invalidDelete(B_4);
boolean deleted = contextManagementImpl.delete(A_5);
Assert.assertTrue(deleted);
/*
* A_1 A_2 B_4 B_3
*/
try {
contextManagementImpl.delete(A_5);
} catch (ContextNotFoundException e) {
logger.debug(
"The context with uuid {} was not found. (Was already deleted)",
A_5);
}
deleted = contextManagementImpl.delete(B_3);
Assert.assertTrue(deleted);
/*
* A_1 A_2 B_4
*/
invalidDelete(A_1);
deleted = contextManagementImpl.delete(B_4);
Assert.assertTrue(deleted);
/*
* A_1 A_2
*/
deleted = contextManagementImpl.delete(A_2);
Assert.assertTrue(deleted);
/*
* A_1
*/
deleted = contextManagementImpl.delete(A_1);
Assert.assertTrue(deleted);
logger.debug("The DB should be now clean");
}
// @Test
public void moveToRootTest() throws Exception {
String contextJsonA1 = contextManagementImpl.create(null, CTX_NAME_A);
Context createdContexA1 = ISMapper.unmarshal(Context.class,
contextJsonA1);
UUID A_1 = createdContexA1.getHeader().getUUID();
logger.info("{}", contextJsonA1);
/*
* A(1)
*/
String contextJsonB2 = contextManagementImpl.create(A_1, CTX_NAME_B);
Context createdContexB2 = ISMapper.unmarshal(Context.class,
contextJsonB2);
logger.info("{}", contextJsonB2);
UUID B_2 = createdContexB2.getHeader().getUUID();
/*
* A_1 B_2
*/
String movedContextJsonB2 = contextManagementImpl.move(null, B_2);
Context movedContexB2 = ISMapper.unmarshal(Context.class,
movedContextJsonB2);
Assert.assertTrue(B_2.compareTo(movedContexB2.getHeader().getUUID()) == 0);
contextManagementImpl.delete(A_1);
contextManagementImpl.delete(B_2);
logger.debug("The DB should be now clean");
}
}