Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
/** Utilities for unit-testing S3 endpoints. */
public final class EndpointTestUtils {

/** Get key tags. */
public static Response getTagging(
ObjectEndpoint subject,
String bucket,
String key
) throws IOException, OS3Exception {
return subject.get(bucket, key, 0, null, 0, null, "");
}

/** Put without content. */
public static Response putDir(
ObjectEndpoint subject,
Expand All @@ -50,6 +59,23 @@ public static Response put(
return put(subject, bucket, key, 0, null, content);
}

/** Add tagging on key. */
public static Response putTagging(
ObjectEndpoint subject,
String bucket,
String key,
String content
) throws IOException, OS3Exception {
if (content == null) {
return subject.put(bucket, key, 0, 0, null, "", null, null);
} else {
final long length = content.length();
try (ByteArrayInputStream body = new ByteArrayInputStream(content.getBytes(UTF_8))) {
return subject.put(bucket, key, length, 0, null, "", null, body);
}
}
}

/** Put with content, part number, upload ID. */
public static Response put(
ObjectEndpoint subject,
Expand All @@ -69,6 +95,15 @@ public static Response put(
}
}

/** Delete key tags. */
public static Response deleteTagging(
ObjectEndpoint subject,
String bucket,
String key
) throws IOException, OS3Exception {
return subject.delete(bucket, key, null, "");
}

/** Verify response is success for {@code request}. */
public static <E extends Exception> void assertSucceeds(CheckedSupplier<Response, E> request) throws E {
try (Response response = request.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,24 @@

package org.apache.hadoop.ozone.s3.endpoint;

import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_NOT_IMPLEMENTED;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.hadoop.ozone.s3.endpoint.EndpointTestUtils.assertErrorResponse;
import static org.apache.hadoop.ozone.s3.endpoint.EndpointTestUtils.deleteTagging;
import static org.apache.hadoop.ozone.s3.endpoint.EndpointTestUtils.put;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.NOT_IMPLEMENTED;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.NO_SUCH_BUCKET;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.NO_SUCH_KEY;
import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_HEADER;
import static org.apache.hadoop.ozone.s3.util.S3Consts.X_AMZ_CONTENT_SHA256;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.ozone.client.ObjectStore;
import org.apache.hadoop.ozone.client.OzoneBucket;
import org.apache.hadoop.ozone.client.OzoneClient;
Expand Down Expand Up @@ -65,55 +62,39 @@ public class TestObjectTaggingDelete {
@BeforeEach
public void init() throws OS3Exception, IOException {
//GIVEN
OzoneConfiguration config = new OzoneConfiguration();
client = new OzoneClientStub();
client.getObjectStore().createS3Bucket(BUCKET_NAME);

HttpHeaders headers = Mockito.mock(HttpHeaders.class);
rest = EndpointBuilder.newObjectEndpointBuilder()
.setClient(client)
.setConfig(config)
.setHeaders(headers)
.build();

ByteArrayInputStream body = new ByteArrayInputStream(CONTENT.getBytes(UTF_8));
// Create a key with object tags
Mockito.when(headers.getHeaderString(TAG_HEADER)).thenReturn("tag1=value1&tag2=value2");
Mockito.when(headers.getHeaderString(X_AMZ_CONTENT_SHA256))
.thenReturn("mockSignature");
rest.put(BUCKET_NAME, KEY_WITH_TAG, CONTENT.length(),
1, null, null, null, body);
put(rest, BUCKET_NAME, KEY_WITH_TAG, CONTENT);
}

@Test
public void testDeleteTagging() throws IOException, OS3Exception {
Response response = rest.delete(BUCKET_NAME, KEY_WITH_TAG, null, "");
Response response = deleteTagging(rest, BUCKET_NAME, KEY_WITH_TAG);
assertEquals(HTTP_NO_CONTENT, response.getStatus());

assertTrue(client.getObjectStore().getS3Bucket(BUCKET_NAME)
.getKey(KEY_WITH_TAG).getTags().isEmpty());
}

@Test
public void testDeleteTaggingNoKeyFound() throws Exception {
try {
rest.delete(BUCKET_NAME, "nonexistent", null, "");
fail("Expected an OS3Exception to be thrown");
} catch (OS3Exception ex) {
assertEquals(HTTP_NOT_FOUND, ex.getHttpCode());
assertEquals(NO_SUCH_KEY.getCode(), ex.getCode());
}
public void testDeleteTaggingNoKeyFound() {
assertErrorResponse(NO_SUCH_KEY, () -> deleteTagging(rest, BUCKET_NAME, "nonexistent"));
}

@Test
public void testDeleteTaggingNoBucketFound() throws Exception {
try {
rest.delete("nonexistent", "nonexistent", null, "");
fail("Expected an OS3Exception to be thrown");
} catch (OS3Exception ex) {
assertEquals(HTTP_NOT_FOUND, ex.getHttpCode());
assertEquals(NO_SUCH_BUCKET.getCode(), ex.getCode());
}
public void testDeleteTaggingNoBucketFound() {
assertErrorResponse(NO_SUCH_BUCKET, () -> deleteTagging(rest, "nonexistent", "any"));
}

@Test
Expand All @@ -134,12 +115,6 @@ public void testDeleteObjectTaggingNotImplemented() throws Exception {
doThrow(new OMException("DeleteObjectTagging is not currently supported for FSO directory",
ResultCodes.NOT_SUPPORTED_OPERATION)).when(mockBucket).deleteObjectTagging("dir/");

try {
endpoint.delete("fsoBucket", "dir/", null, "");
fail("Expected an OS3Exception to be thrown");
} catch (OS3Exception ex) {
assertEquals(HTTP_NOT_IMPLEMENTED, ex.getHttpCode());
assertEquals(NOT_IMPLEMENTED.getCode(), ex.getCode());
}
assertErrorResponse(NOT_IMPLEMENTED, () -> deleteTagging(endpoint, "fsoBucket", "dir/"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package org.apache.hadoop.ozone.s3.endpoint;

import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.hadoop.ozone.s3.endpoint.EndpointTestUtils.assertErrorResponse;
import static org.apache.hadoop.ozone.s3.endpoint.EndpointTestUtils.assertSucceeds;
import static org.apache.hadoop.ozone.s3.endpoint.EndpointTestUtils.getTagging;
import static org.apache.hadoop.ozone.s3.endpoint.EndpointTestUtils.put;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.NO_SUCH_BUCKET;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.NO_SUCH_KEY;
import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_HEADER;
Expand All @@ -28,11 +30,9 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneClientStub;
import org.apache.hadoop.ozone.s3.endpoint.S3Tagging.Tag;
Expand All @@ -52,9 +52,8 @@ public class TestObjectTaggingGet {
private ObjectEndpoint rest;

@BeforeEach
public void init() throws OS3Exception, IOException {
public void init() throws Exception {
//GIVEN
OzoneConfiguration config = new OzoneConfiguration();
OzoneClient client = new OzoneClientStub();
client.getObjectStore().createS3Bucket(BUCKET_NAME);

Expand All @@ -64,21 +63,18 @@ public void init() throws OS3Exception, IOException {

rest = EndpointBuilder.newObjectEndpointBuilder()
.setClient(client)
.setConfig(config)
.setHeaders(headers)
.build();

ByteArrayInputStream body = new ByteArrayInputStream(CONTENT.getBytes(UTF_8));
// Create a key with object tags
Mockito.when(headers.getHeaderString(TAG_HEADER)).thenReturn("tag1=value1&tag2=value2");
rest.put(BUCKET_NAME, KEY_WITH_TAG, CONTENT.length(),
1, null, null, null, body);
assertSucceeds(() -> put(rest, BUCKET_NAME, KEY_WITH_TAG, CONTENT));
}

@Test
public void testGetTagging() throws IOException, OS3Exception {
//WHEN
Response response = rest.get(BUCKET_NAME, KEY_WITH_TAG, 0, null, 0, null, "");
Response response = getTagging(rest, BUCKET_NAME, KEY_WITH_TAG);

assertEquals(HTTP_OK, response.getStatus());
S3Tagging s3Tagging = (S3Tagging) response.getEntity();
Expand All @@ -97,24 +93,12 @@ public void testGetTagging() throws IOException, OS3Exception {
}

@Test
public void testGetTaggingNoKeyFound() throws Exception {
try {
rest.get(BUCKET_NAME, "nonexistent", 0, null, 0, null, "");
fail("Expected an OS3Exception to be thrown");
} catch (OS3Exception ex) {
assertEquals(HTTP_NOT_FOUND, ex.getHttpCode());
assertEquals(NO_SUCH_KEY.getCode(), ex.getCode());
}
public void testGetTaggingNoKeyFound() {
assertErrorResponse(NO_SUCH_KEY, () -> getTagging(rest, BUCKET_NAME, "nonexistent"));
}

@Test
public void testGetTaggingNoBucketFound() throws Exception {
try {
rest.get("nonexistent", "nonexistent", 0, null, 0, null, "");
fail("Expected an OS3Exception to be thrown");
} catch (OS3Exception ex) {
assertEquals(HTTP_NOT_FOUND, ex.getHttpCode());
assertEquals(NO_SUCH_BUCKET.getCode(), ex.getCode());
}
public void testGetTaggingNoBucketFound() {
assertErrorResponse(NO_SUCH_BUCKET, () -> getTagging(rest, "nonexistent", "any"));
}
}
Loading