1313import java .io .InputStream ;
1414import java .nio .file .Files ;
1515import java .util .Collections ;
16+ import java .util .concurrent .CompletableFuture ;
1617
1718import org .junit .*;
1819import static org .junit .Assert .*;
@@ -106,4 +107,36 @@ public void testUploadDownloadDeleteMedia() throws Exception {
106107 ApiResponse <Void > deleteMediaApiResponse = controller .deleteMedia (ACCOUNT_ID , mediaId );
107108 assertEquals ("Response Code is not 204" , 204 , deleteMediaApiResponse .getStatusCode ());
108109 }
109- }
110+
111+ @ Test
112+ public void testUploadDownloadDeleteMediaAsync () throws Exception {
113+ final String fileName = "src/test/resources/mediaUpload.png" ;
114+ final String contentType = "image/png" ;
115+
116+ File file = new File (fileName );
117+ byte [] fileContents = Files .readAllBytes (file .toPath ());
118+ FileWrapper body = new FileWrapper (file , contentType );
119+
120+ final String mediaId = "java-media-test_" + java .util .UUID .randomUUID ();
121+
122+ CompletableFuture <ApiResponse <Void >> uploadMediaApiResponseAsync = controller .uploadMediaAsync (ACCOUNT_ID , mediaId , body , contentType , "no-cache" );
123+ assertEquals ("Response Code is not 204" , 204 , uploadMediaApiResponseAsync .get ().getStatusCode ());
124+
125+ CompletableFuture <ApiResponse <InputStream >> asyncDownloadMediaAPiResponse = controller .getMediaAsync (ACCOUNT_ID , mediaId );
126+ assertEquals ("Response Code is not 200" , 200 , asyncDownloadMediaAPiResponse .get ().getStatusCode ());
127+
128+ InputStream asyncDownloadMediaResponse = asyncDownloadMediaAPiResponse .get ().getResult ();
129+
130+ int asyncBRead ;
131+ ByteArrayOutputStream asyncByteArrayOutputStream = new ByteArrayOutputStream ();
132+ while ((asyncBRead = asyncDownloadMediaResponse .read ()) != -1 ){
133+ asyncByteArrayOutputStream .write (asyncBRead );
134+ }
135+ byte [] asyncResponseContents = asyncByteArrayOutputStream .toByteArray ();
136+
137+ assertArrayEquals ("Media download not equal to media upload" , fileContents , asyncResponseContents );
138+
139+ CompletableFuture <ApiResponse <Void >> deleteMediaApiResponseAsync = controller .deleteMediaAsync (ACCOUNT_ID , mediaId );
140+ assertEquals ("Response Code is not 204" , 204 , deleteMediaApiResponseAsync .get ().getStatusCode ());
141+ }
142+ }
0 commit comments