44import com .intuit .ipp .exception .FMSException ;
55import com .intuit .ipp .util .Config ;
66
7- import mockit .Mock ;
8- import mockit .MockUp ;
97import org .testng .Assert ;
108import org .testng .annotations .BeforeMethod ;
119import org .testng .annotations .BeforeTest ;
1614import javax .xml .bind .annotation .XmlElement ;
1715import javax .xml .bind .annotation .XmlRootElement ;
1816import javax .xml .namespace .QName ;
19- import java .awt .image .RenderedImage ;
17+ import java .awt .image .BufferedImage ;
2018import java .io .ByteArrayInputStream ;
19+ import java .io .ByteArrayOutputStream ;
2120import java .io .IOException ;
2221import java .io .InputStream ;
23- import java . io . OutputStream ;
22+
2423import java .util .Arrays ;
2524import java .util .HashMap ;
2625import java .util .Map ;
@@ -131,68 +130,35 @@ public void execute_uploadFileImageContent() throws FMSException, IOException {
131130 requestParams .put (REQ_PARAM_METHOD_TYPE , "POST" );
132131 message .getRequestElements ().setRequestParameters (requestParams );
133132
134- InputStream mockedStream = new ByteArrayInputStream ("test data" .getBytes ());
135- setTestUploadRequestElements (mockedStream );
136-
137- Attachable attachable = new Attachable ();
138- attachable .setContentType ("something/jpeg" );
139- message .getRequestElements ().setEntity (attachable );
140- message .getRequestElements ().setObjectToSerialize (jaxbElement );
141-
142- new MockUp <ImageIO >() {
143- @ Mock
144- public boolean write (RenderedImage image , String formatName , OutputStream output ) {
145- return true ;
146- }
147- };
148-
149- serializeInterceptor .execute (message );
150- Assert .assertEquals (message .getRequestElements ().getSerializedData (), "EntityBoundary{\" foo\" :\" bar\" }ContentBoundary" );
151- }
152-
153- @ Test (description = "Given a POST request with image file upload, " +
154- "an exception thrown when an internal IO error occurs" ,
155- expectedExceptions = FMSException .class
156- )
157- public void execute_uploadFileImageContent_exception () throws FMSException , IOException {
158- requestParams .put (REQ_PARAM_METHOD_TYPE , "POST" );
159- message .getRequestElements ().setRequestParameters (requestParams );
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 ());
160138
161- InputStream mockedStream = new ByteArrayInputStream ("test data" .getBytes ());
162- setTestUploadRequestElements (mockedStream );
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" );
163144
164145 Attachable attachable = new Attachable ();
165146 attachable .setContentType ("something/jpeg" );
166147 message .getRequestElements ().setEntity (attachable );
167148 message .getRequestElements ().setObjectToSerialize (jaxbElement );
168149
169- new MockUp <ImageIO >() {
170- @ Mock
171- public boolean write (RenderedImage image , String formatName , OutputStream output ) throws IOException {
172- throw new IOException ("IOException thrown" );
173- }
174- };
175-
176150 serializeInterceptor .execute (message );
177151 Assert .assertEquals (message .getRequestElements ().getSerializedData (), "EntityBoundary{\" foo\" :\" bar\" }ContentBoundary" );
178152 }
179153
180154 @ Test (description = "Serialization request format returned should be of " +
181155 "the form: message.request.serialization" )
182156 public void getSerializationRequestFormat () {
183- SerializeInterceptor interceptor = new SerializeInterceptor ();
184- assertTrue (interceptor
157+ assertTrue (serializeInterceptor
185158 .getSerializationRequestFormat ()
186159 .equalsIgnoreCase (Config .getProperty (SERIALIZATION_REQUEST_FORMAT )));
187160 }
188161
189- private void setTestUploadRequestElements (InputStream docContent ) {
190- message .getRequestElements ().setAction ("upload" );
191- message .getRequestElements ().getUploadRequestElements ().setDocContent (docContent );
192- message .getRequestElements ().getUploadRequestElements ().setBoundaryForContent ("ContentBoundary" );
193- message .getRequestElements ().getUploadRequestElements ().setBoundaryForEntity ("EntityBoundary" );
194- }
195-
196162 @ XmlRootElement
197163 class TestJson {
198164
@@ -207,4 +173,5 @@ public void setFoo(String foo) {
207173 this .foo = foo ;
208174 }
209175 }
176+
210177}
0 commit comments