get rid of unnecessary son parsers and use handy SelectJson annotation

This commit is contained in:
danikov 2011-11-16 19:02:28 +00:00
parent 575dac2583
commit 42ba8e9de4
6 changed files with 8 additions and 247 deletions

View File

@ -33,12 +33,10 @@ import javax.ws.rs.core.MediaType;
import org.jclouds.cloudloadbalancers.domain.Node;
import org.jclouds.cloudloadbalancers.domain.NodeAttributes;
import org.jclouds.cloudloadbalancers.domain.NodeRequest;
import org.jclouds.cloudloadbalancers.functions.UnwrapNode;
import org.jclouds.cloudloadbalancers.functions.UnwrapNodes;
import org.jclouds.openstack.filters.AuthenticateRequest;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.ResponseParser;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.annotations.WrapWith;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
@ -65,7 +63,7 @@ public interface NodeAsyncClient {
* @see NodeClient#addNodes
*/
@POST
@ResponseParser(UnwrapNodes.class)
@SelectJson("nodes")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
@Path("/loadbalancers/{lbid}/nodes")
@ -86,7 +84,7 @@ public interface NodeAsyncClient {
* @see NodeClient#listNodes
*/
@GET
@ResponseParser(UnwrapNodes.class)
@SelectJson("nodes")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/loadbalancers/{lbid}/nodes")
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
@ -96,7 +94,7 @@ public interface NodeAsyncClient {
* @see NodeClient#getNode
*/
@GET
@ResponseParser(UnwrapNode.class)
@SelectJson("node")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/loadbalancers/{lbid}/nodes/{nid}")
@ExceptionParser(ReturnNullOnNotFoundOr404.class)

View File

@ -1,59 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.cloudloadbalancers.functions;
import java.util.Map;
import javax.inject.Inject;
import org.jclouds.cloudloadbalancers.domain.Node;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseJson;
import org.jclouds.rest.InvocationContext;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* @author Dan Lo Bianco
*/
public class UnwrapNode implements Function<HttpResponse, Node>, InvocationContext<UnwrapNode> {
private final ParseJson<Map<String, Node>> json;
@Inject
UnwrapNode(ParseJson<Map<String, Node>> json) {
this.json = json;
}
@Override
public Node apply(HttpResponse arg0) {
Map<String, Node> map = json.apply(arg0);
if (map == null || map.size() == 0)
return null;
Node n = Iterables.get(map.values(), 0);
return n;
}
public UnwrapNode setContext(HttpRequest request) {
return this;
}
}

View File

@ -1,63 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.cloudloadbalancers.functions;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import org.jclouds.cloudloadbalancers.domain.Node;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseJson;
import org.jclouds.rest.InvocationContext;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
/**
* @author Dan Lo Bianco
*/
public class UnwrapNodes implements Function<HttpResponse, Set<Node>>,
InvocationContext<UnwrapNodes> {
private final ParseJson<Map<String, Set<Node>>> json;
@Inject
UnwrapNodes(ParseJson<Map<String, Set<Node>>> json) {
this.json = json;
}
@Override
public Set<Node> apply(HttpResponse arg0) {
Map<String, Set<Node>> map = json.apply(arg0);
if (map.size() == 0)
return ImmutableSet.<Node> of();
;
return ImmutableSet.copyOf(Iterables.get(map.values(), 0));
}
@Override
public UnwrapNodes setContext(HttpRequest request) {
return this;
}
}

View File

@ -32,9 +32,8 @@ import org.jclouds.cloudloadbalancers.domain.NodeAttributes;
import org.jclouds.cloudloadbalancers.domain.NodeAttributes.Builder;
import org.jclouds.cloudloadbalancers.domain.NodeRequest;
import org.jclouds.cloudloadbalancers.domain.internal.BaseNode.Condition;
import org.jclouds.cloudloadbalancers.functions.UnwrapNode;
import org.jclouds.cloudloadbalancers.functions.UnwrapNodes;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
import org.jclouds.http.functions.ReleasePayloadAndReturn;
import org.jclouds.rest.RestContextFactory;
import org.jclouds.rest.RestContextSpec;
@ -65,7 +64,7 @@ public class NodeAsyncClientTest extends BaseCloudLoadBalancersAsyncClientTest<N
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapNodes.class);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);
@ -82,7 +81,7 @@ public class NodeAsyncClientTest extends BaseCloudLoadBalancersAsyncClientTest<N
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapNode.class);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
@ -103,7 +102,7 @@ public class NodeAsyncClientTest extends BaseCloudLoadBalancersAsyncClientTest<N
"{\"node\":{\"address\":\"192.168.1.1\",\"port\":8080,\"condition\":\"ENABLED\"}}",
"application/json", false);
assertResponseParserClassEquals(method, httpRequest, UnwrapNode.class);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);

View File

@ -1,53 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.cloudloadbalancers.functions;
import org.jclouds.cloudloadbalancers.domain.Node;
import org.jclouds.cloudloadbalancers.domain.Node.Status;
import org.jclouds.cloudloadbalancers.domain.internal.BaseNode.Condition;
import org.jclouds.http.HttpResponse;
import org.jclouds.json.BaseItemParserTest;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.inject.Injector;
/**
*
* @author Dan Lo Bianco
*/
@Test(groups = "unit")
public class UnwrapNodeTest extends BaseItemParserTest<Node> {
@Override
public String resource() {
return "/getnode.json";
}
@Override
public Node expected() {
return Node.builder().id(410).address("10.1.1.1").port(80)
.condition(Condition.ENABLED).status(Status.ONLINE).weight(12).build();
}
@Override
protected Function<HttpResponse, Node> parser(Injector i) {
return i.getInstance(UnwrapNode.class);
}
}

View File

@ -1,61 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.cloudloadbalancers.functions;
import java.util.Set;
import org.jclouds.cloudloadbalancers.domain.Node;
import org.jclouds.cloudloadbalancers.domain.Node.Status;
import org.jclouds.cloudloadbalancers.domain.internal.BaseNode.Condition;
import org.jclouds.http.HttpResponse;
import org.jclouds.json.BaseSetParserTest;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
/**
*
* @author Dan Lo Bianco
*/
@Test(groups = "unit")
public class UnwrapNodesTest extends BaseSetParserTest<Node> {
@Override
public String resource() {
return "/listnodes.json";
}
@Override
public Set<Node> expected() {
return ImmutableSet.of(
Node.builder().id(410).address("10.1.1.1").port(80).
condition(Condition.ENABLED).weight(3).status(Status.ONLINE).build(),
Node.builder().id(411).address("10.1.1.2").port(80).
condition(Condition.ENABLED).weight(8).status(Status.ONLINE).build(),
Node.builder().id(412).address("10.1.1.3").port(80).
condition(Condition.DISABLED).weight(12).status(Status.ONLINE).build());
}
@Override
protected Function<HttpResponse, Set<Node>> parser(Injector i) {
return i.getInstance(UnwrapNodes.class);
}
}