forked from awslabs/aws-java-nio-spi-for-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS3SeekableByteChannelTest.java
173 lines (152 loc) · 6.54 KB
/
S3SeekableByteChannelTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.nio.spi.s3;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;
import static software.amazon.nio.spi.s3.S3Matchers.anyConsumer;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.OpenOption;
import java.time.Instant;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
@ExtendWith(MockitoExtension.class)
@SuppressWarnings("unchecked")
public class S3SeekableByteChannelTest {
S3FileSystem fs;
S3Path path;
byte[] bytes = "abcdef".getBytes(StandardCharsets.UTF_8);
@Mock
S3AsyncClient mockClient;
@BeforeEach
public void init() {
// forward to the method that uses the HeadObjectRequest parameter
lenient().when(mockClient.headObject(anyConsumer())).thenCallRealMethod();
lenient().when(mockClient.headObject(any(HeadObjectRequest.class))).thenReturn(
CompletableFuture.completedFuture(
HeadObjectResponse.builder().contentLength(100L).lastModified(Instant.now()).build()
)
);
lenient().when(mockClient.getObject(anyConsumer(), any(AsyncResponseTransformer.class))).thenCallRealMethod();
lenient().when(mockClient.getObject(any(GetObjectRequest.class), any(AsyncResponseTransformer.class))).thenReturn(
CompletableFuture.supplyAsync(() -> ResponseBytes.fromByteArray(
GetObjectResponse.builder().contentLength(6L).build(),
bytes)));
var provider = new S3FileSystemProvider();
fs = (S3FileSystem) provider.getFileSystem(URI.create("s3://test-bucket"));
fs.clientProvider(new FixedS3ClientProvider(mockClient));
path = (S3Path) fs.getPath("/object");
}
@AfterEach
public void after() throws IOException {
fs.close();
}
@Test
public void readDelegateConstructedByDefault() throws IOException {
try(var channel = seekableByteChannelForRead()) {
assertNotNull(channel.getReadDelegate());
}
}
@Test
public void read() throws IOException {
try(var channel = seekableByteChannelForRead()) {
var dst = ByteBuffer.allocate(6);
channel.read(dst);
assertArrayEquals(bytes, dst.array());
assertEquals(6L, channel.position());
}
}
@Test
public void write() throws IOException {
when(mockClient.headObject(any(HeadObjectRequest.class))).thenThrow(NoSuchKeyException.class);
when(mockClient.putObject(any(PutObjectRequest.class), any(AsyncRequestBody.class))).thenReturn(CompletableFuture.supplyAsync(() ->
PutObjectResponse.builder().build()));
try(var channel = new S3SeekableByteChannel(path, mockClient, Set.<OpenOption>of(CREATE, WRITE))){
assertEquals(0L, channel.size());
channel.write(ByteBuffer.allocate(12));
assertEquals(12L, channel.size());
}
}
@Test
public void position() throws IOException {
try(var channel = seekableByteChannelForRead()) {
assertEquals(0L, channel.position());
}
}
@Test
public void testPosition() throws IOException {
try(var channel = seekableByteChannelForRead()) {
channel.position(1L);
assertEquals(1L, channel.position());
}
}
@Test
public void size() throws IOException {
try(var channel = seekableByteChannelForRead()) {
assertEquals(100L, channel.size());
}
}
@Test
public void truncate() throws IOException {
try(var channel = seekableByteChannelForRead()) {
assertThrows(UnsupportedOperationException.class, () -> channel.truncate(0L));
}
}
@Test
public void isOpen() throws IOException {
try(var channel = seekableByteChannelForRead()) {
assertTrue(channel.isOpen());
}
}
@Test
public void close() throws IOException {
var channel = seekableByteChannelForRead();
channel.close();
assertFalse(channel.isOpen());
}
private S3SeekableByteChannel seekableByteChannelForRead() throws IOException {
return new S3SeekableByteChannel(path, mockClient, Collections.singleton(READ));
}
// test that the S3SeekableByteChannel uses the buffer size from the configuration set for the FileSystem
@Test
public void testBufferSize() throws IOException {
fs.getConfiguration().withMaxFragmentSize(10000);
fs.getConfiguration().withMaxFragmentNumber(10);
try(var channel = (S3SeekableByteChannel) fs.provider().newByteChannel(path, Set.of(READ))) {
assertEquals(10000, ((S3ReadAheadByteChannel) channel.getReadDelegate()).getMaxFragmentSize());
assertEquals(10, ((S3ReadAheadByteChannel) channel.getReadDelegate()).getMaxNumberFragments());
}
}
}