Adjust the RM codes creation. Add test case.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@161896 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2017-12-26 03:42:12 +00:00
parent 9a9e0a0a06
commit 9e6e2299c8
2 changed files with 33 additions and 11 deletions

View File

@ -0,0 +1,31 @@
/**
*
*/
package org.gcube.resourcemanagement.manager.exception;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
/**
* Tests for {@link CodeException}
*
* @author Manuele Simi (ISTI CNR)
*
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class CodeExceptionTest {
/**
* Test method for {@link org.gcube.resourcemanagement.manager.exception.CodeException#CodeException(org.gcube.resourcemanagement.manager.exception.RMCode)}.
*/
@Test
public void testCodeException() {
CodeException exception = new CodeException(RMCode.CONTEXT_ALREADY_EXIST);
assertTrue("Unexpected id", exception.getErrorCode() == RMCode.CONTEXT_ALREADY_EXIST.getId());
}
}

View File

@ -1,7 +1,5 @@
package org.gcube.resourcemanagement.manager.exception;
import java.util.Arrays;
/**
* Exception codes handled with {@link CodeException}.
*
@ -15,15 +13,12 @@ public enum RMCode implements ErrorCode {
MISSING_HEADER(2, "Required header is missing"),
CONTEXT_ALREADY_EXIST(3, "Required header is missing"),
INVALID_REQUEST_FOR_RR(4, "Failed to validate the request. The request was not submitted to the Resource Registry."),
GENERIC_ERROR_FROM_RR(5, "The Resource Registry returned an error.");
GENERIC_ERROR_FROM_RR(0, "The Resource Registry returned an error.");
private final int id;
private final String msg;
RMCode(int id, String msg) {
if (exists(id))
throw new IllegalArgumentException(String.format("Code %d already exists", id));
private RMCode(int id, String msg) {
this.id = id;
this.msg = msg;
}
@ -35,8 +30,4 @@ public enum RMCode implements ErrorCode {
public String getMessage() {
return this.msg;
}
protected static boolean exists(int id) {
return Arrays.stream(RMCode.values()).filter(v -> v.id == id).count() > 0;
}
}