Skip to content

Commit 2e70489

Browse files
Add unit tests
1 parent 13af340 commit 2e70489

File tree

8 files changed

+1620
-2
lines changed

8 files changed

+1620
-2
lines changed

components/org.wso2.carbon.identity.verification.onfido.api/org.wso2.carbon.identity.verification.onfido.api.v1/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@
102102
<artifactId>org.wso2.carbon.identity.verification.onfido.api.common</artifactId>
103103
<scope>provided</scope>
104104
</dependency>
105+
<!--Test dependencies-->
106+
<dependency>
107+
<groupId>org.testng</groupId>
108+
<artifactId>testng</artifactId>
109+
<scope>test</scope>
110+
</dependency>
111+
<dependency>
112+
<groupId>org.jacoco</groupId>
113+
<artifactId>org.jacoco.agent</artifactId>
114+
<classifier>runtime</classifier>
115+
</dependency>
116+
<dependency>
117+
<groupId>org.mockito</groupId>
118+
<artifactId>mockito-core</artifactId>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.mockito</groupId>
123+
<artifactId>mockito-testng</artifactId>
124+
<scope>test</scope>
125+
</dependency>
105126
</dependencies>
106127
<build>
107128
<plugins>

components/org.wso2.carbon.identity.verification.onfido.api/org.wso2.carbon.identity.verification.onfido.api.v1/src/test/java/org/wso2/carbon/identity/verification/onfido/api/v1/core/OnfidoIdvServiceTest.java

Lines changed: 478 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}

components/org.wso2.carbon.identity.verification.onfido.connector/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@
6262
<artifactId>org.wso2.carbon.user.core</artifactId>
6363
<scope>provided</scope>
6464
</dependency>
65+
<!--Test dependencies-->
66+
<dependency>
67+
<groupId>org.testng</groupId>
68+
<artifactId>testng</artifactId>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.jacoco</groupId>
73+
<artifactId>org.jacoco.agent</artifactId>
74+
<classifier>runtime</classifier>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.mockito</groupId>
78+
<artifactId>mockito-core</artifactId>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.mockito</groupId>
83+
<artifactId>mockito-testng</artifactId>
84+
<scope>test</scope>
85+
</dependency>
6586
</dependencies>
6687

6788
<build>

components/org.wso2.carbon.identity.verification.onfido.connector/src/main/java/org/wso2/carbon/identity/verification/onfido/connector/OnfidoIdentityVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ private JSONObject getApplicantRequestBody(Map<String, String> idVClaimsWithValu
756756
* UniqueIDUserStoreManager.
757757
* @throws UserStoreException If there is a failure in retrieving the UserStoreManager.
758758
*/
759-
private UniqueIDUserStoreManager getUniqueIdEnabledUserStoreManager(int tenantId)
759+
UniqueIDUserStoreManager getUniqueIdEnabledUserStoreManager(int tenantId)
760760
throws IdentityVerificationServerException, UserStoreException {
761761

762762
RealmService realmService = OnfidoIDVDataHolder.getRealmService();

0 commit comments

Comments
 (0)