33import com .intuit .ipp .data .Attachable ;
44import com .intuit .ipp .exception .FMSException ;
55import com .intuit .ipp .util .Config ;
6+ import mockit .Expectations ;
7+
8+ import org .testng .Assert ;
9+ import org .testng .annotations .BeforeMethod ;
10+ import org .testng .annotations .BeforeTest ;
611import org .testng .annotations .Test ;
712
13+ import javax .imageio .ImageIO ;
814import javax .xml .bind .JAXBElement ;
915import javax .xml .bind .annotation .XmlElement ;
1016import javax .xml .bind .annotation .XmlRootElement ;
1117import javax .xml .namespace .QName ;
18+ import java .awt .image .RenderedImage ;
1219import java .io .ByteArrayInputStream ;
20+ import java .io .ByteArrayOutputStream ;
21+ import java .io .IOException ;
1322import java .io .InputStream ;
1423import java .util .Arrays ;
1524import java .util .HashMap ;
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,6 +124,53 @@ 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+ InputStream mockedStream = new ByteArrayInputStream ("test data" .getBytes ());
134+ setTestUploadRequestElements (mockedStream );
135+
136+ Attachable attachable = new Attachable ();
137+ attachable .setContentType ("something/jpeg" );
138+ message .getRequestElements ().setEntity (attachable );
139+ message .getRequestElements ().setObjectToSerialize (jaxbElement );
140+
141+ new Expectations (ImageIO .class ) {{
142+ ImageIO .write ((RenderedImage ) any , anyString , (ByteArrayOutputStream ) any );
143+ }};
144+
145+ serializeInterceptor .execute (message );
146+ Assert .assertEquals (message .getRequestElements ().getSerializedData (), "EntityBoundary{\" foo\" :\" bar\" }ContentBoundary" );
147+ }
148+
149+ @ Test (description = "Given a POST request with image file upload, " +
150+ "an exception thrown when an internal IO error occurs" ,
151+ expectedExceptions = FMSException .class
152+ )
153+ public void execute_uploadFileImageContent_exception () throws FMSException , IOException {
154+ requestParams .put (REQ_PARAM_METHOD_TYPE , "POST" );
155+ message .getRequestElements ().setRequestParameters (requestParams );
156+
157+ InputStream mockedStream = new ByteArrayInputStream ("test data" .getBytes ());
158+ setTestUploadRequestElements (mockedStream );
159+
160+ Attachable attachable = new Attachable ();
161+ attachable .setContentType ("something/jpeg" );
162+ message .getRequestElements ().setEntity (attachable );
163+ message .getRequestElements ().setObjectToSerialize (jaxbElement );
164+
165+ new Expectations (ImageIO .class ) {{
166+ ImageIO .write ((RenderedImage ) any , anyString , (ByteArrayOutputStream ) any );
167+ result = new IOException ("IOException thrown" );
168+ }};
169+
170+ serializeInterceptor .execute (message );
171+ Assert .assertEquals (message .getRequestElements ().getSerializedData (), "EntityBoundary{\" foo\" :\" bar\" }ContentBoundary" );
172+ }
173+
113174 @ Test (description = "Serialization request format returned should be of " +
114175 "the form: message.request.serialization" )
115176 public void getSerializationRequestFormat () {
@@ -119,6 +180,13 @@ public void getSerializationRequestFormat() {
119180 .equalsIgnoreCase (Config .getProperty (SERIALIZATION_REQUEST_FORMAT )));
120181 }
121182
183+ private void setTestUploadRequestElements (InputStream docContent ) {
184+ message .getRequestElements ().setAction ("upload" );
185+ message .getRequestElements ().getUploadRequestElements ().setDocContent (docContent );
186+ message .getRequestElements ().getUploadRequestElements ().setBoundaryForContent ("ContentBoundary" );
187+ message .getRequestElements ().getUploadRequestElements ().setBoundaryForEntity ("EntityBoundary" );
188+ }
189+
122190 @ XmlRootElement
123191 class TestJson {
124192
0 commit comments