Skip to content

Commit

Permalink
service mocks and test base
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanmasek committed Sep 18, 2023
1 parent b0d3a39 commit 6e59fdf
Show file tree
Hide file tree
Showing 14 changed files with 402 additions and 6 deletions.
Binary file not shown.
10 changes: 8 additions & 2 deletions adobe-esign-connector-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.axonivy.connector.adobe.esign</groupId>
<artifactId>adobe-esign-connector-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>10.0.11-SNAPSHOT</version>
<packaging>iar</packaging>
<dependencies>
<dependency>
<groupId>com.axonivy.connector.adobe.esign</groupId>
<artifactId>adobe-esign-connector-demo</artifactId>
<artifactId>adobe-esign-connector</artifactId>
<version>10.0.11-SNAPSHOT</version>
<type>iar</type>
</dependency>
<dependency>
<groupId>com.axonivy.ivy.test</groupId>
<artifactId>unit-tester</artifactId>
<version>10.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<testSourceDirectory>src_test</testSourceDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.axonivy.connector.adobe.esign.connector.test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.annotation.security.PermitAll;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.io.IOUtils;

import ch.ivyteam.api.API;
import io.swagger.v3.oas.annotations.Hidden;

@Hidden
@PermitAll
@Path("adobeSignMock")
public class AdobeSignAgreementsServiceMock {

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("agreements")
public Response createAgreement(String payload) {
API.checkParameterNotNull(payload, "payload");
return Response.status(201).entity(load("json/createAgreement.json")).build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("agreements/{agreementId}/documents")
public Response getDocuments(@PathParam(value = "agreementId") String agreementId) {
API.checkParameterNotNull(agreementId, "agreementId");
return Response.status(201).entity(load("json/getDocuments.json")).build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("agreements/{agreementId}/documents/{documentId}")
public Response downloadDocument(@PathParam(value = "agreementId") String agreementId,
@PathParam(value = "documentId") String documentId) throws IOException {
API.checkParameterNotNull(agreementId, "agreementId");
return Response.status(200).entity(TestService.getSamplePdf()).build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("agreements/{agreementId}/signingUrls")
public Response getSigningUrls(@PathParam(value = "agreementId") String agreementId) {
API.checkParameterNotNull(agreementId, "agreementId");
return Response.status(201).entity(load("json/getSigningUrls.json")).build();
}

private static String load(String path) {
try (InputStream is = AdobeSignAgreementsServiceMock.class.getResourceAsStream(path)) {
return IOUtils.toString(is, StandardCharsets.UTF_8);
} catch (IOException ex) {
throw new RuntimeException("Failed to read resource: " + path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.axonivy.connector.adobe.esign.connector.test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.annotation.security.PermitAll;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.io.IOUtils;

import ch.ivyteam.api.API;
import io.swagger.v3.oas.annotations.Hidden;

@Hidden
@PermitAll
@Path("adobeSignMock")
public class AdobeSignTransientDocumentsServiceMock {

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("transientDocuments")
public Response transientDocuemts(String payload) {
API.checkParameterNotNull(payload, "payload");
return Response.status(201)
.entity(load("json/uploadDocument.json"))
.build();
}

private static String load(String path) {
try (InputStream is = AdobeSignTransientDocumentsServiceMock.class.getResourceAsStream(path)) {
return IOUtils.toString(is, StandardCharsets.UTF_8);
} catch (IOException ex) {
throw new RuntimeException("Failed to read resource: " + path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.axonivy.connector.adobe.esign.connector.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;

import org.apache.commons.compress.utils.IOUtils;

import ch.ivyteam.ivy.cm.ContentObjectValue;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.scripting.objects.File;

public class TestService {
private static final String SAMPLE_PDF_CMS_PATH = "/Files/samplePdf";

public static java.io.File getSamplePdf() throws IOException {
ContentObjectValue cov = Ivy.cm().findValue(SAMPLE_PDF_CMS_PATH).resolve(Locale.ENGLISH).orElse(null);
if (cov == null) {
return null;
}
java.io.File sampleFile = new File("/samplePdf.pdf", true).getJavaFile();
FileOutputStream fos = new FileOutputStream(sampleFile);
IOUtils.copy(cov.read().inputStream(), fos);
return sampleFile;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "CBJCHBCAABAAMaMFFCyiTyCblablablablabla"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"documents": [
{
"createdDate": [
2023,
9,
18
],
"id": "3AAABLblqZhDM2f8YEhDF1N6Oz-8ZY5jVZVgpRh2b_69hblablablablabla",
"label": null,
"mimeType": "application/pdf",
"name": "sample.pdf",
"numPages": 2
}
],
"supportingDocuments": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"signingUrlSetInfos": [
{
"signingUrlSetName": null,
"signingUrls": [
{
"email": "[email protected]",
"esignUrl": "https://secure.eu2.adobesign.com/public/apiesign?pid=CBFCIBAA3AAABLblqZhBfFw-mWHXrt1tSN_AfnQQld3Ea9pq61iY57nLp7Duyp5I8XoIyLkT1TcjoW7lshFZXF4zoa5UPIOBRKUPhP8YuenqrWRNjVAXoTO8P7eMr3qIr8-hOhCOIfKUXiB0UOmQ%2A&client_id=CBJCHblablablablabla"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"transientDocumentId": "3AAABLblqZhDVSvtTTuHIxFoTcvrHMhZwdRBEz62SCvv1G8yTaPzxcu8NGkq9IIILoVyC4UHJ8vgl_Mw5CY9lBSMEbMriLGzZyy6KCWpF8Ac2TpP-3Bpfrr8LHyv92ukAsKnyJBDP9XPpSyVOm1CtLUbNtX7M6cfu_LKRSTl-nGfMK6eRalZ6AsxLzDTW8lALl20Md5x5EyOkBLW1ZvvyF2OITH-cPVgBAoh7ejVkDiHwskPq9NIUoTZ4jN3bvq0ma5Dz7gP1F5RGuLlzGDlje3_MIof7-EgdIQt92grPt7fe0-Dfgjblablabla"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.axonivy.connector.adobe.esign.connector.test;

import ch.ivyteam.ivy.application.IApplication;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.environment.AppFixture;
import ch.ivyteam.ivy.rest.client.RestClient;
import ch.ivyteam.ivy.rest.client.RestClient.Builder;
import ch.ivyteam.ivy.rest.client.RestClients;

@IvyProcessTest
public class TestAdobeSignConnector {
protected static final String TRANSIENT_DOCUMENTS = "TransientDocuments";
protected static final String AGREEMENTS = "Agreements";

protected void prepareRestClient(IApplication app, AppFixture fixture, String clientName) {
fixture.var("adobe-sign-connector.host", "TESTHOST");
fixture.var("adobe-sign-connector.integrationKey", "TESTUSER");
RestClient restClient = RestClients.of(app).find(clientName);
// change created client: use test url and a slightly different version of
// the
// DocuWare Auth feature
Builder builder = RestClient.create(restClient.name()).uuid(restClient.uniqueId())
.uri("http://{ivy.engine.host}:{ivy.engine.http.port}/{ivy.request.application}/api/adobeSignMock")
.description(restClient.description()).properties(restClient.properties());

for (String feature : restClient.features()) {
builder.feature(feature);
}

builder.feature("ch.ivyteam.ivy.rest.client.security.CsrfHeaderFeature");
restClient = builder.toRestClient();
RestClients.of(app).set(restClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package com.axonivy.connector.adobe.esign.connector.test;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.axonivy.connector.adobe.esign.connector.AgreementsData;
import com.axonivy.connector.adobe.esign.connector.rest.DownloadResult;
import com.axonivy.connector.adobe.esign.connector.service.AdobeSignService;

import api.rest.v6.client.AgreementCreationInfo;
import api.rest.v6.client.AgreementCreationInfo.SignatureTypeEnum;
import api.rest.v6.client.AgreementCreationInfo.StateEnum;
import api.rest.v6.client.AgreementCreationInfoParticipantSetsInfo.RoleEnum;
import api.rest.v6.client.AgreementCreationResponse;
import api.rest.v6.client.AgreementDocuments;
import api.rest.v6.client.SigningUrlResponseSigningUrlSetInfos;
import ch.ivyteam.ivy.application.IApplication;
import ch.ivyteam.ivy.bpm.engine.client.BpmClient;
import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult;
import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement;
import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.environment.AppFixture;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.security.ISession;

@IvyProcessTest
public class TestAgreementsService extends TestAdobeSignConnector {

private static final BpmElement testeeCreateAgreement = BpmProcess.path("connector/Agreements")
.elementName("createAgreement(AgreementCreationInfo)");

private static final BpmElement testeeGetDocuments = BpmProcess.path("connector/Agreements")
.elementName("getDocuments(String)");

private static final BpmElement testeeDownloadDocument = BpmProcess.path("connector/Agreements")
.elementName("dowloadDocument(String, String, String, Boolean)");

private static final BpmElement testeeGetSigningUrls = BpmProcess.path("connector/Agreements")
.elementName("getSigningURLs(String,String)");

@Test
public void createAgreement(BpmClient bpmClient, ISession session, AppFixture fixture, IApplication app)
throws IOException {

prepareRestClient(app, fixture, AGREEMENTS);

AgreementCreationInfo agreement = createTestAgreement();

ExecutionResult result = bpmClient.start().subProcess(testeeCreateAgreement).withParam("agreement", agreement)
.execute();
AgreementsData data = result.data().last();
AgreementCreationResponse response = data.getAgreementCreationResponse();
assertThat(response).isNotNull();
assertThat(response.getId()).isNotEmpty();
}

@Test
public void getDocuments(BpmClient bpmClient, ISession session, AppFixture fixture, IApplication app)
throws IOException {

prepareRestClient(app, fixture, AGREEMENTS);

String agreementId = "test-agreement-id";

ExecutionResult result = bpmClient.start().subProcess(testeeGetDocuments).withParam("agreementId", agreementId)
.execute();
AgreementsData data = result.data().last();
AgreementDocuments response = data.getDocuments();
assertThat(response).isNotNull();
assertThat(response.getDocuments()).isNotEmpty();
}

@Test
public void downloadDocument(BpmClient bpmClient, ISession session, AppFixture fixture, IApplication app)
throws IOException {

prepareRestClient(app, fixture, AGREEMENTS);

String agreementId = "test-agreement-id";
String documentId = "test-document-id";
String filename = "sample.pdf";
Boolean asFile = Boolean.TRUE;

ExecutionResult result = bpmClient.start().subProcess(testeeDownloadDocument)
.withParam("agreementId", agreementId).withParam("documentId", documentId)
.withParam("filename", filename).withParam("asFile", asFile).execute();
AgreementsData data = result.data().last();
DownloadResult downloadResult = data.getDownload();
assertThat(downloadResult).isNotNull();
if (downloadResult.getError() != null) {
Ivy.log().error(downloadResult.getError());
}
assertThat(downloadResult.getFile()).isNotNull();
}

@Test
public void getSigningUrls(BpmClient bpmClient, ISession session, AppFixture fixture, IApplication app)
throws IOException {

prepareRestClient(app, fixture, AGREEMENTS);

String agreementId = "test-agreement-id";
String frameParent = "test";

ExecutionResult result = bpmClient.start().subProcess(testeeGetSigningUrls)
.withParam("agreementId", agreementId).withParam("frameParent", frameParent).execute();
AgreementsData data = result.data().last();

List<SigningUrlResponseSigningUrlSetInfos> signingUrls = data.getSigningUrls();

assertThat(signingUrls).isNotNull();
assertThat(signingUrls).isNotEmpty();
}

private AgreementCreationInfo createTestAgreement() {
AgreementCreationInfo agreement = new AgreementCreationInfo();

agreement.setName("test name");
agreement.setMessage("Please sign this document!");
agreement.setSignatureType(SignatureTypeEnum.ESIGN);
agreement.setState(StateEnum.IN_PROCESS);

// add signers
AdobeSignService.getInstance().createParticipantInfoForEmail(Arrays.asList("[email protected]"),
RoleEnum.SIGNER);
agreement.setParticipantSetsInfo(AdobeSignService.getInstance()
.createParticipantInfoForEmail(Arrays.asList("[email protected]"), RoleEnum.SIGNER));

// add documentIds - need to be already transferred with the upload document
// service
agreement.setFileInfos(
AdobeSignService.getInstance().createFileInfosForDocumentIds(Arrays.asList("test-document-id")));

agreement.setEmailOption(AdobeSignService.getInstance().createAllDisabledSendOptions());
return agreement;
}
}
Loading

0 comments on commit 6e59fdf

Please sign in to comment.