|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.wso2.carbon.identity.verification.onfido.api.v1.impl; |
| 20 | + |
| 21 | +import org.mockito.InjectMocks; |
| 22 | +import org.mockito.Mock; |
| 23 | +import org.mockito.MockitoAnnotations; |
| 24 | +import org.testng.annotations.BeforeMethod; |
| 25 | +import org.testng.annotations.DataProvider; |
| 26 | +import org.testng.annotations.Test; |
| 27 | +import org.wso2.carbon.identity.verification.onfido.api.common.Constants; |
| 28 | +import org.wso2.carbon.identity.verification.onfido.api.common.error.APIError; |
| 29 | +import org.wso2.carbon.identity.verification.onfido.api.common.error.ErrorDTO; |
| 30 | +import org.wso2.carbon.identity.verification.onfido.api.common.error.ErrorResponse; |
| 31 | +import org.wso2.carbon.identity.verification.onfido.api.v1.core.OnfidoIdvService; |
| 32 | +import org.wso2.carbon.identity.verification.onfido.api.v1.model.VerifyRequest; |
| 33 | +import org.wso2.carbon.identity.verification.onfido.api.v1.model.VerifyRequestPayload; |
| 34 | +import org.wso2.carbon.identity.verification.onfido.api.v1.model.VerifyRequestPayloadObject; |
| 35 | + |
| 36 | +import javax.ws.rs.core.Response; |
| 37 | + |
| 38 | +import static org.mockito.Mockito.any; |
| 39 | +import static org.mockito.Mockito.anyString; |
| 40 | +import static org.mockito.Mockito.doNothing; |
| 41 | +import static org.mockito.Mockito.doThrow; |
| 42 | +import static org.mockito.Mockito.times; |
| 43 | +import static org.mockito.Mockito.verify; |
| 44 | +import static org.testng.Assert.assertEquals; |
| 45 | +import static org.testng.Assert.assertNotNull; |
| 46 | + |
| 47 | +public class DefaultApiServiceImplTest { |
| 48 | + |
| 49 | + @Mock |
| 50 | + private OnfidoIdvService onfidoIdvService; |
| 51 | + @InjectMocks |
| 52 | + private DefaultApiServiceImpl defaultApiService; |
| 53 | + private VerifyRequest testVerifyRequest; |
| 54 | + |
| 55 | + private static final String TEST_X_SHA2_SIGNATURE = "test-signature"; |
| 56 | + private static final String TEST_IDVP_ID = "test-idvp-id"; |
| 57 | + private static final String TEST_RESOURCE_TYPE = "workflow_run"; |
| 58 | + private static final String TEST_ACTION = "workflow_run.completed"; |
| 59 | + private static final String TEST_WORKFLOW_RUN_ID = "test_workflow_run_id"; |
| 60 | + private static final String TEST_STATUS = "approved"; |
| 61 | + private static final String TEST_COMPLETED_AT = "2024-10-05T09:59:34Z"; |
| 62 | + private static final String TEST_HREF = "https://api.eu.onfido.com/v3.6/workflow_runs/test_workflow_run_id"; |
| 63 | + |
| 64 | + @BeforeMethod |
| 65 | + public void setUp() { |
| 66 | + |
| 67 | + MockitoAnnotations.openMocks(this); |
| 68 | + testVerifyRequest = createTestVerifyRequest(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testVerifySuccess() { |
| 73 | + |
| 74 | + doNothing().when(onfidoIdvService).verify(TEST_X_SHA2_SIGNATURE, TEST_IDVP_ID, testVerifyRequest); |
| 75 | + |
| 76 | + Response response = defaultApiService.verify(TEST_X_SHA2_SIGNATURE, TEST_IDVP_ID, testVerifyRequest); |
| 77 | + |
| 78 | + verify(onfidoIdvService, times(1)).verify(TEST_X_SHA2_SIGNATURE, TEST_IDVP_ID, testVerifyRequest); |
| 79 | + assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "Response status should be OK"); |
| 80 | + } |
| 81 | + |
| 82 | + @DataProvider(name = "serverErrorDataProvider") |
| 83 | + public Object[][] serverErrorDataProvider() { |
| 84 | + return new Object[][] { |
| 85 | + {Constants.ErrorMessage.SERVER_ERROR_GENERAL_ERROR}, |
| 86 | + {Constants.ErrorMessage.SERVER_ERROR_RESOLVING_IDVP}, |
| 87 | + {Constants.ErrorMessage.SERVER_ERROR_IDV_PROVIDER_CONFIG_PROPERTIES_INVALID}, |
| 88 | + {Constants.ErrorMessage.SERVER_ERROR_SIGNATURE_VALIDATION_FAILURE}, |
| 89 | + {Constants.ErrorMessage.SERVER_ERROR_INVALID_WORKFLOW_RUN_STATUS}, |
| 90 | + {Constants.ErrorMessage.SERVER_ERROR_UPDATING_IDV_CLAIM_VERIFICATION_STATUS} |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + @Test(dataProvider = "serverErrorDataProvider") |
| 95 | + public void testVerifyServerError(Constants.ErrorMessage errorMessage) { |
| 96 | + ErrorResponse errorResponse = new ErrorResponse.Builder() |
| 97 | + .withCode(errorMessage.getCode()) |
| 98 | + .withMessage(errorMessage.getMessage()) |
| 99 | + .withDescription(errorMessage.getDescription()) |
| 100 | + .build(); |
| 101 | + |
| 102 | + APIError apiError = new APIError(Response.Status.INTERNAL_SERVER_ERROR, errorResponse); |
| 103 | + |
| 104 | + doThrow(apiError).when(onfidoIdvService).verify(anyString(), anyString(), any(VerifyRequest.class)); |
| 105 | + |
| 106 | + APIError receivedApiError = null; |
| 107 | + try { |
| 108 | + defaultApiService.verify(TEST_X_SHA2_SIGNATURE, TEST_IDVP_ID, testVerifyRequest); |
| 109 | + } catch (APIError e) { |
| 110 | + receivedApiError = e; |
| 111 | + } |
| 112 | + |
| 113 | + assertNotNull(receivedApiError, "APIError should be thrown"); |
| 114 | + assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), |
| 115 | + receivedApiError.getStatus().getStatusCode(), |
| 116 | + "Response status should be 500 INTERNAL_SERVER_ERROR"); |
| 117 | + ErrorDTO errorDTO = receivedApiError.getResponseEntity(); |
| 118 | + assertEquals(errorMessage.getCode(), errorDTO.getCode()); |
| 119 | + assertEquals(errorMessage.getMessage(), errorDTO.getMessage()); |
| 120 | + assertEquals(errorMessage.getDescription(), errorDTO.getDescription()); |
| 121 | + } |
| 122 | + |
| 123 | + @DataProvider(name = "clientErrorDataProvider") |
| 124 | + public Object[][] clientErrorDataProvider() { |
| 125 | + return new Object[][] { |
| 126 | + {Constants.ErrorMessage.CLIENT_ERROR_SIGNATURE_MISMATCH, Response.Status.UNAUTHORIZED}, |
| 127 | + {Constants.ErrorMessage.CLIENT_ERROR_RESOLVING_IDVP, Response.Status.NOT_FOUND}, |
| 128 | + {Constants.ErrorMessage.CLIENT_ERROR_UNSUPPORTED_RESOURCE_TYPE_OR_ACTION, Response.Status.BAD_REQUEST}, |
| 129 | + {Constants.ErrorMessage.CLIENT_ERROR_INVALID_WORKFLOW_OUTPUT, Response.Status.BAD_REQUEST}, |
| 130 | + {Constants.ErrorMessage.CLIENT_ERROR_DATA_COMPARISON_RESULT_NOT_FOUND, Response.Status.BAD_REQUEST}, |
| 131 | + {Constants.ErrorMessage.CLIENT_ERROR_DATA_COMPARISON_RESULT_NULL, Response.Status.BAD_REQUEST}, |
| 132 | + {Constants.ErrorMessage.CLIENT_ERROR_INVALID_REQUEST, Response.Status.BAD_REQUEST} |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + @Test(dataProvider = "clientErrorDataProvider") |
| 137 | + public void testVerifyClientError(Constants.ErrorMessage errorMessage, Response.Status expectedStatus) { |
| 138 | + ErrorResponse errorResponse = new ErrorResponse.Builder() |
| 139 | + .withCode(errorMessage.getCode()) |
| 140 | + .withMessage(errorMessage.getMessage()) |
| 141 | + .withDescription(errorMessage.getDescription()) |
| 142 | + .build(); |
| 143 | + |
| 144 | + APIError apiError = new APIError(expectedStatus, errorResponse); |
| 145 | + |
| 146 | + doThrow(apiError).when(onfidoIdvService).verify(anyString(), anyString(), any(VerifyRequest.class)); |
| 147 | + |
| 148 | + APIError receivedApiError = null; |
| 149 | + try { |
| 150 | + defaultApiService.verify(TEST_X_SHA2_SIGNATURE, TEST_IDVP_ID, testVerifyRequest); |
| 151 | + } catch (APIError e) { |
| 152 | + receivedApiError = e; |
| 153 | + } |
| 154 | + |
| 155 | + assertNotNull(receivedApiError, "APIError should be thrown"); |
| 156 | + assertEquals(expectedStatus.getStatusCode(), receivedApiError.getStatus().getStatusCode(), |
| 157 | + "Response status should match the expected status"); |
| 158 | + ErrorDTO errorDTO = receivedApiError.getResponseEntity(); |
| 159 | + assertEquals(errorMessage.getCode(), errorDTO.getCode()); |
| 160 | + assertEquals(errorMessage.getMessage(), errorDTO.getMessage()); |
| 161 | + assertEquals(errorMessage.getDescription(), errorDTO.getDescription()); |
| 162 | + } |
| 163 | + |
| 164 | + private VerifyRequest createTestVerifyRequest() { |
| 165 | + |
| 166 | + VerifyRequestPayload verifyRequestPayload = new VerifyRequestPayload(); |
| 167 | + verifyRequestPayload.setResourceType(TEST_RESOURCE_TYPE); |
| 168 | + verifyRequestPayload.setAction(TEST_ACTION); |
| 169 | + |
| 170 | + VerifyRequestPayloadObject object = new VerifyRequestPayloadObject(); |
| 171 | + object.setId(TEST_WORKFLOW_RUN_ID); |
| 172 | + object.setStatus(TEST_STATUS); |
| 173 | + object.setCompletedAtIso8601(TEST_COMPLETED_AT); |
| 174 | + object.setHref(TEST_HREF); |
| 175 | + verifyRequestPayload.setObject(object); |
| 176 | + |
| 177 | + VerifyRequest verifyRequest = new VerifyRequest(); |
| 178 | + verifyRequest.setPayload(verifyRequestPayload); |
| 179 | + |
| 180 | + return verifyRequest; |
| 181 | + } |
| 182 | +} |
0 commit comments