diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/exceptions/CodeFinder.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/exceptions/CodeFinder.java new file mode 100644 index 0000000..e41d34c --- /dev/null +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/io/exceptions/CodeFinder.java @@ -0,0 +1,24 @@ +package org.gcube.resourcemanagement.manager.io.exceptions; + +import java.util.stream.Stream; + +/** + * Helper methods to find an error code in an enumeration implementing {@link ErrorCode}. + * + * @author Manuele Simi (ISTI CNR) + * + */ +public class CodeFinder { + + /** + * Finds and convert the given code as enum value. + * @param code the code to look for. + * @param codes the enum values. + * @return the code as enum value. + */ + public static & ErrorCode> E findAndConvert(ErrorCode code, E[] codes) { + return Stream.of(codes).filter(e -> e.getId() == code.getId() && e.getMessage().equals(code.getMessage())) + .findFirst().orElse(null); + } + +} 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 aef48a8..eb4452c 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 @@ -3,7 +3,6 @@ package org.gcube.resourcemanagement.manager.webapp.context; import static org.junit.Assert.*; import java.io.IOException; -import java.util.stream.Stream; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; @@ -19,6 +18,7 @@ import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.impl.entity.ContextImpl; import org.gcube.informationsystem.impl.utils.ISMapper; import org.gcube.informationsystem.model.entity.Context; +import org.gcube.resourcemanagement.manager.io.exceptions.CodeFinder; import org.gcube.resourcemanagement.manager.io.exceptions.RMCode; import org.gcube.resourcemanagement.manager.io.exceptions.SerializableErrorCode; import org.gcube.resourcemanagement.manager.io.rs.RMContextPath; @@ -114,7 +114,8 @@ public class RMContextTest extends JerseyTest { .post(Entity.entity(ISMapper.marshal(newContext), MediaType.APPLICATION_JSON + ";charset=UTF-8")); SerializableErrorCode code = create.readEntity(SerializableErrorCode.class); - RMCode realCode = Stream.of(RMCode.values()).filter(e -> e.getId() == code.getId() && e.getMessage().equals(code.getMessage())).findFirst().orElse(null); + //RMCode realCode = Stream.of(RMCode.values()).filter(e -> e.getId() == code.getId() && e.getMessage().equals(code.getMessage())).findFirst().orElse(null); + RMCode realCode = CodeFinder.findAndConvert(code, RMCode.values()); assertNotNull(create); // TODO: to restore an expected success when create is fully // functional