|
11 | 11 | @Test(groups = {"unit"}) |
12 | 12 | public class ErrorResponseTest { |
13 | 13 |
|
14 | | - public void exceptionFromErrorResponseTspServiceException() throws Exception { |
15 | | - final String staticMsg = "static message"; |
16 | | - final int staticHttpCode = 42; |
17 | | - |
18 | | - // TspServiceException |
19 | | - ErrorResponse unableToMakeReqError = new ErrorResponse( |
20 | | - TenantSecurityErrorCodes.UNABLE_TO_MAKE_REQUEST.getCode(), staticMsg); |
21 | | - TenantSecurityException unableToMakeReqException = |
22 | | - unableToMakeReqError.toTenantSecurityException(staticHttpCode); |
23 | | - assertTspServiceException(staticMsg, staticHttpCode, unableToMakeReqException, |
24 | | - TenantSecurityErrorCodes.UNABLE_TO_MAKE_REQUEST); |
25 | | - |
26 | | - ErrorResponse unknownErrResp = |
27 | | - new ErrorResponse(TenantSecurityErrorCodes.UNKNOWN_ERROR.getCode(), staticMsg); |
28 | | - TenantSecurityException unknownErrException = |
29 | | - unknownErrResp.toTenantSecurityException(staticHttpCode); |
30 | | - assertTspServiceException(staticMsg, staticHttpCode, unknownErrException, |
31 | | - TenantSecurityErrorCodes.UNKNOWN_ERROR); |
32 | | - |
33 | | - ErrorResponse invalidRequestBody = new ErrorResponse( |
34 | | - TenantSecurityErrorCodes.INVALID_REQUEST_BODY.getCode(), staticMsg); |
35 | | - TenantSecurityException invalidRequestException = |
36 | | - invalidRequestBody.toTenantSecurityException(staticHttpCode); |
37 | | - assertTspServiceException(staticMsg, staticHttpCode, invalidRequestException, |
38 | | - TenantSecurityErrorCodes.INVALID_REQUEST_BODY); |
39 | | - |
40 | | - ErrorResponse unauthorizedReqErrResp = new ErrorResponse( |
41 | | - TenantSecurityErrorCodes.UNAUTHORIZED_REQUEST.getCode(), staticMsg); |
42 | | - TenantSecurityException unauthorizedReqException = |
43 | | - unauthorizedReqErrResp.toTenantSecurityException(staticHttpCode); |
44 | | - assertTspServiceException(staticMsg, staticHttpCode, unauthorizedReqException, |
45 | | - TenantSecurityErrorCodes.UNAUTHORIZED_REQUEST); |
46 | | - |
47 | | - // KmsException |
48 | | - ErrorResponse noPrimaryKmsResp = new ErrorResponse( |
49 | | - TenantSecurityErrorCodes.NO_PRIMARY_KMS_CONFIGURATION.getCode(), staticMsg); |
50 | | - TenantSecurityException noPrimaryKmsException = |
51 | | - noPrimaryKmsResp.toTenantSecurityException(staticHttpCode); |
52 | | - assertKmsException(staticMsg, staticHttpCode, noPrimaryKmsException, |
53 | | - TenantSecurityErrorCodes.NO_PRIMARY_KMS_CONFIGURATION); |
54 | | - |
55 | | - ErrorResponse unknownTenantError = new ErrorResponse( |
56 | | - TenantSecurityErrorCodes.UNKNOWN_TENANT_OR_NO_ACTIVE_KMS_CONFIGURATIONS.getCode(), |
57 | | - staticMsg); |
58 | | - TenantSecurityException unknownTenantException = |
59 | | - unknownTenantError.toTenantSecurityException(staticHttpCode); |
60 | | - assertKmsException(staticMsg, staticHttpCode, unknownTenantException, |
61 | | - TenantSecurityErrorCodes.UNKNOWN_TENANT_OR_NO_ACTIVE_KMS_CONFIGURATIONS); |
62 | | - |
63 | | - ErrorResponse kmsCfgDisabledError = new ErrorResponse( |
64 | | - TenantSecurityErrorCodes.KMS_CONFIGURATION_DISABLED.getCode(), staticMsg); |
65 | | - TenantSecurityException kmsCfgDisabledException = |
66 | | - kmsCfgDisabledError.toTenantSecurityException(staticHttpCode); |
67 | | - assertKmsException(staticMsg, staticHttpCode, kmsCfgDisabledException, |
68 | | - TenantSecurityErrorCodes.KMS_CONFIGURATION_DISABLED); |
69 | | - |
70 | | - ErrorResponse invalidEdekErrResp = new ErrorResponse( |
71 | | - TenantSecurityErrorCodes.INVALID_PROVIDED_EDEK.getCode(), staticMsg); |
72 | | - TenantSecurityException invalidEdekException = |
73 | | - invalidEdekErrResp.toTenantSecurityException(staticHttpCode); |
74 | | - assertKmsException(staticMsg, staticHttpCode, invalidEdekException, |
75 | | - TenantSecurityErrorCodes.INVALID_PROVIDED_EDEK); |
76 | | - |
77 | | - ErrorResponse unwrapError = |
78 | | - new ErrorResponse(TenantSecurityErrorCodes.KMS_UNWRAP_FAILED.getCode(), staticMsg); |
79 | | - TenantSecurityException unwrapException = |
80 | | - unwrapError.toTenantSecurityException(staticHttpCode); |
81 | | - assertKmsException(staticMsg, staticHttpCode, unwrapException, |
82 | | - TenantSecurityErrorCodes.KMS_UNWRAP_FAILED); |
83 | | - |
84 | | - ErrorResponse wrapError = |
85 | | - new ErrorResponse(TenantSecurityErrorCodes.KMS_WRAP_FAILED.getCode(), staticMsg); |
86 | | - TenantSecurityException kmsWrapException = |
87 | | - wrapError.toTenantSecurityException(staticHttpCode); |
88 | | - assertKmsException(staticMsg, staticHttpCode, kmsWrapException, |
89 | | - TenantSecurityErrorCodes.KMS_WRAP_FAILED); |
90 | | - |
91 | | - ErrorResponse kmsAuthError = new ErrorResponse( |
92 | | - TenantSecurityErrorCodes.KMS_AUTHORIZATION_FAILED.getCode(), staticMsg); |
93 | | - TenantSecurityException kmsAuthException = |
94 | | - kmsAuthError.toTenantSecurityException(staticHttpCode); |
95 | | - assertKmsException(staticMsg, staticHttpCode, kmsAuthException, |
96 | | - TenantSecurityErrorCodes.KMS_AUTHORIZATION_FAILED); |
97 | | - |
98 | | - ErrorResponse kmsConfigInvalidError = new ErrorResponse( |
99 | | - TenantSecurityErrorCodes.KMS_CONFIGURATION_INVALID.getCode(), staticMsg); |
100 | | - TenantSecurityException kmsConfigInvalidException = |
101 | | - kmsConfigInvalidError.toTenantSecurityException(staticHttpCode); |
102 | | - assertKmsException(staticMsg, staticHttpCode, kmsConfigInvalidException, |
103 | | - TenantSecurityErrorCodes.KMS_CONFIGURATION_INVALID); |
104 | | - |
105 | | - ErrorResponse foo = |
106 | | - new ErrorResponse(TenantSecurityErrorCodes.KMS_ACCOUNT_ISSUE.getCode(), staticMsg); |
107 | | - TenantSecurityException fooException = foo.toTenantSecurityException(staticHttpCode); |
108 | | - assertKmsException(staticMsg, staticHttpCode, fooException, |
109 | | - TenantSecurityErrorCodes.KMS_ACCOUNT_ISSUE); |
110 | | - |
111 | | - ErrorResponse kmsUnreachableError = |
112 | | - new ErrorResponse(TenantSecurityErrorCodes.KMS_UNREACHABLE.getCode(), staticMsg); |
113 | | - TenantSecurityException kmsUnreachableException = |
114 | | - kmsUnreachableError.toTenantSecurityException(staticHttpCode); |
115 | | - assertKmsException(staticMsg, staticHttpCode, kmsUnreachableException, |
116 | | - TenantSecurityErrorCodes.KMS_UNREACHABLE); |
117 | | - |
118 | | - // SecurityEventException |
119 | | - ErrorResponse securityEventRejectedError = new ErrorResponse( |
120 | | - TenantSecurityErrorCodes.SECURITY_EVENT_REJECTED.getCode(), staticMsg); |
121 | | - TenantSecurityException securityEventRejectedException = |
122 | | - securityEventRejectedError.toTenantSecurityException(staticHttpCode); |
123 | | - assertSecurityEventException(staticMsg, staticHttpCode, securityEventRejectedException, |
124 | | - TenantSecurityErrorCodes.SECURITY_EVENT_REJECTED); |
125 | | - } |
126 | | - |
127 | | - private void assertTspServiceException(String expectedMsg, int expectedHttpStatusCode, |
128 | | - TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
129 | | - assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
130 | | - assertTrue(exception instanceof TspServiceException); |
131 | | - } |
132 | | - |
133 | | - private void assertSecurityEventException(String expectedMsg, int expectedHttpStatusCode, |
134 | | - TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
135 | | - assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
136 | | - assertTrue(exception instanceof SecurityEventException); |
137 | | - } |
138 | | - |
139 | | - private void assertKmsException(String expectedMsg, int expectedHttpStatusCode, |
140 | | - TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
141 | | - assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
142 | | - assertTrue(exception instanceof KmsException); |
143 | | - } |
144 | | - |
145 | | - private void assertTenantSecurityException(String expectedMsg, int expectedHttpStatusCode, |
146 | | - TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
147 | | - assertEquals(errorCode, exception.getErrorCode()); |
148 | | - assertEquals(exception.getHttpResponseCode(), expectedHttpStatusCode); |
149 | | - assertEquals(exception.getMessage(), expectedMsg); |
150 | | - } |
| 14 | + public void exceptionFromErrorResponseTspServiceException() throws Exception { |
| 15 | + final String staticMsg = "static message"; |
| 16 | + final int staticHttpCode = 42; |
| 17 | + |
| 18 | + // TspServiceException |
| 19 | + ErrorResponse unableToMakeReqError = |
| 20 | + new ErrorResponse(TenantSecurityErrorCodes.UNABLE_TO_MAKE_REQUEST.getCode(), staticMsg); |
| 21 | + TenantSecurityException unableToMakeReqException = |
| 22 | + unableToMakeReqError.toTenantSecurityException(staticHttpCode); |
| 23 | + assertTspServiceException(staticMsg, staticHttpCode, unableToMakeReqException, |
| 24 | + TenantSecurityErrorCodes.UNABLE_TO_MAKE_REQUEST); |
| 25 | + |
| 26 | + ErrorResponse unknownErrResp = |
| 27 | + new ErrorResponse(TenantSecurityErrorCodes.UNKNOWN_ERROR.getCode(), staticMsg); |
| 28 | + TenantSecurityException unknownErrException = |
| 29 | + unknownErrResp.toTenantSecurityException(staticHttpCode); |
| 30 | + assertTspServiceException(staticMsg, staticHttpCode, unknownErrException, |
| 31 | + TenantSecurityErrorCodes.UNKNOWN_ERROR); |
| 32 | + |
| 33 | + ErrorResponse invalidRequestBody = |
| 34 | + new ErrorResponse(TenantSecurityErrorCodes.INVALID_REQUEST_BODY.getCode(), staticMsg); |
| 35 | + TenantSecurityException invalidRequestException = |
| 36 | + invalidRequestBody.toTenantSecurityException(staticHttpCode); |
| 37 | + assertTspServiceException(staticMsg, staticHttpCode, invalidRequestException, |
| 38 | + TenantSecurityErrorCodes.INVALID_REQUEST_BODY); |
| 39 | + |
| 40 | + ErrorResponse unauthorizedReqErrResp = |
| 41 | + new ErrorResponse(TenantSecurityErrorCodes.UNAUTHORIZED_REQUEST.getCode(), staticMsg); |
| 42 | + TenantSecurityException unauthorizedReqException = |
| 43 | + unauthorizedReqErrResp.toTenantSecurityException(staticHttpCode); |
| 44 | + assertTspServiceException(staticMsg, staticHttpCode, unauthorizedReqException, |
| 45 | + TenantSecurityErrorCodes.UNAUTHORIZED_REQUEST); |
| 46 | + |
| 47 | + // KmsException |
| 48 | + ErrorResponse noPrimaryKmsResp = new ErrorResponse( |
| 49 | + TenantSecurityErrorCodes.NO_PRIMARY_KMS_CONFIGURATION.getCode(), staticMsg); |
| 50 | + TenantSecurityException noPrimaryKmsException = |
| 51 | + noPrimaryKmsResp.toTenantSecurityException(staticHttpCode); |
| 52 | + assertKmsException(staticMsg, staticHttpCode, noPrimaryKmsException, |
| 53 | + TenantSecurityErrorCodes.NO_PRIMARY_KMS_CONFIGURATION); |
| 54 | + |
| 55 | + ErrorResponse unknownTenantError = new ErrorResponse( |
| 56 | + TenantSecurityErrorCodes.UNKNOWN_TENANT_OR_NO_ACTIVE_KMS_CONFIGURATIONS.getCode(), |
| 57 | + staticMsg); |
| 58 | + TenantSecurityException unknownTenantException = |
| 59 | + unknownTenantError.toTenantSecurityException(staticHttpCode); |
| 60 | + assertKmsException(staticMsg, staticHttpCode, unknownTenantException, |
| 61 | + TenantSecurityErrorCodes.UNKNOWN_TENANT_OR_NO_ACTIVE_KMS_CONFIGURATIONS); |
| 62 | + |
| 63 | + ErrorResponse kmsCfgDisabledError = |
| 64 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_CONFIGURATION_DISABLED.getCode(), staticMsg); |
| 65 | + TenantSecurityException kmsCfgDisabledException = |
| 66 | + kmsCfgDisabledError.toTenantSecurityException(staticHttpCode); |
| 67 | + assertKmsException(staticMsg, staticHttpCode, kmsCfgDisabledException, |
| 68 | + TenantSecurityErrorCodes.KMS_CONFIGURATION_DISABLED); |
| 69 | + |
| 70 | + ErrorResponse invalidEdekErrResp = |
| 71 | + new ErrorResponse(TenantSecurityErrorCodes.INVALID_PROVIDED_EDEK.getCode(), staticMsg); |
| 72 | + TenantSecurityException invalidEdekException = |
| 73 | + invalidEdekErrResp.toTenantSecurityException(staticHttpCode); |
| 74 | + assertKmsException(staticMsg, staticHttpCode, invalidEdekException, |
| 75 | + TenantSecurityErrorCodes.INVALID_PROVIDED_EDEK); |
| 76 | + |
| 77 | + ErrorResponse unwrapError = |
| 78 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_UNWRAP_FAILED.getCode(), staticMsg); |
| 79 | + TenantSecurityException unwrapException = unwrapError.toTenantSecurityException(staticHttpCode); |
| 80 | + assertKmsException(staticMsg, staticHttpCode, unwrapException, |
| 81 | + TenantSecurityErrorCodes.KMS_UNWRAP_FAILED); |
| 82 | + |
| 83 | + ErrorResponse wrapError = |
| 84 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_WRAP_FAILED.getCode(), staticMsg); |
| 85 | + TenantSecurityException kmsWrapException = wrapError.toTenantSecurityException(staticHttpCode); |
| 86 | + assertKmsException(staticMsg, staticHttpCode, kmsWrapException, |
| 87 | + TenantSecurityErrorCodes.KMS_WRAP_FAILED); |
| 88 | + |
| 89 | + ErrorResponse kmsAuthError = |
| 90 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_AUTHORIZATION_FAILED.getCode(), staticMsg); |
| 91 | + TenantSecurityException kmsAuthException = |
| 92 | + kmsAuthError.toTenantSecurityException(staticHttpCode); |
| 93 | + assertKmsException(staticMsg, staticHttpCode, kmsAuthException, |
| 94 | + TenantSecurityErrorCodes.KMS_AUTHORIZATION_FAILED); |
| 95 | + |
| 96 | + ErrorResponse kmsConfigInvalidError = |
| 97 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_CONFIGURATION_INVALID.getCode(), staticMsg); |
| 98 | + TenantSecurityException kmsConfigInvalidException = |
| 99 | + kmsConfigInvalidError.toTenantSecurityException(staticHttpCode); |
| 100 | + assertKmsException(staticMsg, staticHttpCode, kmsConfigInvalidException, |
| 101 | + TenantSecurityErrorCodes.KMS_CONFIGURATION_INVALID); |
| 102 | + |
| 103 | + ErrorResponse foo = |
| 104 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_ACCOUNT_ISSUE.getCode(), staticMsg); |
| 105 | + TenantSecurityException fooException = foo.toTenantSecurityException(staticHttpCode); |
| 106 | + assertKmsException(staticMsg, staticHttpCode, fooException, |
| 107 | + TenantSecurityErrorCodes.KMS_ACCOUNT_ISSUE); |
| 108 | + |
| 109 | + ErrorResponse kmsUnreachableError = |
| 110 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_UNREACHABLE.getCode(), staticMsg); |
| 111 | + TenantSecurityException kmsUnreachableException = |
| 112 | + kmsUnreachableError.toTenantSecurityException(staticHttpCode); |
| 113 | + assertKmsException(staticMsg, staticHttpCode, kmsUnreachableException, |
| 114 | + TenantSecurityErrorCodes.KMS_UNREACHABLE); |
| 115 | + |
| 116 | + // SecurityEventException |
| 117 | + ErrorResponse securityEventRejectedError = |
| 118 | + new ErrorResponse(TenantSecurityErrorCodes.SECURITY_EVENT_REJECTED.getCode(), staticMsg); |
| 119 | + TenantSecurityException securityEventRejectedException = |
| 120 | + securityEventRejectedError.toTenantSecurityException(staticHttpCode); |
| 121 | + assertSecurityEventException(staticMsg, staticHttpCode, securityEventRejectedException, |
| 122 | + TenantSecurityErrorCodes.SECURITY_EVENT_REJECTED); |
| 123 | + } |
| 124 | + |
| 125 | + private void assertTspServiceException(String expectedMsg, int expectedHttpStatusCode, |
| 126 | + TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
| 127 | + assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
| 128 | + assertTrue(exception instanceof TspServiceException); |
| 129 | + } |
| 130 | + |
| 131 | + private void assertSecurityEventException(String expectedMsg, int expectedHttpStatusCode, |
| 132 | + TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
| 133 | + assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
| 134 | + assertTrue(exception instanceof SecurityEventException); |
| 135 | + } |
| 136 | + |
| 137 | + private void assertKmsException(String expectedMsg, int expectedHttpStatusCode, |
| 138 | + TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
| 139 | + assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
| 140 | + assertTrue(exception instanceof KmsException); |
| 141 | + } |
| 142 | + |
| 143 | + private void assertTenantSecurityException(String expectedMsg, int expectedHttpStatusCode, |
| 144 | + TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
| 145 | + assertEquals(errorCode, exception.getErrorCode()); |
| 146 | + assertEquals(exception.getHttpResponseCode(), expectedHttpStatusCode); |
| 147 | + assertEquals(exception.getMessage(), expectedMsg); |
| 148 | + } |
151 | 149 |
|
152 | 150 | } |
0 commit comments