Return response from StaticLargeObjectApi.delete

This commit is contained in:
Andrew Gaul 2016-01-22 13:48:50 -08:00
parent a3376d4efe
commit 68ff250c38
6 changed files with 124 additions and 9 deletions

View File

@ -107,6 +107,11 @@
<artifactId>auto-service</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>

View File

@ -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<Type, Object> provideCustomAdapterBindings() {
return ImmutableMap.<Type, Object> 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<ExtractArchiveResponse> {
@ -105,6 +108,40 @@ public class SwiftTypeAdapters extends AbstractModule {
}
}
static final class StaticLargeObjectResponseAdapter extends TypeAdapter<DeleteStaticLargeObjectResponse> {
@Override
public DeleteStaticLargeObjectResponse read(JsonReader reader) throws IOException {
String status = "";
int deleted = 0;
int notFound = 0;
Builder<String, String> errors = ImmutableMap.<String, String> 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<String, String> errors) throws IOException {
reader.beginArray();
while (reader.hasNext()) {

View File

@ -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<String, String> errors) {
return new AutoValue_DeleteStaticLargeObjectResponse(status, deleted, notFound, errors);
}
public abstract String status();
public abstract int deleted();
public abstract int notFound();
public abstract Map<String, String> errors();
}

View File

@ -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.

View File

@ -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<SwiftApi>
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<SwiftApi>
@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);

View File

@ -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<SwiftApi
public void testDelete() throws Exception {
MockWebServer server = mockOpenStackServer();
server.enqueue(addCommonHeaders(new MockResponse().setBody(stringFromResource("/access.json"))));
server.enqueue(addCommonHeaders(new MockResponse().setResponseCode(204)));
server.enqueue(addCommonHeaders(new MockResponse().setResponseCode(200)
.setBody("{\"Number Not Found\": 0, \"Response Status\": \"200 OK\", \"Errors\": [], \"Number Deleted\": 6, \"Response Body\": \"\"}")));
try {
SwiftApi api = api(server.getUrl("/").toString(), "openstack-swift");
api.getStaticLargeObjectApi("DFW", "myContainer").delete("myObject");
DeleteStaticLargeObjectResponse response = api.getStaticLargeObjectApi("DFW", "myContainer").delete("myObject");
assertEquals(server.getRequestCount(), 2);
assertAuthentication(server);
assertRequest(server.takeRequest(), "DELETE", "/v1/MossoCloudFS_5bcf396e-39dd-45ff-93a1-712b9aba90a9/myContainer/myObject?multipart-manifest=delete");
assertThat(response.status()).isEqualTo("200 OK");
assertThat(response.deleted()).isEqualTo(6);
assertThat(response.notFound()).isZero();
assertThat(response.errors()).isEmpty();
} finally {
server.shutdown();
}
@ -186,15 +192,20 @@ public class StaticLargeObjectApiMockTest extends BaseOpenStackMockTest<SwiftApi
public void testAlreadyDeleted() throws Exception {
MockWebServer server = mockOpenStackServer();
server.enqueue(addCommonHeaders(new MockResponse().setBody(stringFromResource("/access.json"))));
server.enqueue(addCommonHeaders(new MockResponse().setResponseCode(404)));
server.enqueue(addCommonHeaders(new MockResponse().setResponseCode(200)
.setBody("{\"Number Not Found\": 1, \"Response Status\": \"200 OK\", \"Errors\": [], \"Number Deleted\": 0, \"Response Body\": \"\"}")));
try {
SwiftApi api = api(server.getUrl("/").toString(), "openstack-swift");
api.getStaticLargeObjectApi("DFW", "myContainer").delete("myObject");
DeleteStaticLargeObjectResponse response = api.getStaticLargeObjectApi("DFW", "myContainer").delete("myObject");
assertEquals(server.getRequestCount(), 2);
assertAuthentication(server);
assertRequest(server.takeRequest(), "DELETE", "/v1/MossoCloudFS_5bcf396e-39dd-45ff-93a1-712b9aba90a9/myContainer/myObject?multipart-manifest=delete");
assertThat(response.status()).isEqualTo("200 OK");
assertThat(response.deleted()).isZero();
assertThat(response.notFound()).isEqualTo(1);
assertThat(response.errors()).isEmpty();
} finally {
server.shutdown();
}