|
12 | 12 | import static org.mockito.Mockito.when; |
13 | 13 |
|
14 | 14 | import java.io.ByteArrayInputStream; |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.IOException; |
15 | 17 | import java.io.InputStream; |
| 18 | +import java.nio.charset.StandardCharsets; |
16 | 19 | import java.util.concurrent.CompletableFuture; |
17 | 20 | import java.util.concurrent.CompletionException; |
18 | 21 |
|
@@ -144,7 +147,8 @@ void noBucket() { |
144 | 147 | @Test |
145 | 148 | void envVariablesPresent() { |
146 | 149 | S3Object s3ObjectMock = mock(S3Object.class); |
147 | | - s3ObjectMock.setObjectContent(new ByteArrayInputStream("1234".getBytes())); |
| 150 | + S3ObjectInputStream objectContent = new S3ObjectInputStream(new ByteArrayInputStream("1234".getBytes(StandardCharsets.UTF_8)), null); |
| 151 | + when(s3ObjectMock.getObjectContent()).thenReturn(objectContent); |
148 | 152 | Mockito.when(amazonS3Client.getObject(any(GetObjectRequest.class))).thenReturn(s3ObjectMock); |
149 | 153 | Mockito.when(environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE)).thenReturn("meep"); |
150 | 154 | underTest.getFileByLocationId("meep"); |
@@ -197,31 +201,53 @@ void test_getCpcPlusValidationFile_NPE() { |
197 | 201 | } |
198 | 202 |
|
199 | 203 | @Test |
200 | | - void test_getCpcPlusValidationFile() { |
201 | | - S3ObjectInputStream expected = new S3ObjectInputStream(null, null); |
| 204 | + void test_getCpcPlusValidationFile() throws IOException { |
| 205 | + byte[] expectedBytes = "Mock Contents".getBytes(StandardCharsets.UTF_8); |
| 206 | + S3ObjectInputStream expectedStream = new S3ObjectInputStream(new ByteArrayInputStream(expectedBytes), null); |
202 | 207 | S3Object mockS3Obj = mock(S3Object.class); |
203 | | - Mockito.when(mockS3Obj.getObjectContent()).thenReturn(expected); |
| 208 | + Mockito.when(mockS3Obj.getObjectContent()).thenReturn(expectedStream); |
204 | 209 |
|
205 | 210 | Mockito.when(environment.getProperty(Constants.CPC_PLUS_BUCKET_NAME_VARIABLE)).thenReturn("Mock_Bucket"); |
206 | 211 | Mockito.when(environment.getProperty(Constants.CPC_PLUS_FILENAME_VARIABLE)).thenReturn("Mock_Filename"); |
207 | | - Mockito.when(amazonS3Client.getObject( any(GetObjectRequest.class) )).thenReturn(mockS3Obj); |
| 212 | + Mockito.when(amazonS3Client.getObject(any(GetObjectRequest.class))).thenReturn(mockS3Obj); |
208 | 213 |
|
209 | | - InputStream actual = underTest.getCpcPlusValidationFile(); |
| 214 | + byte[] actualBytes; |
| 215 | + try (InputStream actual = underTest.getCpcPlusValidationFile()) { |
| 216 | + assertThat(actual).isNotNull(); |
| 217 | + actualBytes = toByteArray(actual); |
| 218 | + } |
210 | 219 |
|
211 | | - assertThat(actual).isEqualTo(expected); |
| 220 | + assertThat(actualBytes).isEqualTo(expectedBytes); |
| 221 | + verify(mockS3Obj, times(1)).close(); |
212 | 222 | } |
213 | 223 |
|
214 | 224 | @Test |
215 | | - void test_getApmValidationFile() { |
216 | | - S3ObjectInputStream expected = new S3ObjectInputStream(null, null); |
| 225 | + void test_getApmValidationFile() throws IOException { |
| 226 | + byte[] expectedBytes = "APM".getBytes(StandardCharsets.UTF_8); |
| 227 | + S3ObjectInputStream expectedStream = new S3ObjectInputStream(new ByteArrayInputStream(expectedBytes), null); |
217 | 228 | S3Object mockS3Obj = mock(S3Object.class); |
218 | | - Mockito.when(mockS3Obj.getObjectContent()).thenReturn(expected); |
| 229 | + Mockito.when(mockS3Obj.getObjectContent()).thenReturn(expectedStream); |
219 | 230 |
|
220 | 231 | Mockito.when(environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE)).thenReturn("Mock_Bucket"); |
221 | | - Mockito.when(amazonS3Client.getObject( any(GetObjectRequest.class) )).thenReturn(mockS3Obj); |
| 232 | + Mockito.when(amazonS3Client.getObject(any(GetObjectRequest.class))).thenReturn(mockS3Obj); |
222 | 233 |
|
223 | | - InputStream actual = underTest.getApmValidationFile(Constants.CPC_PLUS_APM_FILE_NAME_KEY); |
| 234 | + byte[] actualBytes; |
| 235 | + try (InputStream actual = underTest.getApmValidationFile(Constants.CPC_PLUS_APM_FILE_NAME_KEY)) { |
| 236 | + assertThat(actual).isNotNull(); |
| 237 | + actualBytes = toByteArray(actual); |
| 238 | + } |
224 | 239 |
|
225 | | - assertThat(actual).isEqualTo(expected); |
| 240 | + assertThat(actualBytes).isEqualTo(expectedBytes); |
| 241 | + verify(mockS3Obj, times(1)).close(); |
| 242 | + } |
| 243 | + |
| 244 | + private byte[] toByteArray(InputStream inputStream) throws IOException { |
| 245 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 246 | + byte[] data = new byte[1024]; |
| 247 | + int bytesRead; |
| 248 | + while ((bytesRead = inputStream.read(data)) != -1) { |
| 249 | + buffer.write(data, 0, bytesRead); |
| 250 | + } |
| 251 | + return buffer.toByteArray(); |
226 | 252 | } |
227 | 253 | } |
0 commit comments