44import static org .hamcrest .Matchers .*;
55import static org .junit .jupiter .api .Assertions .*;
66import 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 ;
811import java .util .Collections ;
912import java .util .HashMap ;
1013import java .util .Map ;
1114import java .util .Map .Entry ;
1215
13- import jakarta .ws .rs .core .MediaType ;
14-
1516import org .junit .jupiter .api .BeforeAll ;
1617import org .junit .jupiter .api .BeforeEach ;
1718import org .junit .jupiter .api .DisplayName ;
1819import org .junit .jupiter .api .Tag ;
1920import org .junit .jupiter .api .Test ;
2021
22+ import com ._4point .aem .docservices .rest_services .client .RestClient .ContentType ;
2123import com ._4point .aem .docservices .rest_services .client .assembler .RestServicesDocAssemblerServiceAdapter ;
2224import com ._4point .aem .docservices .rest_services .client .helpers .XmlDocument ;
2325import com ._4point .aem .docservices .rest_services .client .jersey .JerseyRestClient ;
2426import com ._4point .aem .docservices .rest_services .it_tests .AemInstance ;
2527import com ._4point .aem .docservices .rest_services .it_tests .Pdf ;
28+ import com ._4point .aem .docservices .rest_services .it_tests .Pdf .PdfException ;
2629import com ._4point .aem .fluentforms .api .Document ;
2730import com ._4point .aem .fluentforms .api .DocumentFactory ;
2831import com ._4point .aem .fluentforms .api .assembler .AssemblerResult ;
4447@ Tag ("client-tests" )
4548public 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