Skip to content

Commit

Permalink
SDK-2528: Helper method for finding the Resources that belong to a Check
Browse files Browse the repository at this point in the history
  • Loading branch information
bucky-boy committed Sep 25, 2024
1 parent 0ffb992 commit 149b765
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public ImportTokenResponse getImportToken() {
return importToken;
}

public ResourceContainer getResourcesForCheck(String checkId) {
CheckResponse checkResponse = this.checks.stream()
.filter(check -> check.getId().equals(checkId))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Check not found"));
return resources.filterForCheck(checkResponse);
}

public List<AuthenticityCheckResponse> getAuthenticityChecks() {
return filterChecksByType(AuthenticityCheckResponse.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.yoti.api.client.docs.session.retrieve;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down Expand Up @@ -72,7 +73,20 @@ public List<ZoomLivenessResourceResponse> getZoomLivenessResources() {
*
* @return the list of static liveness resources
*/
public List<StaticLivenessResourceResponse> getStaticLivenessResources() { return filterLivenessResourcesByType(StaticLivenessResourceResponse.class); }
public List<StaticLivenessResourceResponse> getStaticLivenessResources() {
return filterLivenessResourcesByType(StaticLivenessResourceResponse.class);
}

private <T extends LivenessResourceResponse> List<T> filterLivenessResourcesByType(Class<T> clazz) {
if (livenessCapture == null) {
return Collections.emptyList();
} else {
return livenessCapture.stream()
.filter(clazz::isInstance)
.map(clazz::cast)
.collect(Collectors.toList());
}
}

/**
* Returns ApplicantProfile resources uploaded by the user/relying business
Expand All @@ -83,14 +97,23 @@ public List<ApplicantProfileResourceResponse> getApplicantProfiles() {
return applicantProfiles;
}

private <T extends LivenessResourceResponse> List<T> filterLivenessResourcesByType(Class<T> clazz) {
List<T> filteredList = new ArrayList<>();
for (LivenessResourceResponse livenessResourceResponse : livenessCapture) {
if (clazz.isInstance(livenessResourceResponse)) {
filteredList.add(clazz.cast(livenessResourceResponse));
}
ResourceContainer filterForCheck(CheckResponse checkResponse) {
ResourceContainer newResourceContainer = new ResourceContainer();
newResourceContainer.idDocuments = filterResources(this.idDocuments, checkResponse.getResourcesUsed());
newResourceContainer.supplementaryDocuments = filterResources(this.supplementaryDocuments, checkResponse.getResourcesUsed());
newResourceContainer.livenessCapture = filterResources(this.livenessCapture, checkResponse.getResourcesUsed());
newResourceContainer.faceCapture = filterResources(this.faceCapture, checkResponse.getResourcesUsed());
newResourceContainer.applicantProfiles = filterResources(this.applicantProfiles, checkResponse.getResourcesUsed());
return newResourceContainer;
}

private <T extends ResourceResponse> List<T> filterResources(List<T> resources, List<String> resourceIds) {
if (resources == null) {
return Collections.emptyList();
}
return filteredList;
return resources.stream()
.filter(resource -> resourceIds.contains(resource.getId()))
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.yoti.api.client.spi.remote.util.FieldSetter;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class GetSessionResultTest {

GetSessionResult testObj = new GetSessionResult();
private static final String AUTH_CHECK_ID = "authCheckId";

@InjectMocks GetSessionResult testObj = new GetSessionResult();

@Mock AuthenticityCheckResponse authenticityCheckResponseMock;
@Mock FaceMatchCheckResponse faceMatchCheckResponseMock;
Expand All @@ -33,6 +41,14 @@ public class GetSessionResultTest {
@Mock ProfileDocumentMatchCheckResponse profileDocumentMatchCheckResponseMock;
@Mock SynecticsIdentityFraudCheckResponse synecticsIdentityFraudCheckResponseMock;

@Mock(name = "resources") ResourceContainer resourceContainerMock;
@Mock ResourceContainer checkResourceContainerMock;

@Before
public void setUp() throws Exception {
when(authenticityCheckResponseMock.getId()).thenReturn(AUTH_CHECK_ID);
}

@Test
public void shouldFilterChecks() {
List<CheckResponse> allChecks = Arrays.asList(
Expand Down Expand Up @@ -69,4 +85,21 @@ public void shouldFilterChecks() {
assertThat(testObj.getSynecticsIdentityFraudChecks(), contains(synecticsIdentityFraudCheckResponseMock));
}

@Test
public void getResourcesForCheck_shouldThrowExceptionForBadCheckId() {
FieldSetter.setField(testObj, "checks", Collections.singletonList(authenticityCheckResponseMock));

assertThrows(IllegalArgumentException.class, () -> testObj.getResourcesForCheck("someBadCheckId"));
}

@Test
public void getResourcesForCheck_shouldReturnResultFromResources() {
FieldSetter.setField(testObj, "checks", Collections.singletonList(authenticityCheckResponseMock));
when(resourceContainerMock.filterForCheck(authenticityCheckResponseMock)).thenReturn(checkResourceContainerMock);

ResourceContainer result = testObj.getResourcesForCheck(AUTH_CHECK_ID);

assertThat(result, is(checkResourceContainerMock));
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.yoti.api.client.docs.session.retrieve;

import static java.util.Arrays.asList;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.yoti.api.client.spi.remote.util.FieldSetter;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand All @@ -18,37 +23,123 @@
@RunWith(MockitoJUnitRunner.class)
public class ResourceContainerTest {

private static final String ID_DOC_1_ID = "idDocumentResource1Id";
private static final String SUPPLEMENTARY_DOC_1_ID = "supplementaryDocResource1Id";
private static final String ZOOM_RESOURCE_1_ID = "zoomLivenessResource1MockId";
private static final String FACE_CAPTURE_1_ID = "faceCaptureResource1Id";
private static final String PROFILE_1_ID = "applicantProfileResource1Id";

ResourceContainer testObj = new ResourceContainer();

@Mock ZoomLivenessResourceResponse zoomLivenessResourceMock;
@Mock IdDocumentResourceResponse idDocResourceResponse1Mock;
@Mock IdDocumentResourceResponse idDocResourceResponse2Mock;
@Mock SupplementaryDocumentResourceResponse supplementaryDocResourceResponse1Mock;
@Mock SupplementaryDocumentResourceResponse supplementaryDocResourceResponse2Mock;
@Mock ZoomLivenessResourceResponse zoomLivenessResource1Mock;
@Mock ZoomLivenessResourceResponse zoomLivenessResource2Mock;
@Mock StaticLivenessResourceResponse staticLivenessResourceMock;
@Mock LivenessResourceResponse livenessResource;
@Mock FaceCaptureResourceResponse faceCaptureResourceResponse1Mock;
@Mock FaceCaptureResourceResponse faceCaptureResourceResponse2Mock;
@Mock ApplicantProfileResourceResponse applicantProfileResourceResponse1Mock;
@Mock ApplicantProfileResourceResponse applicantProfileResourceResponse2Mock;

@Mock CheckResponse checkResponseMock;

@Before
public void setUp() throws Exception {
when(idDocResourceResponse1Mock.getId()).thenReturn(ID_DOC_1_ID);
when(idDocResourceResponse2Mock.getId()).thenReturn("idDocumentResource2Id");
when(supplementaryDocResourceResponse1Mock.getId()).thenReturn(SUPPLEMENTARY_DOC_1_ID);
when(supplementaryDocResourceResponse2Mock.getId()).thenReturn("supplementaryDocResource2Id");
when(zoomLivenessResource1Mock.getId()).thenReturn(ZOOM_RESOURCE_1_ID);
when(zoomLivenessResource2Mock.getId()).thenReturn("zoomLivenessResource2MockId");
when(faceCaptureResourceResponse1Mock.getId()).thenReturn(FACE_CAPTURE_1_ID);
when(faceCaptureResourceResponse2Mock.getId()).thenReturn("faceCaptureResource2Id");
when(applicantProfileResourceResponse1Mock.getId()).thenReturn(PROFILE_1_ID);
when(applicantProfileResourceResponse2Mock.getId()).thenReturn("applicantProfileResource2Id");
}

@Test
public void shouldFilterZoomLivenessResources() {
FieldSetter.setField(testObj, "livenessCapture", Arrays.asList(zoomLivenessResourceMock, staticLivenessResourceMock, livenessResource));
public void getZoomLivenessResources_shouldFilterZoomLivenessResources() {
FieldSetter.setField(testObj, "livenessCapture", asList(zoomLivenessResource1Mock, staticLivenessResourceMock, livenessResource));

List<ZoomLivenessResourceResponse> result = testObj.getZoomLivenessResources();

assertThat(result, contains(zoomLivenessResourceMock));
assertThat(result, contains(zoomLivenessResource1Mock));
}

@Test
public void shouldFilterStaticLivenessResources() {
FieldSetter.setField(testObj, "livenessCapture", Arrays.asList(zoomLivenessResourceMock, staticLivenessResourceMock, livenessResource));
public void getZoomLivenessResources_shouldReturnEmptyList() {
FieldSetter.setField(testObj, "livenessCapture", new ArrayList<>());

List<ZoomLivenessResourceResponse> result = testObj.getZoomLivenessResources();

assertThat(result, hasSize(0));
}

@Test
public void getStaticLivenessResources_shouldFilterStaticLivenessResources() {
FieldSetter.setField(testObj, "livenessCapture", asList(zoomLivenessResource1Mock, staticLivenessResourceMock, livenessResource));

List<StaticLivenessResourceResponse> result = testObj.getStaticLivenessResources();

assertThat(result, contains(staticLivenessResourceMock));
}

@Test
public void shouldReturnEmptyList() {
public void getStaticLivenessResources_shouldReturnEmptyList() {
FieldSetter.setField(testObj, "livenessCapture", new ArrayList<>());

List<ZoomLivenessResourceResponse> result = testObj.getZoomLivenessResources();
List<StaticLivenessResourceResponse> result = testObj.getStaticLivenessResources();

assertThat(result, hasSize(0));
}

@Test
public void filterForCheck_shouldHandleNullResources() {
ResourceContainer result = testObj.filterForCheck(checkResponseMock);

assertThat(result.getIdDocuments(), is(emptyIterable()));
assertThat(result.getSupplementaryDocuments(), is(emptyIterable()));
assertThat(result.getLivenessCapture(), is(emptyIterable()));
assertThat(result.getFaceCapture(), is(emptyIterable()));
assertThat(result.getApplicantProfiles(), is(emptyIterable()));
}

@Test
public void filterForCheck_shouldReturnEmptyWhenNoMatches() {
FieldSetter.setField(testObj, "idDocuments", new ArrayList<>());
FieldSetter.setField(testObj, "supplementaryDocuments", new ArrayList<>());
FieldSetter.setField(testObj, "livenessCapture", new ArrayList<>());
FieldSetter.setField(testObj, "faceCapture", new ArrayList<>());
FieldSetter.setField(testObj, "applicantProfiles", new ArrayList<>());

ResourceContainer result = testObj.filterForCheck(checkResponseMock);

assertThat(result.getIdDocuments(), is(emptyIterable()));
assertThat(result.getSupplementaryDocuments(), is(emptyIterable()));
assertThat(result.getLivenessCapture(), is(emptyIterable()));
assertThat(result.getFaceCapture(), is(emptyIterable()));
assertThat(result.getApplicantProfiles(), is(emptyIterable()));
}

@Test
public void filterForCheck_shouldReturnFilteredCollections() {
FieldSetter.setField(testObj, "idDocuments", asList(idDocResourceResponse1Mock, idDocResourceResponse2Mock));
FieldSetter.setField(testObj, "supplementaryDocuments", asList(supplementaryDocResourceResponse1Mock, supplementaryDocResourceResponse2Mock));
FieldSetter.setField(testObj, "livenessCapture", asList(zoomLivenessResource1Mock, zoomLivenessResource2Mock, staticLivenessResourceMock));
FieldSetter.setField(testObj, "faceCapture", asList(faceCaptureResourceResponse1Mock, faceCaptureResourceResponse2Mock));
FieldSetter.setField(testObj, "applicantProfiles", asList(applicantProfileResourceResponse1Mock, applicantProfileResourceResponse2Mock));
when(checkResponseMock.getResourcesUsed()).thenReturn(asList(ID_DOC_1_ID, SUPPLEMENTARY_DOC_1_ID, ZOOM_RESOURCE_1_ID, FACE_CAPTURE_1_ID, PROFILE_1_ID));

ResourceContainer result = testObj.filterForCheck(checkResponseMock);

assertThat(result.getIdDocuments(), contains(idDocResourceResponse1Mock));
assertThat(result.getSupplementaryDocuments(), contains(supplementaryDocResourceResponse1Mock));
assertThat(result.getLivenessCapture(), contains(zoomLivenessResource1Mock));
assertThat(result.getFaceCapture(), contains(faceCaptureResourceResponse1Mock));
assertThat(result.getApplicantProfiles(), contains(applicantProfileResourceResponse1Mock));
}

}

0 comments on commit 149b765

Please sign in to comment.