From 3adf77e8bd20a4a10a5fde7cc0b7b508eab5157d Mon Sep 17 00:00:00 2001 From: "manuele.simi" Date: Wed, 31 Jan 2018 14:31:01 +0000 Subject: [PATCH] Add deserializers for ErrorCode and Exception. This makes the serializable entity a POJO. git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@162801 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../SerializableErrorEntity.java | 38 ------------- .../deserializer/ErrorCodeDeserializer.java | 43 ++++++++++++++ .../deserializer/ErrorEntityManager.java | 39 +++++++++++++ .../deserializer/ExceptionDeserializer.java | 28 +++++++++ .../deserializer/package-info.java | 8 +++ .../manager/webapp/context/RMContextTest.java | 57 ++++++++++--------- 6 files changed, 148 insertions(+), 65 deletions(-) create mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorCodeDeserializer.java create mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorEntityManager.java create mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ExceptionDeserializer.java create mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/package-info.java diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/SerializableErrorEntity.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/SerializableErrorEntity.java index 6344cb7..991b344 100644 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/SerializableErrorEntity.java +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/SerializableErrorEntity.java @@ -1,6 +1,5 @@ package org.gcube.resourcemanagement.manager.io.codeexceptions; -import java.lang.reflect.InvocationTargetException; /** * An entity that can be serialized in a {@link WebCodeException}. @@ -42,43 +41,6 @@ public class SerializableErrorEntity { public String getMessage() { return this.message; } - - /** - * The exception embedded in the entity, if any. - * @return the exception or null - */ - @SuppressWarnings("unchecked") - public E getYourException() { - try { - final Class[] ctorParams = {String.class}; - return (E) Class.forName(exceptionClass).getConstructor(ctorParams).newInstance(this.message); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { - return null; - } - } - - /** - * The error code, if any - * @return the error code or null - */ - public ErrorCode getErrorCode() { - if (id != 1) { - return new ErrorCode() { - - @Override - public String getMessage() { - return SerializableErrorEntity.this.message; - } - - @Override - public int getId() { - return SerializableErrorEntity.this.id; - } - }; - } else - return null; - - } /** * @return the full qualified name of the embedded {@link Exception} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorCodeDeserializer.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorCodeDeserializer.java new file mode 100644 index 0000000..5e90ffe --- /dev/null +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorCodeDeserializer.java @@ -0,0 +1,43 @@ +/** + * + */ +package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer; + +import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode; + +/** + * Don't forget to comment! + * + * @author Manuele Simi (ISTI CNR) + * + */ +final class ErrorCodeDeserializer { + + /** + * + */ + private ErrorCodeDeserializer() {} + + /** + * The error code, if any + * @return the error code or null + */ + public static ErrorCode deserialize(int id, String message) { + if (id != 1) { + return new ErrorCode() { + + @Override + public String getMessage() { + return message; + } + + @Override + public int getId() { + return id; + } + }; + } else + return null; + + } +} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorEntityManager.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorEntityManager.java new file mode 100644 index 0000000..6ddd2e8 --- /dev/null +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorEntityManager.java @@ -0,0 +1,39 @@ +package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer; + +import java.util.Objects; + +import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode; +import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity; + +/** + * Don't forget to comment! + * + * @author Manuele Simi (ISTI CNR) + * + */ +public class ErrorEntityManager { + + private final SerializableErrorEntity entity; + /** + * + */ + public ErrorEntityManager(SerializableErrorEntity entity) { + this.entity = entity; + } + + public boolean hasException() { + return Objects.nonNull(this.entity.getExceptionClass()); + } + + public E getException() { + return ExceptionDeserializer.deserialize(this.entity.getExceptionClass(),this.entity.getMessage()); + } + + public boolean hasErrorCode() { + return this.entity.getId() != -1; + } + + public ErrorCode getErrorCode(){ + return ErrorCodeDeserializer.deserialize(this.entity.getId(), this.entity.getMessage()); + } +} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ExceptionDeserializer.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ExceptionDeserializer.java new file mode 100644 index 0000000..42320f5 --- /dev/null +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ExceptionDeserializer.java @@ -0,0 +1,28 @@ +package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer; + +import java.lang.reflect.InvocationTargetException; + +/** + * Deserializer for {@link Exception}. + * + * @author Manuele Simi (ISTI CNR) + * + */ +class ExceptionDeserializer { + + /** + * + */ + private ExceptionDeserializer() {} + + + @SuppressWarnings("unchecked") + public static E deserialize(String exceptionClass, String message) { + try { + final Class[] ctorParams = {String.class}; + return (E) Class.forName(exceptionClass).getConstructor(ctorParams).newInstance(message); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { + return null; + } +} +} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/package-info.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/package-info.java new file mode 100644 index 0000000..8bbb6b3 --- /dev/null +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/package-info.java @@ -0,0 +1,8 @@ + +/** + * Don't forget to comment! + * + * @author Manuele Simi (ISTI CNR) + * + */ +package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer; \ No newline at end of file diff --git a/webapp/src/test/java/org/gcube/resourcemanagement/manager/webapp/context/RMContextTest.java b/webapp/src/test/java/org/gcube/resourcemanagement/manager/webapp/context/RMContextTest.java index 48d5169..8650c1b 100644 --- a/webapp/src/test/java/org/gcube/resourcemanagement/manager/webapp/context/RMContextTest.java +++ b/webapp/src/test/java/org/gcube/resourcemanagement/manager/webapp/context/RMContextTest.java @@ -26,6 +26,7 @@ import org.gcube.informationsystem.resourceregistry.context.ResourceRegistryCont import org.gcube.informationsystem.resourceregistry.context.ResourceRegistryContextClientImpl; import org.gcube.resourcemanagement.manager.io.codeexceptions.CodeFinder; import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity; +import org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer.ErrorEntityManager; import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode; import org.gcube.resourcemanagement.manager.io.rs.RMContextDoesNotExistException; import org.gcube.resourcemanagement.manager.io.rs.RMContextPath; @@ -39,6 +40,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.MethodSorters; +import org.omg.IOP.ExceptionDetailMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -141,9 +143,10 @@ public class RMContextTest extends JerseyTest { /** * Test method for * {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#create(String)}. + * @throws Exception */ @Test(expected = RMContextDoesNotExistException.class) - public void step1_Create() { + public void step1_Create() throws Exception { if (skipTest) return; Context newContext = new ContextImpl(context1, context1UUID); @@ -157,24 +160,21 @@ public class RMContextTest extends JerseyTest { String message = create.readEntity(String.class); logger.info("RM says: " + message); }else { - //assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(), - // Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus()); - SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class); - try { - logger.info("about to load " + code.getExceptionClass()); - throw code.getYourException(); - } catch (Exception e) { - // TODO Auto-generated catch block - logger.error("Failed", e); - } - /*code.getYourException(); - assertNotNull("No exception returned", exception); - //Class.forName("Somthing").newInstance(); - //RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values()); - //assertEquals(RMCreateContextCode.CONTEXT_ALREADY_EXISTS, realCode); - logger.info("RM says exception: " + exception.getMessage()); - - assertNotNull(exception);*/ + assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(), + Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus()); + SerializableErrorEntity entity = create.readEntity(SerializableErrorEntity.class); + //try { + logger.info("about to load " + entity.getExceptionClass()); + ErrorEntityManager response = new ErrorEntityManager(entity); + if (response.hasException()) { + try { + throw response.getException(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + throw e; + } + } } } catch (JsonProcessingException e) { @@ -199,8 +199,8 @@ public class RMContextTest extends JerseyTest { assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(), Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus()); SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class); - RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values()); - assertEquals(RMCreateContextCode.CONTEXT_PARENT_DOES_NOT_EXIST, realCode); + //RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values()); + //assertEquals(RMCreateContextCode.CONTEXT_PARENT_DOES_NOT_EXIST, realCode); } catch (JsonProcessingException e) { assertFalse("Failed to marshal the context.", false); } @@ -230,8 +230,8 @@ public class RMContextTest extends JerseyTest { assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(), Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus()); SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class); - RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values()); - assertEquals(RMCreateContextCode.CONTEXT_ALREADY_EXISTS, realCode); + //RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values()); + //assertEquals(RMCreateContextCode.CONTEXT_ALREADY_EXISTS, realCode); } } catch (JsonProcessingException e) { assertFalse("Failed to marshal the context.", false); @@ -241,9 +241,10 @@ public class RMContextTest extends JerseyTest { /** * Test method for * {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#delete(String, String)} + * @throws Exception */ @Test - public void step4_Delete() { + public void step4_Delete() throws Exception { if (skipTest) return; @@ -256,13 +257,15 @@ public class RMContextTest extends JerseyTest { }else { assertEquals("Unexpected returned code. Reason: " + delete.getStatusInfo().getReasonPhrase(), Status.NOT_ACCEPTABLE.getStatusCode(), delete.getStatus()); - SerializableErrorEntity code = delete.readEntity(SerializableErrorEntity.class); - if (Objects.nonNull(code.getYourException())) { + SerializableErrorEntity entity = delete.readEntity(SerializableErrorEntity.class); + ErrorEntityManager response = new ErrorEntityManager(entity); + if (response.hasException()) { try { - throw code.getYourException(); + throw response.getException(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); + throw e; } } }