33import com .intuit .ipp .data .Attachable ;
44import com .intuit .ipp .exception .FMSException ;
55import com .intuit .ipp .util .Config ;
6+
7+ import org .testng .Assert ;
8+ import org .testng .annotations .BeforeMethod ;
9+ import org .testng .annotations .BeforeTest ;
610import org .testng .annotations .Test ;
711
12+ import javax .imageio .ImageIO ;
813import javax .xml .bind .JAXBElement ;
914import javax .xml .bind .annotation .XmlElement ;
1015import javax .xml .bind .annotation .XmlRootElement ;
1116import javax .xml .namespace .QName ;
17+ import java .awt .image .BufferedImage ;
1218import java .io .ByteArrayInputStream ;
19+ import java .io .ByteArrayOutputStream ;
20+ import java .io .IOException ;
1321import java .io .InputStream ;
22+
1423import java .util .Arrays ;
1524import java .util .HashMap ;
1625import java .util .Map ;
2433
2534public class SerializeInterceptorTest {
2635
27- private SerializeInterceptor serializeInterceptor = new SerializeInterceptor ();
36+ private SerializeInterceptor serializeInterceptor ;
37+ private IntuitMessage message ;
38+ Map <String , String > requestParams ;
39+ JAXBElement jaxbElement ;
40+
41+ @ BeforeTest
42+ public void setUp () {
43+ TestJson json = new TestJson ();
44+ json .setFoo ("bar" );
45+ jaxbElement = new JAXBElement (new QName (TestJson .class .getSimpleName ()), TestJson .class , json );
46+ serializeInterceptor = new SerializeInterceptor ();
47+ }
48+
49+ @ BeforeMethod
50+ public void beforeEach () {
51+ message = new IntuitMessage ();
52+ requestParams = new HashMap <>();
53+ }
2854
2955 @ Test (description = "Given a POST request with object for serialization, " +
3056 "the serialized data should be present in the serializedData object" )
3157 public void execute_positiveCase1 () throws FMSException {
32- IntuitMessage message = new IntuitMessage ();
33- TestJson json = new TestJson ();
34- json .setFoo ("bar" );
35- JAXBElement <TestJson > jaxbElement = new JAXBElement (new QName (TestJson .class .getSimpleName ()), TestJson .class , json );
36- Map <String , String > requestParams = new HashMap <>();
3758 requestParams .put (REQ_PARAM_METHOD_TYPE , "POST" );
3859 message .getRequestElements ().setRequestParameters (requestParams );
3960 message .getRequestElements ().setObjectToSerialize (jaxbElement );
@@ -44,8 +65,6 @@ public void execute_positiveCase1() throws FMSException {
4465 @ Test (description = "Given a POST request with post body for serialization, " +
4566 "the serialized data should be present in the serializedData object" )
4667 public void execute_positiveCase2 () throws FMSException {
47- IntuitMessage message = new IntuitMessage ();
48- Map <String , String > requestParams = new HashMap <>();
4968 requestParams .put (REQ_PARAM_METHOD_TYPE , "POST" );
5069 message .getRequestElements ().setRequestParameters (requestParams );
5170 String jsonInput = "{\" foo\" :\" bar\" }" ;
@@ -56,8 +75,6 @@ public void execute_positiveCase2() throws FMSException {
5675
5776 @ Test (description = "Given a GET request for serialization, the serialized data should be null" )
5877 public void execute_positiveCase3 () throws FMSException {
59- IntuitMessage message = new IntuitMessage ();
60- Map <String , String > requestParams = new HashMap <>();
6178 requestParams .put (REQ_PARAM_METHOD_TYPE , "GET" );
6279 message .getRequestElements ().setRequestParameters (requestParams );
6380 serializeInterceptor .execute (message );
@@ -69,17 +86,14 @@ public void execute_positiveCase3() throws FMSException {
6986 "a file to be uploaded. The serialized data should be present in the " +
7087 "serializedData object along with the file that would be uploaded." )
7188 public void execute_positiveCase4 () throws FMSException {
72- IntuitMessage message = new IntuitMessage ();
73- TestJson json = new TestJson ();
74- json .setFoo ("bar" );
75- JAXBElement <TestJson > jaxbElement = new JAXBElement (new QName (TestJson .class .getSimpleName ()), TestJson .class , json );
76- Map <String , String > requestParams = new HashMap <>();
7789 requestParams .put (REQ_PARAM_METHOD_TYPE , "POST" );
7890 message .getRequestElements ().setRequestParameters (requestParams );
7991 message .getRequestElements ().setObjectToSerialize (jaxbElement );
92+
8093 message .getRequestElements ().setAction (UPLOAD .toString ());
8194 message .getRequestElements ().getUploadRequestElements ().setBoundaryForEntity ("Entity" );
8295 message .getRequestElements ().getUploadRequestElements ().setBoundaryForContent ("Content" );
96+
8397 Attachable attachable = new Attachable ();
8498 attachable .setFileName ("test.txt" );
8599 message .getRequestElements ().setEntity (attachable );
@@ -110,11 +124,37 @@ public void execute_positiveCase5() throws FMSException {
110124 assertEquals (message .getRequestElements ().getSerializedData (), "{\" foo\" :\" bar\" }" );
111125 }
112126
127+ @ Test (description = "Given a POST request with an image file upload, " +
128+ "the serializeData object should return the boundaries with the serialized data" )
129+ public void execute_uploadFileImageContent () throws FMSException , IOException {
130+ requestParams .put (REQ_PARAM_METHOD_TYPE , "POST" );
131+ message .getRequestElements ().setRequestParameters (requestParams );
132+
133+ // Generate an image byte array
134+ BufferedImage image = new BufferedImage (10 , 10 , BufferedImage .TYPE_INT_RGB );
135+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
136+ ImageIO .write (image , "jpg" , baos );
137+ InputStream mockedStream = new ByteArrayInputStream (baos .toByteArray ());
138+
139+ // set message request elements
140+ message .getRequestElements ().setAction ("upload" );
141+ message .getRequestElements ().getUploadRequestElements ().setDocContent (mockedStream );
142+ message .getRequestElements ().getUploadRequestElements ().setBoundaryForContent ("ContentBoundary" );
143+ message .getRequestElements ().getUploadRequestElements ().setBoundaryForEntity ("EntityBoundary" );
144+
145+ Attachable attachable = new Attachable ();
146+ attachable .setContentType ("something/jpeg" );
147+ message .getRequestElements ().setEntity (attachable );
148+ message .getRequestElements ().setObjectToSerialize (jaxbElement );
149+
150+ serializeInterceptor .execute (message );
151+ Assert .assertEquals (message .getRequestElements ().getSerializedData (), "EntityBoundary{\" foo\" :\" bar\" }ContentBoundary" );
152+ }
153+
113154 @ Test (description = "Serialization request format returned should be of " +
114155 "the form: message.request.serialization" )
115156 public void getSerializationRequestFormat () {
116- SerializeInterceptor interceptor = new SerializeInterceptor ();
117- assertTrue (interceptor
157+ assertTrue (serializeInterceptor
118158 .getSerializationRequestFormat ()
119159 .equalsIgnoreCase (Config .getProperty (SERIALIZATION_REQUEST_FORMAT )));
120160 }
@@ -133,4 +173,5 @@ public void setFoo(String foo) {
133173 this .foo = foo ;
134174 }
135175 }
176+
136177}
0 commit comments