From 42ba8e9de406a1334e9d02617f2ee7db46c3ee7a Mon Sep 17 00:00:00 2001 From: danikov Date: Wed, 16 Nov 2011 19:02:28 +0000 Subject: [PATCH] get rid of unnecessary son parsers and use handy SelectJson annotation --- .../features/NodeAsyncClient.java | 10 ++- .../functions/UnwrapNode.java | 59 ----------------- .../functions/UnwrapNodes.java | 63 ------------------- .../features/NodeAsyncClientTest.java | 9 ++- .../functions/UnwrapNodeTest.java | 53 ---------------- .../functions/UnwrapNodesTest.java | 61 ------------------ 6 files changed, 8 insertions(+), 247 deletions(-) delete mode 100644 apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNode.java delete mode 100644 apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodes.java delete mode 100644 apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodeTest.java delete mode 100644 apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodesTest.java diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClient.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClient.java index e8876db96e..e04b54d2c2 100644 --- a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClient.java +++ b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClient.java @@ -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) diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNode.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNode.java deleted file mode 100644 index 310fb126a8..0000000000 --- a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNode.java +++ /dev/null @@ -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, InvocationContext { - - private final ParseJson> json; - - @Inject - UnwrapNode(ParseJson> json) { - this.json = json; - } - - @Override - public Node apply(HttpResponse arg0) { - Map 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; - } - -} \ No newline at end of file diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodes.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodes.java deleted file mode 100644 index ebb7a538ec..0000000000 --- a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodes.java +++ /dev/null @@ -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>, - InvocationContext { - - private final ParseJson>> json; - - @Inject - UnwrapNodes(ParseJson>> json) { - this.json = json; - } - - @Override - public Set apply(HttpResponse arg0) { - Map> map = json.apply(arg0); - if (map.size() == 0) - return ImmutableSet. of(); - ; - return ImmutableSet.copyOf(Iterables.get(map.values(), 0)); - } - - @Override - public UnwrapNodes setContext(HttpRequest request) { - return this; - } - -} \ No newline at end of file diff --git a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClientTest.java b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClientTest.java index cb6d059173..28ba926a26 100644 --- a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClientTest.java +++ b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClientTest.java @@ -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 { - - @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 parser(Injector i) { - return i.getInstance(UnwrapNode.class); - } -} diff --git a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodesTest.java b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodesTest.java deleted file mode 100644 index 35b43f8094..0000000000 --- a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodesTest.java +++ /dev/null @@ -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 { - - @Override - public String resource() { - return "/listnodes.json"; - } - - @Override - public Set 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> parser(Injector i) { - return i.getInstance(UnwrapNodes.class); - } -} \ No newline at end of file