|
| 1 | +/* |
| 2 | + * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.core.internal.async; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import com.google.common.jimfs.Configuration; |
| 21 | +import com.google.common.jimfs.Jimfs; |
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.io.OutputStream; |
| 26 | +import java.io.UncheckedIOException; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.nio.file.FileAlreadyExistsException; |
| 29 | +import java.nio.file.FileSystem; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.nio.file.StandardOpenOption; |
| 33 | +import java.util.concurrent.CompletableFuture; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Before; |
| 36 | +import org.junit.Test; |
| 37 | +import org.reactivestreams.Subscriber; |
| 38 | +import org.reactivestreams.Subscription; |
| 39 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 40 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
| 41 | +import software.amazon.awssdk.core.async.SdkPublisher; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests for {@link FileAsyncResponseTransformer}. |
| 45 | + */ |
| 46 | +public class FileAsyncResponseTransformerTest { |
| 47 | + private FileSystem testFs; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setup() { |
| 51 | + testFs = Jimfs.newFileSystem(Configuration.forCurrentPlatform()); |
| 52 | + } |
| 53 | + |
| 54 | + @After |
| 55 | + public void teardown() throws IOException { |
| 56 | + testFs.close(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void defaultCreatesNewWritableFile() { |
| 61 | + byte[] content = "test".getBytes(StandardCharsets.UTF_8); |
| 62 | + Path testFile = testFs.getPath("testFile"); |
| 63 | + AsyncResponseTransformer<String, String> transformer = AsyncResponseTransformer.toFile(testFile); |
| 64 | + CompletableFuture<String> transformFuture = transformer.prepare(); |
| 65 | + transformer.onResponse("some response"); |
| 66 | + transformer.onStream(AsyncRequestBody.fromBytes(content)); |
| 67 | + transformFuture.join(); |
| 68 | + |
| 69 | + assertFileContentsEquals(testFile, "test"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void honorsPosition() throws IOException { |
| 74 | + byte[] content = "test".getBytes(StandardCharsets.UTF_8); |
| 75 | + Path testFile = testFs.getPath("testFile"); |
| 76 | + AsyncResponseTransformer<String, String> transformer = AsyncResponseTransformer.toFile(testFile, content.length, true, true); |
| 77 | + CompletableFuture<String> transformFuture = transformer.prepare(); |
| 78 | + transformer.onResponse("some response"); |
| 79 | + transformer.onStream(AsyncRequestBody.fromBytes(content)); |
| 80 | + transformFuture.join(); |
| 81 | + |
| 82 | + assertThat(Files.size(testFile)).isEqualTo(content.length * 2); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void honorsNewFileFlags_False() throws IOException { |
| 87 | + Path exists = testFs.getPath("exists"); |
| 88 | + createFileWithContents(exists, "Hello".getBytes(StandardCharsets.UTF_8)); |
| 89 | + |
| 90 | + honorsNewFileFlagTest(exists, 5, false, "Test", "HelloTest"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void honorsNewFileFlag_True_FileNotExists() { |
| 95 | + Path notExists = testFs.getPath("notExists"); |
| 96 | + honorsNewFileFlagTest(notExists, 0, true, "Test", "Test"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void honorsNewFileFlag_True_FileExists() throws IOException { |
| 101 | + Path exists = testFs.getPath("exists"); |
| 102 | + createFileWithContents(exists, "Hello".getBytes(StandardCharsets.UTF_8)); |
| 103 | + assertThatThrownBy(() -> honorsNewFileFlagTest(exists, 5, true, "Test", null)) |
| 104 | + .hasCauseInstanceOf(FileAlreadyExistsException.class); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void honorsDeleteOnFailure_True_NoExistingFile() { |
| 109 | + Path notExists = testFs.getPath("notExists"); |
| 110 | + honorsDeleteOnFailureTest(notExists, true, true); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void honorsDeleteOnFailure_True_ExistingFile() throws IOException { |
| 115 | + Path exists = testFs.getPath("exists"); |
| 116 | + createFileWithContents(exists, "Hello".getBytes(StandardCharsets.UTF_8)); |
| 117 | + honorsDeleteOnFailureTest(exists, false, true); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void honorsDeleteOnFailure_False_NonExistingFile() { |
| 122 | + Path notExists = testFs.getPath("notExists"); |
| 123 | + honorsDeleteOnFailureTest(notExists, true, false); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void honorsDeleteOnFailure_False_ExistingFile() throws IOException { |
| 128 | + Path exists = testFs.getPath("exists"); |
| 129 | + createFileWithContents(exists, "Hello".getBytes(StandardCharsets.UTF_8)); |
| 130 | + honorsDeleteOnFailureTest(exists, false, false); |
| 131 | + } |
| 132 | + |
| 133 | + private void honorsNewFileFlagTest(Path file, long position, boolean isNewFile, String streamContents, String expectedContents) { |
| 134 | + AsyncResponseTransformer<String, String> transformer = AsyncResponseTransformer.toFile(file, position, isNewFile, true); |
| 135 | + CompletableFuture<String> transformFuture = transformer.prepare(); |
| 136 | + transformer.onResponse("some response"); |
| 137 | + transformer.onStream(AsyncRequestBody.fromString(streamContents)); |
| 138 | + transformFuture.join(); |
| 139 | + |
| 140 | + if (expectedContents != null) { |
| 141 | + assertFileContentsEquals(file, expectedContents); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void honorsDeleteOnFailureTest(Path file, boolean isNewFile, boolean deleteOnFailure) { |
| 146 | + AsyncResponseTransformer<String, String> transformer = AsyncResponseTransformer.toFile(file, 0, isNewFile, deleteOnFailure); |
| 147 | + CompletableFuture<String> transformFuture = transformer.prepare(); |
| 148 | + IOException error = new IOException("Something went wrong"); |
| 149 | + transformer.onResponse("some response"); |
| 150 | + transformer.onStream(new ErrorPublisher<>(error)); |
| 151 | + transformer.exceptionOccurred(error); |
| 152 | + assertThatThrownBy(transformFuture::join).hasCause(error); |
| 153 | + if (deleteOnFailure) { |
| 154 | + assertThat(Files.exists(file)).isFalse(); |
| 155 | + } else { |
| 156 | + assertThat(Files.exists(file)).isTrue(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private static void createFileWithContents(Path file, byte[] contents) throws IOException { |
| 161 | + OutputStream os = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); |
| 162 | + os.write(contents); |
| 163 | + os.close(); |
| 164 | + } |
| 165 | + |
| 166 | + private static void assertFileContentsEquals(Path file, String expected) { |
| 167 | + StringBuilder sb = new StringBuilder(); |
| 168 | + try { |
| 169 | + BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file))); |
| 170 | + String s; |
| 171 | + while ((s = reader.readLine()) != null) { |
| 172 | + sb.append(s); |
| 173 | + } |
| 174 | + } catch (IOException ioe) { |
| 175 | + throw new UncheckedIOException(ioe); |
| 176 | + } |
| 177 | + assertThat(sb.toString()).isEqualTo(expected); |
| 178 | + } |
| 179 | + |
| 180 | + private static final class ErrorPublisher<T> implements SdkPublisher<T> { |
| 181 | + private final Throwable error; |
| 182 | + |
| 183 | + private ErrorPublisher(Throwable error) { |
| 184 | + this.error = error; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void subscribe(Subscriber<? super T> subscriber) { |
| 189 | + subscriber.onSubscribe(new ErrorSubscription(subscriber, error)); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static final class ErrorSubscription implements Subscription { |
| 194 | + private final Subscriber<?> subscriber; |
| 195 | + private final Throwable error; |
| 196 | + |
| 197 | + public ErrorSubscription(Subscriber<?> subscriber, Throwable error) { |
| 198 | + this.subscriber = subscriber; |
| 199 | + this.error = error; |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public void request(long l) { |
| 204 | + subscriber.onError(error); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public void cancel() { |
| 209 | + |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments