From 5364ec33da8e9f47a2cee67e205538344d389f6b Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Wed, 14 Sep 2011 18:08:58 +0200 Subject: [PATCH] Fixed admin parameter binding to the payload --- .../org/jclouds/chef/ChefAsyncClient.java | 3 +- .../BindCreateClientOptionsToJsonPayload.java | 62 +++++++++++++++++++ .../org/jclouds/chef/ChefAsyncClientTest.java | 4 +- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 apis/chef/src/main/java/org/jclouds/chef/binders/BindCreateClientOptionsToJsonPayload.java diff --git a/apis/chef/src/main/java/org/jclouds/chef/ChefAsyncClient.java b/apis/chef/src/main/java/org/jclouds/chef/ChefAsyncClient.java index bd265e4f17..b22dc3835a 100644 --- a/apis/chef/src/main/java/org/jclouds/chef/ChefAsyncClient.java +++ b/apis/chef/src/main/java/org/jclouds/chef/ChefAsyncClient.java @@ -36,6 +36,7 @@ import org.jclouds.chef.binders.AdminFlagFromCreateClientOptions; import org.jclouds.chef.binders.BindAdminClientToJsonPayload; import org.jclouds.chef.binders.BindChecksumsToJsonPayload; import org.jclouds.chef.binders.BindClientnameToJsonPayload; +import org.jclouds.chef.binders.BindCreateClientOptionsToJsonPayload; import org.jclouds.chef.binders.BindGenerateKeyForClientToJsonPayload; import org.jclouds.chef.binders.BindIsCompletedToJsonPayload; import org.jclouds.chef.binders.BindNameToJsonPayload; @@ -162,7 +163,7 @@ public interface ChefAsyncClient { @POST @Path("/clients") - @MapBinder(BindToJsonPayload.class) + @MapBinder(BindCreateClientOptionsToJsonPayload.class) ListenableFuture createClient(@PayloadParam("name") String clientname, @PayloadParam("admin") @ParamParser(AdminFlagFromCreateClientOptions.class) CreateClientOptions options); diff --git a/apis/chef/src/main/java/org/jclouds/chef/binders/BindCreateClientOptionsToJsonPayload.java b/apis/chef/src/main/java/org/jclouds/chef/binders/BindCreateClientOptionsToJsonPayload.java new file mode 100644 index 0000000000..34a6397549 --- /dev/null +++ b/apis/chef/src/main/java/org/jclouds/chef/binders/BindCreateClientOptionsToJsonPayload.java @@ -0,0 +1,62 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.chef.binders; + +import java.util.Map; + +import javax.inject.Inject; + +import org.jclouds.chef.options.CreateClientOptions; +import org.jclouds.http.HttpRequest; +import org.jclouds.json.Json; +import org.jclouds.rest.binders.BindToJsonPayload; + +import com.google.common.base.Function; +import com.google.common.collect.Maps; + +/** + * Bind the parameters of a {@link CreateClientOptions} to the payload, taking care of transforming + * all boolean strings to boolean values. + * + * @author Ignasi Barrera + */ +public class BindCreateClientOptionsToJsonPayload extends BindToJsonPayload +{ + @Inject + public BindCreateClientOptionsToJsonPayload(Json jsonBinder) { + super(jsonBinder); + } + + @Override + public R bindToRequest(R request, Map postParams) + { + Map params = + Maps.transformValues(postParams, new Function() { + @Override + public Object apply(String input) { + // Transform boolean values to Boolean objects so they are serialized as boolean + return input.equals("true") || input.equals("false") ? Boolean.valueOf(input) + : input; + } + }); + + return bindToRequest(request, (Object) params); + } + +} diff --git a/apis/chef/src/test/java/org/jclouds/chef/ChefAsyncClientTest.java b/apis/chef/src/test/java/org/jclouds/chef/ChefAsyncClientTest.java index d75d784b6e..51900f9ad4 100644 --- a/apis/chef/src/test/java/org/jclouds/chef/ChefAsyncClientTest.java +++ b/apis/chef/src/test/java/org/jclouds/chef/ChefAsyncClientTest.java @@ -225,12 +225,12 @@ public class ChefAsyncClientTest extends RestClientTest { } public void testCreateAdminClient() throws SecurityException, NoSuchMethodException, IOException { - Method method = ChefAsyncClient.class.getMethod("createClient", String.class); + Method method = ChefAsyncClient.class.getMethod("createClient", String.class, CreateClientOptions.class); GeneratedHttpRequest httpRequest = processor.createRequest(method, "client", CreateClientOptions.Builder.admin()); assertRequestLineEquals(httpRequest, "POST http://localhost:4000/clients HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\nX-Chef-Version: 0.9.8\n"); - assertPayloadEquals(httpRequest, "{\"name\":\"client\", \"admin\": true}", "application/json", false); + assertPayloadEquals(httpRequest, "{\"admin\":true,\"name\":\"client\"}", "application/json", false); assertResponseParserClassEquals(method, httpRequest, ParseJson.class); assertSaxResponseParserClassEquals(method, null);