From 68ff250c382cc8fac757cff28eb8ad9b9dfdd6dc Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 22 Jan 2016 13:48:50 -0800 Subject: [PATCH] Return response from StaticLargeObjectApi.delete --- apis/openstack-swift/pom.xml | 5 +++ .../swift/v1/config/SwiftTypeAdapters.java | 39 ++++++++++++++++++- .../DeleteStaticLargeObjectResponse.java | 39 +++++++++++++++++++ .../v1/features/StaticLargeObjectApi.java | 3 +- .../StaticLargeObjectApiLiveTest.java | 26 ++++++++++++- .../StaticLargeObjectApiMockTest.java | 21 +++++++--- 6 files changed, 124 insertions(+), 9 deletions(-) create mode 100644 apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/domain/DeleteStaticLargeObjectResponse.java diff --git a/apis/openstack-swift/pom.xml b/apis/openstack-swift/pom.xml index 389eb1c89e..bc13fb8ec7 100644 --- a/apis/openstack-swift/pom.xml +++ b/apis/openstack-swift/pom.xml @@ -107,6 +107,11 @@ auto-service true + + com.google.auto.value + auto-value + provided + diff --git a/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/config/SwiftTypeAdapters.java b/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/config/SwiftTypeAdapters.java index 4eafdb697c..e2311bb8f3 100644 --- a/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/config/SwiftTypeAdapters.java +++ b/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/config/SwiftTypeAdapters.java @@ -25,6 +25,7 @@ import org.jclouds.json.config.GsonModule.DateAdapter; import org.jclouds.json.config.GsonModule.Iso8601DateAdapter; import org.jclouds.openstack.swift.v1.domain.BulkDeleteResponse; import org.jclouds.openstack.swift.v1.domain.ExtractArchiveResponse; +import org.jclouds.openstack.swift.v1.domain.DeleteStaticLargeObjectResponse; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; @@ -45,7 +46,9 @@ public class SwiftTypeAdapters extends AbstractModule { public Map provideCustomAdapterBindings() { return ImmutableMap. builder() .put(ExtractArchiveResponse.class, new ExtractArchiveResponseAdapter()) - .put(BulkDeleteResponse.class, new BulkDeleteResponseAdapter()).build(); + .put(BulkDeleteResponse.class, new BulkDeleteResponseAdapter()) + .put(DeleteStaticLargeObjectResponse.class, new StaticLargeObjectResponseAdapter()) + .build(); } static class ExtractArchiveResponseAdapter extends TypeAdapter { @@ -105,6 +108,40 @@ public class SwiftTypeAdapters extends AbstractModule { } } + static final class StaticLargeObjectResponseAdapter extends TypeAdapter { + + @Override + public DeleteStaticLargeObjectResponse read(JsonReader reader) throws IOException { + String status = ""; + int deleted = 0; + int notFound = 0; + Builder errors = ImmutableMap. builder(); + reader.beginObject(); + while (reader.hasNext()) { + String key = reader.nextName(); + if (key.equals("Response Status")) { + status = reader.nextString(); + } else if (key.equals("Number Deleted")) { + deleted = reader.nextInt(); + } else if (key.equals("Number Not Found")) { + notFound = reader.nextInt(); + } else if (key.equals("Errors")) { + readErrors(reader, errors); + } else { + // Response Body + reader.skipValue(); + } + } + reader.endObject(); + return DeleteStaticLargeObjectResponse.create(status, deleted, notFound, errors.build()); + } + + @Override + public void write(JsonWriter arg0, DeleteStaticLargeObjectResponse arg1) throws IOException { + throw new UnsupportedOperationException(); + } + } + static void readErrors(JsonReader reader, Builder errors) throws IOException { reader.beginArray(); while (reader.hasNext()) { diff --git a/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/domain/DeleteStaticLargeObjectResponse.java b/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/domain/DeleteStaticLargeObjectResponse.java new file mode 100644 index 0000000000..2263c304bb --- /dev/null +++ b/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/domain/DeleteStaticLargeObjectResponse.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jclouds.openstack.swift.v1.domain; + +import java.util.Map; + +import com.google.auto.value.AutoValue; + +/** + * Represents a response from a Static Large Object Delete request. + * + * @see org.jclouds.openstack.swift.v1.features.StaticLargeObjectApi + */ +@AutoValue +public abstract class DeleteStaticLargeObjectResponse { + public static DeleteStaticLargeObjectResponse create(String status, int deleted, int notFound, Map errors) { + return new AutoValue_DeleteStaticLargeObjectResponse(status, deleted, notFound, errors); + } + + public abstract String status(); + public abstract int deleted(); + public abstract int notFound(); + public abstract Map errors(); +} diff --git a/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApi.java b/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApi.java index b81df13b54..1e484894f9 100644 --- a/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApi.java +++ b/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApi.java @@ -35,6 +35,7 @@ import org.jclouds.openstack.keystone.v2_0.filters.AuthenticateRequest; import org.jclouds.openstack.swift.v1.binders.BindManifestToJsonPayload; import org.jclouds.openstack.swift.v1.binders.BindMetadataToHeaders.BindObjectMetadataToHeaders; import org.jclouds.openstack.swift.v1.binders.BindToHeaders; +import org.jclouds.openstack.swift.v1.domain.DeleteStaticLargeObjectResponse; import org.jclouds.openstack.swift.v1.domain.Segment; import org.jclouds.openstack.swift.v1.functions.ETagHeader; import org.jclouds.rest.annotations.BinderParam; @@ -114,7 +115,7 @@ public interface StaticLargeObjectApi { @DELETE @Fallback(VoidOnNotFoundOr404.class) @QueryParams(keys = "multipart-manifest", values = "delete") - void delete(@PathParam("objectName") String objectName); + DeleteStaticLargeObjectResponse delete(@PathParam("objectName") String objectName); /** * Get a static large object's manifest. diff --git a/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApiLiveTest.java b/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApiLiveTest.java index 3e92af5c55..6eca88c92f 100644 --- a/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApiLiveTest.java +++ b/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApiLiveTest.java @@ -17,6 +17,7 @@ package org.jclouds.openstack.swift.v1.features; import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThat; import static org.jclouds.io.Payloads.newByteSourcePayload; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; @@ -26,6 +27,7 @@ import java.util.List; import java.util.UUID; import org.jclouds.openstack.swift.v1.SwiftApi; +import org.jclouds.openstack.swift.v1.domain.DeleteStaticLargeObjectResponse; import org.jclouds.openstack.swift.v1.domain.Segment; import org.jclouds.openstack.swift.v1.domain.SwiftObject; import org.jclouds.openstack.swift.v1.internal.BaseSwiftApiLiveTest; @@ -47,7 +49,11 @@ public class StaticLargeObjectApiLiveTest extends BaseSwiftApiLiveTest public void testNotPresentWhenDeleting() throws Exception { for (String regionId : regions) { - api.getStaticLargeObjectApi(regionId, containerName).delete(UUID.randomUUID().toString()); + DeleteStaticLargeObjectResponse resp = api.getStaticLargeObjectApi(regionId, containerName).delete(UUID.randomUUID().toString()); + assertThat(resp.status()).isEqualTo("200 OK"); + assertThat(resp.deleted()).isZero(); + assertThat(resp.notFound()).isEqualTo(1); + assertThat(resp.errors()).isEmpty(); } } @@ -88,11 +94,27 @@ public class StaticLargeObjectApiLiveTest extends BaseSwiftApiLiveTest @Test(dependsOnMethods = "testReplaceManifest") public void testDelete() throws Exception { for (String regionId : regions) { - api.getStaticLargeObjectApi(regionId, containerName).delete(name); + DeleteStaticLargeObjectResponse resp = api.getStaticLargeObjectApi(regionId, containerName).delete(name); + assertThat(resp.status()).isEqualTo("200 OK"); + assertThat(resp.deleted()).isEqualTo(3); + assertThat(resp.notFound()).isZero(); + assertThat(resp.errors()).isEmpty(); assertEquals(api.getContainerApi(regionId).get(containerName).getObjectCount(), 0); } } + public void testDeleteSinglePartObjectWithMultiPartDelete() throws Exception { + String objectName = "testDeleteSinglePartObjectWithMultiPartDelete"; + for (String regionId : regions) { + api.getObjectApi(regionId, containerName).put(objectName, newByteSourcePayload(ByteSource.wrap("swifty".getBytes()))); + DeleteStaticLargeObjectResponse resp = api.getStaticLargeObjectApi(regionId, containerName).delete(objectName); + assertThat(resp.status()).isEqualTo("400 Bad Request"); + assertThat(resp.deleted()).isZero(); + assertThat(resp.notFound()).isZero(); + assertThat(resp.errors()).hasSize(1); + } + } + protected void assertMegabyteAndETagMatches(String regionId, String name, String etag1s) { SwiftObject object1s = api.getObjectApi(regionId, containerName).get(name); assertEquals(object1s.getETag(), etag1s); diff --git a/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApiMockTest.java b/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApiMockTest.java index ec135c1e9b..e466b1c68f 100644 --- a/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApiMockTest.java +++ b/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/features/StaticLargeObjectApiMockTest.java @@ -16,12 +16,14 @@ */ package org.jclouds.openstack.swift.v1.features; +import static org.assertj.core.api.Assertions.assertThat; import static org.jclouds.openstack.swift.v1.reference.SwiftHeaders.OBJECT_METADATA_PREFIX; import static org.testng.Assert.assertEquals; import java.util.List; import org.jclouds.openstack.swift.v1.SwiftApi; +import org.jclouds.openstack.swift.v1.domain.DeleteStaticLargeObjectResponse; import org.jclouds.openstack.swift.v1.domain.Segment; import org.jclouds.openstack.v2_0.internal.BaseOpenStackMockTest; import org.testng.annotations.Test; @@ -168,16 +170,20 @@ public class StaticLargeObjectApiMockTest extends BaseOpenStackMockTest