Skip to content

Commit 60edd90

Browse files
committed
✨ Added new tests for GeneratePrintedOutput() in OutputService
1 parent 40da6ee commit 60edd90

2 files changed

Lines changed: 204 additions & 13 deletions

File tree

rest-services/it.tests/src/test/java/com/_4point/aem/docservices/rest_services/it_tests/TestUtils.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66

77
import java.io.IOException;
88
import java.io.InputStream;
9+
import java.io.OutputStream;
910
import java.nio.charset.StandardCharsets;
1011
import java.nio.file.Files;
1112
import java.nio.file.Path;
1213
import java.nio.file.Paths;
1314

1415
import jakarta.ws.rs.core.Response;
1516

16-
import org.apache.commons.io.IOUtils;
1717
import org.junit.jupiter.api.function.Executable;
1818

1919
import com._4point.aem.docservices.rest_services.client.helpers.AemServerType;
20-
import com._4point.aem.docservices.rest_services.it_tests.Pdf.PdfException;
2120

2221
public class TestUtils {
2322

@@ -83,28 +82,23 @@ public class TestUtils {
8382

8483
private static final boolean SAVE_RESULTS = false;
8584

86-
// private static Path getPath(String name) {
87-
// try {
88-
// return Paths.get(classLoader.getResource(name).toURI());
89-
// } catch (URISyntaxException e) {
90-
// throw new IllegalStateException("getResource returned invalid URI. (This should never happen!)", e);
91-
// }
92-
// }
93-
//
9485
public static String readEntityToString(Response result) {
9586
try {
96-
return IOUtils.toString((InputStream)result.getEntity(), StandardCharsets.UTF_8);
87+
return new String(((InputStream) result.getEntity()).readAllBytes(), StandardCharsets.UTF_8);
9788
} catch (IOException e) {
9889
throw new IllegalStateException("Exception while reading response stream.", e);
9990
}
10091
}
10192

10293
@SuppressWarnings("unused")
10394
public static void validatePdfResult(byte[] pdfBytes, String testResultFilename, boolean dynamic, boolean interactive, boolean hasRights)
104-
throws IOException, Exception, PdfException {
95+
throws Exception {
10596
if (SAVE_RESULTS == true) {
106-
IOUtils.write(pdfBytes, Files.newOutputStream(ACTUAL_RESULTS_DIR.resolve(testResultFilename)));
97+
try (OutputStream saveStream = Files.newOutputStream(ACTUAL_RESULTS_DIR.resolve(testResultFilename))) {
98+
saveStream.write(pdfBytes);
99+
}
107100
}
101+
108102
assertThat("Expected a PDF to be returned.", ByteArrayString.toString(pdfBytes, 8), containsString("%, P, D, F, -, 1, ., 7"));
109103
try (Pdf pdf = Pdf.from(pdfBytes)) {
110104
Executable dynamicTest = dynamic ? ()->assertTrue(pdf.isDynamic(), "Expected Pdf to be dynamic.") : ()->assertFalse(pdf.isDynamic(), "Expected Pdf to be static.");
@@ -114,6 +108,25 @@ public static void validatePdfResult(byte[] pdfBytes, String testResultFilename,
114108
}
115109
}
116110

111+
@SuppressWarnings("unused")
112+
public static void validatePrintedResult(byte[] pdfBytes, String testResultFilename) throws IOException {
113+
if (SAVE_RESULTS == true) {
114+
try (OutputStream saveStream = Files.newOutputStream(ACTUAL_RESULTS_DIR.resolve(testResultFilename))) {
115+
saveStream.write(pdfBytes);
116+
}
117+
}
118+
119+
if (testResultFilename.endsWith(".pcl")) {
120+
assertThat("Expected PCL to be returned.", ByteArrayString.toString(pdfBytes, 60 ), containsString("@, P, J, L, , E, N, T, E, R, , L, A, N, G, U, A, G, E, , =, , P, C, L"));
121+
} else if (testResultFilename.endsWith(".ps")) {
122+
assertThat("Expected Postscript to be returned.", ByteArrayString.toString(pdfBytes, 62 ), containsString("%, !, P, S, -, A, d, o, b, e"));
123+
} else if (testResultFilename.endsWith(".zpl")) {
124+
assertThat("Expected ZPL to be returned.", ByteArrayString.toString(pdfBytes, 10 ), containsString("^, X, A"));
125+
} else {
126+
throw new IllegalArgumentException("Unknown file type for testResultFilename: " + testResultFilename);
127+
}
128+
}
129+
117130
}
118131

119132

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com._4point.aem.docservices.rest_services.it_tests.client.output;
2+
3+
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.*;
4+
5+
import java.net.URL;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.Locale;
9+
10+
import org.junit.jupiter.api.BeforeAll;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Disabled;
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Tag;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.condition.EnabledIf;
17+
import org.junit.jupiter.api.condition.EnabledOnOs;
18+
import org.junit.jupiter.api.condition.OS;
19+
20+
import com._4point.aem.docservices.rest_services.client.jersey.JerseyRestClient;
21+
import com._4point.aem.docservices.rest_services.client.output.RestServicesOutputServiceAdapter;
22+
import com._4point.aem.docservices.rest_services.it_tests.AemInstance;
23+
import com._4point.aem.docservices.rest_services.it_tests.TestUtils;
24+
import com._4point.aem.fluentforms.api.Document;
25+
import com._4point.aem.fluentforms.api.PathOrUrl;
26+
import com._4point.aem.fluentforms.api.output.OutputService;
27+
import com._4point.aem.fluentforms.api.output.PrintConfig;
28+
import com._4point.aem.fluentforms.impl.SimpleDocumentFactoryImpl;
29+
import com._4point.aem.fluentforms.impl.UsageContext;
30+
import com._4point.aem.fluentforms.impl.output.OutputServiceImpl;
31+
import com.adobe.fd.output.api.PaginationOverride;
32+
33+
@Tag("client-tests")
34+
class GeneratePrintedOutputTest {
35+
36+
private static final String CRX_CONTENT_ROOT = "crx:/content/dam/formsanddocuments/sample-forms";
37+
38+
private OutputService underTest;
39+
40+
@BeforeAll
41+
static void setUpAll() throws Exception {
42+
AemInstance.AEM_1.prepareForTests();
43+
}
44+
45+
@BeforeEach
46+
void setUp() throws Exception {
47+
RestServicesOutputServiceAdapter adapter = RestServicesOutputServiceAdapter.builder(JerseyRestClient.factory())
48+
.machineName(AemInstance.AEM_1.aemHost())
49+
.port(AemInstance.AEM_1.aemPort())
50+
.basicAuthentication(TEST_USER, TEST_USER_PASSWORD)
51+
.useSsl(false)
52+
.aemServerType(TEST_MACHINE_AEM_TYPE)
53+
.build();
54+
55+
underTest = new OutputServiceImpl(adapter, UsageContext.CLIENT_SIDE);
56+
}
57+
58+
@Test
59+
@DisplayName("Test generatePrintedOutput() Just Form and Data.")
60+
void testgeneratePrintedOutput_JustFormAndData() throws Exception {
61+
Document pdfResult = underTest.generatePrintedOutput()
62+
.setPrintConfig(PrintConfig.HP_PCL_5e)
63+
.executeOn(REMOTE_SAMPLE_FORM_XDP, SimpleDocumentFactoryImpl.getFactory().create(LOCAL_SAMPLE_FORM_DATA_XML));
64+
65+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "generatePrintedOutput_JustFormAndData.pcl");
66+
}
67+
68+
@Test
69+
@DisplayName("Test generatePrintedOutput() Just Form Document and Data.")
70+
void testgeneratePrintedOutput_JustFormDocAndData() throws Exception {
71+
Document pdfResult = underTest.generatePrintedOutput()
72+
.setPrintConfig(PrintConfig.Generic_PS_L3)
73+
.executeOn(SimpleDocumentFactoryImpl.getFactory().create(LOCAL_SAMPLE_FORM_XDP), SimpleDocumentFactoryImpl.getFactory().create(LOCAL_SAMPLE_FORM_DATA_XML));
74+
75+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "testgeneratePrintedOutput_JustFormDocAndData.ps");
76+
}
77+
78+
@Test
79+
@DisplayName("Test generatePrintedOutput() CRX Form and Data.")
80+
void testgeneratePrintedOutput_CRXFormAndData() throws Exception {
81+
Document pdfResult = underTest.generatePrintedOutput()
82+
.setPrintConfig(PrintConfig.ZPL600)
83+
.setContentRoot(PathOrUrl.from(CRX_CONTENT_ROOT))
84+
.executeOn(REMOTE_SAMPLE_FORM_XDP.getFileName(), SimpleDocumentFactoryImpl.getFactory().create(LOCAL_SAMPLE_FORM_DATA_XML));
85+
86+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "generatePrintedOutput_CRXFormAndData.zpl");
87+
}
88+
89+
@Test
90+
@DisplayName("Test generatePrintedOutput() All Arguments.")
91+
void testgeneratePrintedOutput_AllArgs() throws Exception {
92+
Path contentRoot = REMOTE_SAMPLE_FORM_XDP.getParent();
93+
// Path debugDir = null;
94+
95+
Document pdfResult = underTest.generatePrintedOutput()
96+
.setContentRoot(contentRoot)
97+
.setCopies(2)
98+
// .setDebugDir(debugDir)
99+
.setLocale(Locale.CANADA_FRENCH)
100+
.setPaginationOverride(PaginationOverride.duplexShortEdge)
101+
.setPrintConfig(PrintConfig.HP_PCL_5e)
102+
.xci()
103+
.embedPclFonts(true)
104+
.done()
105+
// TODO: Since contentRoot is set, the full path should not be necessary. Try changing to just the filename.
106+
.executeOn(REMOTE_SAMPLE_FORM_XDP, SimpleDocumentFactoryImpl.getFactory().create(LOCAL_SAMPLE_FORM_DATA_XML));
107+
108+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "generatePrintedOutput_AllArgs.pcl");
109+
}
110+
111+
@Disabled("Sample artwork fails when sent as Document for some reason, but works when sent as a reference.")
112+
@Test
113+
@DisplayName("Test generatePrintedOutput() Just Form Doc. FluentFormsAPI Issue #15")
114+
void testgeneratePrintedOutput_JustFormDocIssue15() throws Exception {
115+
Document pdfResult = underTest.generatePrintedOutput()
116+
.setPrintConfig(PrintConfig.HP_PCL_5e)
117+
.executeOn(SimpleDocumentFactoryImpl.getFactory().create(LOCAL_SAMPLE_ARTWORK_PDF));
118+
119+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "generatePrintedOutput_JustFormDocIssue15.pcl");
120+
}
121+
122+
@Test
123+
@DisplayName("Test generatePrintedOutput() Just Form. FluentFormsAPI Issue #15")
124+
void testgeneratePrintedOutput_JustFormIssue15() throws Exception {
125+
Document pdfResult = underTest.generatePrintedOutput()
126+
.setPrintConfig(PrintConfig.Generic_PS_L3)
127+
.executeOn(REMOTE_SAMPLE_ARTWORK_PDF);
128+
129+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "testgeneratePrintedOutput_JustFormIssue15.ps");
130+
}
131+
132+
@Test
133+
@DisplayName("Test generatePrintedOutput() for issue #32.")
134+
void testgeneratePrintedOutput_MixedContentRoot_Issue32() throws Exception {
135+
Path contentRoot = REMOTE_SAMPLE_FORM_XDP.getParent().getParent(); // Get a content root two directories up
136+
Path template = REMOTE_SAMPLE_FORM_XDP.getParent().resolve(REMOTE_SAMPLE_FORM_XDP.getFileName()); // Get a template that contains a parent directory
137+
138+
Document pdfResult = underTest.generatePrintedOutput()
139+
.setPrintConfig(PrintConfig.HP_PCL_5e)
140+
.setContentRoot(contentRoot)
141+
.executeOn(template, SimpleDocumentFactoryImpl.getFactory().create(LOCAL_SAMPLE_FORM_DATA_XML));
142+
143+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "testgeneratePrintedOutput_MixedContentRoot_Issue32.pcl");
144+
}
145+
146+
147+
@Test
148+
@EnabledOnOs(OS.WINDOWS)
149+
@EnabledIf("targetIsLinux")
150+
@DisplayName("Test generatePrintedOutput() Just Form with Linux Path. From Windows Machine")
151+
void testgeneratePrintedOutput_JustForm() throws Exception {
152+
URL formPath = new URL("file:/opt/adobe/ff_it_files/SampleForm.xdp");
153+
Document pdfResult = underTest.generatePrintedOutput()
154+
.setPrintConfig(PrintConfig.HP_PCL_5e)
155+
.executeOn(formPath);
156+
157+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "testgeneratePrintedOutput_JustForm.pcl");
158+
}
159+
160+
@Test
161+
@EnabledOnOs(OS.WINDOWS)
162+
@EnabledIf("targetIsLinux")
163+
@DisplayName("Test generatePrintedOutput() Just Form with Linux ContentRoot. From Windows Machine")
164+
void testgeneratePrintedOutput_JustFormContentRoot() throws Exception {
165+
Path contentPath = Paths.get("\\","opt", "adobe", "ff_it_files");
166+
Path formPath = Paths.get("SampleForm.xdp");
167+
Document pdfResult = underTest.generatePrintedOutput()
168+
.setPrintConfig(PrintConfig.Generic_PS_L3)
169+
.setContentRoot(contentPath)
170+
.executeOn(formPath);
171+
172+
TestUtils.validatePrintedResult(pdfResult.getInlineData(), "testgeneratePrintedOutput_JustFormContentRoot.ps");
173+
}
174+
175+
boolean targetIsLinux() {
176+
return AemInstance.AEM_1.isLinux();
177+
}
178+
}

0 commit comments

Comments
 (0)