1- /**
2- * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
1+ /*
2+ * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License").
55 * You may not use this file except in compliance with the License.
1717
1818import android .content .Context ;
1919import android .util .Log ;
20-
2120import androidx .test .platform .app .InstrumentationRegistry ;
2221
2322import com .amazonaws .services .s3 .AmazonS3Client ;
4645import static org .mockito .Mockito .times ;
4746import static org .mockito .Mockito .verify ;
4847
48+ /**
49+ * Tests that new upload with input stream method calls
50+ * the existing methods with expected parameters.
51+ */
4952@ RunWith (RobolectricTestRunner .class )
5053public class UploadInputStreamTest {
51-
5254 private static final String TAG = "InputStreamTest" ;
55+ private static final String DEFAULT_BUCKET = "s3-upload-input-stream-unit-test" ;
5356
54- private static final String DEFAULT_BUCKET = "s3-upload-input-stream-unit-test-" + getRandomString ();
5557 private InputStream inputStream ;
5658 private ObjectMetadata metadata ;
5759 private CannedAccessControlList cannedAcl ;
5860 private TransferListener listener ;
5961 private TransferUtility transferUtility ;
60- private TransferObserver observer ;
6162
63+ /**
64+ * Sets up testing environment.
65+ */
6266 @ Before
6367 public void setup () {
6468 AmazonS3Client s3 = mock (AmazonS3Client .class );
@@ -68,78 +72,111 @@ public void setup() {
6872 .s3Client (s3 )
6973 .context (context )
7074 .build ());
75+ transferUtility .getDbUtil ().closeDB ();
7176 inputStream = new ByteArrayInputStream ("test data" .getBytes ());
7277 metadata = new ObjectMetadata ();
7378 cannedAcl = CannedAccessControlList .Private ;
7479 listener = new UploadListener ();
7580 }
81+
82+ /**
83+ * Test that {@link TransferUtility#upload(String, InputStream)} calls the File based
84+ * upload method with all of the expected defaults.
85+ * @throws IOException if upload fails to read input file
86+ */
7687 @ Test
7788 public void testUpload () throws IOException {
7889 String key = getRandomString ();
7990 transferUtility .upload (key , inputStream );
80- verify (transferUtility , times (1 )).upload (eq (DEFAULT_BUCKET ), eq (key ),
81- any (File .class ), any (ObjectMetadata .class ), isNull (CannedAccessControlList .class ),
82- isNull (TransferListener .class ));
91+ verify (transferUtility , times (1 )).upload (
92+ eq (DEFAULT_BUCKET ),
93+ eq (key ),
94+ any (File .class ),
95+ any (ObjectMetadata .class ),
96+ isNull (CannedAccessControlList .class ),
97+ isNull (TransferListener .class )
98+ );
8399 }
84100
85101 /**
86- * Test that upload(String key, String inputStream , UploadOptions options) with
102+ * Test that {@link TransferUtility# upload(String, InputStream , UploadOptions)} with
87103 * a default Builder calls the File based upload method with all of the expected defaults.
88- * @throws IOException
104+ * @throws IOException if upload fails to read input file
89105 */
90106 @ Test
91107 public void testUploadWithDefaults () throws IOException {
92108 String key = getRandomString ();
93109 transferUtility .upload (key , inputStream , UploadOptions .builder ().build ());
94- verify (transferUtility , times (1 )).upload (eq (DEFAULT_BUCKET ), eq (key ), any (File .class ),
95- any (ObjectMetadata .class ), isNull (CannedAccessControlList .class ), isNull (TransferListener .class ));
110+ verify (transferUtility , times (1 )).upload (
111+ eq (DEFAULT_BUCKET ),
112+ eq (key ),
113+ any (File .class ),
114+ any (ObjectMetadata .class ),
115+ isNull (CannedAccessControlList .class ),
116+ isNull (TransferListener .class )
117+ );
96118 }
97119
98120 /**
99- * Test that upload(String key, String inputStream, UploadOptions options) with
100- * all parameters specified calls the File based upload method with the same parameters provided as input.
101- * @throws IOException
121+ * Test that {@link TransferUtility#upload(String, InputStream, UploadOptions)} with
122+ * all parameters specified calls the File based upload method with the same parameters
123+ * provided as input.
124+ * @throws IOException if upload fails to read input file
102125 */
103126 @ Test
104127 public void testUploadWithAllParametersSet () throws IOException {
105128 String key = getRandomString ();
106129 String bucket = getRandomString ();
107- transferUtility .upload (key , inputStream , UploadOptions .builder ()
108- .bucket (bucket )
109- .objectMetadata (metadata )
110- .cannedAcl (cannedAcl )
111- .transferListener (listener )
112- .build ());
113- verify (transferUtility , times (1 )).upload (eq (bucket ), eq (key ), any (File .class ),
114- eq (metadata ), eq (cannedAcl ), eq (listener ));
130+ UploadOptions options = UploadOptions .builder ()
131+ .bucket (bucket )
132+ .objectMetadata (metadata )
133+ .cannedAcl (cannedAcl )
134+ .transferListener (listener )
135+ .build ();
136+ transferUtility .upload (key , inputStream , options );
137+ verify (transferUtility , times (1 )).upload (
138+ eq (bucket ),
139+ eq (key ),
140+ any (File .class ),
141+ eq (metadata ),
142+ eq (cannedAcl ),
143+ eq (listener )
144+ );
115145 }
116146
117147 /**
118148 * Verify that the File does in fact get deleted after the upload is complete.
149+ * @throws IOException if upload fails to read input file
119150 */
120151 @ Test
121152 public void testDeleteTempFileAfterUpload () throws IOException {
122153 // Call upload, so that a temporary File will be created
123154 final String key = getRandomString ();
124- observer = transferUtility .upload (key , inputStream );
155+ TransferObserver observer = transferUtility .upload (key , inputStream );
156+
125157 // Verify the file exists
126158 File file = new File (observer .getAbsoluteFilePath ());
127159 assertTrue (file .exists ());
160+
128161 // Set state to COMPLETED
129162 Context context = InstrumentationRegistry .getInstrumentation ().getContext ();
130163 TransferStatusUpdater transferStatusUpdater = TransferStatusUpdater .getInstance (context );
131164 transferStatusUpdater .updateState (observer .getId (), TransferState .COMPLETED );
165+
132166 // Verify the file was deleted
133167 assertFalse (file .exists ());
134168 }
135169
170+ /**
171+ * Cleans up input stream
172+ * @throws IOException if input stream fails to close
173+ */
136174 @ After
137175 public void teardown () throws IOException {
138176 inputStream .close ();
139- transferUtility .getDbUtil ().closeDB ();
140177 }
141178
142- private static String getRandomString () {
179+ private String getRandomString () {
143180 return UUID .randomUUID ().toString ();
144181 }
145182
0 commit comments