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 new file mode 100644 index 0000000000..cb6d059173 --- /dev/null +++ b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClientTest.java @@ -0,0 +1,171 @@ +/** + * 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.features; + +import static org.jclouds.Constants.PROPERTY_API_VERSION; +import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.Properties; + +import org.jclouds.cloudloadbalancers.CloudLoadBalancersAsyncClient; +import org.jclouds.cloudloadbalancers.CloudLoadBalancersClient; +import org.jclouds.cloudloadbalancers.CloudLoadBalancersContextBuilder; +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.ReleasePayloadAndReturn; +import org.jclouds.rest.RestContextFactory; +import org.jclouds.rest.RestContextSpec; +import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions; +import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; +import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.testng.annotations.Test; + +import com.google.inject.TypeLiteral; + +/** + * Tests behavior of {@code NodeAsyncClient} + * + * @author Dan Lo Bianco + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire +@Test(groups = "unit", testName = "NodeAsyncClientTest") +public class NodeAsyncClientTest extends BaseCloudLoadBalancersAsyncClientTest { + + public void testListNodes() throws SecurityException, NoSuchMethodException, IOException { + Method method = NodeAsyncClient.class.getMethod("listNodes", int.class); + HttpRequest httpRequest = processor.createRequest(method, 2); + + assertRequestLineEquals(httpRequest, + "GET https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/2/nodes HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, UnwrapNodes.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testGetNode() throws SecurityException, NoSuchMethodException, IOException { + Method method = NodeAsyncClient.class.getMethod("getNode", int.class, int.class); + HttpRequest httpRequest = processor.createRequest(method, 2, 3); + + assertRequestLineEquals(httpRequest, + "GET https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/2/nodes/3 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, UnwrapNode.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testCreateNodeWithType() throws SecurityException, NoSuchMethodException, IOException { + Method method = NodeAsyncClient.class.getMethod("createNode", int.class, NodeRequest.class); + HttpRequest httpRequest = processor.createRequest(method, 3, NodeRequest.builder(). + address("192.168.1.1").port(8080).build()); + + assertRequestLineEquals(httpRequest, + "POST https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/3/nodes HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals( + httpRequest, + "{\"node\":{\"address\":\"192.168.1.1\",\"port\":8080,\"condition\":\"ENABLED\"}}", + "application/json", false); + + assertResponseParserClassEquals(method, httpRequest, UnwrapNode.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testModifyNode() throws SecurityException, NoSuchMethodException, IOException { + Method method = NodeAsyncClient.class.getMethod("modifyNode", int.class, int.class, + NodeAttributes.class); + HttpRequest httpRequest = processor.createRequest(method, 7, 8, Builder.condition(Condition.DISABLED).weight(13)); + + assertRequestLineEquals(httpRequest, + "PUT https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/7/nodes/8 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, "{\"node\":{\"condition\":\"DISABLED\",\"weight\":13}}", "application/json", false); + + assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class); + + checkFilters(httpRequest); + + } + + public void testRemoveLoadBalancer() throws SecurityException, NoSuchMethodException, IOException { + Method method = NodeAsyncClient.class.getMethod("removeNode", int.class, int.class); + HttpRequest httpRequest = processor.createRequest(method, 4, 9); + + assertRequestLineEquals(httpRequest, + "DELETE https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/4/nodes/9 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: */*\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + @Override + protected TypeLiteral> createTypeLiteral() { + return new TypeLiteral>() { + }; + } + + protected String provider = "cloudloadbalancers"; + + @Override + public RestContextSpec createContextSpec() { + return new RestContextFactory(getProperties()).createContextSpec(provider, "user", "password", new Properties()); + } + + @Override + protected Properties getProperties() { + Properties overrides = new Properties(); + overrides.setProperty(PROPERTY_REGIONS, "US"); + overrides.setProperty(PROPERTY_API_VERSION, "1"); + overrides.setProperty(provider + ".endpoint", "https://auth"); + overrides.setProperty(provider + ".contextbuilder", CloudLoadBalancersContextBuilder.class.getName()); + return overrides; + } +} diff --git a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeClientLiveTest.java b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeClientLiveTest.java new file mode 100644 index 0000000000..3def14665d --- /dev/null +++ b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeClientLiveTest.java @@ -0,0 +1,141 @@ +/** + * 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.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.logging.Logger; + +import org.jclouds.cloudloadbalancers.domain.LoadBalancer; +import org.jclouds.cloudloadbalancers.domain.LoadBalancerRequest; +import org.jclouds.cloudloadbalancers.domain.Node; +import org.jclouds.cloudloadbalancers.domain.NodeAttributes; +import org.jclouds.cloudloadbalancers.domain.NodeRequest; +import org.jclouds.cloudloadbalancers.domain.VirtualIP.Type; +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; + +/** + * Tests behavior of {@code NodeClient} + * + * @author Dan Lo Bianco + */ +@Test(groups = "live", singleThreaded = true, testName = "NodeClientLiveTest") +public class NodeClientLiveTest extends BaseCloudLoadBalancersClientLiveTest { + private Map> nodes = new HashMap>(); + + @BeforeGroups(groups = "live") + protected void setup() { + assertTrue(client.getConfiguredRegions().size() > 0, "Need to have some regions!"); + Logger.getAnonymousLogger().info("running against regions "+client.getConfiguredRegions()); + for (String region : client.getConfiguredRegions()) { + Logger.getAnonymousLogger().info("starting lb in region " + region); + LoadBalancer lb = client.getLoadBalancerClient(region).createLoadBalancer( + LoadBalancerRequest.builder().name(prefix + "-" + region).protocol("HTTP").port(80).virtualIPType( + Type.PUBLIC).node(NodeRequest.builder().address("192.168.1.1").port(8080).build()).build()); + nodes.put(lb, new HashSet()); + + assert loadBalancerActive.apply(lb) : lb; + } + } + + @AfterGroups(groups = "live") + protected void tearDown() { + for (Entry> entry : nodes.entrySet()) { + LoadBalancer lb = entry.getKey(); + for (Node n : entry.getValue()) { + client.getNodeClient(lb.getRegion()).removeNode(lb.getId(), n.getId()); + assertEquals(client.getNodeClient(lb.getRegion()). + getNode(lb.getId(), n.getId()), null); + } + + client.getLoadBalancerClient(lb.getRegion()).removeLoadBalancer(lb.getId()); + assert loadBalancerDeleted.apply(lb) : lb; + } + super.tearDown(); + } + + public void testListNodes() throws Exception { + for (LoadBalancer lb : nodes.keySet()) { + Set response = client.getNodeClient(lb.getRegion()).listNodes(lb.getId()); + assert null != response; + assertTrue(response.size() >= 0); + for (Node n : response) { + assert n.getId() != -1 : n; + assert n.getCondition() != null : n; + assert n.getAddress() != null : n; + assert n.getPort() != -1 : n; + assert n.getStatus() != null : n; + assert n.getWeight() != -1 : n; + + Node getDetails = client.getNodeClient(lb.getRegion()).getNode(lb.getId(), n.getId()); + System.out.println(n.toString()); + try { + assertEquals(getDetails.getId(), n.getId()); + assertEquals(getDetails.getCondition(), n.getCondition()); + assertEquals(getDetails.getAddress(), n.getAddress()); + assertEquals(getDetails.getPort(), n.getPort()); + assertEquals(getDetails.getStatus(), n.getStatus()); + assertEquals(getDetails.getWeight(), n.getWeight()); + } catch (AssertionError e) { + throw new AssertionError(String.format("%s\n%s - %s", e.getMessage(),getDetails, n)); + } + } + } + } + + public void testCreateNode() throws Exception { + for (LoadBalancer lb : nodes.keySet()) { + String region = lb.getRegion(); + Logger.getAnonymousLogger().info("starting node on loadbalancer "+lb.getId()+" in region "+region); + Node n = client.getNodeClient(region).createNode(lb.getId(), + NodeRequest.builder().address("192.168.1.1").port(8080).build()); + assertEquals(lb.getStatus(), Node.Status.ONLINE); + nodes.get(lb).add(n); + assertEquals(client.getNodeClient(region).getNode(lb.getId(), n.getId()).getStatus(), Node.Status.ONLINE); + + Node newNode = client.getNodeClient(region).getNode(lb.getId(), n.getId()); + assertEquals(newNode.getStatus(), Node.Status.ONLINE); + } + } + + @Test(dependsOnMethods = "testCreateNode") + public void testModifyNode() throws Exception { + for (Entry> entry : nodes.entrySet()) { + for (Node n : entry.getValue()) { + String region = entry.getKey().getRegion(); + client.getNodeClient(region).modifyNode(entry.getKey().getId(), n.getId(), + NodeAttributes.Builder.weight(23)); + assertEquals(client.getNodeClient(region) + .getNode(entry.getKey().getId(), n.getId()).getStatus(), Node.Status.ONLINE); + + Node newNode = client.getNodeClient(region).getNode(entry.getKey().getId(), n.getId()); + assertEquals(newNode.getStatus(), Node.Status.ONLINE); + assertEquals(newNode.getWeight(), (Integer)23); + } + } + } +} diff --git a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodeTest.java b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodeTest.java new file mode 100644 index 0000000000..734eb4d897 --- /dev/null +++ b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodeTest.java @@ -0,0 +1,53 @@ +/** + * 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 { + + @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 new file mode 100644 index 0000000000..35b43f8094 --- /dev/null +++ b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodesTest.java @@ -0,0 +1,61 @@ +/** + * 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 diff --git a/apis/cloudloadbalancers/src/test/resources/getnode.json b/apis/cloudloadbalancers/src/test/resources/getnode.json new file mode 100644 index 0000000000..f78881ea75 --- /dev/null +++ b/apis/cloudloadbalancers/src/test/resources/getnode.json @@ -0,0 +1,9 @@ +{"node": { + "id":410, + "address":"10.1.1.1", + "port":80, + "condition":"ENABLED", + "status":"ONLINE", + "weight":12 + } +} \ No newline at end of file diff --git a/apis/cloudloadbalancers/src/test/resources/listnodes.json b/apis/cloudloadbalancers/src/test/resources/listnodes.json new file mode 100644 index 0000000000..65ec77bf4b --- /dev/null +++ b/apis/cloudloadbalancers/src/test/resources/listnodes.json @@ -0,0 +1,28 @@ +{ + "nodes": [ + { + "id":"410", + "address":"10.1.1.1", + "port":80, + "condition":"ENABLED", + "status":"ONLINE", + "weight":3 + }, + { + "id":"411", + "address":"10.1.1.2", + "port":80, + "condition":"ENABLED", + "status":"ONLINE", + "weight":8 + }, + { + "id":"412", + "address":"10.1.1.3", + "port":80, + "condition":"DISABLED", + "status":"ONLINE", + "weight":12 + } + ] +} \ No newline at end of file diff --git a/providers/cloudloadbalancers-uk/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKNodeClientLiveTest.java b/providers/cloudloadbalancers-uk/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKNodeClientLiveTest.java new file mode 100644 index 0000000000..6de0b4ed92 --- /dev/null +++ b/providers/cloudloadbalancers-uk/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKNodeClientLiveTest.java @@ -0,0 +1,33 @@ +/** + * 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.rackspace.cloudloadbalancers; + +import org.jclouds.cloudloadbalancers.features.NodeClientLiveTest; +import org.testng.annotations.Test; + +/** + * + * @author Dan Lo Bianco + */ +@Test(groups = "live", singleThreaded = true) +public class CloudLoadBalancersUKNodeClientLiveTest extends NodeClientLiveTest { + public CloudLoadBalancersUKNodeClientLiveTest() { + provider = "cloudloadbalancers-uk"; + } +} diff --git a/providers/cloudloadbalancers-us/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSLoadBalancerClientLiveTest.java b/providers/cloudloadbalancers-us/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSLoadBalancerClientLiveTest.java new file mode 100644 index 0000000000..6610ebcb0e --- /dev/null +++ b/providers/cloudloadbalancers-us/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSLoadBalancerClientLiveTest.java @@ -0,0 +1,33 @@ +/** + * 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.rackspace.cloudloadbalancers; + +import org.jclouds.cloudloadbalancers.features.LoadBalancerClientLiveTest; +import org.testng.annotations.Test; + +/** + * + * @author Dan Lo Bianco + */ +@Test(groups = "live", singleThreaded = true) +public class CloudLoadBalancersUSLoadBalancerClientLiveTest extends LoadBalancerClientLiveTest { + public CloudLoadBalancersUSLoadBalancerClientLiveTest() { + provider = "cloudloadbalancers-us"; + } +}