11package com ._4point .aem .docservices .rest_services .it_tests .server .assembler ;
2+
3+ import static org .hamcrest .Matchers .*;
4+ import static org .hamcrest .MatcherAssert .assertThat ;
25import static com ._4point .aem .docservices .rest_services .it_tests .TestUtils .*;
36import static org .junit .jupiter .api .Assertions .*;
47
8+ import java .io .IOException ;
59import java .io .InputStream ;
610import java .nio .file .Files ;
7- import java .util .HashMap ;
811import java .util .Map ;
912import java .util .Map .Entry ;
1013
2932import com ._4point .aem .fluentforms .api .Document ;
3033import com ._4point .aem .fluentforms .api .assembler .AssemblerResult ;
3134import com ._4point .aem .fluentforms .api .assembler .LogLevel ;
35+ import com ._4point .testing .matchers .aem .Pdf ;
36+ import com ._4point .testing .matchers .aem .Pdf .PdfException ;
3237
3338@ Tag ("server-tests" )
3439public class AssembleDocumentsTest {
@@ -47,7 +52,7 @@ public class AssembleDocumentsTest {
4752 private static final String SOURCE_DOCUMENT_KEY = "sourceDocumentMap.key" ;
4853 private static final String SOURCE_DOCUMENT_VALUE = "sourceDocumentMap.value" ;
4954
50- private static final boolean SAVE_RESULTS = false ;
55+ private static final boolean SAVE_RESULTS = true ;
5156
5257 private WebTarget target ;
5358
@@ -65,8 +70,8 @@ void setUp() throws Exception {
6570
6671 @ Test
6772 void testAssembleDocuments_AllArgs () throws Exception {
68- byte [] samplePdf1 = SAMPLE_FORM_PDF . toString (). getBytes ( );
69- byte [] samplePdf2 = SAMPLE_FORM_PDF . toString (). getBytes ( );
73+ byte [] samplePdf1 = Files . readAllBytes ( SAMPLE_FORM_WITHOUT_DATA_PDF );
74+ byte [] samplePdf2 = Files . readAllBytes ( SAMPLE_FORM_WITHOUT_DATA_PDF );
7075 try (final FormDataMultiPart multipart = new FormDataMultiPart ()) {
7176 multipart .field (DATA_PARAM_NAME , SAMPLE_FORM_DDX .toFile (), MediaType .APPLICATION_XML_TYPE )
7277 .field (IS_FAIL_ON_ERROR , String .valueOf (Boolean .FALSE ))
@@ -76,13 +81,11 @@ void testAssembleDocuments_AllArgs() throws Exception {
7681 .field (DEFAULT_STYLE , "" )
7782 .field (FIRST_BATES_NUMBER , Integer .toString (0 ));
7883
79- Map <String , Object > inputs = new HashMap <String , Object >();
80- inputs .put ("File0.pdf" , samplePdf1 );
81- inputs .put ("File1.pdf" , samplePdf2 );
82- inputs .forEach ((docName , doc ) -> {
83- multipart .field (SOURCE_DOCUMENT_KEY , docName );
84- multipart .field (SOURCE_DOCUMENT_VALUE , (byte []) doc , APPLICATION_PDF );
85- });
84+ Map .of ("File0.pdf" , samplePdf1 , "File1.pdf" , samplePdf2 )
85+ .forEach ((docName , doc ) -> {
86+ multipart .field (SOURCE_DOCUMENT_KEY , docName );
87+ multipart .field (SOURCE_DOCUMENT_VALUE , (byte []) doc , APPLICATION_PDF );
88+ });
8689
8790 Response result = target .request ()
8891 .accept (APPLICATION_XML )
@@ -91,15 +94,7 @@ void testAssembleDocuments_AllArgs() throws Exception {
9194 assertEquals (Response .Status .OK .getStatusCode (), result .getStatus (), () -> "Expected response to be 'OK', entity='" + TestUtils .readEntityToString (result ) + "'." );
9295
9396 AssemblerResult assemblerResult = AssemblerServiceTestHelper .convertXmlToAssemblerResult ((InputStream ) result .getEntity ());
94- Map <String , Document > resultDocument = assemblerResult .getDocuments ();
95- byte [] resultByte = null ;
96- for (Entry <String , Document > entry : resultDocument .entrySet ()) {
97- if (entry .getKey ().equals ("concatenatedPDF.pdf" )) {
98- resultByte = entry .getValue ().getInlineData ();
99- assertNotNull (resultByte );
100- assertEquals (APPLICATION_PDF .toString (),entry .getValue ().getContentType ());
101- }
102- }
97+ byte [] resultByte = validateAssemblerResult (assemblerResult );
10398 if (SAVE_RESULTS ) {
10499 IOUtils .write (resultByte , Files .newOutputStream (TestUtils .ACTUAL_RESULTS_DIR .resolve ("testAssembleDocumentPDFWithAllArguments_result.pdf" )));
105100 }
@@ -109,41 +104,43 @@ void testAssembleDocuments_AllArgs() throws Exception {
109104
110105 @ Test
111106 void testAssembleDocuments_JustWithDDXandInputDocuments () throws Exception {
112- byte [] samplePdf1 = SAMPLE_FORM_PDF . toString (). getBytes ( );
113- byte [] samplePdf2 = SAMPLE_FORM_PDF . toString (). getBytes ( );
107+ byte [] samplePdf1 = Files . readAllBytes ( SAMPLE_FORM_WITHOUT_DATA_PDF );
108+ byte [] samplePdf2 = Files . readAllBytes ( SAMPLE_FORM_WITHOUT_DATA_PDF );
114109 try (final FormDataMultiPart multipart = new FormDataMultiPart ()) {
115110 multipart .field (DATA_PARAM_NAME , SAMPLE_FORM_DDX .toFile (), MediaType .APPLICATION_XML_TYPE );
116- Map <String , Object > inputs = new HashMap <String , Object >();
117- inputs .put ("File0.pdf" , samplePdf1 );
118- inputs .put ("File1.pdf" , samplePdf2 );
119- inputs .forEach ((docName , doc ) -> {
120- multipart .field (SOURCE_DOCUMENT_KEY , docName );
121- multipart .field (SOURCE_DOCUMENT_VALUE , (byte []) doc , APPLICATION_PDF );
122- });
111+ Map .of ("File0.pdf" , samplePdf1 , "File1.pdf" , samplePdf2 )
112+ .forEach ((docName , doc ) -> {
113+ multipart .field (SOURCE_DOCUMENT_KEY , docName );
114+ multipart .field (SOURCE_DOCUMENT_VALUE , (byte []) doc , APPLICATION_PDF );
115+ });
123116 Response result = target .request ()
124117 .accept (APPLICATION_XML )
125118 .post (Entity .entity (multipart , multipart .getMediaType ()));
126119 assertTrue (result .hasEntity (), "Expected the response to have an entity." );
127120 assertEquals (Response .Status .OK .getStatusCode (), result .getStatus (), () -> "Expected response to be 'OK', entity='" + TestUtils .readEntityToString (result ) + "'." );
128121
129122 AssemblerResult assemblerResult = AssemblerServiceTestHelper .convertXmlToAssemblerResult ((InputStream ) result .getEntity ());
130- Map <String , Document > sourceDocuments = assemblerResult .getDocuments ();
131- byte [] resultByte = null ;
132- for (Entry <String , Document > entry : sourceDocuments .entrySet ()) {
133- if (entry .getKey ().equals ("concatenatedPDF.pdf" )) {
134- resultByte = entry .getValue ().getInlineData ();
135- assertNotNull (resultByte );
136- assertEquals (APPLICATION_PDF .toString (),entry .getValue ().getContentType ());
137- }
138- }
123+ byte [] resultByte = validateAssemblerResult (assemblerResult );
139124 if (SAVE_RESULTS ) {
140125 IOUtils .write (resultByte , Files .newOutputStream (TestUtils .ACTUAL_RESULTS_DIR .resolve ("testAssembleDocumentPDF_result.pdf" )));
141126 }
142127
143128 }
144129 }
145- }
146-
147-
148-
149130
131+ private byte [] validateAssemblerResult (AssemblerResult assemblerResult ) throws IOException , PdfException {
132+ Map <String , Document > sourceDocuments = assemblerResult .getDocuments ();
133+ for (Entry <String , Document > entry : sourceDocuments .entrySet ()) {
134+ if (entry .getKey ().equals ("concatenatedPDF.pdf" )) {
135+ byte [] resultByte = entry .getValue ().getInputStream ().readAllBytes ();
136+ assertNotNull (resultByte );
137+ assertEquals (APPLICATION_PDF .toString (),entry .getValue ().getContentType ());
138+ assertThat (resultByte .length , greaterThan (0 ));
139+ Pdf .from (resultByte ); // Ensure that the byte array is a valid PDF
140+ return resultByte ;
141+ }
142+ }
143+ fail ("Expected the assembler result to contain a document named 'concatenatedPDF.pdf'." );
144+ throw new IllegalStateException ("Routine should fail before this point." );
145+ }
146+ }
0 commit comments