From 6f463f1f7979ab5856d8955027632d4d1d096b9c Mon Sep 17 00:00:00 2001 From: andreisavu Date: Tue, 10 Jan 2012 02:11:24 +0200 Subject: [PATCH] Extensive testing using HTTP Expect tests --- .../features/FirewallClientExpectTest.java | 314 ++++++++++++++++++ .../parse/ListFirewallRulesResponseTest.java | 1 - .../createfirewallrulesresponse.json | 1 + .../createportforwardingrulesresponse.json | 1 + .../deletefirewallrulesresponse.json | 1 + .../deleteportforwardingrulesresponse.json | 1 + .../resources/getfirewallrulesresponse.json | 2 + .../getportforwardingrulesresponse.json | 2 + 8 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientExpectTest.java create mode 100644 apis/cloudstack/src/test/resources/createfirewallrulesresponse.json create mode 100644 apis/cloudstack/src/test/resources/createportforwardingrulesresponse.json create mode 100644 apis/cloudstack/src/test/resources/deletefirewallrulesresponse.json create mode 100644 apis/cloudstack/src/test/resources/deleteportforwardingrulesresponse.json create mode 100644 apis/cloudstack/src/test/resources/getfirewallrulesresponse.json create mode 100644 apis/cloudstack/src/test/resources/getportforwardingrulesresponse.json diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientExpectTest.java new file mode 100644 index 0000000000..1e1d27297d --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientExpectTest.java @@ -0,0 +1,314 @@ +/** + * 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.cloudstack.features; + +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; +import org.jclouds.cloudstack.domain.Account; +import org.jclouds.cloudstack.domain.AsyncCreateResponse; +import org.jclouds.cloudstack.domain.FirewallRule; +import org.jclouds.cloudstack.domain.PortForwardingRule; +import org.jclouds.cloudstack.domain.User; +import org.jclouds.date.internal.SimpleDateFormatDateService; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; + +import java.net.URI; +import java.util.Set; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +/** + * Test the CloudStack FirewallClient + * + * @author Andrei Savu + */ +@Test(groups = "unit", testName = "FirewallClientExpectTest") +public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest { + + public void testListFirewallRulesWhenResponseIs2xx() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listFirewallRules&" + + "apiKey=identity&signature=MktZKKH3USVKiC9SlYTSHMCaCcg%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/listfirewallrulesresponse.json")) + .build()) + .getFirewallClient(); + + assertEquals(client.listFirewallRules(), + ImmutableSet.of( + FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30) + .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state("Active").CIDRs("0.0.0.0/0").build(), + FirewallRule.builder().id(2016).protocol(FirewallRule.Protocol.TCP).startPort(22) + .endPort(22).ipAddressId(2).ipAddress("10.27.27.51").state("Active").CIDRs("0.0.0.0/0").build(), + FirewallRule.builder().id(10).protocol(FirewallRule.Protocol.TCP).startPort(22) + .endPort(22).ipAddressId(8).ipAddress("10.27.27.57").state("Active").CIDRs("0.0.0.0/0").build() + )); + } + + public void testListFirewallRulesWhenReponseIs404() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listFirewallRules&" + + "apiKey=identity&signature=MktZKKH3USVKiC9SlYTSHMCaCcg%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(404) + .build()) + .getFirewallClient(); + + assertEquals(client.listFirewallRules(), ImmutableSet.of()); + } + + public void testGetFirewallRuleWhenResponseIs2xx() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listFirewallRules&" + + "id=2017&apiKey=identity&signature=0r5iL%2Bzix9rmD07lJIOhY68mYY0%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/getfirewallrulesresponse.json")) + .build()) + .getFirewallClient(); + + assertEquals(client.getFirewallRule(2017), + FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30) + .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state("Active").CIDRs("0.0.0.0/0").build() + ); + } + + public void testGetFirewallRuleWhenResponseIs404() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listFirewallRules&" + + "id=4&apiKey=identity&signature=PPX5U9kmaS116SgG4Ihf8xK%2BcSE%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(404) + .build()) + .getFirewallClient(); + + assertNull(client.getFirewallRule(4)); + } + + public void testCreateFirewallRuleForIpAndProtocol() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=createFirewallRule&" + + "ipaddressid=2&protocol=TCP&apiKey=identity&signature=d0MZ%2FyhQPAaV%2BYQmfZsQtQL2C28%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/createfirewallrulesresponse.json")) + .build()) + .getFirewallClient(); + + AsyncCreateResponse response = client.createFirewallRuleForIpAndProtocol(2, FirewallRule.Protocol.TCP); + assertEquals(response.getJobId(), 2036); + assertEquals(response.getId(), 2017); + } + + public void testDeleteFirewallRule() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=deleteFirewallRule&id=2015&apiKey=identity&signature=%2FT5FAO2yGPctaPmg7TEtIEFW3EU%3D")) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/deletefirewallrulesresponse.json")) + .build()) + .getFirewallClient(); + + client.deleteFirewallRule(2015); + } + + public void testListPortForwardingRulesWhenResponseIs2xx() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listPortForwardingRules&apiKey=identity&signature=YFBu1VOSkiDKxm0K42sIXJWy%2BBo%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/listportforwardingrulesresponse.json")) + .build()) + .getFirewallClient(); + + Set cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); + + assertEquals(client.listPortForwardingRules(), + ImmutableSet.of( + PortForwardingRule.builder().id(15).privatePort(22).protocol("tcp").publicPort(2022).virtualMachineId(3) + .virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build(), + PortForwardingRule.builder().id(18).privatePort(22).protocol("tcp").publicPort(22).virtualMachineId(89) + .virtualMachineName("i-3-89-VM").IPAddressId(34).IPAddress("72.52.126.63").state("Active").build()) + ); + } + + public void testListPortForwardingRulesWhenReponseIs404() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listPortForwardingRules&apiKey=identity&signature=YFBu1VOSkiDKxm0K42sIXJWy%2BBo%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(404) + .build()) + .getFirewallClient(); + + assertEquals(client.listPortForwardingRules(), ImmutableSet.of()); + } + + public void testGetPortForwardingRuleWhenResponseIs2xx() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listPortForwardingRules&id=15&apiKey=identity&signature=ABJsciF4n2tXaiyUmEvc3oYh9MA%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/getportforwardingrulesresponse.json")) + .build()) + .getFirewallClient(); + + Set cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); + + assertEquals(client.getPortForwardingRule(15), + PortForwardingRule.builder().id(15).privatePort(22).protocol("tcp").publicPort(2022).virtualMachineId(3) + .virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build()); + } + + public void testGetPortForwardingRuleWhenResponseIs404() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listPortForwardingRules&id=4&apiKey=identity&signature=CTOmmIOGIiZx0YATqh%2FFk0zIplw%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(404) + .build()) + .getFirewallClient(); + + assertNull(client.getPortForwardingRule(4)); + } + + public void testCreatePortForwardingRuleForVirtualMachine() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=createPortForwardingRule&" + + "ipaddressid=2&publicport=22&protocol=tcp&virtualmachineid=1234&privateport=22&" + + "apiKey=identity&signature=84dtGzQp0G6k3z3Gkc3F%2FHBNS2Y%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/createportforwardingrulesresponse.json")) + .build()) + .getFirewallClient(); + + AsyncCreateResponse response = client.createPortForwardingRuleForVirtualMachine(2, "tcp", 22, 1234, 22); + assertEquals(response.getJobId(), 2035); + assertEquals(response.getId(), 2015); + } + + public void testDeletePortForwardingRule() { + FirewallClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=deletePortForwardingRule&id=2015&apiKey=identity&signature=2UE7KB3wm5ocmR%2BGMNFKPKfiDo8%3D")) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/deleteportforwardingrulesresponse.json")) + .build()) + .getFirewallClient(); + + client.deletePortForwardingRule(2015); + } +} \ No newline at end of file diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListFirewallRulesResponseTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListFirewallRulesResponseTest.java index 642a5cbee3..be524452cd 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListFirewallRulesResponseTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListFirewallRulesResponseTest.java @@ -58,7 +58,6 @@ public class ListFirewallRulesResponseTest extends BaseSetParserTest expected() { - Set cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); return ImmutableSet.of( FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30) .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state("Active").CIDRs("0.0.0.0/0").build(), diff --git a/apis/cloudstack/src/test/resources/createfirewallrulesresponse.json b/apis/cloudstack/src/test/resources/createfirewallrulesresponse.json new file mode 100644 index 0000000000..f6a727b408 --- /dev/null +++ b/apis/cloudstack/src/test/resources/createfirewallrulesresponse.json @@ -0,0 +1 @@ +{ "createfirewallruleresponse" : {"jobid":2036,"id":2017} } \ No newline at end of file diff --git a/apis/cloudstack/src/test/resources/createportforwardingrulesresponse.json b/apis/cloudstack/src/test/resources/createportforwardingrulesresponse.json new file mode 100644 index 0000000000..cf29e1cbe3 --- /dev/null +++ b/apis/cloudstack/src/test/resources/createportforwardingrulesresponse.json @@ -0,0 +1 @@ +{ "createportforwardingruleresponse" : {"jobid":2035,"id":2015} } \ No newline at end of file diff --git a/apis/cloudstack/src/test/resources/deletefirewallrulesresponse.json b/apis/cloudstack/src/test/resources/deletefirewallrulesresponse.json new file mode 100644 index 0000000000..abf5b6da0a --- /dev/null +++ b/apis/cloudstack/src/test/resources/deletefirewallrulesresponse.json @@ -0,0 +1 @@ +{ "deletefirewallruleresponse" : {"jobid":2037} } \ No newline at end of file diff --git a/apis/cloudstack/src/test/resources/deleteportforwardingrulesresponse.json b/apis/cloudstack/src/test/resources/deleteportforwardingrulesresponse.json new file mode 100644 index 0000000000..552ad94410 --- /dev/null +++ b/apis/cloudstack/src/test/resources/deleteportforwardingrulesresponse.json @@ -0,0 +1 @@ +{ "deleteportforwardingruleresponse" : {"jobid":2038} } \ No newline at end of file diff --git a/apis/cloudstack/src/test/resources/getfirewallrulesresponse.json b/apis/cloudstack/src/test/resources/getfirewallrulesresponse.json new file mode 100644 index 0000000000..88d3d47990 --- /dev/null +++ b/apis/cloudstack/src/test/resources/getfirewallrulesresponse.json @@ -0,0 +1,2 @@ +{ "listfirewallrulesresponse" : { "count":1 ,"firewallrule" : [ + {"id":2017,"protocol":"tcp","startport":"30","endport":"35","ipaddressid":2,"ipaddress":"10.27.27.51","state":"Active","cidrlist":"0.0.0.0/0"} ] } } \ No newline at end of file diff --git a/apis/cloudstack/src/test/resources/getportforwardingrulesresponse.json b/apis/cloudstack/src/test/resources/getportforwardingrulesresponse.json new file mode 100644 index 0000000000..3ea044d8fe --- /dev/null +++ b/apis/cloudstack/src/test/resources/getportforwardingrulesresponse.json @@ -0,0 +1,2 @@ +{ "listportforwardingrulesresponse" : { "portforwardingrule" : [ + {"id":15,"privateport":"22","protocol":"tcp","publicport":"2022","virtualmachineid":3,"virtualmachinename":"i-3-3-VM","ipaddressid":3,"ipaddress":"72.52.126.32","state":"Active","cidrlist":"0.0.0.0/1,128.0.0.0/1"} ] } } \ No newline at end of file