http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/EventApiLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/EventApiLiveTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/EventApiLiveTest.java new file mode 100644 index 0000000..2f4cdef --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/EventApiLiveTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.testng.Assert.assertTrue; + +import java.util.Set; + +import org.jclouds.cloudstack.domain.Event; +import org.jclouds.cloudstack.internal.BaseCloudStackApiLiveTest; +import org.testng.annotations.Test; + +/** + * Tests behavior of {@code EventApi} + */ +@Test(groups = "live", singleThreaded = true, testName = "EventApiLiveTest") +public class EventApiLiveTest extends BaseCloudStackApiLiveTest { + + public void testlistEventTypes() throws Exception { + final Set<String> response = client.getEventApi().listEventTypes(); + assert null != response; + assertTrue(response.size() >= 0); + for (String type : response) { + checkEventType(type); + } + } + + public void testlistEvents() throws Exception { + final Set<Event> response = client.getEventApi().listEvents(); + assert null != response; + assertTrue(response.size() >= 0); + for (Event event : response) { + checkEvent(event); + } + } + + private void checkEvent(Event event) { + assert event.getAccount() != null : event; + assert event.getCreated() != null : event; + assert event.getDescription() != null : event; + assert event.getDomain() != null : event; + assert event.getId() != null : event; + assert event.getLevel() != null : event; + assert event.getState() != null : event; + assert event.getType() != null : event; + assert event.getUsername() != null : event; + } + + protected void checkEventType(String eventType) { + assert eventType != null : eventType; + } + +}
http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/EventApiTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/EventApiTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/EventApiTest.java new file mode 100644 index 0000000..3f70d9d --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/EventApiTest.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.jclouds.reflect.Reflection2.method; + +import java.io.IOException; + +import org.jclouds.Fallbacks.EmptySetOnNotFoundOr404; +import org.jclouds.cloudstack.functions.ParseEventTypesFromHttpResponse; +import org.jclouds.cloudstack.internal.BaseCloudStackApiTest; +import org.jclouds.cloudstack.options.ListEventsOptions; +import org.jclouds.http.functions.ParseFirstJsonValueNamed; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.reflect.Invokable; +/** + * Tests behavior of {@code EventApi} + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during +// surefire +@Test(groups = "unit", testName = "EventApiTest") +public class EventApiTest extends BaseCloudStackApiTest<EventApi> { + + public void testListEventTypes() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(EventApi.class, "listEventTypes"); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.of()); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&listAll=true&command=listEventTypes HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseEventTypesFromHttpResponse.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + + public void testListEvents() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(EventApi.class, "listEvents", ListEventsOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.of()); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&listAll=true&command=listEvents HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testEventsListOptions() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(EventApi.class, "listEvents", ListEventsOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(ListEventsOptions.Builder.account("jclouds"))); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&listAll=true&command=listEvents&account=jclouds HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiExpectTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiExpectTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiExpectTest.java new file mode 100644 index 0000000..fdc2544 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiExpectTest.java @@ -0,0 +1,402 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +import java.net.URI; +import java.util.Set; + +import org.jclouds.cloudstack.CloudStackContext; +import org.jclouds.cloudstack.domain.AsyncCreateResponse; +import org.jclouds.cloudstack.domain.FirewallRule; +import org.jclouds.cloudstack.domain.PortForwardingRule; +import org.jclouds.cloudstack.internal.BaseCloudStackExpectTest; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; + +/** + * Test the CloudStack FirewallApi + */ +@Test(groups = "unit", testName = "FirewallApiExpectTest") +public class FirewallApiExpectTest extends BaseCloudStackExpectTest<FirewallApi> { + + public void testListFirewallRulesWhenResponseIs2xx() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listFirewallRules&listAll=true&" + + "apiKey=identity&signature=9%2BtdTXe2uYLzAexPNgrMy5Tq8hE%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/listfirewallrulesresponse.json")) + .build()); + + Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0"); + 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(FirewallRule.State.ACTIVE) + .CIDRs(CIDRs).build(), + FirewallRule.builder().id("2016").protocol(FirewallRule.Protocol.TCP).startPort(22) + .endPort(22).ipAddressId("2").ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE) + .CIDRs(CIDRs).build(), + FirewallRule.builder().id("10").protocol(FirewallRule.Protocol.TCP).startPort(22) + .endPort(22).ipAddressId("8").ipAddress("10.27.27.57").state(FirewallRule.State.ACTIVE) + .CIDRs(CIDRs).build() + )); + } + + public void testListFirewallRulesWhenReponseIs404() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listFirewallRules&listAll=true&" + + "apiKey=identity&signature=9%2BtdTXe2uYLzAexPNgrMy5Tq8hE%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertEquals(client.listFirewallRules(), ImmutableSet.of()); + } + + public void testGetFirewallRuleWhenResponseIs2xx() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listFirewallRules&listAll=true&" + + "id=2017&apiKey=identity&signature=6coh9Qdwla94TN1Dl008WlhzZUY%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/getfirewallrulesresponse.json")) + .build()); + + assertEquals(client.getFirewallRule("2017"), + FirewallRule.builder().id("2017").protocol(FirewallRule.Protocol.TCP).startPort(30) + .endPort(35).ipAddressId("2").ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE) + .CIDRs(ImmutableSet.of("0.0.0.0/0")).build() + ); + } + + public void testGetFirewallRuleWhenResponseIs404() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listFirewallRules&listAll=true&" + + "id=4&apiKey=identity&signature=rYd8gr7ptdSZlIehBEMQEKsm07Q%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertNull(client.getFirewallRule("4")); + } + + public void testCreateFirewallRuleForIpAndProtocol() { + FirewallApi 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/yhQPAaV%2BYQmfZsQtQL2C28%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/createfirewallrulesresponse.json")) + .build()); + + AsyncCreateResponse response = client.createFirewallRuleForIpAndProtocol("2", FirewallRule.Protocol.TCP); + assertEquals(response.getJobId(), "2036"); + assertEquals(response.getId(), "2017"); + } + + public void testDeleteFirewallRule() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=deleteFirewallRule&id=2015&apiKey=identity&signature=/T5FAO2yGPctaPmg7TEtIEFW3EU%3D")) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/deletefirewallrulesresponse.json")) + .build()); + + client.deleteFirewallRule("2015"); + } + + public void testListPortForwardingRulesWhenResponseIs2xx() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listPortForwardingRules&listAll=true&apiKey=identity&signature=8SXGJZWdcJfVz4V90Pyod12x9dM%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/listportforwardingrulesresponse.json")) + .build()); + + Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); + + assertEquals(client.listPortForwardingRules(), + ImmutableSet.<PortForwardingRule>of( + PortForwardingRule.builder().id("15").privatePort(22).protocol(PortForwardingRule.Protocol.TCP) + .publicPort(2022).virtualMachineId("3").virtualMachineName("i-3-3-VM").IPAddressId("3") + .IPAddress("72.52.126.32").state(PortForwardingRule.State.ACTIVE).CIDRs(cidrs).build(), + PortForwardingRule.builder().id("18").privatePort(22).protocol(PortForwardingRule.Protocol.TCP) + .publicPort(22).virtualMachineId("89").virtualMachineName("i-3-89-VM").IPAddressId("34") + .IPAddress("72.52.126.63").state(PortForwardingRule.State.ACTIVE).build()) + ); + } + + public void testListPortForwardingRulesWhenReponseIs404() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listPortForwardingRules&listAll=true&apiKey=identity&signature=8SXGJZWdcJfVz4V90Pyod12x9dM%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertEquals(client.listPortForwardingRules(), ImmutableSet.of()); + } + + public void testGetPortForwardingRuleWhenResponseIs2xx() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listPortForwardingRules&listAll=true&id=15&apiKey=identity&signature=JL63p6cJzbb9vaffeV4u60IGlWE%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/getportforwardingrulesresponse.json")) + .build()); + + Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); + + assertEquals(client.getPortForwardingRule("15"), + PortForwardingRule.builder().id("15").privatePort(22).protocol(PortForwardingRule.Protocol.TCP) + .publicPort(2022).virtualMachineId("3").virtualMachineName("i-3-3-VM").IPAddressId("3") + .IPAddress("72.52.126.32").state(PortForwardingRule.State.ACTIVE).CIDRs(cidrs).build()); + } + + public void testGetPortForwardingRuleWhenResponseIs404() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listPortForwardingRules&listAll=true&id=4&apiKey=identity&signature=4blbBVn2%2BZfF8HwoglbmtYoDAjs%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertNull(client.getPortForwardingRule("4")); + } + + public void testCreatePortForwardingRuleForVirtualMachine() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder().method("GET") + .endpoint("http://localhost:8080/client/api") + .addQueryParam("response", "json") + .addQueryParam("command", "createPortForwardingRule") + .addQueryParam("ipaddressid", "2") + .addQueryParam("protocol", "tcp") + .addQueryParam("publicport", "22") + .addQueryParam("virtualmachineid", "1234") + .addQueryParam("privateport", "22") + .addQueryParam("apiKey", "identity") + .addQueryParam("signature", "84dtGzQp0G6k3z3Gkc3F/HBNS2Y%3D") + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/createportforwardingrulesresponse.json")) + .build()); + + AsyncCreateResponse response = client.createPortForwardingRuleForVirtualMachine( + "2", PortForwardingRule.Protocol.TCP, 22, "1234", 22); + assertEquals(response.getJobId(), "2035"); + assertEquals(response.getId(), "2015"); + } + + public void testDeletePortForwardingRule() { + FirewallApi 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()); + + client.deletePortForwardingRule("2015"); + } + + public void testListEgressFirewallRulesWhenResponseIs2xx() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listEgressFirewallRules&listAll=true&" + + "apiKey=identity&signature=j3OpRXs7mEwVKs9KIb4ncRKVO9A%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/listegressfirewallrulesresponse.json")) + .build()); + + Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0"); + assertEquals(client.listEgressFirewallRules(), + ImmutableSet.of( + FirewallRule.builder().id("2017").protocol(FirewallRule.Protocol.TCP).startPort(30) + .endPort(35).ipAddressId("2").ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE) + .CIDRs(CIDRs).build(), + FirewallRule.builder().id("2016").protocol(FirewallRule.Protocol.TCP).startPort(22) + .endPort(22).ipAddressId("2").ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE) + .CIDRs(CIDRs).build(), + FirewallRule.builder().id("10").protocol(FirewallRule.Protocol.TCP).startPort(22) + .endPort(22).ipAddressId("8").ipAddress("10.27.27.57").state(FirewallRule.State.ACTIVE) + .CIDRs(CIDRs).build() + )); + } + + public void testListEgressFirewallRulesWhenReponseIs404() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listEgressFirewallRules&listAll=true&" + + "apiKey=identity&signature=j3OpRXs7mEwVKs9KIb4ncRKVO9A%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertEquals(client.listEgressFirewallRules(), ImmutableSet.of()); + } + + public void testGetEgressFirewallRuleWhenResponseIs2xx() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listEgressFirewallRules&listAll=true&" + + "id=2017&apiKey=identity&signature=Hi1K5VA3yd3mk0AmgJ2F6y%2BVzMo%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/getegressfirewallrulesresponse.json")) + .build()); + + assertEquals(client.getEgressFirewallRule("2017"), + FirewallRule.builder().id("2017").protocol(FirewallRule.Protocol.TCP).startPort(30) + .endPort(35).ipAddressId("2").ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE) + .CIDRs(ImmutableSet.of("0.0.0.0/0")).build() + ); + } + + public void testGetEgressFirewallRuleWhenResponseIs404() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listEgressFirewallRules&listAll=true&" + + "id=4&apiKey=identity&signature=dzb5azKxXZsuGrNRJbRHfna7FMo%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertNull(client.getEgressFirewallRule("4")); + } + + public void testCreateEgressFirewallRuleForNetworkAndProtocol() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=createEgressFirewallRule&" + + "networkid=2&protocol=TCP&apiKey=identity&signature=I/OJEqiLp8ZHlZskKUiT5uTRE3M%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/createegressfirewallrulesresponse.json")) + .build()); + + AsyncCreateResponse response = client.createEgressFirewallRuleForNetworkAndProtocol("2", FirewallRule.Protocol.TCP); + assertEquals(response.getJobId(), "2036"); + assertEquals(response.getId(), "2017"); + } + + public void testDeleteEgressFirewallRule() { + FirewallApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=deleteEgressFirewallRule&id=2015&apiKey=identity&signature=S119WNmamKwc5d9qvvkIJznXytg%3D")) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/deleteegressfirewallrulesresponse.json")) + .build()); + + client.deleteEgressFirewallRule("2015"); + } + @Override + protected FirewallApi clientFrom(CloudStackContext context) { + return context.getApi().getFirewallApi(); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiLiveTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiLiveTest.java new file mode 100644 index 0000000..a96fad8 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiLiveTest.java @@ -0,0 +1,233 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static com.google.common.collect.Iterables.find; +import static org.jclouds.cloudstack.predicates.NetworkPredicates.supportsPortForwarding; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.util.NoSuchElementException; +import java.util.Set; + +import org.jclouds.cloudstack.domain.AsyncCreateResponse; +import org.jclouds.cloudstack.domain.FirewallRule; +import org.jclouds.cloudstack.domain.Network; +import org.jclouds.cloudstack.domain.PortForwardingRule; +import org.jclouds.cloudstack.domain.PublicIPAddress; +import org.jclouds.cloudstack.domain.VirtualMachine; +import org.jclouds.cloudstack.internal.BaseCloudStackApiLiveTest; +import org.jclouds.cloudstack.options.CreateFirewallRuleOptions; +import org.jclouds.logging.Logger; +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; + +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.net.HostAndPort; + +/** + * Tests behavior of {@code FirewallApiLiveTest} + */ +@Test(groups = "live", singleThreaded = true, testName = "FirewallApiLiveTest") +public class FirewallApiLiveTest extends BaseCloudStackApiLiveTest { + private PublicIPAddress ip = null; + private VirtualMachine vm; + + private FirewallRule firewallRule; + private FirewallRule egressFirewallRule; + private PortForwardingRule portForwardingRule; + + private Network network; + private boolean networksDisabled; + + @BeforeGroups(groups = "live") + public void setupContext() { + super.setupContext(); + prefix += "rule"; + try { + network = find(client.getNetworkApi().listNetworks(), Predicates.and(supportsPortForwarding(), + new Predicate<Network>() { + @Override + public boolean apply(Network network) { + return network.isDefault() + && !network.isSecurityGroupEnabled() + && network.getAccount().equals(user.getAccount()); + } + })); + + String defaultTemplate = template != null ? template.getImageId() : null; + + vm = VirtualMachineApiLiveTest.createVirtualMachineInNetwork(network, + defaultTemplateOrPreferredInZone(defaultTemplate, client, network.getZoneId()), + client, jobComplete, virtualMachineRunning); + + if (vm.getPassword() != null && !loginCredentials.getOptionalPassword().isPresent()) + loginCredentials = loginCredentials.toBuilder().password(vm.getPassword()).build(); + + } catch (NoSuchElementException e) { + networksDisabled = true; + } + } + + public void testCreatePortForwardingRule() throws Exception { + if (networksDisabled) + return; + while (portForwardingRule == null) { + ip = reuseOrAssociate.apply(network); + try { + AsyncCreateResponse job = client.getFirewallApi() + .createPortForwardingRuleForVirtualMachine(ip.getId(), PortForwardingRule.Protocol.TCP, 22, vm.getId(), 22); + assertTrue(jobComplete.apply(job.getJobId())); + portForwardingRule = client.getFirewallApi().getPortForwardingRule(job.getId()); + + } catch (IllegalStateException e) { + Logger.CONSOLE.error("Failed while trying to allocate ip: " + e); + // very likely an ip conflict, so retry; + } + } + + assertEquals(portForwardingRule.getIPAddressId(), ip.getId()); + assertEquals(portForwardingRule.getVirtualMachineId(), vm.getId()); + assertEquals(portForwardingRule.getPublicPort(), 22); + assertEquals(portForwardingRule.getProtocol(), PortForwardingRule.Protocol.TCP); + + checkPortForwardingRule(portForwardingRule); + checkSSH(HostAndPort.fromParts(ip.getIPAddress(), 22)); + } + + @Test(dependsOnMethods = "testCreatePortForwardingRule") + public void testListPortForwardingRules() throws Exception { + Set<PortForwardingRule> response = client.getFirewallApi().listPortForwardingRules(); + assert null != response; + assertTrue(response.size() >= 0); + for (final PortForwardingRule rule : response) { + checkPortForwardingRule(rule); + } + } + + @Test(dependsOnMethods = "testCreatePortForwardingRule") + public void testCreateFirewallRule() { + if (networksDisabled) + return; + + AsyncCreateResponse job = client.getFirewallApi().createFirewallRuleForIpAndProtocol( + ip.getId(), FirewallRule.Protocol.TCP, CreateFirewallRuleOptions.Builder.startPort(30).endPort(35)); + assertTrue(jobComplete.apply(job.getJobId())); + firewallRule = client.getFirewallApi().getFirewallRule(job.getId()); + + assertEquals(firewallRule.getStartPort(), 30); + assertEquals(firewallRule.getEndPort(), 35); + assertEquals(firewallRule.getProtocol(), FirewallRule.Protocol.TCP); + + checkFirewallRule(firewallRule); + } + + @Test(dependsOnMethods = "testCreateFirewallRule") + public void testListFirewallRules() { + Set<FirewallRule> rules = client.getFirewallApi().listFirewallRules(); + + assert rules != null; + assertTrue(!rules.isEmpty()); + + for (FirewallRule rule : rules) { + checkFirewallRule(rule); + } + } + + @Test(dependsOnMethods = "testCreatePortForwardingRule") + public void testCreateEgressFirewallRule() { + if (networksDisabled) + return; + + AsyncCreateResponse job = client.getFirewallApi().createEgressFirewallRuleForNetworkAndProtocol( + network.getId(), FirewallRule.Protocol.TCP, CreateFirewallRuleOptions.Builder.startPort(30).endPort(35)); + assertTrue(jobComplete.apply(job.getJobId())); + egressFirewallRule = client.getFirewallApi().getEgressFirewallRule(job.getId()); + + assertEquals(egressFirewallRule.getStartPort(), 30); + assertEquals(egressFirewallRule.getEndPort(), 35); + assertEquals(egressFirewallRule.getProtocol(), FirewallRule.Protocol.TCP); + + checkEgressFirewallRule(egressFirewallRule); + } + + @Test(dependsOnMethods = "testCreateEgressFirewallRule") + public void testListEgressFirewallRules() { + Set<FirewallRule> rules = client.getFirewallApi().listEgressFirewallRules(); + + assert rules != null; + assertTrue(!rules.isEmpty()); + + for (FirewallRule rule : rules) { + checkEgressFirewallRule(rule); + } + } + @AfterGroups(groups = "live") + @Override + protected void tearDownContext() { + if (firewallRule != null) { + client.getFirewallApi().deleteFirewallRule(firewallRule.getId()); + } + if (egressFirewallRule != null) { + client.getFirewallApi().deleteEgressFirewallRule(egressFirewallRule.getId()); + } + if (portForwardingRule != null) { + client.getFirewallApi().deletePortForwardingRule(portForwardingRule.getId()); + } + if (vm != null) { + jobComplete.apply(client.getVirtualMachineApi().destroyVirtualMachine(vm.getId())); + } + if (ip != null) { + client.getAddressApi().disassociateIPAddress(ip.getId()); + } + super.tearDownContext(); + } + + protected void checkFirewallRule(FirewallRule rule) { + assertEquals(rule, + client.getFirewallApi().getFirewallRule(rule.getId())); + assert rule.getId() != null : rule; + assert rule.getStartPort() > 0 : rule; + assert rule.getEndPort() >= rule.getStartPort() : rule; + assert rule.getProtocol() != null; + } + + protected void checkEgressFirewallRule(FirewallRule rule) { + assertEquals(rule, + client.getFirewallApi().getEgressFirewallRule(rule.getId())); + assert rule.getId() != null : rule; + assert rule.getStartPort() > 0 : rule; + assert rule.getEndPort() >= rule.getStartPort() : rule; + assert rule.getProtocol() != null; + } + + protected void checkPortForwardingRule(PortForwardingRule rule) { + assertEquals(rule, + client.getFirewallApi().getPortForwardingRule(rule.getId())); + assert rule.getId() != null : rule; + assert rule.getIPAddress() != null : rule; + assert rule.getIPAddressId() != null : rule; + assert rule.getPrivatePort() > 0 : rule; + assert rule.getProtocol() != null : rule; + assert rule.getPublicPort() > 0 : rule; + assert rule.getState() != null : rule; + assert rule.getVirtualMachineId() != null : rule; + assert rule.getVirtualMachineName() != null : rule; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiTest.java new file mode 100644 index 0000000..229e390 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/FirewallApiTest.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.jclouds.reflect.Reflection2.method; + +import java.io.IOException; + +import org.jclouds.Fallbacks.EmptySetOnNotFoundOr404; +import org.jclouds.Fallbacks.VoidOnNotFoundOr404; +import org.jclouds.cloudstack.domain.PortForwardingRule; +import org.jclouds.cloudstack.internal.BaseCloudStackApiTest; +import org.jclouds.cloudstack.options.ListPortForwardingRulesOptions; +import org.jclouds.fallbacks.MapHttp4xxCodesToExceptions; +import org.jclouds.http.functions.ParseFirstJsonValueNamed; +import org.jclouds.http.functions.ReleasePayloadAndReturn; +import org.jclouds.http.functions.UnwrapOnlyJsonValue; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.reflect.Invokable; +/** + * Tests behavior of {@code FirewallApi} + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during +// surefire +@Test(groups = "unit", testName = "FirewallApiTest") +public class FirewallApiTest extends BaseCloudStackApiTest<FirewallApi> { + public void testListPortForwardingRules() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(FirewallApi.class, "listPortForwardingRules", + ListPortForwardingRulesOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.of()); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listPortForwardingRules&listAll=true HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testListPortForwardingRulesOptions() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(FirewallApi.class, "listPortForwardingRules", + ListPortForwardingRulesOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(ListPortForwardingRulesOptions.Builder.ipAddressId("3"))); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listPortForwardingRules&listAll=true&ipaddressid=3 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testCreatePortForwardingRuleForVirtualMachine() throws SecurityException, NoSuchMethodException, + IOException { + Invokable<?, ?> method = method(FirewallApi.class, "createPortForwardingRuleForVirtualMachine", String.class, + PortForwardingRule.Protocol.class, int.class, String.class, int.class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of("6", PortForwardingRule.Protocol.TCP, 22, "7", 22)); + + assertRequestLineEquals( + httpRequest, + "GET http://localhost:8080/client/api?response=json&command=createPortForwardingRule&ipaddressid=6&protocol=tcp&publicport=22&virtualmachineid=7&privateport=22 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyJsonValue.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, MapHttp4xxCodesToExceptions.class); + + checkFilters(httpRequest); + + } + + public void testDeletePortForwardingRule() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(FirewallApi.class, "deletePortForwardingRule", String.class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(5)); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=deletePortForwardingRule&id=5 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, ""); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, VoidOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAccountApiLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAccountApiLiveTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAccountApiLiveTest.java new file mode 100644 index 0000000..d965fd6 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAccountApiLiveTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static com.google.common.base.Charsets.UTF_8; +import static com.google.common.hash.Hashing.md5; +import static com.google.common.io.BaseEncoding.base16; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +import org.jclouds.cloudstack.CloudStackGlobalApi; +import org.jclouds.cloudstack.domain.Account; +import org.jclouds.cloudstack.internal.BaseCloudStackApiLiveTest; +import org.testng.annotations.Test; + +/** + * Tests behavior of {@code GlobalAccountApi} + */ +@Test(groups = "live", singleThreaded = true, testName = "GlobalAccountApiLiveTest") +public class GlobalAccountApiLiveTest extends BaseCloudStackApiLiveTest { + + public static Account createTestAccount(CloudStackGlobalApi client, String prefix) { + return client.getAccountApi().createAccount(prefix + "-account", Account.Type.USER, "[email protected]", + "First", "Last", base16().lowerCase().encode(md5().hashString("password", UTF_8).asBytes())); + } + + @Test + public void testCreateAndRemoveAccount() { + skipIfNotGlobalAdmin(); + + Account account = null; + try { + account = createTestAccount(globalAdminClient, prefix); + + assertNotNull(account); + assertEquals(account.getName(), prefix + "-account"); + assertEquals(account.getType(), Account.Type.USER); + + Account updated = globalAdminClient.getAccountApi().updateAccount( + account.getName(), account.getDomainId(), prefix + "-account-2"); + + assertNotNull(updated); + assertEquals(updated.getName(), prefix + "-account-2"); + + } finally { + if (account != null) { + globalAdminClient.getAccountApi().deleteAccount(account.getId()); + } + } + + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAccountApiTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAccountApiTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAccountApiTest.java new file mode 100644 index 0000000..ea176a1 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAccountApiTest.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.jclouds.reflect.Reflection2.method; + +import org.jclouds.Fallbacks.NullOnNotFoundOr404; +import org.jclouds.cloudstack.domain.Account; +import org.jclouds.cloudstack.internal.BaseCloudStackApiTest; +import org.jclouds.cloudstack.options.CreateAccountOptions; +import org.jclouds.cloudstack.options.UpdateAccountOptions; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.functions.ParseFirstJsonValueNamed; +import org.jclouds.http.functions.ReleasePayloadAndReturn; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.reflect.Invokable; +/** + * Tests behavior of {@code GlobalAccountApi} + */ +@Test(groups = "unit", testName = "GlobalAccountApiTest") +public class GlobalAccountApiTest extends BaseCloudStackApiTest<GlobalAccountApi> { + + HttpRequest create = HttpRequest.builder().method("GET") + .endpoint("http://localhost:8080/client/api") + .addQueryParam("response", "json") + .addQueryParam("command", "createAccount") + .addQueryParam("username", "user") + .addQueryParam("accounttype", "0") + .addQueryParam("email", "[email protected]") + .addQueryParam("firstname", "FirstName") + .addQueryParam("lastname", "LastName") + .addQueryParam("password", "hashed-password") + .build(); + + public void testCreateAccount() throws Exception { + Invokable<?, ?> method = method(GlobalAccountApi.class, "createAccount", String.class, Account.Type.class, + String.class, String.class, String.class, String.class, CreateAccountOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of("user", Account.Type.USER, "[email protected]", + "FirstName", "LastName", "hashed-password")); + + assertRequestLineEquals(httpRequest, create.getRequestLine()); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, NullOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + + HttpRequest update = HttpRequest.builder().method("GET") + .endpoint("http://localhost:8080/client/api") + .addQueryParam("response", "json") + .addQueryParam("command", "updateAccount") + .addQueryParam("account", "account") + .addQueryParam("domainid", "42") + .addQueryParam("newname", "new-account-name") + .build(); + + public void testUpdateAccount() throws Exception { + Invokable<?, ?> method = method(GlobalAccountApi.class, "updateAccount", String.class, String.class, + String.class, UpdateAccountOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of("account", 42L, "new-account-name")); + + assertRequestLineEquals(httpRequest, update.getRequestLine()); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, NullOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + + public void testDeleteAccount() throws Exception { + Invokable<?, ?> method = method(GlobalAccountApi.class, "deleteAccount", String.class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(42L)); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=deleteAccount&id=42 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, NullOnNotFoundOr404.class); + + checkFilters(httpRequest); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAlertApiLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAlertApiLiveTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAlertApiLiveTest.java new file mode 100644 index 0000000..84b4602 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAlertApiLiveTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNotSame; +import static org.testng.Assert.assertTrue; + +import java.util.Set; + +import org.jclouds.cloudstack.domain.Alert; +import org.jclouds.cloudstack.internal.BaseCloudStackApiLiveTest; +import org.testng.annotations.Test; + +/** + * Tests behavior of {@code GlobalAlertsClient} + */ +@Test(groups = "live", singleThreaded = true, testName = "GlobalAlertApiLiveTest") +public class GlobalAlertApiLiveTest extends BaseCloudStackApiLiveTest { + + @Test(groups = "live", enabled = true) + public void testListAlerts() throws Exception { + skipIfNotGlobalAdmin(); + + final Set<Alert> response = globalAdminClient.getAlertClient().listAlerts(); + assert null != response; + assertTrue(response.size() >= 0); + int count = 0; + for (Alert alert : response) { + assertNotNull(alert.getDescription()); + assertNotSame(alert.getId(), 0); + assertNotNull(alert.getType()); + assertNotNull(alert.getSent()); + count++; + } + assertTrue(count > 0, "No alerts were returned, so I couldn't test"); + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAlertApiTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAlertApiTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAlertApiTest.java new file mode 100644 index 0000000..fe87440 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalAlertApiTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.jclouds.reflect.Reflection2.method; + +import java.io.IOException; + +import org.jclouds.Fallbacks.EmptySetOnNotFoundOr404; +import org.jclouds.cloudstack.internal.BaseCloudStackApiTest; +import org.jclouds.cloudstack.options.ListAlertsOptions; +import org.jclouds.http.functions.ParseFirstJsonValueNamed; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.reflect.Invokable; +/** + * Tests behavior of {@code GlobalAlertsClient} + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during +// surefire +@Test(groups = "unit", testName = "GlobalAlertApiTest") +public class GlobalAlertApiTest extends BaseCloudStackApiTest<GlobalAlertApi> { + + public void testListAlerts() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(GlobalAlertApi.class, "listAlerts", ListAlertsOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.of()); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listAlerts&listAll=true HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testListAlertsOptions() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(GlobalAlertApi.class, "listAlerts", ListAlertsOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(ListAlertsOptions.Builder.id("42").keyword("jclouds").type("TEMPLATE"))); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listAlerts&listAll=true&id=42&keyword=jclouds&type=TEMPLATE HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalCapacityApiLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalCapacityApiLiveTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalCapacityApiLiveTest.java new file mode 100644 index 0000000..5207a8f --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalCapacityApiLiveTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.testng.Assert.assertNotEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.util.Set; + +import org.jclouds.cloudstack.domain.Capacity; +import org.jclouds.cloudstack.internal.BaseCloudStackApiLiveTest; +import org.testng.annotations.Test; + +/** + * Tests behavior of {@code GlobalCapacityApi} + */ +@Test(groups = "live", singleThreaded = true, testName = "GlobalCapacityApiLiveTest") +public class GlobalCapacityApiLiveTest extends BaseCloudStackApiLiveTest { + + @Test(groups = "live", enabled = true) + public void testListCapacity() throws Exception { + skipIfNotGlobalAdmin(); + + final Set<Capacity> response = globalAdminClient.getCapacityClient().listCapacity(); + assertNotNull(response); + assertNotEquals(0, response.size()); + int count = 0; + for (Capacity capacity : response) { + assertTrue(capacity.getCapacityTotal() >= 0); + assertTrue(capacity.getCapacityUsed() >= 0); + assertTrue(capacity.getPercentUsed() >= 0); + assertNotEquals(Capacity.Type.UNRECOGNIZED, capacity.getType()); + assertNotNull(capacity.getZoneName()); + count++; + } + assertTrue(count > 0, "No capacities were returned, so I couldn't test"); + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalCapacityApiTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalCapacityApiTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalCapacityApiTest.java new file mode 100644 index 0000000..8ca53d4 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalCapacityApiTest.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.jclouds.reflect.Reflection2.method; + +import java.io.IOException; + +import org.jclouds.Fallbacks.EmptySetOnNotFoundOr404; +import org.jclouds.cloudstack.domain.Capacity; +import org.jclouds.cloudstack.internal.BaseCloudStackApiTest; +import org.jclouds.cloudstack.options.ListCapacityOptions; +import org.jclouds.http.functions.ParseFirstJsonValueNamed; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.reflect.Invokable; +/** + * Tests behavior of {@code GlobalCapacityApi} + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during +// surefire +@Test(groups = "unit", testName = "GlobalCapacityApiTest") +public class GlobalCapacityApiTest extends BaseCloudStackApiTest<GlobalCapacityApi> { + + public void testListCapacity() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(GlobalCapacityApi.class, "listCapacity", ListCapacityOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.of()); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listCapacity&listAll=true HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + + public void testListCapacityOptions() throws SecurityException, NoSuchMethodException, IOException { + Invokable<?, ?> method = method(GlobalCapacityApi.class, "listCapacity", ListCapacityOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(ListCapacityOptions.Builder.hostId("3").keyword("fred").podId("4").type(Capacity.Type.CPU_ALLOCATED_MHZ).zoneId("6"))); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listCapacity&listAll=true&hostid=3&keyword=fred&podid=4&type=1&zoneid=6 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertSaxResponseParserClassEquals(method, null); + assertFallbackClassEquals(method, EmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalConfigurationApiExpectTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalConfigurationApiExpectTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalConfigurationApiExpectTest.java new file mode 100644 index 0000000..71830ea --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalConfigurationApiExpectTest.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +import java.net.URI; + +import org.jclouds.cloudstack.CloudStackContext; +import org.jclouds.cloudstack.domain.ConfigurationEntry; +import org.jclouds.cloudstack.internal.BaseCloudStackExpectTest; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; + +/** + * Test the CloudStack GlobalConfigurationApi + */ +@Test(groups = "unit", testName = "GlobalConfigurationApiExpectTest") +public class GlobalConfigurationApiExpectTest extends BaseCloudStackExpectTest<GlobalConfigurationApi> { + + @Test + public void testListConfigurationEntriesWhenResponseIs2xx() { + GlobalConfigurationApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listConfigurations&listAll=true&apiKey=identity&signature=%2BJ9mTuw%2BZXaumzMAJAXgZQaO2cc%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/listconfigurationsresponse.json")) + .build()); + + assertEquals(client.listConfigurationEntries(), + ImmutableSet.of( + ConfigurationEntry.builder().category("Advanced").name("account.cleanup.interval").value("86400") + .description("The interval (in seconds) between cleanup for removed accounts").build(), + ConfigurationEntry.builder().category("Advanced").name("agent.lb.enabled").value("true") + .description("If agent load balancing enabled in cluster setup").build() + )); + } + + @Test + public void testListConfigurationEntriesEmptyOn404() { + GlobalConfigurationApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listConfigurations&listAll=true&apiKey=identity&signature=%2BJ9mTuw%2BZXaumzMAJAXgZQaO2cc%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertEquals(client.listConfigurationEntries(), ImmutableSet.of()); + } + + @Test + public void testUpdateConfigurationEntryWhenResponseIs2xx() { + GlobalConfigurationApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=updateConfiguration&name=expunge.delay&value=11&" + + "apiKey=identity&signature=I2yG35EhfgIXYObeLfU3cvf%2BPeE%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/updateconfigurationsresponse.json")) + .build()); + + assertEquals(client.updateConfigurationEntry("expunge.delay", "11"), + ConfigurationEntry.builder().category("Advanced").name("expunge.delay").value("11") + .description("Determines how long (in seconds) to wait before actually expunging " + + "destroyed vm. The default value = the default value of expunge.interval").build() + ); + } + + @Test + public void testUpdateConfigurationEntryNullOn404() { + GlobalConfigurationApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=updateConfiguration&name=expunge.delay&value=11&" + + "apiKey=identity&signature=I2yG35EhfgIXYObeLfU3cvf%2BPeE%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertNull(client.updateConfigurationEntry("expunge.delay", "11")); + } + + @Override + protected GlobalConfigurationApi clientFrom(CloudStackContext context) { + return context.getGlobalApi().getConfigurationApi(); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalConfigurationApiLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalConfigurationApiLiveTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalConfigurationApiLiveTest.java new file mode 100644 index 0000000..dce9614 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalConfigurationApiLiveTest.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static com.google.common.collect.Iterables.getOnlyElement; +import static org.jclouds.cloudstack.options.ListConfigurationEntriesOptions.Builder.name; +import static org.testng.Assert.assertEquals; + +import java.util.Set; + +import org.jclouds.cloudstack.domain.ConfigurationEntry; +import org.jclouds.cloudstack.internal.BaseCloudStackApiLiveTest; +import org.testng.annotations.Test; +import org.testng.collections.Sets; + +import com.google.common.base.Objects; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; + +/** + * Tests behavior of {@code GlobalConfigurationApi} + */ +@Test(groups = "live", singleThreaded = true, testName = "GlobalConfigurationApiLiveTest") +public class GlobalConfigurationApiLiveTest extends BaseCloudStackApiLiveTest { + + @Test + public void testListConfigurationEntries() { + skipIfNotGlobalAdmin(); + + Set<ConfigurationEntry> entries = globalAdminClient + .getConfigurationApi().listConfigurationEntries(); + + Set<String> categories = Sets.newHashSet(); + for (ConfigurationEntry entry : entries) { + checkConfigurationEntry(entry); + categories.add(entry.getCategory()); + } + + assert categories.containsAll(ImmutableSet.<Object>of("Network", "Advanced", + "Storage", "Usage", "Snapshots", "Account Defaults", "Console Proxy", "Alert")); + } + + @Test + public void testUpdateConfigurationEntry() { + skipIfNotGlobalAdmin(); + + Set<ConfigurationEntry> entries = globalAdminClient + .getConfigurationApi().listConfigurationEntries(); + + long expungeDelay = Long.parseLong(getValueByName(entries, "expunge.delay")); + assert expungeDelay > 0; + + globalAdminClient.getConfigurationApi() + .updateConfigurationEntry("expunge.delay", "" + (expungeDelay + 1)); + + long newDelay = Long.parseLong(getOnlyElement(globalAdminClient.getConfigurationApi() + .listConfigurationEntries(name("expunge.delay"))).getValue()); + assertEquals(newDelay, expungeDelay + 1); + + globalAdminClient.getConfigurationApi() + .updateConfigurationEntry("expunge.delay", "" + expungeDelay); + } + + private void checkConfigurationEntry(ConfigurationEntry entry) { + assertEquals(entry, getEntryByName(globalAdminClient.getConfigurationApi() + .listConfigurationEntries(name(entry.getName())), entry.getName())); + assert entry.getCategory() != null : entry; + // Description apparently can be null, so ... assert entry.getDescription() != null : entry; + assert entry.getName() != null : entry; + } + + private String getValueByName(Set<ConfigurationEntry> entries, String name) { + return getEntryByName(entries, name).getValue(); + } + + private ConfigurationEntry getEntryByName(Set<ConfigurationEntry> entries, final String name) { + return Iterables.find(entries, new Predicate<ConfigurationEntry>() { + @Override + public boolean apply(ConfigurationEntry entry) { + return entry != null && Objects.equal(name, entry.getName()); + } + }); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/86fd5cf2/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalDomainApiExpectTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalDomainApiExpectTest.java b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalDomainApiExpectTest.java new file mode 100644 index 0000000..cd79b31 --- /dev/null +++ b/dependencies/jclouds/apis/cloudstack/1.8.0-stratos/src/test/java/org/jclouds/cloudstack/features/GlobalDomainApiExpectTest.java @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 static org.jclouds.cloudstack.options.UpdateDomainOptions.Builder.name; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +import java.net.URI; + +import org.jclouds.cloudstack.CloudStackContext; +import org.jclouds.cloudstack.domain.Domain; +import org.jclouds.cloudstack.internal.BaseCloudStackExpectTest; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; + +/** + * Test the CloudStack GlobalDomainApi + */ +@Test(groups = "unit", testName = "GlobalDomainApiExpectTest") +public class GlobalDomainApiExpectTest extends BaseCloudStackExpectTest<GlobalDomainApi> { + + public void testCreateDomainWhenResponseIs2xx() { + GlobalDomainApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=createDomain&" + + "name=test&apiKey=identity&signature=6cxzEo7h63G0hgTTMLm4lGsSDK8%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/createdomainresponse.json")) + .build()); + + assertEquals(client.createDomain("test"), + Domain.builder().id("10").name("test").level(1).parentDomainId("1") + .parentDomainName("ROOT").hasChild(false).build()); + } + + public void testCreateDomainWhenResponseIs404() { + GlobalDomainApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=createDomain&" + + "name=test&apiKey=identity&signature=6cxzEo7h63G0hgTTMLm4lGsSDK8%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertNull(client.createDomain("test")); + } + + public void testUpdateDomainWhenResponseIs2xx() { + GlobalDomainApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=updateDomain&id=10&name=test-2&apiKey=identity&signature=5t1eUf2Eyf/aB6qt%2BqIj%2BmcwFIo%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/updatedomainresponse.json")) + .build()); + + assertEquals(client.updateDomain("10", name("test-2")), + Domain.builder().id("10").name("test-2").level(1).parentDomainId("1") + .parentDomainName("ROOT").hasChild(false).build()); + } + + public void testUpdateDomainWhenResponseIs404() { + GlobalDomainApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=updateDomain&id=10&name=test-2&apiKey=identity&signature=5t1eUf2Eyf/aB6qt%2BqIj%2BmcwFIo%3D")) + .addHeader("Accept", "application/json") + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertNull(client.updateDomain("10", name("test-2"))); + } + + public void testDeleteOnlyDomain() { + GlobalDomainApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=deleteDomain&cleanup=false&id=1&apiKey=identity&signature=/5aLbigg612t9IrZi0JZO7CyiOU%3D")) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/deletedomainresponse.json")) + .build()); + + client.deleteOnlyDomain("1"); + } + + public void testDeleteDomainAndAttachedResources() { + GlobalDomainApi client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=deleteDomain&cleanup=true&id=1&apiKey=identity&signature=grL7JStvtYUT89Jr0D8FgwMyJpU%3D")) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/deletedomainresponse.json")) + .build()); + + client.deleteDomainAndAttachedResources("1"); + } + + @Override + protected GlobalDomainApi clientFrom(CloudStackContext context) { + return context.getGlobalApi().getDomainClient(); + } +}
