Skip to content

Commit 560cada

Browse files
committed
🐛 Fixed tests so that they not pass
- Fixed bug where we were passing invalid PDF - Altered tests to use non-deprecated calls - Adjusted BadData test to test for different exception message that os now generated from RestClient code.
1 parent 5b4310a commit 560cada

2 files changed

Lines changed: 38 additions & 35 deletions

File tree

  • rest-services

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public interface Builder {
7676
Builder add(String fieldName, InputStream fieldData, ContentType contentType);
7777
default Builder add(String fieldName, Document document) {
7878
try {
79-
return add(fieldName, document.getInputStream(), ContentType.of(document.getContentType()));
79+
String contentType = document.getContentType();
80+
return add(fieldName, document.getInputStream(), contentType != null ? ContentType.of(contentType) : ContentType.APPLICATION_OCTET_STREAM);
8081
} catch (IOException e) {
8182
throw new UncheckedIOException(e);
8283
}

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,28 @@
44
import static org.hamcrest.Matchers.*;
55
import static org.junit.jupiter.api.Assertions.*;
66
import static com._4point.aem.docservices.rest_services.it_tests.TestUtils.*;
7+
import static com._4point.testing.matchers.javalang.ExceptionMatchers.*;
78

9+
import java.io.IOException;
10+
import java.nio.file.Files;
811
import java.util.Collections;
912
import java.util.HashMap;
1013
import java.util.Map;
1114
import java.util.Map.Entry;
1215

13-
import jakarta.ws.rs.core.MediaType;
14-
1516
import org.junit.jupiter.api.BeforeAll;
1617
import org.junit.jupiter.api.BeforeEach;
1718
import org.junit.jupiter.api.DisplayName;
1819
import org.junit.jupiter.api.Tag;
1920
import org.junit.jupiter.api.Test;
2021

22+
import com._4point.aem.docservices.rest_services.client.RestClient.ContentType;
2123
import com._4point.aem.docservices.rest_services.client.assembler.RestServicesDocAssemblerServiceAdapter;
2224
import com._4point.aem.docservices.rest_services.client.helpers.XmlDocument;
2325
import com._4point.aem.docservices.rest_services.client.jersey.JerseyRestClient;
2426
import com._4point.aem.docservices.rest_services.it_tests.AemInstance;
2527
import com._4point.aem.docservices.rest_services.it_tests.Pdf;
28+
import com._4point.aem.docservices.rest_services.it_tests.Pdf.PdfException;
2629
import com._4point.aem.fluentforms.api.Document;
2730
import com._4point.aem.fluentforms.api.DocumentFactory;
2831
import com._4point.aem.fluentforms.api.assembler.AssemblerResult;
@@ -44,7 +47,6 @@
4447
@Tag("client-tests")
4548
public class AssembleDocumentsTest {
4649
private static final DocumentFactory DOC_FACTORY = SimpleDocumentFactoryImpl.getFactory();
47-
private static final MediaType APPLICATION_PDF = new MediaType("application", "pdf");
4850
private AssemblerService underTest;
4951

5052
@BeforeAll
@@ -68,32 +70,37 @@ void setUp() throws Exception {
6870
@Test
6971
@DisplayName("Test AssembleDocuments() Happy Path.")
7072
void testAssembleDocuments() throws Exception {
71-
byte[] samplePdf1 = SAMPLE_FORM_PDF.toString().getBytes();
72-
byte[] samplePdf2 = SAMPLE_FORM_PDF.toString().getBytes();
73-
Map<String, Object> inputs = new HashMap<String, Object>();
74-
inputs.put("File0.pdf", DOC_FACTORY.create(samplePdf1));
75-
inputs.put("File1.pdf", DOC_FACTORY.create(samplePdf2));
76-
77-
AssemblerResult assemblerResult = underTest.invoke().executeOn(DOC_FACTORY.create(SAMPLE_FORM_DDX), inputs);
78-
Map<String, Document> resultDocument = assemblerResult.getDocuments();
79-
byte[] resultByte = null;
73+
byte[] samplePdf1 = Files.readAllBytes(SAMPLE_FORM_WITHOUT_DATA_PDF);
74+
byte[] samplePdf2 = Files.readAllBytes(SAMPLE_FORM_WITHOUT_DATA_PDF);
75+
76+
AssemblerResult assemblerResult = underTest.invoke()
77+
.add("File0.pdf", DOC_FACTORY.create(samplePdf1))
78+
.add("File1.pdf", DOC_FACTORY.create(samplePdf2).setContentType("application/pdf"))
79+
.executeOn(DOC_FACTORY.create(SAMPLE_FORM_DDX));
80+
81+
Map<String, Document> resultDocuments = assemblerResult.getDocuments();
82+
validateResultMap(resultDocuments);
83+
}
84+
85+
private void validateResultMap(Map<String, Document> resultDocument) throws IOException, PdfException {
8086
for(Entry<String, Document> entry: resultDocument.entrySet()){
8187
if(entry.getKey().equals("concatenatedPDF.pdf")) {
82-
resultByte = entry.getValue().getInlineData();
88+
byte[] resultByte = entry.getValue().getInlineData();
8389
assertNotNull(resultByte);
84-
assertEquals(APPLICATION_PDF.toString(), entry.getValue().getContentType());
90+
Pdf pdfResult = Pdf.from(resultByte); // Validate that it's a real PDF.
91+
assertAll(
92+
()->assertEquals(ContentType.APPLICATION_PDF.contentType(), entry.getValue().getContentType()),
93+
()->assertThat(pdfResult.getProducer(), not(emptyOrNullString()))
94+
);
8595
}
8696
}
8797
}
8898

8999
@Test
90100
@DisplayName("Test AssembleDocuments() with all arguments Happy Path.")
91101
void testAssembleDocuments_withAllArgs() throws Exception {
92-
byte[] samplePdf1 = SAMPLE_FORM_PDF.toString().getBytes();
93-
byte[] samplePdf2 = SAMPLE_FORM_PDF.toString().getBytes();
94-
Map<String, Object> inputs = new HashMap<String, Object>();
95-
inputs.put("File0.pdf", DOC_FACTORY.create(samplePdf1));
96-
inputs.put("File1.pdf", DOC_FACTORY.create(samplePdf2));
102+
byte[] samplePdf1 = Files.readAllBytes(SAMPLE_FORM_WITHOUT_DATA_PDF);
103+
byte[] samplePdf2 = Files.readAllBytes(SAMPLE_FORM_WITHOUT_DATA_PDF);
97104

98105
AssemblerResult assemblerResult = underTest.invoke()
99106
.setDefaultStyle("")
@@ -102,32 +109,27 @@ void testAssembleDocuments_withAllArgs() throws Exception {
102109
.setLogLevel(LogLevel.ALL)
103110
.setTakeOwnership(Boolean.FALSE)
104111
.setValidateOnly(Boolean.FALSE)
105-
.executeOn(DOC_FACTORY.create(SAMPLE_FORM_DDX), inputs);
106-
Map<String, Document> resultDocument = assemblerResult.getDocuments();
107-
byte[] resultByte = null;
108-
for(Entry<String, Document> entry: resultDocument.entrySet()){
109-
if(entry.getKey().equals("concatenatedPDF.pdf")) {
110-
resultByte = entry.getValue().getInlineData();
111-
assertNotNull(resultByte);
112-
assertEquals(APPLICATION_PDF.toString(), entry.getValue().getContentType());
113-
}
114-
}
112+
.add("File0.pdf", DOC_FACTORY.create(samplePdf1).setContentType("application/pdf"))
113+
.add("File1.pdf", DOC_FACTORY.create(samplePdf2))
114+
.executeOn(DOC_FACTORY.create(SAMPLE_FORM_DDX));
115+
116+
Map<String, Document> resultDocuments = assemblerResult.getDocuments();
117+
validateResultMap(resultDocuments);
115118
}
116119

117120
@Test
118121
@DisplayName("Test testAssembleDocuments with bad data.")
119122
void testAssembleDocuments_BadData() throws Exception {
120-
byte[] samplePdf1 = SAMPLE_FORM_PDF.toString().getBytes();
121-
byte[] samplePdf2 = SAMPLE_FORM_PDF.toString().getBytes();
123+
byte[] samplePdf1 = Files.readAllBytes(SAMPLE_FORM_PDF);
124+
byte[] samplePdf2 = Files.readAllBytes(SAMPLE_FORM_PDF);
122125
Map<String, Object> sourceDocuments = new HashMap<String, Object>();
123126
sourceDocuments.put("File0.pdf", DOC_FACTORY.create(samplePdf1));
124127
sourceDocuments.put("File1.pdf", DOC_FACTORY.create(samplePdf2));
125128
AssemblerOptionsSpecImpl assemblerOptionsSpecImpl = new AssemblerOptionsSpecImpl();
126129
assemblerOptionsSpecImpl.setLogLevel(LogLevel.ALL);
127130
AssemblerServiceException ex = assertThrows(AssemblerServiceException.class, ()->underTest.invoke(DOC_FACTORY.create(SAMPLE_FORM_DOCX), sourceDocuments, assemblerOptionsSpecImpl));
128-
String msg = ex.getMessage();
129-
assertNotNull(msg);
130-
assertTrue(msg.contains("Call to server failed"));
131+
132+
assertThat(ex, exceptionMsgContainsAll("Error while POSTing to server"));
131133
}
132134

133135
@Test

0 commit comments

Comments
 (0)