Re-writing ClientExpectTest in the required format

This commit is contained in:
Adam Lowe 2012-01-11 01:05:56 +00:00
parent f1a8649c3f
commit 41dedd0498
8 changed files with 814 additions and 352 deletions

View File

@ -19,89 +19,206 @@
package org.jclouds.glesys.features; package org.jclouds.glesys.features;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import org.jclouds.glesys.GleSYSClient; import org.jclouds.glesys.GleSYSClient;
import org.jclouds.glesys.domain.Archive; import org.jclouds.glesys.domain.Archive;
import org.jclouds.glesys.domain.ArchiveAllowedArguments; import org.jclouds.glesys.domain.ArchiveAllowedArguments;
import org.jclouds.glesys.domain.ArchiveDetails; import org.jclouds.glesys.domain.ArchiveDetails;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException;
import org.jclouds.rest.BaseRestClientExpectTest;
import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.Map; import java.net.URI;
import java.util.List;
import static org.jclouds.io.Payloads.newUrlEncodedFormPayload;
import static org.testng.Assert.*; import static org.testng.Assert.*;
/** /**
* Tests annotation parsing of {@code ArchiveAsyncClient} * Tests parsing of {@code ArchiveAsyncClient}
* *
* @author Adam Lowe * @author Adam Lowe
*/ */
@Test(groups = "unit", testName = "ArchiveAsyncClientTest") @Test(groups = "unit", testName = "ArchiveClientExpectTest")
public class ArchiveClientExpectTest extends BaseGleSYSClientExpectTest<ArchiveClient> { public class ArchiveClientExpectTest extends BaseRestClientExpectTest<GleSYSClient> {
public ArchiveClientExpectTest() { public ArchiveClientExpectTest() {
remoteServicePrefix = "archive"; provider = "glesys";
} }
private Map.Entry<String, String> userName = entry("username", "xxxxxx_test1"); public void testListArchivesWhenReponseIs2xx() throws Exception {
ArchiveClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/list/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/archive_list.json")).build())
.getArchiveClient();
public void testListArchives() throws Exception { List<Archive> expected = ImmutableList.<Archive>of(
ArchiveClient client = createMock("list", "POST", 200, "/archive_list.json"); Archive.builder().username("xxxxx_test1").freeSize("20 GB").totalSize("20 GB").locked(false).build());
assertEquals(client.listArchives(),
ImmutableList.<Archive>of(Archive.builder().username("xxxxx_test1").freeSize("20 GB").totalSize("20 GB").locked(false).build())); assertEquals(client.listArchives(), expected);
}
public void testListArchivesWhenResponseIs4xxReturnsEmpty() {
ArchiveClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/list/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(404).build()).getArchiveClient();
// check not found response
client = createMock("list", "POST", 404, "Something not found");
assertTrue(client.listArchives().isEmpty()); assertTrue(client.listArchives().isEmpty());
} }
public void testArchiveDetails() throws Exception { public void testArchiveDetailsWhenResponseIs2xx() throws Exception {
assertEquals(createMock("details", "POST", 200, "/archive_details.json", userName).getArchiveDetails("xxxxxx_test1"), ArchiveClient client = requestSendsResponse(
ArchiveDetails.builder().username("xxxxxx_test1").freeSize("30 GB").totalSize("30 GB").locked(false).build()); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/details/format/json"))
assertNull(createMock("details", "POST", 404, "/archive_details.json", userName).getArchiveDetails("xxxxxx_test1")); .headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(
ImmutableMultimap.<String, String>builder().put("username", "xxxxxx_test1").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/archive_details.json")).build())
.getArchiveClient();
ArchiveDetails expected = ArchiveDetails.builder().username("xxxxxx_test1").freeSize("30 GB").totalSize("30 GB").locked(false).build();
assertEquals(client.getArchiveDetails("xxxxxx_test1"), expected);
} }
public void testCreateArchive() throws Exception { public void testArchiveDetailsWhenResponseIs4xxReturnsNull() {
createMock("create", "POST", 200, null, userName, entry("size", 5), ArchiveClient client = requestSendsResponse(
entry("password", "somepass")).createArchive(userName.getValue(), "somepass", 5); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/details/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(
ImmutableMultimap.<String, String>builder().put("username", "xxxxxx_test1").build())).build(),
HttpResponse.builder().statusCode(404).build())
.getArchiveClient();
assertNull(client.getArchiveDetails("xxxxxx_test1"));
} }
public void testDeleteArchive() throws Exception { public void testCreateArchiveWhenResponseIs2xx() throws Exception {
createMock("delete", "POST", 200, null, userName).deleteArchive(userName.getValue()); ArchiveClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/create/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("username", "xxxxxx_test1")
.put("size", "5")
.put("password", "somepass").build())).build(),
HttpResponse.builder().statusCode(200).build()).getArchiveClient();
client.createArchive("xxxxxx_test1", "somepass", 5);
}
public void testDeleteArchiveWhenResponseIs2xx() throws Exception {
ArchiveClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/delete/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("username", "xxxxxx_test1").build())).build(),
HttpResponse.builder().statusCode(200).build()).getArchiveClient();
client.deleteArchive("xxxxxx_test1");
}
@Test(expectedExceptions = {HttpResponseException.class})
public void testDeleteArchiveWhenResponseIs4xxThrows() throws Exception {
ArchiveClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/delete/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("username", "xxxxxx_test1").build())).build(),
HttpResponse.builder().statusCode(402).build()).getArchiveClient();
client.deleteArchive("xxxxxx_test1");
}
public void testResizeArchiveWhenResponseIs2xx() throws Exception {
ArchiveClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/resize/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("username", "username1")
.put("size", "5").build())).build(),
HttpResponse.builder().statusCode(200).build()).getArchiveClient();
client.resizeArchive("username1", 5);
} }
@Test(expectedExceptions = {ResourceNotFoundException.class}) @Test(expectedExceptions = {ResourceNotFoundException.class})
public void testDeleteArchiveNotFound() throws Exception { public void testResizeArchiveWhenResponseIs4xxThrows() throws Exception {
createMock("delete", "POST", 404, null, userName).deleteArchive(userName.getValue()); ArchiveClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/archive/resize/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("username", "username1")
.put("size", "5").build())).build(),
HttpResponse.builder().statusCode(404).build()).getArchiveClient();
client.resizeArchive("username1", 5);
} }
public void testResizeArchive() throws Exception { public void testChangeArchivePasswordWhenResponseIs2xx() throws Exception {
createMock("resize", "POST", 200, null, entry("username", "username"), ArchiveClient client = requestSendsResponse(
entry("size", "5")).resizeArchive("username", 5); HttpRequest.builder().method("POST")
.endpoint(URI.create("https://api.glesys.com/archive/changepassword/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("username", "username")
.put("password", "newpass").build())).build(),
HttpResponse.builder().statusCode(200).build()).getArchiveClient();
client.changeArchivePassword("username", "newpass");
} }
@Test(expectedExceptions = {ResourceNotFoundException.class}) @Test(expectedExceptions = {ResourceNotFoundException.class})
public void testResizeArchiveNotFound() throws Exception { public void testChangeArchivePasswordWhenResponseIs4xxThrows() throws Exception {
createMock("resize", "POST", 404, null, entry("username", "username"), entry("size", "5")).resizeArchive("username", 5); ArchiveClient client = requestSendsResponse(
HttpRequest.builder().method("POST")
.endpoint(URI.create("https://api.glesys.com/archive/changepassword/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("username", "username")
.put("password", "newpass").build())).build(),
HttpResponse.builder().statusCode(404).build()).getArchiveClient();
client.changeArchivePassword("username", "newpass");
} }
public void testChangeArchivePassword() throws Exception { public void testGetArchiveAllowedArgumentsWhenResponseIs2xx() throws Exception {
createMock("changepassword", "POST", 200, null, userName, ArchiveClient client = requestSendsResponse(
entry("password", "newpass")).changeArchivePassword(userName.getValue(), "newpass"); HttpRequest.builder().method("GET")
.endpoint(URI.create("https://api.glesys.com/archive/allowedarguments/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==")
.build()).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/archive_allowed_arguments.json")).build()).getArchiveClient();
ArchiveAllowedArguments expected = ArchiveAllowedArguments.builder().archiveSizes(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000).build();
assertEquals(client.getArchiveAllowedArguments(), expected);
} }
@Test(expectedExceptions = {ResourceNotFoundException.class}) public void testGetArchiveAllowedArguments4xxWhenResponseIs2xx() throws Exception {
public void testChangeArchivePasswordNotFound() throws Exception { ArchiveClient client = requestSendsResponse(
createMock("changepassword", "POST", 404, null, userName, HttpRequest.builder().method("GET")
entry("password", "newpass")).changeArchivePassword(userName.getValue(), "newpass"); .endpoint(URI.create("https://api.glesys.com/archive/allowedarguments/format/json"))
} .headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(404).build()).getArchiveClient();
public void testGetArchiveAllowedArguments() throws Exception { assertNull(client.getArchiveAllowedArguments());
assertEquals(createMock("allowedarguments", "GET", 200, "/archive_allowed_arguments.json").getArchiveAllowedArguments(),
ArchiveAllowedArguments.builder().archiveSizes(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000).build());
assertNull(createMock("allowedarguments", "GET", 404, "/archive_allowed_arguments.json").getArchiveAllowedArguments());
}
@Override
protected ArchiveClient getClient(GleSYSClient gleSYSClient) {
return gleSYSClient.getArchiveClient();
} }
} }

View File

@ -45,7 +45,6 @@ import com.google.common.collect.Maps;
/** /**
* @author Adrian Cole * @author Adrian Cole
* @author Adam Lowe
*/ */
public abstract class BaseGleSYSAsyncClientTest<T> extends RestClientTest<T> { public abstract class BaseGleSYSAsyncClientTest<T> extends RestClientTest<T> {

View File

@ -1,115 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.glesys.features;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import org.jclouds.glesys.GleSYSClient;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.options.BaseHttpRequestOptions;
import org.jclouds.io.payloads.StringPayload;
import org.jclouds.rest.BaseRestClientExpectTest;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.jclouds.io.Payloads.newUrlEncodedFormPayload;
/**
* Support for short-hand generation of ClientExpectTests for the GleSYS clients
*
* @author Adam Lowe
*/
public abstract class BaseGleSYSClientExpectTest<T> extends BaseRestClientExpectTest<GleSYSClient> {
protected String remoteServicePrefix;
public BaseGleSYSClientExpectTest() {
provider = "glesys";
}
protected abstract T getClient(GleSYSClient gleSYSClient);
protected Map.Entry<String, String> entry(String key, Object value) {
return Maps.immutableEntry(key, value.toString());
}
/**
* Build a mock of a GleSYS client that responds as instructed
*
* @param remoteCall the name of the expected call on the remote server
* @param httpMethod "GET" or "POST"
* @param returnCode the http status code expected (ordinarily 200)
* @param expectedResponse ensure this is not-null for calls that expect a response - for OK responses this should be
* the classpath location of a file with a valid server response, for errors any String
* @param args either Map.Entry or BaseHttpRequestOption objects that make up the arguments to the method
* @return the appropriate client for test to invoke methods on (by calling getClient() in the appropriate subclass)
*/
@SuppressWarnings("unchecked")
protected T createMock(String remoteCall, String httpMethod, int returnCode, String expectedResponse, Object... args) throws Exception {
List<Object> argValues = new ArrayList<Object>();
Multimap<String, String> map = LinkedHashMultimap.create();
for (Object arg : args) {
if (arg instanceof BaseHttpRequestOptions) {
for (Map.Entry<String, String> httpEntry : ((BaseHttpRequestOptions) arg).buildFormParameters().entries()) {
map.put(httpEntry.getKey(), httpEntry.getValue());
}
argValues.add(arg);
} else {
Map.Entry<String, String> entry = (Map.Entry<String, String>) arg;
map.put(entry.getKey(), entry.getValue());
argValues.add(entry.getValue());
}
}
HttpRequest.Builder httpRequestBuilder = HttpRequest.builder().method(httpMethod).endpoint(
URI.create("https://api.glesys.com/" + remoteServicePrefix + "/" + remoteCall + "/format/json"));
if (expectedResponse == null) {
httpRequestBuilder.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build());
} else {
httpRequestBuilder.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json").put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build());
}
if (!map.isEmpty()) {
httpRequestBuilder.payload(newUrlEncodedFormPayload(
ImmutableMultimap.<String, String>builder().putAll(map).build()));
}
HttpResponse.Builder responseBuilder = HttpResponse.builder().statusCode(returnCode);
if (expectedResponse != null) {
if (returnCode < 300) {
responseBuilder.payload(payloadFromResource(expectedResponse));
} else {
responseBuilder.payload(new StringPayload(expectedResponse));
}
}
return getClient(requestSendsResponse(httpRequestBuilder.build(), responseBuilder.build()));
}
}

View File

@ -18,15 +18,22 @@
*/ */
package org.jclouds.glesys.features; package org.jclouds.glesys.features;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.jclouds.glesys.GleSYSClient; import org.jclouds.glesys.GleSYSClient;
import org.jclouds.glesys.domain.Domain; import org.jclouds.glesys.domain.Domain;
import org.jclouds.glesys.options.DomainAddOptions; import org.jclouds.glesys.options.DomainAddOptions;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.BaseRestClientExpectTest;
import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.net.URI;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Set;
import static org.jclouds.io.Payloads.newUrlEncodedFormPayload;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
@ -35,50 +42,123 @@ import static org.testng.Assert.assertTrue;
* *
* @author Adam Lowe * @author Adam Lowe
*/ */
@Test(groups = "unit", testName = "DomainAsyncClientTest") @Test(groups = "unit", testName = "DomainClientExpectTest")
public class DomainClientExpectTest extends BaseGleSYSClientExpectTest<DomainClient> { public class DomainClientExpectTest extends BaseRestClientExpectTest<GleSYSClient> {
public DomainClientExpectTest() { public DomainClientExpectTest() {
remoteServicePrefix = "domain"; provider = "glesys";
} }
public void testListDomains() throws Exception { public void testListDomainsWhenResponseIs2xx() throws Exception {
DomainClient client = createMock("list", "POST", 200, "/domain_list.json"); //DomainClient client = createMock("list", "POST", 200, "/domain_list.json");
assertEquals(client.listDomains(), ImmutableSet.<Domain>of(
Domain.builder().domain("adamlowe.net").createTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2011-12-20 10:58:51")).build() DomainClient client = requestSendsResponse(
)); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/domain/list/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/domain_list.json")).build()).getDomainClient();
Set<Domain> expected = ImmutableSet.<Domain>of(
Domain.builder().domain("adamlowe.net").createTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2011-12-20 10:58:51")).build());
assertEquals(client.listDomains(), expected);
}
public void testListDomainsWhenResponseIs4xxReturnsEmpty() throws Exception {
DomainClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/domain/list/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.build(),
HttpResponse.builder().statusCode(404).build()).getDomainClient();
// check not found response
client = createMock("list", "POST", 404, "/domain_list.json");
assertTrue(client.listDomains().isEmpty()); assertTrue(client.listDomains().isEmpty());
} }
public void testAddDomain() throws Exception { public void testAddDomainWhenResponseIs2xx() throws Exception {
createMock("add", "POST", 200, null, entry("name", "cl66666_x")).addDomain("cl66666_x"); DomainClient client = requestSendsResponse(
DomainAddOptions options = (DomainAddOptions) DomainAddOptions.Builder.primaryNameServer("ns1.somewhere.x").expire(1).minimum(1).refresh(1). HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/domain/add/format/json"))
responsiblePerson("Tester").retry(1).ttl(1); createMock("add", "POST", 200, null, entry("name", "cl66666_x")); .headers(ImmutableMultimap.<String, String>builder().put(
createMock("add", "POST", 200, null, entry("name", "cl66666_x"), options).addDomain("cl66666_x", options); "Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("name", "cl66666_x").build())).build(),
HttpResponse.builder().statusCode(200).build()).getDomainClient();
client.addDomain("cl66666_x");
} }
public void testEditDomain() throws Exception {
createMock("edit", "POST", 200, null, entry("domain", "x")).editDomain("x"); public void testAddDomainWithOptsWhenResponseIs2xx() throws Exception {
DomainClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/domain/add/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("name", "cl66666_x")
.put("primary_ns", "ns1.somewhere.x")
.put("expire", "1")
.put("minimum", "1")
.put("refresh", "1")
.put("resp_person", "Tester.")
.put("retry", "1")
.put("ttl", "1")
.build())).build(),
HttpResponse.builder().statusCode(200).build()).getDomainClient();
DomainAddOptions options = (DomainAddOptions) DomainAddOptions.Builder.primaryNameServer("ns1.somewhere.x")
.expire(1).minimum(1).refresh(1).responsiblePerson("Tester").retry(1).ttl(1);
client.addDomain("cl66666_x", options);
}
public void testEditDomainWhenResponseIs2xx() throws Exception {
DomainClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/domain/edit/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("domain", "x").build())).build(),
HttpResponse.builder().statusCode(200).build()).getDomainClient();
client.editDomain("x");
} }
@Test(expectedExceptions = {ResourceNotFoundException.class}) @Test(expectedExceptions = {ResourceNotFoundException.class})
public void testEditDomainNotFound() throws Exception { public void testEditDomainWhenResponseIs4xxThrows() throws Exception {
createMock("edit", "POST", 404, null, entry("domain", "x")).editDomain("x"); DomainClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/domain/edit/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("domain", "x").build())).build(),
HttpResponse.builder().statusCode(404).build()).getDomainClient();
client.editDomain("x");
} }
public void testDeleteDomain() throws Exception { public void testDeleteDomainWhenResponseIs2xx() throws Exception {
createMock("delete", "POST", 200, null, entry("domain", "cl666666someuser")).deleteDomain("cl666666someuser"); DomainClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/domain/delete/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("domain", "x").build())).build(),
HttpResponse.builder().statusCode(200).build()).getDomainClient();
client.deleteDomain("x");
} }
@Test(expectedExceptions = {ResourceNotFoundException.class}) @Test(expectedExceptions = {ResourceNotFoundException.class})
public void testDeleteDomainNotFound() throws Exception { public void testDeleteDomainWhenResponseIs4xxThrows() throws Exception {
createMock("delete", "POST", 404, null, entry("domain", "cl666666someuser")).deleteDomain("cl666666someuser"); DomainClient client = requestSendsResponse(
} HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/domain/delete/format/json"))
.headers(ImmutableMultimap.<String, String>builder().put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("domain", "x").build())).build(),
HttpResponse.builder().statusCode(404).build()).getDomainClient();
@Override client.deleteDomain("x");
protected DomainClient getClient(GleSYSClient gleSYSClient) {
return gleSYSClient.getDomainClient();
} }
} }

View File

@ -18,36 +18,51 @@
*/ */
package org.jclouds.glesys.features; package org.jclouds.glesys.features;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.jclouds.glesys.GleSYSClient; import org.jclouds.glesys.GleSYSClient;
import org.jclouds.glesys.domain.Email; import org.jclouds.glesys.domain.Email;
import org.jclouds.glesys.domain.EmailOverview; import org.jclouds.glesys.domain.EmailOverview;
import org.jclouds.glesys.domain.EmailOverviewDomain; import org.jclouds.glesys.domain.EmailOverviewDomain;
import org.jclouds.glesys.domain.EmailOverviewSummary; import org.jclouds.glesys.domain.EmailOverviewSummary;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.BaseRestClientExpectTest;
import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.net.URI;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Set; import java.util.Set;
import static org.jclouds.io.Payloads.newUrlEncodedFormPayload;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
/** /**
* Tests annotation parsing of {@code ArchiveAsyncClient} * Tests annotation parsing of {@code EmailClient}
* *
* @author Adam Lowe * @author Adam Lowe
*/ */
@Test(groups = "unit", testName = "EmailAsyncClientTest") @Test(groups = "unit", testName = "EmailAsyncClientTest")
public class EmailClientExpectTest extends BaseGleSYSClientExpectTest<EmailClient> { public class EmailClientExpectTest extends BaseRestClientExpectTest<GleSYSClient> {
public EmailClientExpectTest() { public EmailClientExpectTest() {
remoteServicePrefix = "email"; provider = "glesys";
} }
public void testList() throws Exception { public void testListWhenResponseIs2xx() throws Exception {
EmailClient client = createMock("list", "POST", 200, "/email_list.json", entry("domain", "test")); EmailClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/list/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(
ImmutableMultimap.<String, String>builder().put("domain", "test").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/email_list.json")).build()).getEmailClient();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Email.Builder builder = Email.builder().quota("200 MB").usedQuota("0 MB").antispamLevel(3).antiVirus(true).autoRespond(false).autoRespondSaveEmail(true).autoRespondMessage("false"); Email.Builder builder = Email.builder().quota("200 MB").usedQuota("0 MB").antispamLevel(3).antiVirus(true).autoRespond(false).autoRespondSaveEmail(true).autoRespondMessage("false");
@ -56,61 +71,141 @@ public class EmailClientExpectTest extends BaseGleSYSClientExpectTest<EmailClien
builder.account("test@adamlowe.net").created(dateFormat.parse("2011-12-22T12:13:14")).modified(dateFormat.parse("2011-12-22T12:13:35")).build(), builder.account("test@adamlowe.net").created(dateFormat.parse("2011-12-22T12:13:14")).modified(dateFormat.parse("2011-12-22T12:13:35")).build(),
builder.account("test2@adamlowe.net").created(dateFormat.parse("2011-12-22T12:14:29")).modified(dateFormat.parse("2011-12-22T12:14:31")).build() builder.account("test2@adamlowe.net").created(dateFormat.parse("2011-12-22T12:14:29")).modified(dateFormat.parse("2011-12-22T12:14:31")).build()
); );
assertEquals(client.listAccounts("test"), expected);
// check not found response assertEquals(client.listAccounts("test"), expected);
client = createMock("list", "POST", 404, "Domain not found", entry("domain", "test")); }
public void testListWhenResponseIs404IsEmpty() throws Exception {
EmailClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/list/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(
ImmutableMultimap.<String, String>builder().put("domain", "test").build())).build(),
HttpResponse.builder().statusCode(404).build()).getEmailClient();
assertTrue(client.listAccounts("test").isEmpty()); assertTrue(client.listAccounts("test").isEmpty());
} }
public void testOverview() throws Exception { public void testOverviewWhenResponseIs2xx() throws Exception {
EmailClient client = createMock("overview", "POST", 200, "/email_overview.json"); EmailClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/overview/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/email_overview.json")).build()).getEmailClient();
EmailOverviewSummary summary = EmailOverviewSummary.builder().accounts(2).maxAccounts(50).aliases(0).maxAliases(1000).build(); EmailOverviewSummary summary = EmailOverviewSummary.builder().accounts(2).maxAccounts(50).aliases(0).maxAliases(1000).build();
EmailOverviewDomain domain = EmailOverviewDomain.builder().domain("adamlowe.net").accounts(2).aliases(0).build(); EmailOverviewDomain domain = EmailOverviewDomain.builder().domain("adamlowe.net").accounts(2).aliases(0).build();
EmailOverview expected = EmailOverview.builder().summary(summary).domains(domain).build(); EmailOverview expected = EmailOverview.builder().summary(summary).domains(domain).build();
assertEquals(client.getEmailOverview(), expected); assertEquals(client.getEmailOverview(), expected);
assertNull(createMock("overview", "POST", 404, "Not found").getEmailOverview());
} }
public void testCreateAccount() throws Exception { public void testOverviewWhenResponseIs404ReturnsNull() throws Exception {
createMock("createaccount", "POST", 200, null, EmailClient client = requestSendsResponse(
entry("emailaccount", "test@jclouds.org"), entry("password", "newpass")).createAccount("test@jclouds.org", "newpass"); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/overview/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.build(),
HttpResponse.builder().statusCode(404).build()).getEmailClient();
assertNull(client.getEmailOverview());
}
public void testCreateAccountWhenResponseIs2xx() throws Exception {
EmailClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/createaccount/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("emailaccount", "test@jclouds.org")
.put("password", "newpass")
.build()))
.build(),
HttpResponse.builder().statusCode(200).build()).getEmailClient();
client.createAccount("test@jclouds.org", "newpass");
} }
@Test(expectedExceptions = {ResourceNotFoundException.class}) @Test(expectedExceptions = {ResourceNotFoundException.class})
public void testCreateAccountDomainNotFound() throws Exception { public void testCreateAccountWhenResponseIs4xxThrows() throws Exception {
createMock("createaccount", "POST", 404, null, EmailClient client = requestSendsResponse(
entry("emailaccount", "test@jclouds.org"), entry("password", "newpass")).createAccount("test@jclouds.org", "newpass"); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/createaccount/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("emailaccount", "test@jclouds.org")
.put("password", "newpass")
.build()))
.build(),
HttpResponse.builder().statusCode(404).build()).getEmailClient();
client.createAccount("test@jclouds.org", "newpass");
} }
public void testCreateAliasWhenResponseIs2xx() throws Exception {
EmailClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/createalias/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("emailalias", "test2@jclouds.org")
.put("goto", "test@jclouds.org")
.build()))
.build(),
HttpResponse.builder().statusCode(200).build()).getEmailClient();
public void testCreateAlias() throws Exception { client.createAlias("test2@jclouds.org", "test@jclouds.org");
createMock("createalias", "POST", 200, null, }
entry("emailalias", "test2@jclouds.org"), entry("goto", "test@jclouds.org")).createAlias("test2@jclouds.org", "test@jclouds.org");
@Test(expectedExceptions = {AuthorizationException.class})
public void testCreateAliasWhenResponseIs4xxThrows() throws Exception {
EmailClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/createalias/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("emailalias", "test2@jclouds.org")
.put("goto", "test@jclouds.org")
.build()))
.build(),
HttpResponse.builder().statusCode(401).build()).getEmailClient();
client.createAlias("test2@jclouds.org", "test@jclouds.org");
}
public void testEditAliasWhenResponseIs2xx() throws Exception {
EmailClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/editalias/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("emailalias", "test2@jclouds.org")
.put("goto", "test@jclouds.org")
.build()))
.build(),
HttpResponse.builder().statusCode(200).build()).getEmailClient();
client.editAlias("test2@jclouds.org", "test@jclouds.org");
} }
@Test(expectedExceptions = {ResourceNotFoundException.class}) @Test(expectedExceptions = {ResourceNotFoundException.class})
public void testCreateAliasNotFound() throws Exception { public void testEditAliasWhenResponseIs4xxThrows() throws Exception {
createMock("createalias", "POST", 404, null, EmailClient client = requestSendsResponse(
entry("emailalias", "test2@jclouds.org"), entry("goto", "test@jclouds.org")).createAlias("test2@jclouds.org", "test@jclouds.org"); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/email/editalias/format/json"))
} .headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("emailalias", "test2@jclouds.org")
.put("goto", "test@jclouds.org")
.build()))
.build(),
HttpResponse.builder().statusCode(404).build()).getEmailClient();
public void testEditAlias() throws Exception { client.editAlias("test2@jclouds.org", "test@jclouds.org");
createMock("editalias", "POST", 200, null,
entry("emailalias", "test2@jclouds.org"), entry("goto", "test1@jclouds.org")).editAlias("test2@jclouds.org", "test1@jclouds.org");
}
@Test(expectedExceptions = {ResourceNotFoundException.class})
public void testEditAliasNotFound() throws Exception {
createMock("editalias", "POST", 404, null,
entry("emailalias", "test2@jclouds.org"), entry("goto", "test1@jclouds.org")).editAlias("test2@jclouds.org", "test1@jclouds.org");
}
@Override
protected EmailClient getClient(GleSYSClient gleSYSClient) {
return gleSYSClient.getEmailClient();
} }
} }

View File

@ -18,24 +18,26 @@
*/ */
package org.jclouds.glesys.features; package org.jclouds.glesys.features;
import java.util.Map; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.jclouds.glesys.GleSYSClient; import org.jclouds.glesys.GleSYSClient;
import org.jclouds.glesys.domain.*; import org.jclouds.glesys.domain.Server;
import org.jclouds.glesys.options.ServerCloneOptions; import org.jclouds.glesys.domain.ServerCreated;
import org.jclouds.glesys.options.ServerCreateOptions; import org.jclouds.glesys.domain.ServerCreatedIp;
import org.jclouds.glesys.options.ServerDestroyOptions; import org.jclouds.glesys.domain.ServerDetails;
import org.jclouds.glesys.options.ServerEditOptions; import org.jclouds.glesys.options.*;
import org.jclouds.glesys.options.ServerStatusOptions; import org.jclouds.glesys.parse.*;
import org.jclouds.glesys.options.ServerStopOptions; import org.jclouds.http.HttpRequest;
import org.jclouds.glesys.parse.ParseServerAllowedArgumentsTest; import org.jclouds.http.HttpResponse;
import org.jclouds.glesys.parse.ParseServerDetailsTest; import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.BaseRestClientExpectTest;
import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals; import java.net.URI;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue; import static org.jclouds.io.Payloads.newUrlEncodedFormPayload;
import static org.testng.Assert.*;
/** /**
* Tests annotation parsing of {@code ServerAsyncClient} * Tests annotation parsing of {@code ServerAsyncClient}
@ -44,115 +46,407 @@ import static org.testng.Assert.assertTrue;
* @author Adam Lowe * @author Adam Lowe
*/ */
@Test(groups = "unit", testName = "ServerAsyncClientTest") @Test(groups = "unit", testName = "ServerAsyncClientTest")
public class ServerClientExpectTest extends BaseGleSYSClientExpectTest<ServerClient> { public class ServerClientExpectTest extends BaseRestClientExpectTest<GleSYSClient> {
public ServerClientExpectTest() { public ServerClientExpectTest() {
remoteServicePrefix = "server"; provider = "glesys";
} }
private final String serverId = "abcd"; public void testListServersWhenResponseIs2xx() throws Exception {
private final Map.Entry<String, String> serverIdOnly = entry("serverid", serverId); ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/list/format/json"))
public void testListServers() throws Exception { .headers(ImmutableMultimap.<String, String>builder()
ServerClient client = createMock("list", "POST", 200, "/server_list.json"); .put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(204).payload(payloadFromResource("/server_list.json")).build()).getServerClient();
Server expected = Server.builder().id("vz1541880").hostname("mammamia").datacenter("Falkenberg").platform("OpenVZ").build(); Server expected = Server.builder().id("vz1541880").hostname("mammamia").datacenter("Falkenberg").platform("OpenVZ").build();
assertEquals(client.listServers(), ImmutableSet.<Server>of(expected)); assertEquals(client.listServers(), ImmutableSet.<Server>of(expected));
// check we get empty set, if not-found
assertTrue(createMock("list", "POST", 404, "Not found").listServers().isEmpty());
} }
public void testGetAllowedArguments() throws Exception { public void testListServersWhenReponseIs404IsEmpty() {
ServerClient client = createMock("allowedarguments", "GET", 200, "/server_allowed_arguments.json"); ServerClient client = requestSendsResponse(
assertEquals(client.getServerAllowedArguments(), ParseServerAllowedArgumentsTest.getData()); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/list/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(404).build()).getServerClient();
assertTrue(client.listServers().isEmpty());
} }
public void testGetTemplates() throws Exception { public void testGetAllowedArgumentsWhenResponseIs2xx() throws Exception {
createMock("templates", "GET", 200, "/server_templates.json"); ServerClient client = requestSendsResponse(
HttpRequest.builder().method("GET").endpoint(URI.create("https://api.glesys.com/server/allowedarguments/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(204).payload(payloadFromResource("/server_allowed_arguments.json")).build()).getServerClient();
assertEquals(client.getServerAllowedArguments(), new ParseServerAllowedArgumentsTest().expected());
} }
public void testGetServer() throws Exception { public void testGetTemplatesWhenResponseIs2xx() throws Exception {
ServerClient client = createMock("details", "POST", 200, "/server_details.json", serverIdOnly); ServerClient client = requestSendsResponse(
HttpRequest.builder().method("GET").endpoint(URI.create("https://api.glesys.com/server/templates/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_templates.json")).build()).getServerClient();
ServerDetails actual = client.getServerDetails(serverId); assertEquals(client.getTemplates(), new ParseServerTemplatesTest().expected());
assertEquals(actual, ParseServerDetailsTest.getData()); }
assertEquals(actual.toString(), ParseServerDetailsTest.getData().toString());
public void testGetServerWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/details/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server1ssg-1.1").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_details.json")).build()).getServerClient();
ServerDetails actual = client.getServerDetails("server1ssg-1.1");
assertEquals(actual.toString(), new ParseServerDetailsTest().expected().toString());
} }
@Test @Test
public void testCreateServer() throws Exception { public void testCreateServerWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/create/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("cpucores", "1").put("memorysize", "512")
.put("datacenter", "Falkenberg")
.put("transfer", "50")
.put("rootpw", "password")
.put("hostname", "jclouds-test")
.put("platform", "OpenVZ")
.put("template", "Ubuntu 32-bit")
.put("disksize", "5").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_created.json")).build()).getServerClient();
ServerCreated expected = ServerCreated.builder().hostname("jclouds-test").id("xm3630641").ips(ServerCreatedIp.builder().ip("109.74.10.27").build()).build(); ServerCreated expected = ServerCreated.builder().hostname("jclouds-test").id("xm3630641").ips(ServerCreatedIp.builder().ip("109.74.10.27").build()).build();
ServerClient client = createMock("create", "POST", 200, "/server_created.json",
entry("cpucores", 1), entry("memorysize", 512),
entry("datacenter", "Falkenberg"), entry("transfer", 50),
entry("rootpw", "password"), entry("hostname", "jclouds-test"), entry("platform", "OpenVZ"),
entry("template", "Ubuntu 32-bit"),
entry("disksize", 5));
assertEquals(client.createServer("Falkenberg", "OpenVZ", "jclouds-test", "Ubuntu 32-bit", 5, 512, 1, "password", 50), expected);
assertEquals(client.createServer("Falkenberg", "OpenVZ", "jclouds-test", "Ubuntu 32-bit", 5, 512, 1, "password", 50), expected);
}
public void testCreateServerWithOptsWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/create/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("cpucores", "1").put("memorysize", "512")
.put("datacenter", "Falkenberg")
.put("transfer", "50")
.put("rootpw", "password")
.put("hostname", "jclouds-test")
.put("platform", "OpenVZ")
.put("template", "Ubuntu 32-bit")
.put("disksize", "5")
.put("description", "Description-of-server")
.put("ip", "10.0.0.1").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_created.json")).build()).getServerClient();
ServerCreateOptions options = ServerCreateOptions.Builder.description("Description-of-server").ip("10.0.0.1"); ServerCreateOptions options = ServerCreateOptions.Builder.description("Description-of-server").ip("10.0.0.1");
client = createMock("create", "POST", 200, "/server_created.json", ServerCreated expected = ServerCreated.builder().hostname("jclouds-test").id("xm3630641").ips(ServerCreatedIp.builder().ip("109.74.10.27").build()).build();
entry("cpucores", 1), entry("memorysize", 512),
entry("datacenter", "Falkenberg"), entry("transfer", 50),
entry("rootpw", "password"), entry("hostname", "jclouds-test"), entry("platform", "OpenVZ"),
entry("template", "Ubuntu 32-bit"),
entry("disksize", 5), options);
assertEquals(client.createServer("Falkenberg", "OpenVZ", "jclouds-test", "Ubuntu 32-bit", 5, 512, 1, "password", 50, options), expected); assertEquals(client.createServer("Falkenberg", "OpenVZ", "jclouds-test", "Ubuntu 32-bit", 5, 512, 1, "password", 50, options), expected);
} }
@Test @Test
public void testEditServer() throws Exception { public void testEditServerWhenResponseIs2xx() throws Exception {
createMock("edit", "POST", 200, null, serverIdOnly).editServer(serverId); ServerClient client = requestSendsResponse(
ServerEditOptions options = HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/edit/format/json"))
ServerEditOptions.Builder.description("Description-of-server").disksize(1).memorysize(512).cpucores(1).hostname("jclouds-test"); .headers(ImmutableMultimap.<String, String>builder()
createMock("edit", "POST", 200, null, serverIdOnly, options).editServer(serverId, options); .put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server111").build())).build(),
HttpResponse.builder().statusCode(206).build()).getServerClient();
client.editServer("server111");
} }
@Test @Test
public void testCloneServer() throws Exception { public void testEditServerWithOptsWhenResponseIs2xx() throws Exception {
createMock("clone", "POST", 200, "/server_created.json", serverIdOnly, entry("hostname", "somename")).cloneServer(serverId, "somename"); ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/edit/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server111")
.put("description", "Description-of-server")
.put("disksize", "1")
.put("memorysize", "512")
.put("cpucores", "1")
.put("hostname", "jclouds-test")
.build())).build(),
HttpResponse.builder().statusCode(200).build()).getServerClient();
ServerEditOptions options =
ServerEditOptions.Builder.description("Description-of-server").disksize(1).memorysize(512).cpucores(1).hostname("jclouds-test");
client.editServer("server111", options);
}
@Test
public void testCloneServerWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/clone/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server111")
.put("hostname", "hostname1").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_created.json")).build()).getServerClient();
ServerCreated expected = ServerCreated.builder().hostname("jclouds-test").id("xm3630641").ips(ServerCreatedIp.builder().ip("109.74.10.27").build()).build();
assertEquals(client.cloneServer("server111", "hostname1"), expected);
}
@Test
public void testCloneServerWithOptsWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/clone/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server111")
.put("hostname", "hostname1")
.put("description", "Description-of-server")
.put("disksize", "1")
.put("memorysize", "512")
.put("cpucores", "1").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_created.json")).build()).getServerClient();
ServerCloneOptions options = (ServerCloneOptions) ServerCloneOptions.Builder.description("Description-of-server").disksize(1).memorysize(512).cpucores(1); ServerCloneOptions options = (ServerCloneOptions) ServerCloneOptions.Builder.description("Description-of-server").disksize(1).memorysize(512).cpucores(1);
createMock("clone", "POST", 200, "/server_created.json", serverIdOnly, entry("hostname", "somename"), options).cloneServer(serverId, "somename", options); ServerCreated expected = ServerCreated.builder().hostname("jclouds-test").id("xm3630641").ips(ServerCreatedIp.builder().ip("109.74.10.27").build()).build();
assertEquals(client.cloneServer("server111", "hostname1", options), expected);
} }
public void testGetServerStatus() throws Exception { @Test(expectedExceptions = {ResourceNotFoundException.class})
createMock("status", "POST", 200, "/server_status.json", serverIdOnly).getServerStatus(serverId); public void testCloneServerWhenResponseIs4xx() throws Exception {
createMock("status", "POST", 200, "/server_status.json", serverIdOnly, ServerStatusOptions.Builder.state()). ServerClient client = requestSendsResponse(
getServerStatus(serverId, ServerStatusOptions.Builder.state()); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/clone/format/json"))
createMock("status", "POST", 404, "Not found", serverIdOnly).getServerStatus(serverId); .headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server111")
.put("hostname", "hostname1").build())).build(),
HttpResponse.builder().statusCode(404).build()).getServerClient();
client.cloneServer("server111", "hostname1");
} }
public void testGetServerLimits() throws Exception { public void testGetServerStatusWhenResponseIs2xx() throws Exception {
createMock("limits", "POST", 200, "/server_limits.json", serverIdOnly).getServerLimits(serverId); ServerClient client = requestSendsResponse(
assertNull(createMock("limits", "POST", 404, "Not found", serverIdOnly).getServerLimits(serverId)); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/status/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server111").build())).build(),
HttpResponse.builder().statusCode(206).payload(payloadFromResource("/server_status.json")).build())
.getServerClient();
assertEquals(client.getServerStatus("server111"), new ParseServerStatusTest().expected());
} }
public void testGetServerConsole() throws Exception { public void testGetServerStatusWithOptsWhenResponseIs2xx() throws Exception {
createMock("console", "POST", 200, "/server_console.json", serverIdOnly).getServerConsole(serverId); ServerClient client = requestSendsResponse(
assertNull(createMock("console", "POST", 404, "Not found", serverIdOnly).getServerConsole(serverId)); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/status/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server321").put("statustype", "state").build())).build(),
HttpResponse.builder().statusCode(206).payload(payloadFromResource("/server_status.json")).build())
.getServerClient();
assertEquals(client.getServerStatus("server321", ServerStatusOptions.Builder.state()), new ParseServerStatusTest().expected());
} }
public void testStartServer() throws Exception { public void testGetServerStatusWhenResponseIs4xx() throws Exception {
createMock("start", "POST", 200, null, serverIdOnly).startServer(serverId); ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/status/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server321").put("statustype", "state").build())).build(),
HttpResponse.builder().statusCode(404).build())
.getServerClient();
assertNull(client.getServerStatus("server321", ServerStatusOptions.Builder.state()));
} }
public void testStopServer() throws Exception { public void testGetServerLimitsWhenResponseIs2xx() throws Exception {
createMock("stop", "POST", 200, null, serverIdOnly).stopServer(serverId); ServerClient client = requestSendsResponse(
createMock("stop", "POST", 200, null, serverIdOnly, ServerStopOptions.Builder.hard()).stopServer(serverId, ServerStopOptions.Builder.hard()); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/limits/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server321").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_limits.json")).build())
.getServerClient();
client.getServerLimits("server321");
} }
public void testRebootServer() throws Exception { public void testGetServerConsoleWhenResponseIs2xx() throws Exception {
createMock("reboot", "POST", 200, null, serverIdOnly).rebootServer(serverId); ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/console/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server322").build())).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_console.json")).build())
.getServerClient();
assertEquals(client.getServerConsole("server322"), new ParseServerConsoleTest().expected());
} }
public void testDestroyServer() throws Exception { public void testGetServerConsoleWhenResponseIs4xx() throws Exception {
createMock("destroy", "POST", 200, null, serverIdOnly, ServerDestroyOptions.Builder.keepIp()).destroyServer(serverId, ServerDestroyOptions.Builder.keepIp()); ServerClient client = requestSendsResponse(
createMock("destroy", "POST", 200, null, serverIdOnly, ServerDestroyOptions.Builder.discardIp()).destroyServer(serverId, ServerDestroyOptions.Builder.discardIp()); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/console/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server322").build())).build(),
HttpResponse.builder().statusCode(404).build())
.getServerClient();
assertNull(client.getServerConsole("server322"));
} }
@Override public void testStartServerWhenResponseIs2xx() throws Exception {
protected ServerClient getClient(GleSYSClient gleSYSClient) { ServerClient client = requestSendsResponse(
return gleSYSClient.getServerClient(); HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/start/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").build())).build(),
HttpResponse.builder().statusCode(200).build())
.getServerClient();
client.startServer("server777");
}
@Test(expectedExceptions = {AuthorizationException.class})
public void testStartServerWhenResponseIs4xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/start/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").build())).build(),
HttpResponse.builder().statusCode(401).build())
.getServerClient();
client.startServer("server777");
}
public void testStopServerWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/stop/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").build())).build(),
HttpResponse.builder().statusCode(200).build())
.getServerClient();
client.stopServer("server777");
}
public void testHardStopServerWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/stop/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").put("type", "hard").build())).build(),
HttpResponse.builder().statusCode(200).build())
.getServerClient();
client.stopServer("server777", ServerStopOptions.Builder.hard());
}
@Test(expectedExceptions = {AuthorizationException.class})
public void testStopServerWhenResponseIs4xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/stop/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").build())).build(),
HttpResponse.builder().statusCode(401).build())
.getServerClient();
client.stopServer("server777");
}
public void testRebootServerWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/reboot/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").build())).build(),
HttpResponse.builder().statusCode(200).build())
.getServerClient();
client.rebootServer("server777");
}
@Test(expectedExceptions = {AuthorizationException.class})
public void testRebootServerWhenResponseIs4xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/reboot/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").build())).build(),
HttpResponse.builder().statusCode(401).build())
.getServerClient();
client.rebootServer("server777");
}
public void testDestroyServerWhenResponseIs2xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/destroy/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").put("keepip", "1").build())).build(),
HttpResponse.builder().statusCode(200).build())
.getServerClient();
client.destroyServer("server777", ServerDestroyOptions.Builder.keepIp());
}
@Test(expectedExceptions = {AuthorizationException.class})
public void testDestroyServerWhenResponseIs4xx() throws Exception {
ServerClient client = requestSendsResponse(
HttpRequest.builder().method("POST").endpoint(URI.create("https://api.glesys.com/server/destroy/format/json"))
.headers(ImmutableMultimap.<String, String>builder()
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(newUrlEncodedFormPayload(ImmutableMultimap.<String, String>builder()
.put("serverid", "server777").put("keepip", "0").build())).build(),
HttpResponse.builder().statusCode(401).build())
.getServerClient();
client.destroyServer("server777", ServerDestroyOptions.Builder.discardIp());
} }
} }

View File

@ -47,10 +47,6 @@ public class ParseServerAllowedArgumentsTest extends BaseItemParserTest<Map<Stri
@SelectJson("argumentslist") @SelectJson("argumentslist")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public Map<String, ServerAllowedArguments> expected() { public Map<String, ServerAllowedArguments> expected() {
return getData();
}
public static Map<String, ServerAllowedArguments> getData() {
Map<String, ServerAllowedArguments> result = new LinkedHashMap<String, ServerAllowedArguments>(); Map<String, ServerAllowedArguments> result = new LinkedHashMap<String, ServerAllowedArguments>();
ServerAllowedArguments openvz = ServerAllowedArguments.builder() ServerAllowedArguments openvz = ServerAllowedArguments.builder()
.dataCenters("Amsterdam", "Falkenberg", "New York City", "Stockholm") .dataCenters("Amsterdam", "Falkenberg", "New York City", "Stockholm")

View File

@ -49,10 +49,6 @@ public class ParseServerDetailsTest extends BaseItemParserTest<ServerDetails> {
@SelectJson("server") @SelectJson("server")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public ServerDetails expected() { public ServerDetails expected() {
return getData();
}
public static ServerDetails getData() {
Cost cost = Cost.builder().amount(6.38).currency("EUR").timePeriod("month").build(); Cost cost = Cost.builder().amount(6.38).currency("EUR").timePeriod("month").build();
return ServerDetails.builder().id("vz1908384").hostname("jclouds-unit").cpuCores(1). return ServerDetails.builder().id("vz1908384").hostname("jclouds-unit").cpuCores(1).
memory(128).disk(5). memory(128).disk(5).