Skip to content

Commit 15f9307

Browse files
authored
HDDS-14222. Reduce duplication in TestObjectPut (#9539)
1 parent 5290112 commit 15f9307

10 files changed

Lines changed: 321 additions & 413 deletions

File tree

hadoop-ozone/client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@
9797
</dependency>
9898

9999
<!-- Test dependencies -->
100+
<dependency>
101+
<groupId>commons-io</groupId>
102+
<artifactId>commons-io</artifactId>
103+
<scope>test</scope>
104+
</dependency>
100105
<dependency>
101106
<groupId>org.apache.ozone</groupId>
102107
<artifactId>hdds-client</artifactId>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hadoop.ozone.client;
19+
20+
import static java.nio.charset.StandardCharsets.UTF_8;
21+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import org.apache.commons.io.IOUtils;
26+
27+
/** Utilities for tests using Ozone client. */
28+
public final class OzoneClientTestUtils {
29+
30+
/** Verify contents of a key.
31+
* @return key details for convenience (further checks) */
32+
public static OzoneKeyDetails assertKeyContent(
33+
OzoneBucket bucket,
34+
String keyName,
35+
String expected
36+
) throws IOException {
37+
return assertKeyContent(bucket, keyName, expected.getBytes(UTF_8));
38+
}
39+
40+
/** Verify contents of a key.
41+
* @return key details for convenience (further checks) */
42+
public static OzoneKeyDetails assertKeyContent(
43+
OzoneBucket bucket,
44+
String keyName,
45+
byte[] expected
46+
) throws IOException {
47+
try (InputStream is = bucket.readKey(keyName)) {
48+
assertArrayEquals(expected, IOUtils.readFully(is, expected.length));
49+
}
50+
return bucket.getKey(keyName);
51+
}
52+
53+
private OzoneClientTestUtils() {
54+
// no instances
55+
}
56+
}

hadoop-ozone/integration-test/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@
384384
<artifactId>ozone-client</artifactId>
385385
<scope>test</scope>
386386
</dependency>
387+
<dependency>
388+
<groupId>org.apache.ozone</groupId>
389+
<artifactId>ozone-client</artifactId>
390+
<type>test-jar</type>
391+
<scope>test</scope>
392+
</dependency>
387393
<dependency>
388394
<groupId>org.apache.ozone</groupId>
389395
<artifactId>ozone-common</artifactId>

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/client/rpc/OzoneRpcClientTests.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.apache.hadoop.ozone.OzoneConsts.GB;
3939
import static org.apache.hadoop.ozone.OzoneConsts.MD5_HASH;
4040
import static org.apache.hadoop.ozone.OzoneConsts.OZONE_URI_DELIMITER;
41+
import static org.apache.hadoop.ozone.client.OzoneClientTestUtils.assertKeyContent;
4142
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_DIR_DELETING_SERVICE_INTERVAL;
4243
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.KEY_NOT_FOUND;
4344
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.NO_SUCH_MULTIPART_UPLOAD_ERROR;
@@ -1405,20 +1406,6 @@ private static void rewriteKey(
14051406
}
14061407
}
14071408

1408-
private static OzoneKeyDetails assertKeyContent(
1409-
OzoneBucket bucket, String keyName, byte[] expectedContent
1410-
) throws IOException {
1411-
OzoneKeyDetails updatedKeyDetails = bucket.getKey(keyName);
1412-
1413-
try (OzoneInputStream is = bucket.readKey(keyName)) {
1414-
byte[] fileContent = new byte[expectedContent.length];
1415-
IOUtils.readFully(is, fileContent);
1416-
assertArrayEquals(expectedContent, fileContent);
1417-
}
1418-
1419-
return updatedKeyDetails;
1420-
}
1421-
14221409
private OzoneBucket createBucket(BucketLayout layout) throws IOException {
14231410
String volumeName = UUID.randomUUID().toString();
14241411
String bucketName = UUID.randomUUID().toString();

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/client/rpc/TestOzoneRpcClientWithKeyLatestVersion.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919

2020
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor.THREE;
2121
import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_CLIENT_KEY_LATEST_VERSION_LOCATION;
22-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22+
import static org.apache.hadoop.ozone.client.OzoneClientTestUtils.assertKeyContent;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
2424
import static org.junit.jupiter.api.Assertions.assertNotNull;
2525

2626
import java.io.IOException;
27-
import java.io.InputStream;
2827
import java.util.List;
2928
import java.util.UUID;
30-
import org.apache.commons.io.IOUtils;
3129
import org.apache.commons.lang3.RandomUtils;
3230
import org.apache.hadoop.hdds.client.RatisReplicationConfig;
3331
import org.apache.hadoop.hdds.client.ReplicationConfig;
@@ -107,13 +105,6 @@ private static void writeKey(OzoneBucket bucket, String key, byte[] content,
107105
TestDataUtil.createKey(bucket, key, replication, content);
108106
}
109107

110-
public static void assertKeyContent(OzoneBucket bucket, String key,
111-
byte[] expected) throws Exception {
112-
try (InputStream in = bucket.readKey(key)) {
113-
assertArrayEquals(expected, IOUtils.readFully(in, expected.length));
114-
}
115-
}
116-
117108
private void assertListStatus(OzoneBucket bucket, String keyName,
118109
int expectedVersionCount) throws Exception {
119110
List<OzoneFileStatus> files = bucket.listStatus(keyName, false, "", 1);

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/client/rpc/TestReadRetries.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.hadoop.ozone.client.rpc;
1919

2020
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor.THREE;
21-
import static org.apache.hadoop.ozone.client.rpc.TestOzoneRpcClientWithKeyLatestVersion.assertKeyContent;
21+
import static org.apache.hadoop.ozone.client.OzoneClientTestUtils.assertKeyContent;
2222
import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.configureFSOptimizedPaths;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
2424
import static org.junit.jupiter.api.Assertions.assertThrows;

hadoop-ozone/s3gateway/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@
227227
<artifactId>weld-servlet-shaded</artifactId>
228228
<scope>runtime</scope>
229229
</dependency>
230+
<dependency>
231+
<groupId>org.apache.ozone</groupId>
232+
<artifactId>ozone-client</artifactId>
233+
<type>test-jar</type>
234+
<scope>test</scope>
235+
</dependency>
230236
</dependencies>
231237
<build>
232238
<plugins>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hadoop.ozone.s3.endpoint;
19+
20+
import static java.nio.charset.StandardCharsets.UTF_8;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import java.io.ByteArrayInputStream;
25+
import java.io.IOException;
26+
import javax.ws.rs.core.Response;
27+
import org.apache.hadoop.ozone.s3.exception.OS3Exception;
28+
import org.apache.http.HttpStatus;
29+
import org.apache.ratis.util.function.CheckedSupplier;
30+
31+
/** Utilities for unit-testing S3 endpoints. */
32+
public final class EndpointTestUtils {
33+
34+
/** Put without content. */
35+
public static Response putDir(
36+
ObjectEndpoint subject,
37+
String bucket,
38+
String key
39+
) throws IOException, OS3Exception {
40+
return put(subject, bucket, key, 0, null, null);
41+
}
42+
43+
/** Put with content. */
44+
public static Response put(
45+
ObjectEndpoint subject,
46+
String bucket,
47+
String key,
48+
String content
49+
) throws IOException, OS3Exception {
50+
return put(subject, bucket, key, 0, null, content);
51+
}
52+
53+
/** Put with content, part number, upload ID. */
54+
public static Response put(
55+
ObjectEndpoint subject,
56+
String bucket,
57+
String key,
58+
int partNumber,
59+
String uploadID,
60+
String content
61+
) throws IOException, OS3Exception {
62+
if (content == null) {
63+
return subject.put(bucket, key, 0, partNumber, uploadID, null, null, null);
64+
} else {
65+
final long length = content.length();
66+
try (ByteArrayInputStream body = new ByteArrayInputStream(content.getBytes(UTF_8))) {
67+
return subject.put(bucket, key, length, partNumber, uploadID, null, null, body);
68+
}
69+
}
70+
}
71+
72+
/** Verify response is success for {@code request}. */
73+
public static <E extends Exception> void assertSucceeds(CheckedSupplier<Response, E> request) throws E {
74+
try (Response response = request.get()) {
75+
assertEquals(HttpStatus.SC_OK, response.getStatus());
76+
}
77+
}
78+
79+
/** Verify error response for {@code request} matching {@code expected} {@link OS3Exception}. */
80+
public static OS3Exception assertErrorResponse(OS3Exception expected, CheckedSupplier<Response, ?> request) {
81+
OS3Exception actual = assertThrows(OS3Exception.class, () -> request.get().close());
82+
assertEquals(expected.getCode(), actual.getCode());
83+
assertEquals(expected.getHttpCode(), actual.getHttpCode());
84+
return actual;
85+
}
86+
87+
private EndpointTestUtils() {
88+
// no instances
89+
}
90+
}

0 commit comments

Comments
 (0)