From c1c79bf30d6ffb7217955c017fd44d0a41c72b50 Mon Sep 17 00:00:00 2001 From: "manuele.simi" Date: Fri, 29 Dec 2017 17:46:27 +0000 Subject: [PATCH] Add CodeFinder helper class to find codes and convert them into their equivalent enum value. git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@161927 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../manager/io/exceptions/CodeFinder.java | 24 +++++++++++++++++++ .../manager/webapp/context/RMContextTest.java | 5 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/exceptions/CodeFinder.java 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