Fixed admin parameter binding to the payload

This commit is contained in:
Ignasi Barrera 2011-09-14 18:08:58 +02:00
parent 5154ab2ee8
commit 5364ec33da
3 changed files with 66 additions and 3 deletions

View File

@ -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<Client> createClient(@PayloadParam("name") String clientname,
@PayloadParam("admin") @ParamParser(AdminFlagFromCreateClientOptions.class) CreateClientOptions options);

View File

@ -0,0 +1,62 @@
/**
*
* 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.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 extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams)
{
Map<String, Object> params =
Maps.transformValues(postParams, new Function<String, Object>() {
@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);
}
}

View File

@ -225,12 +225,12 @@ public class ChefAsyncClientTest extends RestClientTest<ChefAsyncClient> {
}
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<ChefAsyncClient> 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);