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
This commit is contained in:
Manuele Simi 2017-12-29 17:46:27 +00:00
parent 0dbd823c26
commit c1c79bf30d
2 changed files with 27 additions and 2 deletions

View File

@ -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 <E extends Enum<E> & 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);
}
}

View File

@ -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