Issue 125: applied to cloudservers

This commit is contained in:
Adrian Cole 2011-02-14 17:00:05 +01:00
parent b86fe9eec3
commit d4018a5828
17 changed files with 91 additions and 823 deletions

View File

@ -32,16 +32,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jclouds.http.functions.ReturnFalseOn404;
import org.jclouds.cloudservers.binders.BindAdminPassToJsonPayload;
import org.jclouds.cloudservers.binders.BindBackupScheduleToJsonPayload;
import org.jclouds.cloudservers.binders.BindConfirmResizeToJsonPayload;
import org.jclouds.cloudservers.binders.BindCreateImageToJsonPayload;
import org.jclouds.cloudservers.binders.BindRebootTypeToJsonPayload;
import org.jclouds.cloudservers.binders.BindResizeFlavorToJsonPayload;
import org.jclouds.cloudservers.binders.BindRevertResizeToJsonPayload;
import org.jclouds.cloudservers.binders.BindServerNameToJsonPayload;
import org.jclouds.cloudservers.binders.BindSharedIpGroupToJsonPayload;
import org.jclouds.cloudservers.domain.Addresses;
import org.jclouds.cloudservers.domain.BackupSchedule;
import org.jclouds.cloudservers.domain.Flavor;
@ -53,12 +44,14 @@ import org.jclouds.cloudservers.options.CreateServerOptions;
import org.jclouds.cloudservers.options.CreateSharedIpGroupOptions;
import org.jclouds.cloudservers.options.ListOptions;
import org.jclouds.cloudservers.options.RebuildServerOptions;
import org.jclouds.http.functions.ReturnFalseOn404;
import org.jclouds.openstack.filters.AddTimestampQuery;
import org.jclouds.openstack.filters.AuthenticateRequest;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Endpoint;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.MapBinder;
import org.jclouds.rest.annotations.Payload;
import org.jclouds.rest.annotations.PayloadParam;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
@ -123,8 +116,9 @@ public interface CloudServersAsyncClient {
@POST
@QueryParams(keys = "format", values = "json")
@Path("/servers/{id}/action")
ListenableFuture<Void> rebootServer(@PathParam("id") int id,
@BinderParam(BindRebootTypeToJsonPayload.class) RebootType rebootType);
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"reboot\":%7B\"type\":\"{type}\"%7D%7D")
ListenableFuture<Void> rebootServer(@PathParam("id") int id, @PayloadParam("type") RebootType rebootType);
/**
* @see CloudServersClient#resizeServer
@ -132,8 +126,9 @@ public interface CloudServersAsyncClient {
@POST
@QueryParams(keys = "format", values = "json")
@Path("/servers/{id}/action")
ListenableFuture<Void> resizeServer(@PathParam("id") int id,
@BinderParam(BindResizeFlavorToJsonPayload.class) int flavorId);
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"resize\":%7B\"flavorId\":{flavorId}%7D%7D")
ListenableFuture<Void> resizeServer(@PathParam("id") int id, @PayloadParam("flavorId") int flavorId);
/**
* @see CloudServersClient#confirmResizeServer
@ -142,7 +137,8 @@ public interface CloudServersAsyncClient {
@QueryParams(keys = "format", values = "json")
@Path("/servers/{id}/action")
@Produces(MediaType.APPLICATION_JSON)
ListenableFuture<Void> confirmResizeServer(@PathParam("id") @BinderParam(BindConfirmResizeToJsonPayload.class) int id);
@Payload("{\"confirmResize\":null}")
ListenableFuture<Void> confirmResizeServer(@PathParam("id") int id);
/**
* @see CloudServersClient#revertResizeServer
@ -151,7 +147,8 @@ public interface CloudServersAsyncClient {
@QueryParams(keys = "format", values = "json")
@Path("/servers/{id}/action")
@Produces(MediaType.APPLICATION_JSON)
ListenableFuture<Void> revertResizeServer(@PathParam("id") @BinderParam(BindRevertResizeToJsonPayload.class) int id);
@Payload("{\"revertResize\":null}")
ListenableFuture<Void> revertResizeServer(@PathParam("id") int id);
/**
* @see CloudServersClient#createServer
@ -179,7 +176,8 @@ public interface CloudServersAsyncClient {
*/
@PUT
@Path("/servers/{id}/ips/public/{address}")
@MapBinder(BindSharedIpGroupToJsonPayload.class)
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"shareIp\":%7B\"sharedIpGroupId\":{sharedIpGroupId},\"configureServer\":{configureServer}%7D%7D")
ListenableFuture<Void> shareIp(@PathParam("address") String addressToShare,
@PathParam("id") int serverToTosignBindressTo, @PayloadParam("sharedIpGroupId") int sharedIpGroup,
@PayloadParam("configureServer") boolean configureServer);
@ -198,16 +196,18 @@ public interface CloudServersAsyncClient {
*/
@PUT
@Path("/servers/{id}")
ListenableFuture<Void> changeAdminPass(@PathParam("id") int id,
@BinderParam(BindAdminPassToJsonPayload.class) String adminPass);
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"server\":%7B\"adminPass\":\"{adminPass}\"%7D%7D")
ListenableFuture<Void> changeAdminPass(@PathParam("id") int id, @PayloadParam("adminPass") String adminPass);
/**
* @see CloudServersClient#renameServer
*/
@PUT
@Path("/servers/{id}")
ListenableFuture<Void> renameServer(@PathParam("id") int id,
@BinderParam(BindServerNameToJsonPayload.class) String newName);
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"server\":%7B\"name\":\"{name}\"%7D%7D")
ListenableFuture<Void> renameServer(@PathParam("id") int id, @PayloadParam("name") String newName);
/**
* @see CloudServersClient#listFlavors
@ -268,9 +268,10 @@ public interface CloudServersAsyncClient {
@Unwrap
@Consumes(MediaType.APPLICATION_JSON)
@QueryParams(keys = "format", values = "json")
@MapBinder(BindCreateImageToJsonPayload.class)
@Path("/images")
ListenableFuture<Image> createImageFromServer(@PayloadParam("imageName") String imageName,
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"image\":%7B\"serverId\":{serverId},\"name\":\"{name}\"%7D%7D")
ListenableFuture<Image> createImageFromServer(@PayloadParam("name") String imageName,
@PayloadParam("serverId") int serverId);
/**

View File

@ -1,53 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.binders.BindToJsonPayload;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindAdminPassToJsonPayload extends BindToJsonPayload {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
throw new IllegalStateException("Change Admin Pass is a PUT operation");
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
checkArgument(toBind instanceof String, "this binder is only valid for Strings!");
return super.bindToRequest(request,
ImmutableMap.of("server", ImmutableMap.of("adminPass", checkNotNull(toBind, "adminPass"))));
}
}

View File

@ -1,39 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindConfirmResizeToJsonPayload implements Binder {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
request.setPayload("{\"confirmResize\":null}");
return request;
}
}

View File

@ -1,65 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.binders.BindToJsonPayload;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindCreateImageToJsonPayload extends BindToJsonPayload {
@SuppressWarnings("unused")
private class CreateImageRequest {
final int serverId;
final String name;
private CreateImageRequest(int serverId, String name) {
this.serverId = serverId;
this.name = name;
}
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
CreateImageRequest createRequest = new CreateImageRequest(Integer
.parseInt(checkNotNull(postParams.get("serverId"))), checkNotNull(postParams
.get("imageName")));
return super.bindToRequest(request, ImmutableMap.of("image", createRequest));
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalArgumentException("image is needs parameters");
}
}

View File

@ -1,54 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.cloudservers.domain.RebootType;
import org.jclouds.rest.binders.BindToJsonPayload;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindRebootTypeToJsonPayload extends BindToJsonPayload {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
throw new IllegalStateException("Reboot doesn't take map parameters");
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
checkArgument(toBind instanceof RebootType, "this binder is only valid for RebootTypes!");
return super.bindToRequest(request, ImmutableMap.of("reboot", ImmutableMap.of("type", checkNotNull(
toBind, "type"))));
}
}

View File

@ -1,53 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.binders.BindToJsonPayload;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindResizeFlavorToJsonPayload extends BindToJsonPayload {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
throw new IllegalStateException("Resize doesn't take map parameters");
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
checkArgument(toBind instanceof Integer, "this binder is only valid for integers!");
return super.bindToRequest(request, ImmutableMap.of("resize", ImmutableMap.of("flavorId",
(Integer) checkNotNull(toBind, "flavorId"))));
}
}

View File

@ -1,39 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindRevertResizeToJsonPayload implements Binder {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
request.setPayload("{\"revertResize\":null}");
return request;
}
}

View File

@ -1,53 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.binders.BindToJsonPayload;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindServerNameToJsonPayload extends BindToJsonPayload {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
throw new IllegalStateException("Change Server Name is a PUT operation");
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
checkArgument(toBind instanceof String, "this binder is only valid for Strings!");
return super.bindToRequest(request, ImmutableMap.of("server", ImmutableMap.of("name", checkNotNull(
toBind, "name"))));
}
}

View File

@ -1,66 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.binders.BindToJsonPayload;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindSharedIpGroupToJsonPayload extends BindToJsonPayload {
@SuppressWarnings("unused")
private class ShareIpRequest {
final int sharedIpGroupId;
Boolean configureServer;
private ShareIpRequest(int sharedIpGroupId) {
this.sharedIpGroupId = sharedIpGroupId;
}
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
ShareIpRequest createRequest = new ShareIpRequest(Integer.parseInt(checkNotNull(postParams
.get("sharedIpGroupId"))));
if (Boolean.parseBoolean(checkNotNull(postParams.get("configureServer")))) {
createRequest.configureServer = new Boolean(true);
}
return super.bindToRequest(request, ImmutableMap.of("shareIp", createRequest));
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalStateException("shareIp is needs parameters");
}
}

View File

@ -65,7 +65,7 @@ public class BackupSchedule {
@Override
public String toString() {
return "BackupSchedule [daily=" + daily + ", enabled=" + enabled + ", weekly=" + weekly + "]";
return "[daily=" + daily + ", enabled=" + enabled + ", weekly=" + weekly + "]";
}
@Override

View File

@ -168,8 +168,8 @@ public class CloudServersAsyncClientTest extends RestClientTest<CloudServersAsyn
UnknownHostException {
Method method = CloudServersAsyncClient.class.getMethod("createServer", String.class, int.class, int.class,
createServerOptionsVarargsClass);
HttpRequest request = processor.createRequest(method, "ralphie", 2, 1, withSharedIpGroup(2).withSharedIp(
"127.0.0.1"));
HttpRequest request = processor.createRequest(method, "ralphie", 2, 1,
withSharedIpGroup(2).withSharedIp("127.0.0.1"));
assertRequestLineEquals(request, "POST http://serverManagementUrl/servers?format=json HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Accept: application/json\n");
@ -439,7 +439,8 @@ public class CloudServersAsyncClientTest extends RestClientTest<CloudServersAsyn
assertRequestLineEquals(request, "PUT http://serverManagementUrl/servers/2/ips/public/127.0.0.1 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, "{\"shareIp\":{\"sharedIpGroupId\":3}}", MediaType.APPLICATION_JSON, false);
assertPayloadEquals(request, "{\"shareIp\":{\"sharedIpGroupId\":3,\"configureServer\":false}}",
MediaType.APPLICATION_JSON, false);
assertResponseParserClassEquals(method, request, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);

View File

@ -1,79 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.net.URI;
import javax.ws.rs.HttpMethod;
import org.jclouds.http.HttpRequest;
import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* Tests behavior of {@code BindAdminPassToJsonPayload}
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class BindAdminPassToJsonPayloadTest {
Injector injector = Guice.createInjector(new GsonModule());
@Test(expectedExceptions = IllegalStateException.class)
public void testPostIsIncorrect() {
BindAdminPassToJsonPayload binder = new BindAdminPassToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, ImmutableMap.of("adminPass", "foo"));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustBeString() {
BindAdminPassToJsonPayload binder = new BindAdminPassToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, new File("foo"));
}
@Test
public void testCorrect() {
BindAdminPassToJsonPayload binder = new BindAdminPassToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost"));
binder.bindToRequest(request, "foo");
assertEquals("{\"server\":{\"adminPass\":\"foo\"}}", request.getPayload().getRawContent());
}
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })
public void testNullIsBad() {
BindAdminPassToJsonPayload binder = new BindAdminPassToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost"));
binder.bindToRequest(request, null);
}
}

View File

@ -1,72 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.net.URI;
import javax.ws.rs.HttpMethod;
import org.jclouds.http.HttpRequest;
import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* Tests behavior of {@code BindCreateImageToJsonPayload}
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class BindCreateImageToJsonPayloadTest {
Injector injector = Guice.createInjector(new GsonModule());
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustBeMap() {
BindCreateImageToJsonPayload binder = new BindCreateImageToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, new File("foo"));
}
@Test
public void testCorrect() {
BindCreateImageToJsonPayload binder = new BindCreateImageToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost"));
binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2"));
assertEquals("{\"image\":{\"serverId\":2,\"name\":\"foo\"}}", request.getPayload()
.getRawContent());
}
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })
public void testNullIsBad() {
BindCreateImageToJsonPayload binder = new BindCreateImageToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost"));
binder.bindToRequest(request, null);
}
}

View File

@ -1,89 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.net.URI;
import javax.ws.rs.HttpMethod;
import org.jclouds.http.HttpRequest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.cloudservers.domain.RebootType;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* Tests behavior of {@code BindRebootTypeToJsonPayload}
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class BindRebootTypeToJsonPayloadTest {
Injector injector = Guice.createInjector(new GsonModule());
@Test(expectedExceptions = IllegalStateException.class)
public void testPostIsIncorrect() {
BindRebootTypeToJsonPayload binder = new BindRebootTypeToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, ImmutableMap.of("adminPass", "foo"));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustBeRebootType() {
BindRebootTypeToJsonPayload binder = new BindRebootTypeToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, new File("foo"));
}
@Test
public void testHard() {
BindRebootTypeToJsonPayload binder = new BindRebootTypeToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, RebootType.HARD);
assertEquals("{\"reboot\":{\"type\":\"HARD\"}}", request.getPayload().getRawContent());
}
@Test
public void testSoft() {
BindRebootTypeToJsonPayload binder = new BindRebootTypeToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, RebootType.SOFT);
assertEquals("{\"reboot\":{\"type\":\"SOFT\"}}", request.getPayload().getRawContent());
}
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })
public void testNullIsBad() {
BindRebootTypeToJsonPayload binder = new BindRebootTypeToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, null);
}
}

View File

@ -1,79 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.cloudservers.binders;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.net.URI;
import javax.ws.rs.HttpMethod;
import org.jclouds.http.HttpRequest;
import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* Tests behavior of {@code BindServerNameToJsonPayload}
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class BindServerNameToJsonPayloadTest {
Injector injector = Guice.createInjector(new GsonModule());
@Test(expectedExceptions = IllegalStateException.class)
public void testPostIsIncorrect() {
BindServerNameToJsonPayload binder = new BindServerNameToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, ImmutableMap.of("name", "foo"));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustBeString() {
BindServerNameToJsonPayload binder = new BindServerNameToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, new File("foo"));
}
@Test
public void testCorrect() {
BindServerNameToJsonPayload binder = new BindServerNameToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost"));
binder.bindToRequest(request, "foo");
assertEquals("{\"server\":{\"name\":\"foo\"}}", request.getPayload().getRawContent());
}
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })
public void testNullIsBad() {
BindServerNameToJsonPayload binder = new BindServerNameToJsonPayload();
injector.injectMembers(binder);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost"));
binder.bindToRequest(request, null);
}
}

View File

@ -37,6 +37,8 @@ import javax.ws.rs.Path;
public @interface Payload {
/**
* Note that if you'd like curly braces literally in the payload, urlencode them first.
*
* @see Path#value()
* @see PayloadParam
*/

View File

@ -50,10 +50,15 @@ public class BindMapToStringPayload implements MapBinder {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
GeneratedHttpRequest<?> r = GeneratedHttpRequest.class.cast(request);
String payload = r.getJavaMethod().getAnnotation(Payload.class).value();
if (postParams.size() > 0) {
UriBuilder builder = uriBuilders.get();
builder.path(r.getJavaMethod().getAnnotation(Payload.class).value());
builder.uri(URI.create("http://test/"));
builder.path(payload);
URI fake = builder.buildFromMap(postParams);
return (R) request.toBuilder().payload(Payloads.newStringPayload(fake.getPath())).build();
payload = fake.getPath().substring(1);
}
return (R) request.toBuilder().payload(Payloads.newStringPayload(payload)).build();
}
@Override