diff --git a/libs/java/instance_provider/src/main/java/com/yahoo/athenz/instance/provider/ResourceError.java b/libs/java/instance_provider/src/main/java/com/yahoo/athenz/instance/provider/ResourceError.java index 73cb7f62c33..6e702cc0db1 100644 --- a/libs/java/instance_provider/src/main/java/com/yahoo/athenz/instance/provider/ResourceError.java +++ b/libs/java/instance_provider/src/main/java/com/yahoo/athenz/instance/provider/ResourceError.java @@ -20,17 +20,23 @@ public class ResourceError { private int code; private String message; - public ResourceError code(int code) { + public void setCode(int code) { this.code = code; - return this; } - public ResourceError message(String message) { + + public void setMessage(String message) { this.message = message; - return this; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; } public String toString() { return "{code: " + code + ", message: \"" + message + "\"}"; } - } diff --git a/libs/java/instance_provider/src/main/java/com/yahoo/athenz/instance/provider/ResourceException.java b/libs/java/instance_provider/src/main/java/com/yahoo/athenz/instance/provider/ResourceException.java index 00eb5068bce..09c2b835464 100644 --- a/libs/java/instance_provider/src/main/java/com/yahoo/athenz/instance/provider/ResourceException.java +++ b/libs/java/instance_provider/src/main/java/com/yahoo/athenz/instance/provider/ResourceException.java @@ -15,69 +15,13 @@ */ package com.yahoo.athenz.instance.provider; -@SuppressWarnings("WeakerAccess") public class ResourceException extends RuntimeException { - public final static int OK = 200; - public final static int CREATED = 201; - public final static int ACCEPTED = 202; - public final static int NO_CONTENT = 204; - public final static int MOVED_PERMANENTLY = 301; - public final static int FOUND = 302; - public final static int SEE_OTHER = 303; - public final static int NOT_MODIFIED = 304; - public final static int TEMPORARY_REDIRECT = 307; - public final static int BAD_REQUEST = 400; - public final static int UNAUTHORIZED = 401; + public final static int FORBIDDEN = 403; public final static int NOT_FOUND = 404; - public final static int CONFLICT = 409; - public final static int GONE = 410; - public final static int PRECONDITION_FAILED = 412; - public final static int UNSUPPORTED_MEDIA_TYPE = 415; - public final static int PRECONDITION_REQUIRED = 428; - public final static int TOO_MANY_REQUESTS = 429; - public final static int REQUEST_HEADER_FIELDS_TOO_LARGE = 431; - public final static int INTERNAL_SERVER_ERROR = 500; - public final static int NOT_IMPLEMENTED = 501; - public final static int SERVICE_UNAVAILABLE = 503; - public final static int NETWORK_AUTHENTICATION_REQUIRED = 511; - - public static String codeToString(int code) { - switch (code) { - case OK: return "OK"; - case CREATED: return "Created"; - case ACCEPTED: return "Accepted"; - case NO_CONTENT: return "No Content"; - case MOVED_PERMANENTLY: return "Moved Permanently"; - case FOUND: return "Found"; - case SEE_OTHER: return "See Other"; - case NOT_MODIFIED: return "Not Modified"; - case TEMPORARY_REDIRECT: return "Temporary Redirect"; - case BAD_REQUEST: return "Bad Request"; - case UNAUTHORIZED: return "Unauthorized"; - case FORBIDDEN: return "Forbidden"; - case NOT_FOUND: return "Not Found"; - case CONFLICT: return "Conflict"; - case GONE: return "Gone"; - case PRECONDITION_FAILED: return "Precondition Failed"; - case UNSUPPORTED_MEDIA_TYPE: return "Unsupported Media Type"; - case PRECONDITION_REQUIRED: return "Precondition Required"; - case TOO_MANY_REQUESTS: return "Too Many Requests"; - case REQUEST_HEADER_FIELDS_TOO_LARGE: return "Request Header Fields Too Large"; - case INTERNAL_SERVER_ERROR: return "Internal Server Error"; - case NOT_IMPLEMENTED: return "Not Implemented"; - case SERVICE_UNAVAILABLE: return "Service Unavailable"; - case NETWORK_AUTHENTICATION_REQUIRED: return "Network Authentication Required"; - default: return "" + code; - } - } - final int code; - final Object data; - - public ResourceException(int code) { - this(code, new ResourceError().code(code).message(codeToString(code))); - } + final private int code; + final private Object data; public ResourceException(int code, Object data) { super("ResourceException (" + code + "): " + data); @@ -96,5 +40,4 @@ public Object getData() { public T getData(Class cl) { return cl.cast(data); } - } diff --git a/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ProviderHostnameVerifierTest.java b/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ProviderHostnameVerifierTest.java index eb73a8ec6c4..6d534fcd2fa 100644 --- a/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ProviderHostnameVerifierTest.java +++ b/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ProviderHostnameVerifierTest.java @@ -30,7 +30,6 @@ import org.testng.annotations.Test; import com.yahoo.athenz.auth.util.Crypto; -import com.yahoo.athenz.instance.provider.ProviderHostnameVerifier; public class ProviderHostnameVerifierTest { diff --git a/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ResourceErrorTest.java b/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ResourceErrorTest.java index 1917a224c39..928de02afb6 100644 --- a/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ResourceErrorTest.java +++ b/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ResourceErrorTest.java @@ -23,7 +23,9 @@ public class ResourceErrorTest { @Test public void testResourceError() { - ResourceError error = new ResourceError().code(401).message("unauthorized"); + ResourceError error = new ResourceError(); + error.setCode(401); + error.setMessage("unauthorized"); assertEquals(error.toString(), "{code: 401, message: \"unauthorized\"}"); } diff --git a/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ResourceExceptionTest.java b/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ResourceExceptionTest.java index c63d77ab4f8..98ff4d347fc 100644 --- a/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ResourceExceptionTest.java +++ b/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/ResourceExceptionTest.java @@ -21,40 +21,13 @@ public class ResourceExceptionTest { - @Test - public void testCodeToString() { - - assertEquals("OK", ResourceException.codeToString(200)); - assertEquals("Created", ResourceException.codeToString(201)); - assertEquals("Accepted", ResourceException.codeToString(202)); - assertEquals("No Content", ResourceException.codeToString(204)); - assertEquals("Moved Permanently", ResourceException.codeToString(301)); - assertEquals("Found", ResourceException.codeToString(302)); - assertEquals("See Other", ResourceException.codeToString(303)); - assertEquals("Not Modified", ResourceException.codeToString(304)); - assertEquals("Temporary Redirect", ResourceException.codeToString(307)); - assertEquals("Bad Request", ResourceException.codeToString(400)); - assertEquals("Unauthorized", ResourceException.codeToString(401)); - assertEquals("Forbidden", ResourceException.codeToString(403)); - assertEquals("Not Found", ResourceException.codeToString(404)); - assertEquals("Conflict", ResourceException.codeToString(409)); - assertEquals("Gone", ResourceException.codeToString(410)); - assertEquals("Precondition Failed", ResourceException.codeToString(412)); - assertEquals("Unsupported Media Type", ResourceException.codeToString(415)); - assertEquals("Precondition Required", ResourceException.codeToString(428)); - assertEquals("Too Many Requests", ResourceException.codeToString(429)); - assertEquals("Request Header Fields Too Large", ResourceException.codeToString(431)); - assertEquals("Internal Server Error", ResourceException.codeToString(500)); - assertEquals("Not Implemented", ResourceException.codeToString(501)); - assertEquals("Service Unavailable", ResourceException.codeToString(503)); - assertEquals("Network Authentication Required", ResourceException.codeToString(511)); - assertEquals("1001", ResourceException.codeToString(1001)); - } - @Test public void testCodeOnly() { - - ResourceException exc = new ResourceException(400); + + ResourceError resError = new ResourceError(); + resError.setCode(400); + resError.setMessage("Bad Request"); + ResourceException exc = new ResourceException(400, resError); assertEquals(exc.getData().toString(), "{code: 400, message: \"Bad Request\"}"); } diff --git a/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/impl/InstanceAWSProviderTest.java b/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/impl/InstanceAWSProviderTest.java index 061fce5997b..dc1be168d67 100644 --- a/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/impl/InstanceAWSProviderTest.java +++ b/libs/java/instance_provider/src/test/java/com/yahoo/athenz/instance/provider/impl/InstanceAWSProviderTest.java @@ -509,7 +509,8 @@ public void testVerifyInstanceIdentityException() { MockInstanceAWSProvider provider = new MockInstanceAWSProvider(); provider.setIdentitySuper(true); AWSSecurityTokenServiceClient mockClient = Mockito.mock(AWSSecurityTokenServiceClient.class); - Mockito.when(mockClient.getCallerIdentity(ArgumentMatchers.any())).thenThrow(new ResourceException(101)); + Mockito.when(mockClient.getCallerIdentity(ArgumentMatchers.any())) + .thenThrow(new ResourceException(101, "invaliderror")); provider.setStsClient(mockClient); AWSAttestationData info = new AWSAttestationData(); diff --git a/servers/zts/src/test/java/com/yahoo/athenz/zts/ZTSImplTest.java b/servers/zts/src/test/java/com/yahoo/athenz/zts/ZTSImplTest.java index 7f657cdd1c7..909582cd116 100644 --- a/servers/zts/src/test/java/com/yahoo/athenz/zts/ZTSImplTest.java +++ b/servers/zts/src/test/java/com/yahoo/athenz/zts/ZTSImplTest.java @@ -4670,7 +4670,7 @@ public void testPostInstanceRefreshInformationForbidden() throws IOException { InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager); Mockito.when(instanceProviderManager.getProvider("athenz.provider")).thenReturn(providerClient); - Mockito.when(providerClient.refreshInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(403)); + Mockito.when(providerClient.refreshInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(403, "Forbidden")); X509CertRecord certRecord = new X509CertRecord(); certRecord.setInstanceId("1001"); @@ -4737,7 +4737,7 @@ public void testPostInstanceRefreshInformationNotFound() throws IOException { InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager); Mockito.when(instanceProviderManager.getProvider("athenz.provider")).thenReturn(providerClient); - Mockito.when(providerClient.refreshInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(404)); + Mockito.when(providerClient.refreshInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(404, "Not Found")); X509CertRecord certRecord = new X509CertRecord(); certRecord.setInstanceId("1001");