Skip to content

Commit

Permalink
test(qes): cover document download
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscocastanho-onfido committed Jul 17, 2024
1 parent 4427e1c commit 7eb79ce
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.onfido.integration;

import com.google.gson.internal.LinkedTreeMap;
import com.onfido.model.TaskItem;
import com.onfido.model.WorkflowRun;
import com.onfido.model.WorkflowRunBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class QualifiedElectronicSignatureTest extends TestBase {

@Test
public void documentsTest() throws Exception {
UUID applicantId = createApplicant().getId();
UUID workflowId = UUID.fromString("8b74614f-9e7f-42fd-852a-5f2bcc852587");

Map<String, Object> customData = new HashMap<>();
customData.put("country_of_operation", "GBR");
customData.put("document_date_of_expiry", "2022-01-01");
customData.put("document_issuing_country", "FRA");
customData.put("document_issuing_date", "2022-01-01");
customData.put("document_number", "Example string");
customData.put(
"document_to_sign_url",
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
customData.put("document_type", "driving_licence");

WorkflowRun workflowRun =
onfido.createWorkflowRun(
new WorkflowRunBuilder()
.workflowId(workflowId)
.applicantId(applicantId)
.customData(customData));
UUID workflowRunId = workflowRun.getId();

TaskItem task = onfido.listTasks(workflowRunId).get(0);

LinkedTreeMap output =
(LinkedTreeMap)
repeatRequestUntilTaskOutputChanges(
"findTask", new Object[] {workflowRunId, task.getId()}, 10, 3000)
.getOutput();

LinkedTreeMap properties = (LinkedTreeMap) output.get("properties");
ArrayList signedDocuments = (ArrayList) properties.get("signed_documents");
LinkedTreeMap signedDocument = (LinkedTreeMap) signedDocuments.get(0);
UUID fileId = UUID.fromString((String) signedDocument.get("id"));

byte[] byteArray = onfido.downloadQesDocument(workflowRunId, fileId).getByteArray();

Assertions.assertEquals("%PDF", new String(byteArray, 0, 4));
Assertions.assertTrue(byteArray.length > 0);
}
}
40 changes: 26 additions & 14 deletions src/test/java/com/onfido/integration/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,7 @@
import com.onfido.Configuration;
import com.onfido.FileTransfer;
import com.onfido.api.DefaultApi;
import com.onfido.model.Applicant;
import com.onfido.model.ApplicantBuilder;
import com.onfido.model.Check;
import com.onfido.model.CheckBuilder;
import com.onfido.model.CountryCodes;
import com.onfido.model.Document;
import com.onfido.model.IdPhoto;
import com.onfido.model.LivePhoto;
import com.onfido.model.LocationBuilder;
import com.onfido.model.WatchlistMonitor;
import com.onfido.model.WatchlistMonitorBuilder;
import com.onfido.model.Webhook;
import com.onfido.model.WorkflowRun;
import com.onfido.model.WorkflowRunBuilder;
import com.onfido.model.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -65,6 +52,8 @@ public Applicant createApplicant(String first_name)
new ApplicantBuilder()
.firstName(first_name)
.lastName("Last")
.email("[email protected]")
.phoneNumber("351911111111")
.location(
new LocationBuilder()
.ipAddress("127.0.0.1")
Expand Down Expand Up @@ -190,6 +179,29 @@ public Object repeatRequestUntilStatusChanges(
return instance;
}

public Task repeatRequestUntilTaskOutputChanges(
String methodName, Object[] params, int maxRetries, int sleepTime)
throws NoSuchMethodException,
IllegalAccessException,
InterruptedException,
InvocationTargetException {
Method method = getMethod(methodName, params);
Task instance = (Task) method.invoke(onfido, params);

int iteration = 0;

while (instance.getOutput() == null) {
if (iteration > maxRetries) {
throw new RuntimeException("Task output did not change in time");
}

iteration += 1;
Thread.sleep(sleepTime);
instance = (Task) method.invoke(onfido, params);
}
return instance;
}

public Object repeatRequestUntilHttpCodeChanges(
String methodName, Object[] params, int maxRetries, int sleepTime)
throws NoSuchMethodException, IllegalAccessException, InterruptedException {
Expand Down

0 comments on commit 7eb79ce

Please sign in to comment.