Skip to content

Commit c285ed4

Browse files
authored
Merge pull request #64 from rmcdouga/20260317_Issue63_ConvertPdfService_toPS_fails
Fixes issue #63 - ConvertPdfService toPS() fails
2 parents 6d2ce96 + 3bfb014 commit c285ed4

6 files changed

Lines changed: 306 additions & 12 deletions

File tree

rest-services/client/src/main/java/com/_4point/aem/docservices/rest_services/client/convertPdf/RestServicesConvertPdfServiceAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public Document toPS(Document inPdfDoc, ToPSOptionsSpec toPSOptionsSpec) throws
227227
Objects.requireNonNull(inPdfDoc, "inPdfDoc argument cannot be null.");
228228
Objects.requireNonNull(toPSOptionsSpec, "ToPSOptionsSpec argument cannot be null.");
229229

230-
try (MultipartPayload payload = toImageRestClient.multipartPayloadBuilder()
230+
try (MultipartPayload payload = toPsRestClient.multipartPayloadBuilder()
231231
.add(PDF_PARAM, inPdfDoc, ContentType.APPLICATION_PDF)
232232
.addStringVersion(COLOR_PARAM, toPSOptionsSpec.getColor())
233233
.addStringVersion(FONT_INCLUSION_PARAM, toPSOptionsSpec.getFontInclusion())

rest-services/client/src/test/java/com/_4point/aem/docservices/rest_services/client/convertPdf/RestServicesConvertPdfServiceAdapterTest.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ public class RestServicesConvertPdfServiceAdapterTest {
123123
// private static final MediaType APPLICATION_PS = new MediaType("application", "postscript");
124124

125125
@Mock(stubOnly = true) RestClientFactory mockClientFactory;
126-
@Mock(stubOnly = true) RestClient mockClient;
126+
@Mock(stubOnly = true) RestClient mockClientToImage;
127+
@Mock(stubOnly = true) RestClient mockClientToPs;
127128
@Mock(stubOnly = true) MultipartPayload mockPayload;
128129
@Mock(stubOnly = true) MultipartPayload.Builder mockPayloadBuilder;
129130
@Mock(stubOnly = true) Response mockResponse;
@@ -137,7 +138,8 @@ public class RestServicesConvertPdfServiceAdapterTest {
137138

138139
@BeforeEach
139140
void setUp() throws Exception {
140-
when(mockClientFactory.apply(aemConfig.capture(), servicePath.capture(), correlationIdFn.capture())).thenReturn(mockClient);
141+
when(mockClientFactory.apply(aemConfig.capture(), Mockito.contains("ToImage"), correlationIdFn.capture())).thenReturn(mockClientToImage);
142+
when(mockClientFactory.apply(aemConfig.capture(), Mockito.contains("ToPS"), correlationIdFn.capture())).thenReturn(mockClientToPs);
141143
}
142144

143145
@Test
@@ -167,7 +169,7 @@ void testToPS_NullArguments() throws Exception {
167169
@Test
168170
void testToImage_HappyPath() throws Exception {
169171
byte[] responseData = "response Document Data".getBytes();
170-
setupMocks(setupMockResponse(responseData, ContentType.IMAGE_JPEG));
172+
setupMocks(mockClientToImage, setupMockResponse(responseData, ContentType.IMAGE_JPEG));
171173

172174
RestServicesConvertPdfServiceAdapter underTest = createAdapter(mockClientFactory);
173175

@@ -240,7 +242,7 @@ void testToImage_RestClientException() throws Exception {
240242
var underTest = createAdapter(mockClientFactory);
241243
when(toImageOptionsSpec.getImageConvertFormat()).thenReturn(ImageConvertFormat.TIFF);
242244

243-
var ex = mockForException(cause, ()->underTest.toImage(DUMMY_PDF, toImageOptionsSpec));
245+
var ex = mockForException(mockClientToImage, cause, ()->underTest.toImage(DUMMY_PDF, toImageOptionsSpec));
244246

245247
assertThat(ex, allOf(ExceptionMatchers.exceptionMsgContainsAll("Error while POSTing to server"),
246248
ExceptionMatchers.hasCause(cause)
@@ -255,7 +257,7 @@ void testToImage_IOException() throws Exception {
255257
var underTest = createAdapter(mockClientFactory);
256258
when(toImageOptionsSpec.getImageConvertFormat()).thenReturn(ImageConvertFormat.PNG);
257259

258-
var ex = mockForException(cause, ()->underTest.toImage(DUMMY_PDF, toImageOptionsSpec));
260+
var ex = mockForException(mockClientToImage, cause, ()->underTest.toImage(DUMMY_PDF, toImageOptionsSpec));
259261

260262
assertThat(ex, allOf(ExceptionMatchers.exceptionMsgContainsAll("I/O Error while securing document"),
261263
ExceptionMatchers.hasCause(cause)
@@ -265,7 +267,7 @@ void testToImage_IOException() throws Exception {
265267
@Test
266268
void testToPS_HappyPath() throws Exception {
267269
byte[] responseData = "response Document Data".getBytes();
268-
setupMocks(setupMockResponse(responseData, ContentType.APPLICATION_PS));
270+
setupMocks(mockClientToPs, setupMockResponse(responseData, ContentType.APPLICATION_PS));
269271

270272
RestServicesConvertPdfServiceAdapter underTest = createAdapter(mockClientFactory);
271273

@@ -347,7 +349,7 @@ void testToPS_RestClientException() throws Exception {
347349
var toPSOptionsSpec = Mockito.mock(ToPSOptionsSpec.class);
348350
var underTest = createAdapter(mockClientFactory);
349351

350-
var ex = mockForException(cause, ()->underTest.toPS(DUMMY_PDF, toPSOptionsSpec));
352+
var ex = mockForException(mockClientToPs, cause, ()->underTest.toPS(DUMMY_PDF, toPSOptionsSpec));
351353

352354
assertThat(ex, allOf(ExceptionMatchers.exceptionMsgContainsAll("Error while POSTing to server"),
353355
ExceptionMatchers.hasCause(cause)
@@ -361,14 +363,14 @@ void testToPS_IOException() throws Exception {
361363
var toPSOptionsSpec = Mockito.mock(ToPSOptionsSpec.class);
362364
var underTest = createAdapter(mockClientFactory);
363365

364-
var ex = mockForException(cause, ()->underTest.toPS(DUMMY_PDF, toPSOptionsSpec));
366+
var ex = mockForException(mockClientToPs, cause, ()->underTest.toPS(DUMMY_PDF, toPSOptionsSpec));
365367

366368
assertThat(ex, allOf(ExceptionMatchers.exceptionMsgContainsAll("I/O Error while securing document"),
367369
ExceptionMatchers.hasCause(cause)
368370
));
369371
}
370372

371-
private <T extends Exception> ConvertPdfServiceException mockForException(T exception, Executable test) throws Exception {
373+
private <T extends Exception> ConvertPdfServiceException mockForException(RestClient mockClient, T exception, Executable test) throws Exception {
372374

373375
Builder mockPayloadBuilder2 = Mockito.mock(Builder.class, Answers.RETURNS_SELF);
374376
when(mockClient.multipartPayloadBuilder()).thenReturn(mockPayloadBuilder2);
@@ -395,7 +397,7 @@ private static RestServicesConvertPdfServiceAdapter createAdapter(RestClientFact
395397
.build();
396398
}
397399

398-
private void setupMocks(Optional<Response> mockedResponse) throws RestClientException {
400+
private void setupMocks(RestClient mockClient, Optional<Response> mockedResponse) throws RestClientException {
399401
when(mockClient.multipartPayloadBuilder()).thenReturn(mockPayloadBuilder);
400402
when(mockPayloadBuilder.build()).thenReturn(mockPayload);
401403
when(mockPayload.postToServer(acceptableContentType.capture())).thenReturn(mockedResponse);
@@ -406,4 +408,5 @@ private Optional<Response> setupMockResponse(byte[] responseData, ContentType ex
406408
when(mockResponse.data()).thenReturn(new ByteArrayInputStream(responseData));
407409
return Optional.of(mockResponse);
408410
}
409-
}
411+
412+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com._4point.aem.docservices.rest_services.it_tests.client.convertPdf;
2+
3+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_MACHINE_AEM_TYPE;
4+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_USER;
5+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_USER_PASSWORD;
6+
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.List;
10+
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Disabled;
14+
import org.junit.jupiter.api.DisplayName;
15+
import org.junit.jupiter.api.Tag;
16+
import org.junit.jupiter.api.Test;
17+
18+
import com._4point.aem.docservices.rest_services.client.convertPdf.RestServicesConvertPdfServiceAdapter;
19+
import com._4point.aem.docservices.rest_services.client.jersey.JerseyRestClient;
20+
import com._4point.aem.docservices.rest_services.it_tests.AemInstance;
21+
import com._4point.aem.docservices.rest_services.it_tests.TestUtils;
22+
import com._4point.aem.fluentforms.api.Document;
23+
import com._4point.aem.fluentforms.impl.convertPdf.ConvertPdfServiceImpl;
24+
import com.adobe.fd.cpdf.api.enumeration.ImageConvertFormat;
25+
26+
@Disabled("This test is currently disabled because the ToImage service is not working correctly. It is work in progress.")
27+
@Tag("client-tests")
28+
public class ToImageTest {
29+
30+
private static final boolean SAVE_RESULTS = true;
31+
32+
private ConvertPdfServiceImpl underTest;
33+
34+
@BeforeAll
35+
static void setUpAll() throws Exception {
36+
AemInstance.AEM_1.prepareForTests();
37+
}
38+
39+
@BeforeEach
40+
void setUp() throws Exception {
41+
RestServicesConvertPdfServiceAdapter adapter = RestServicesConvertPdfServiceAdapter.builder(JerseyRestClient.factory())
42+
.machineName(AemInstance.AEM_1.aemHost())
43+
.port(AemInstance.AEM_1.aemPort())
44+
.basicAuthentication(TEST_USER, TEST_USER_PASSWORD)
45+
.useSsl(false)
46+
.aemServerType(TEST_MACHINE_AEM_TYPE)
47+
.build();
48+
49+
underTest = new ConvertPdfServiceImpl(adapter);
50+
}
51+
52+
@Test
53+
@DisplayName("Test toImage() Happy Path.")
54+
void testToImage() throws Exception {
55+
List<Document> result = underTest.toImage()
56+
.setImageConvertFormat(ImageConvertFormat.PNG)
57+
.executeOn(Files.readAllBytes(TestUtils.LOCAL_SAMPLE_FORM_PDF));
58+
59+
if (SAVE_RESULTS) {
60+
for (int i = 0; i < result.size(); i++) {
61+
Document imageDoc = result.get(i);
62+
byte[] imageBytes = imageDoc.getInputStream().readAllBytes();
63+
String outputFileName = String.format("ToImageResult_Page%d.png", i + 1);
64+
Path outputPath = TestUtils.ACTUAL_RESULTS_DIR.resolve(outputFileName);
65+
Files.write(outputPath, imageBytes);
66+
System.out.println("Saved result to " + outputPath.toAbsolutePath());
67+
}
68+
}
69+
}
70+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com._4point.aem.docservices.rest_services.it_tests.client.convertPdf;
2+
3+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_MACHINE_AEM_TYPE;
4+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_USER;
5+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_USER_PASSWORD;
6+
import static org.hamcrest.CoreMatchers.containsString;
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.DisplayName;
15+
import org.junit.jupiter.api.Tag;
16+
import org.junit.jupiter.api.Test;
17+
18+
import com._4point.aem.docservices.rest_services.client.convertPdf.RestServicesConvertPdfServiceAdapter;
19+
import com._4point.aem.docservices.rest_services.client.jersey.JerseyRestClient;
20+
import com._4point.aem.docservices.rest_services.it_tests.AemInstance;
21+
import com._4point.aem.docservices.rest_services.it_tests.ByteArrayString;
22+
import com._4point.aem.docservices.rest_services.it_tests.TestUtils;
23+
import com._4point.aem.fluentforms.api.Document;
24+
import com._4point.aem.fluentforms.impl.convertPdf.ConvertPdfServiceImpl;
25+
26+
@Tag("client-tests")
27+
public class ToPSTest {
28+
29+
private static final boolean SAVE_RESULTS = false;
30+
31+
private ConvertPdfServiceImpl underTest;
32+
33+
@BeforeAll
34+
static void setUpAll() throws Exception {
35+
AemInstance.AEM_1.prepareForTests();
36+
}
37+
38+
@BeforeEach
39+
void setUp() throws Exception {
40+
RestServicesConvertPdfServiceAdapter adapter = RestServicesConvertPdfServiceAdapter.builder(JerseyRestClient.factory())
41+
.machineName(AemInstance.AEM_1.aemHost())
42+
.port(AemInstance.AEM_1.aemPort())
43+
.basicAuthentication(TEST_USER, TEST_USER_PASSWORD)
44+
.useSsl(false)
45+
.aemServerType(TEST_MACHINE_AEM_TYPE)
46+
.build();
47+
48+
underTest = new ConvertPdfServiceImpl(adapter);
49+
}
50+
51+
@Test
52+
@DisplayName("Test toPS() Happy Path.")
53+
void testToPS() throws Exception {
54+
55+
Document result = underTest.toPS()
56+
.executeOn(Files.readAllBytes(TestUtils.LOCAL_SAMPLE_FORM_PDF));
57+
58+
byte[] resultBytes = result.getInputStream().readAllBytes();
59+
60+
if (SAVE_RESULTS) {
61+
Path outputPath = TestUtils.ACTUAL_RESULTS_DIR.resolve("ToPSResult.ps");
62+
Files.write(outputPath, resultBytes);
63+
System.out.println("Saved result to " + outputPath.toAbsolutePath());
64+
}
65+
66+
assertThat("Expected PostScript to be returned.", ByteArrayString.toString(resultBytes, 10), containsString("%, !, P, S, -, A, d, o, b, e"));
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com._4point.aem.docservices.rest_services.it_tests.server.convertPdf;
2+
3+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.LOCAL_SAMPLE_FORM_WITHOUT_DATA_PDF;
4+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_MACHINE_AEM_TYPE;
5+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_USER;
6+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.TEST_USER_PASSWORD;
7+
8+
import java.nio.file.Files;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
13+
import org.apache.commons.io.IOUtils;
14+
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
15+
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
16+
import org.glassfish.jersey.media.multipart.MultiPartFeature;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Disabled;
20+
import org.junit.jupiter.api.Tag;
21+
import org.junit.jupiter.api.Test;
22+
23+
import com._4point.aem.docservices.rest_services.it_tests.AemInstance;
24+
import com._4point.aem.docservices.rest_services.it_tests.TestUtils;
25+
26+
import jakarta.ws.rs.client.ClientBuilder;
27+
import jakarta.ws.rs.client.Entity;
28+
import jakarta.ws.rs.client.WebTarget;
29+
import jakarta.ws.rs.core.MediaType;
30+
import jakarta.ws.rs.core.Response;
31+
32+
@Disabled("This test is currently disabled because the ToImage service is not working correctly. It is work in progress.")
33+
@Tag("server-tests")
34+
public class ToImageTest {
35+
private static final String TO_IMAGE_URL = "http://" + AemInstance.AEM_1.aemHost() + ":" + AemInstance.AEM_1.aemPort() + TEST_MACHINE_AEM_TYPE.pathPrefix() + "/services/ConvertPdfService/ToImage";
36+
37+
private static final String PDF_PARAM = "inPdfDoc";
38+
private static final String IMAGE_CONVERT_FORMAT_PARAM = "toImageOptionsSpec.imageConvertFormat";
39+
40+
private static final boolean SAVE_RESULTS = true;
41+
42+
@BeforeAll
43+
static void setUpAll() throws Exception {
44+
AemInstance.AEM_1.prepareForTests();
45+
}
46+
47+
private WebTarget target;
48+
49+
@BeforeEach
50+
void setUp() throws Exception {
51+
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(TEST_USER, TEST_USER_PASSWORD); // default AEM passwords
52+
target = ClientBuilder.newClient().register(feature).register(MultiPartFeature.class)
53+
.target(TO_IMAGE_URL);
54+
}
55+
56+
@Test
57+
void testToImage_MinArgs() throws Exception {
58+
byte[] samplePdf = Files.readAllBytes(LOCAL_SAMPLE_FORM_WITHOUT_DATA_PDF);
59+
try (final FormDataMultiPart multipart = new FormDataMultiPart()) {
60+
multipart.field(PDF_PARAM, samplePdf, new MediaType("application", "pdf"))
61+
.field(IMAGE_CONVERT_FORMAT_PARAM, "TIFF");
62+
63+
Response result = target.request()
64+
.accept(MediaType.valueOf("image/png"))
65+
.post(Entity.entity(multipart, multipart.getMediaType()));
66+
67+
assertTrue(result.hasEntity(), "Expected the response to have an entity.");
68+
assertEquals(Response.Status.OK.getStatusCode(), result.getStatus(), () -> "Expected response to be 'OK', entity='" + TestUtils.readEntityToString(result) + "'.");
69+
70+
byte[] resultBytes = result.readEntity(byte[].class);
71+
if (SAVE_RESULTS) {
72+
IOUtils.write(resultBytes, Files.newOutputStream(TestUtils.ACTUAL_RESULTS_DIR.resolve("testToImage_MinArgs_result.tif")));
73+
}
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
 (0)