From 1b9dfc71023c10ba658877ab87b1c9098c09e153 Mon Sep 17 00:00:00 2001 From: andreisavu Date: Fri, 6 Jan 2012 15:18:50 +0200 Subject: [PATCH] Test for AccountClient.listAccounts using the mock webserver --- .../features/AccountClientExpectTest.java | 110 ++++++++++++++++++ .../BaseCloudStackRestClientExpectTest.java | 53 +++++++++ 2 files changed, 163 insertions(+) create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AccountClientExpectTest.java create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackRestClientExpectTest.java diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AccountClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AccountClientExpectTest.java new file mode 100644 index 0000000000..dd27d7f742 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AccountClientExpectTest.java @@ -0,0 +1,110 @@ +/** + * 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 java.net.URI; +import java.util.Set; + +import com.google.common.collect.ImmutableSet; +import org.jclouds.cloudstack.CloudStackClient; +import org.jclouds.cloudstack.domain.Account; +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 com.google.common.collect.ImmutableMultimap; + +import static org.testng.Assert.assertEquals; + +/** + * Test the CloudStack AccountClient + * + * @author Andrei Savu + */ +@Test(groups = "unit", testName = "AccountClientExpectTest") +public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest { + + public void testListAccountsWhenResponseIs2xx() { + + AccountClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=listAccounts")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/listaccountsresponse.json")) + .build()) + .getAccountClient(); + + Set users = ImmutableSet.of( + User.builder() + .id(505) + .name("jclouds") + .firstName("Adrian") + .lastName("Cole") + .email("adrian@jclouds.org") + .created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-04-19T01:57:24+0000")) + .state(User.State.ENABLED) + .account("jclouds") + .accountType(Account.Type.USER) + .domainId(457) + .domain("AA000062-jclouds-dev") + .apiKey("APIKEY") + .secretKey("SECRETKEY").build()); + + assertEquals(client.listAccounts(), + ImmutableSet.of(Account.builder() + .id(505) + .name("jclouds") + .type(Account.Type.USER) + .domainId(457) + .domain("AA000062-jclouds-dev") + .receivedBytes(318900216) + .sentBytes(23189677) + .VMLimit(15l) + .VMs(1) + .IPsAvailable(14l) + .IPLimit(15l) + .IPs(0) + .IPsAvailable(15l) + .volumeLimit(90l) + .volumes(2) + .volumesAvailable(88l) + .snapshotLimit(250l) + .snapshots(0) + .snapshotsAvailable(250l) + .templateLimit(15l) + .templates(0) + .templatesAvailable(15l) + .VMsAvailable(14l) + .VMsStopped(0) + .VMsRunning(1) + .state(Account.State.ENABLED) + .users(users).build())); + } +} \ No newline at end of file diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackRestClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackRestClientExpectTest.java new file mode 100644 index 0000000000..87803a1cb0 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackRestClientExpectTest.java @@ -0,0 +1,53 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.cloudstack.features; + +import org.jclouds.cloudstack.CloudStackContextBuilder; +import org.jclouds.cloudstack.CloudStackPropertiesBuilder; +import org.jclouds.rest.BaseRestClientExpectTest; + +import java.util.Properties; + +/** + * Base class for writing CloudStack Rest Client Expect tests + * + * @author Andrei Savu + */ +public class BaseCloudStackRestClientExpectTest extends BaseRestClientExpectTest { + + public BaseCloudStackRestClientExpectTest() { + provider = "cloudstack"; + } + + @Override + public Properties setupRestProperties() { + Properties overrides = new Properties(); + overrides.put("cloudstack.contextbuilder", CloudStackContextBuilder.class.getName()); + overrides.put("cloudstack.propertiesbuilder", CloudStackPropertiesBuilder.class.getName()); + return overrides; + } + + @Override + public Properties setupProperties() { + Properties overrides = new Properties(); + overrides.put("jclouds.endpoint", "http://localhost:8080/client/api"); + return overrides; + } + +}