From a0ada77eeca1c785d571e4db931f15e43a403efd Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Tue, 7 Apr 2015 23:46:07 +0200 Subject: [PATCH] replace ExpectTests with MockWebServerTests refactor AccountApiExpectedTest to AccountApiMockTest refactor DatacenterApiExpectTest to DatacenterApiMockTest refactor SoftwareDescriptionApiExpectTest to SoftwareDescriptionApiMockTest refactor VirtualGuestApiExpectTest to VirtualGuestApiMockTest refactor VirtualGuestBlockDeviceTemplateGroupApiExpectTest to VirtualGuestBlockDeviceTemplateGroupApiMockTest --- .../java/org/jclouds/reflect/Reflection2.java | 20 +- providers/softlayer/pom.xml | 17 ++ .../org/jclouds/softlayer/domain/Address.java | 12 +- .../softlayer/features/AccountApi.java | 6 +- .../softlayer/features/DatacenterApi.java | 7 +- .../softlayer/features/VirtualGuestApi.java | 19 +- ...rtualGuestBlockDeviceTemplateGroupApi.java | 7 +- .../features/AccountApiExpectTest.java | 94 ------- .../features/AccountApiMockTest.java | 88 ++++++ .../features/DatacenterApiExpectTest.java | 93 ------- .../features/DatacenterApiMockTest.java | 86 ++++++ .../SoftwareDescriptionApiExpectTest.java | 55 ---- .../SoftwareDescriptionApiMockTest.java | 48 ++++ .../features/VirtualGuestApiExpectTest.java | 263 ------------------ .../features/VirtualGuestApiMockTest.java | 234 ++++++++++++++++ ...BlockDeviceTemplateGroupApiExpectTest.java | 91 ------ ...stBlockDeviceTemplateGroupApiMockTest.java | 86 ++++++ .../internal/BaseSoftLayerMockTest.java | 73 +++++ ...onseTest.java => DatacenterParseTest.java} | 2 +- ...nseTest.java => DatacentersParseTest.java} | 2 +- ...a => GetCreateObjectOptionsParseTest.java} | 2 +- ...seTest.java => PublicImagesParseTest.java} | 2 +- ...ava => SoftwareDescriptionsParseTest.java} | 2 +- ...estBlockDeviceTemplateGroupParseTest.java} | 2 +- ...stBlockDeviceTemplateGroupsParseTest.java} | 2 +- ...seTest.java => VirtualGuestParseTest.java} | 2 +- ...eTest.java => VirtualGuestsParseTest.java} | 2 +- 27 files changed, 679 insertions(+), 638 deletions(-) delete mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/AccountApiExpectTest.java create mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/AccountApiMockTest.java delete mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/DatacenterApiExpectTest.java create mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/DatacenterApiMockTest.java delete mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/SoftwareDescriptionApiExpectTest.java create mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/SoftwareDescriptionApiMockTest.java delete mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestApiExpectTest.java create mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestApiMockTest.java delete mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApiExpectTest.java create mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApiMockTest.java create mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/internal/BaseSoftLayerMockTest.java rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{GetDatacenterResponseTest.java => DatacenterParseTest.java} (96%) rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{ListDatacentersResponseTest.java => DatacentersParseTest.java} (97%) rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{GetCreateObjectOptionsResponseTest.java => GetCreateObjectOptionsParseTest.java} (97%) rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{ListPublicImagesResponseTest.java => PublicImagesParseTest.java} (99%) rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{GetAllObjectsResponseTest.java => SoftwareDescriptionsParseTest.java} (95%) rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{GetVirtualGuestBlockDeviceTemplateGroupResponseTest.java => VirtualGuestBlockDeviceTemplateGroupParseTest.java} (99%) rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{GetVirtualGuestBlockDeviceTemplateGroupsResponseTest.java => VirtualGuestBlockDeviceTemplateGroupsParseTest.java} (99%) rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{GetVirtualGuestResponseTest.java => VirtualGuestParseTest.java} (96%) rename providers/softlayer/src/test/java/org/jclouds/softlayer/parse/{ListVirtualGuestsResponseTest.java => VirtualGuestsParseTest.java} (96%) diff --git a/core/src/main/java/org/jclouds/reflect/Reflection2.java b/core/src/main/java/org/jclouds/reflect/Reflection2.java index f592466341..eb43ce0907 100644 --- a/core/src/main/java/org/jclouds/reflect/Reflection2.java +++ b/core/src/main/java/org/jclouds/reflect/Reflection2.java @@ -49,7 +49,7 @@ import com.google.common.util.concurrent.UncheckedExecutionException; /** * Utilities that allow access to {@link Invokable}s with {@link Invokable#getOwnerType() owner types}. - * + * * @since 1.6 */ @Beta @@ -73,12 +73,12 @@ public class Reflection2 { /** * returns an {@link Invokable} object that reflects a constructor present in the {@link TypeToken} type. - * + * * @param ownerType * corresponds to {@link Invokable#getOwnerType()} * @param parameterTypes * corresponds to {@link Constructor#getParameterTypes()} - * + * * @throws IllegalArgumentException * if the constructor doesn't exist or a security exception occurred */ @@ -90,7 +90,7 @@ public class Reflection2 { /** * return all constructors or static factory methods present in the class as {@link Invokable}s. - * + * * @param ownerType * corresponds to {@link Invokable#getOwnerType()} */ @@ -101,7 +101,7 @@ public class Reflection2 { /** * returns an {@link Invokable} object that links the {@code method} to its owner. - * + * * @param ownerType * corresponds to {@link Invokable#getOwnerType()} * @param method @@ -116,14 +116,14 @@ public class Reflection2 { * returns an {@link Invokable} object that reflects a method present in the {@link TypeToken} type. * If there are multiple methods of the same name and parameter list, returns the method in the nearest * ancestor with the most specific return type (see {@link Class#getDeclaredMethod}). - * + * * @param ownerType * corresponds to {@link Invokable#getOwnerType()} * @param name * name of the method to be returned * @param parameterTypes * corresponds to {@link Method#getParameterTypes()} - * + * * @throws IllegalArgumentException * if the method doesn't exist or a security exception occurred */ @@ -135,7 +135,7 @@ public class Reflection2 { /** * return all methods present in the class as {@link Invokable}s. - * + * * @param ownerType * corresponds to {@link Invokable#getOwnerType()} */ @@ -216,7 +216,7 @@ public class Reflection2 { /** * Returns the {@link Parameter}s associated with the given {@link Invokable}. This function is backed by a cache. - * + * * @param invokable * The {@link Invokable} we want to get Parameters from */ @@ -258,7 +258,7 @@ public class Reflection2 { Set> methods = get(methodsForTypeToken, key.type); /* * There may be multiple instances, even on the most immediate ancestor, - * of a method with the required name and parameter set. This will occur + * of a method with the required name and parameter set. This will occur * if the method overrides one declared in a parent class with a less specific * return type. These bridge methods inserted by the compiler will be marked * as "synthetic". diff --git a/providers/softlayer/pom.xml b/providers/softlayer/pom.xml index 9988fd4780..31a0c76a22 100644 --- a/providers/softlayer/pom.xml +++ b/providers/softlayer/pom.xml @@ -66,6 +66,11 @@ jclouds-compute ${project.version} + + com.google.auto.value + auto-value + provided + org.apache.jclouds jclouds-core @@ -97,6 +102,18 @@ logback-classic test + + com.squareup.okhttp + mockwebserver + test + + + + org.bouncycastle + bcprov-jdk15on + + + com.google.auto.service auto-service diff --git a/providers/softlayer/src/main/java/org/jclouds/softlayer/domain/Address.java b/providers/softlayer/src/main/java/org/jclouds/softlayer/domain/Address.java index 82ea706e66..3ee33341b8 100644 --- a/providers/softlayer/src/main/java/org/jclouds/softlayer/domain/Address.java +++ b/providers/softlayer/src/main/java/org/jclouds/softlayer/domain/Address.java @@ -16,14 +16,14 @@ */ package org.jclouds.softlayer.domain; -import com.google.common.base.Objects; -import com.google.inject.name.Named; -import org.jclouds.javax.annotation.Nullable; - -import java.beans.ConstructorProperties; - import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Strings.emptyToNull; +import java.beans.ConstructorProperties; + +import org.jclouds.javax.annotation.Nullable; + +import com.google.common.base.Objects; +import com.google.inject.name.Named; /** * Class Address diff --git a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/AccountApi.java b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/AccountApi.java index e7dce83bba..05f01e0e8c 100644 --- a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/AccountApi.java +++ b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/AccountApi.java @@ -24,7 +24,7 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.MediaType; -import org.jclouds.Fallbacks; +import org.jclouds.Fallbacks.EmptySetOnNotFoundOr404; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.QueryParams; @@ -54,7 +54,7 @@ public interface AccountApi { @GET @Path("/SoftLayer_Account/VirtualGuests") @QueryParams(keys = "objectMask", values = LIST_GUEST_MASK) - @Fallback(Fallbacks.EmptySetOnNotFoundOr404.class) + @Fallback(EmptySetOnNotFoundOr404.class) Set listVirtualGuests(); /** @@ -65,7 +65,7 @@ public interface AccountApi { @GET @Path("/SoftLayer_Account/getBlockDeviceTemplateGroups") @QueryParams(keys = "objectMask", values = GUEST_MASK) - @Fallback(Fallbacks.EmptySetOnNotFoundOr404.class) + @Fallback(EmptySetOnNotFoundOr404.class) Set getBlockDeviceTemplateGroups(); } diff --git a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/DatacenterApi.java b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/DatacenterApi.java index 7d88e67c72..e46f943bda 100644 --- a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/DatacenterApi.java +++ b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/DatacenterApi.java @@ -16,6 +16,8 @@ */ package org.jclouds.softlayer.features; +import static org.jclouds.Fallbacks.EmptySetOnNotFoundOr404; +import static org.jclouds.Fallbacks.NullOnNotFoundOr404; import java.util.Set; import javax.ws.rs.Consumes; @@ -24,7 +26,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; -import org.jclouds.Fallbacks; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.QueryParams; @@ -47,7 +48,7 @@ public interface DatacenterApi { @GET @Path("/SoftLayer_Location_Datacenter/Datacenters") @QueryParams(keys = "objectMask", values = "locationAddress;regions") - @Fallback(Fallbacks.EmptySetOnNotFoundOr404.class) + @Fallback(EmptySetOnNotFoundOr404.class) Set listDatacenters(); /** @@ -58,6 +59,6 @@ public interface DatacenterApi { @GET @Path("/SoftLayer_Location_Datacenter/{id}") @QueryParams(keys = "objectMask", values = "locationAddress;regions") - @Fallback(Fallbacks.NullOnNotFoundOr404.class) + @Fallback(NullOnNotFoundOr404.class) Datacenter getDatacenter(@PathParam("id") long id); } diff --git a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestApi.java b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestApi.java index 046a711034..75d61717b2 100644 --- a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestApi.java +++ b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestApi.java @@ -16,6 +16,9 @@ */ package org.jclouds.softlayer.features; +import static org.jclouds.Fallbacks.FalseOnNotFoundOr404; +import static org.jclouds.Fallbacks.NullOnNotFoundOr404; +import static org.jclouds.Fallbacks.VoidOnNotFoundOr404; import java.util.Set; import javax.inject.Named; @@ -27,7 +30,6 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; -import org.jclouds.Fallbacks; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.BinderParam; import org.jclouds.rest.annotations.Fallback; @@ -78,7 +80,7 @@ public interface VirtualGuestApi { @GET @Path("/SoftLayer_Virtual_Guest/{id}/getObject") @QueryParams(keys = "objectMask", values = GUEST_MASK) - @Fallback(Fallbacks.NullOnNotFoundOr404.class) + @Fallback(NullOnNotFoundOr404.class) VirtualGuest getVirtualGuest(@PathParam("id") long id); /** @@ -90,7 +92,7 @@ public interface VirtualGuestApi { @Named("VirtualGuests:delete") @GET @Path("/SoftLayer_Virtual_Guest/{id}/deleteObject") - @Fallback(Fallbacks.FalseOnNotFoundOr404.class) + @Fallback(FalseOnNotFoundOr404.class) boolean deleteVirtualGuest(@PathParam("id") long id); /** @@ -100,7 +102,7 @@ public interface VirtualGuestApi { @Named("VirtualGuests:getCreateObjectOptions") @GET @Path("/SoftLayer_Virtual_Guest/getCreateObjectOptions") - @Fallback(Fallbacks.NullOnNotFoundOr404.class) + @Fallback(NullOnNotFoundOr404.class) ContainerVirtualGuestConfiguration getCreateObjectOptions(); /** @@ -112,7 +114,7 @@ public interface VirtualGuestApi { @Named("VirtualGuest:rebootHard") @GET @Path("/SoftLayer_Virtual_Guest/{id}/rebootHard.json") - @Fallback(Fallbacks.VoidOnNotFoundOr404.class) + @Fallback(VoidOnNotFoundOr404.class) void rebootHardVirtualGuest(@PathParam("id") long id); /** @@ -124,7 +126,7 @@ public interface VirtualGuestApi { @Named("VirtualGuest:pause") @GET @Path("/SoftLayer_Virtual_Guest/{id}/pause.json") - @Fallback(Fallbacks.VoidOnNotFoundOr404.class) + @Fallback(VoidOnNotFoundOr404.class) void pauseVirtualGuest(@PathParam("id") long id); /** @@ -136,7 +138,7 @@ public interface VirtualGuestApi { @Named("VirtualGuest:resume") @GET @Path("/SoftLayer_Virtual_Guest/{id}/resume.json") - @Fallback(Fallbacks.VoidOnNotFoundOr404.class) + @Fallback(VoidOnNotFoundOr404.class) void resumeVirtualGuest(@PathParam("id") long id); /** @@ -149,6 +151,7 @@ public interface VirtualGuestApi { @POST @Path("/SoftLayer_Virtual_Guest/{id}/setTags") @Produces(MediaType.APPLICATION_JSON) + @Fallback(FalseOnNotFoundOr404.class) boolean setTags(@PathParam("id") long id, @BinderParam(TagToJson.class) Set tags); /** @@ -177,6 +180,6 @@ public interface VirtualGuestApi { @Path("/SoftLayer_Virtual_Guest/{id}/getObject") @Produces(MediaType.APPLICATION_JSON) @QueryParams(keys = "objectMask", values = NOTES_MASK) - @Fallback(Fallbacks.NullOnNotFoundOr404.class) + @Fallback(NullOnNotFoundOr404.class) VirtualGuest getNotes(@PathParam("id") long id); } diff --git a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApi.java b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApi.java index 56a6030ada..7b0dd9da49 100644 --- a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApi.java +++ b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApi.java @@ -16,6 +16,8 @@ */ package org.jclouds.softlayer.features; +import static org.jclouds.Fallbacks.EmptySetOnNotFoundOr404; +import static org.jclouds.Fallbacks.NullOnNotFoundOr404; import java.util.Set; import javax.inject.Named; @@ -25,7 +27,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; -import org.jclouds.Fallbacks; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.QueryParams; @@ -53,7 +54,7 @@ public interface VirtualGuestBlockDeviceTemplateGroupApi { @GET @Path("/SoftLayer_Virtual_Guest_Block_Device_Template_Group/getPublicImages") @QueryParams(keys = "objectMask", values = MASK) - @Fallback(Fallbacks.EmptySetOnNotFoundOr404.class) + @Fallback(EmptySetOnNotFoundOr404.class) Set getPublicImages(); /** @@ -68,6 +69,6 @@ public interface VirtualGuestBlockDeviceTemplateGroupApi { @GET @Path("/SoftLayer_Virtual_Guest_Block_Device_Template_Group/{id}/getObject") @QueryParams(keys = "objectMask", values = MASK) - @Fallback(Fallbacks.NullOnNotFoundOr404.class) + @Fallback(NullOnNotFoundOr404.class) VirtualGuestBlockDeviceTemplateGroup getObject(@PathParam("id") String id); } diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/AccountApiExpectTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/AccountApiExpectTest.java deleted file mode 100644 index 407e8124db..0000000000 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/AccountApiExpectTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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.softlayer.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.jclouds.softlayer.SoftLayerApi; -import org.jclouds.softlayer.parse.GetVirtualGuestBlockDeviceTemplateGroupsResponseTest; -import org.jclouds.softlayer.parse.ListVirtualGuestsResponseTest; -import org.testng.annotations.Test; - -import com.google.common.collect.Iterables; - -@Test(groups = "unit", testName = "AccountApiExpectTest") -public class AccountApiExpectTest extends BaseSoftLayerApiExpectTest { - - public void testListVirtualGuestsWhenResponseIs2xx() { - - HttpRequest listVirtualGuestsRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Account/VirtualGuests?objectMask=powerState%3BoperatingSystem.passwords%3Bdatacenter%3BbillingItem%3BblockDevices.diskImage%3BtagReferences") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse listVirtualGuestsResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/account_list.json")).build(); - - SoftLayerApi api = requestSendsResponse(listVirtualGuestsRequest, listVirtualGuestsResponse); - - assertEquals(api.getAccountApi().listVirtualGuests(), - new ListVirtualGuestsResponseTest().expected()); - } - - public void testListVirtualGuestsWhenResponseIs4xx() { - - HttpRequest listVirtualGuestsRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Account/VirtualGuests?objectMask=powerState%3BoperatingSystem.passwords%3Bdatacenter%3BbillingItem%3BblockDevices.diskImage%3BtagReferences") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse listVirtualGuestsResponse = HttpResponse.builder().statusCode(404).build(); - - SoftLayerApi api = requestSendsResponse(listVirtualGuestsRequest, listVirtualGuestsResponse); - - assertTrue(Iterables.isEmpty(api.getAccountApi().listVirtualGuests())); - } - - public void testGetBlockDeviceTemplateGroupsWhenResponseIs2xx() { - - HttpRequest getVirtualGuestBlockDeviceTemplateGroup = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Account/getBlockDeviceTemplateGroups?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getVirtualGuestBlockDeviceTemplateGroupResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/account_get_block_devices_template_groups.json")).build(); - - SoftLayerApi api = requestSendsResponse(getVirtualGuestBlockDeviceTemplateGroup, getVirtualGuestBlockDeviceTemplateGroupResponse); - - assertEquals(api.getAccountApi().getBlockDeviceTemplateGroups(), - new GetVirtualGuestBlockDeviceTemplateGroupsResponseTest().expected()); - } - - public void testGetBlockDeviceTemplateGroupsWhenResponseIs4xx() { - - HttpRequest getObjectRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Account/getBlockDeviceTemplateGroups?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getObjectResponse = HttpResponse.builder().statusCode(404).build(); - - SoftLayerApi api = requestSendsResponse(getObjectRequest, getObjectResponse); - - assertTrue(Iterables.isEmpty(api.getAccountApi().getBlockDeviceTemplateGroups())); - } - -} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/AccountApiMockTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/AccountApiMockTest.java new file mode 100644 index 0000000000..46ca8e4636 --- /dev/null +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/AccountApiMockTest.java @@ -0,0 +1,88 @@ +/* + * 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.softlayer.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; +import org.jclouds.softlayer.SoftLayerApi; +import org.jclouds.softlayer.config.SoftLayerParserModule; +import org.jclouds.softlayer.internal.BaseSoftLayerMockTest; +import org.jclouds.softlayer.parse.VirtualGuestBlockDeviceTemplateGroupsParseTest; +import org.jclouds.softlayer.parse.VirtualGuestsParseTest; +import org.testng.annotations.Test; + +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; + +/** + * Mock tests for the {@link org.jclouds.softlayer.features.AccountApi} class. + */ +@Test(groups = "unit", testName = "AccountApiMockTest") +public class AccountApiMockTest extends BaseSoftLayerMockTest { + + public void testListVirtualGuests() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/account_list.json"))); + AccountApi api = getAccountApi(server); + try { + assertEquals(api.listVirtualGuests(), new VirtualGuestsParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Account/VirtualGuests?objectMask=powerState%3BoperatingSystem.passwords%3Bdatacenter%3BbillingItem%3BblockDevices.diskImage%3BtagReferences"); + } finally { + server.shutdown(); + } + } + + public void testEmptyListVirtualGuests() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + AccountApi api = getAccountApi(server); + try { + assertTrue(api.listVirtualGuests().isEmpty()); + assertSent(server, "GET", "/SoftLayer_Account/VirtualGuests?objectMask=powerState%3BoperatingSystem.passwords%3Bdatacenter%3BbillingItem%3BblockDevices.diskImage%3BtagReferences"); + } finally { + server.shutdown(); + } + } + + public void testGetBlockDeviceTemplateGroups() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/account_get_block_devices_template_groups.json"))); + AccountApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getAccountApi(); + try { + assertEquals(api.getBlockDeviceTemplateGroups(), new VirtualGuestBlockDeviceTemplateGroupsParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Account/getBlockDeviceTemplateGroups?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription"); + } finally { + server.shutdown(); + } + } + + public void testEmptyBlockDeviceTemplateGroups() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + AccountApi api = getAccountApi(server); + try { + assertTrue(api.getBlockDeviceTemplateGroups().isEmpty()); + assertSent(server, "GET", "/SoftLayer_Account/getBlockDeviceTemplateGroups?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription"); + } finally { + server.shutdown(); + } + } + + private AccountApi getAccountApi(MockWebServer server) { + return api(SoftLayerApi.class, server.getUrl("/").toString(), new + JavaUrlHttpCommandExecutorServiceModule(), new SoftLayerParserModule()).getAccountApi(); + } + +} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/DatacenterApiExpectTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/DatacenterApiExpectTest.java deleted file mode 100644 index 7a7cfadd07..0000000000 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/DatacenterApiExpectTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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.softlayer.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.jclouds.softlayer.parse.GetDatacenterResponseTest; -import org.jclouds.softlayer.parse.ListDatacentersResponseTest; -import org.testng.annotations.Test; - -import com.google.common.collect.Iterables; - -@Test(groups = "unit", testName = "DatacenterApiExpectTest") -public class DatacenterApiExpectTest extends BaseSoftLayerApiExpectTest { - - public void testListDatacentersWhenResponseIs2xx() { - - HttpRequest listDatacentersRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Location_Datacenter/Datacenters?objectMask=locationAddress%3Bregions") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse listDatacentersResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/datacenter_list.json")).build(); - - DatacenterApi api = requestSendsResponse(listDatacentersRequest, listDatacentersResponse).getDatacenterApi(); - - assertEquals(api.listDatacenters(), - new ListDatacentersResponseTest().expected()); - } - - public void testListDatacenterWhenResponseIs4xx() { - - HttpRequest listDatacentersRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Location_Datacenter/Datacenters?objectMask=locationAddress%3Bregions") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse listDatacentersResponse = HttpResponse.builder().statusCode(404).build(); - - DatacenterApi api = requestSendsResponse(listDatacentersRequest, listDatacentersResponse).getDatacenterApi(); - - assertTrue(Iterables.isEmpty(api.listDatacenters())); - } - - public void testGetDatacenterWhenResponseIs2xx() { - - HttpRequest getDatacenterRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Location_Datacenter/265592?objectMask=locationAddress%3Bregions") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getDatacenterResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/datacenter_get.json")).build(); - - DatacenterApi api = requestSendsResponse(getDatacenterRequest, getDatacenterResponse).getDatacenterApi(); - - assertEquals(api.getDatacenter(265592), - new GetDatacenterResponseTest().expected()); - } - - public void testGetDatacenterWhenResponseIs4xx() { - - HttpRequest getDatacenterRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Location_Datacenter/265592?objectMask=locationAddress%3Bregions") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getDatacenterResponse = HttpResponse.builder().statusCode(404).build(); - - DatacenterApi api = requestSendsResponse(getDatacenterRequest, getDatacenterResponse).getDatacenterApi(); - - assertNull(api.getDatacenter(265592)); - } -} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/DatacenterApiMockTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/DatacenterApiMockTest.java new file mode 100644 index 0000000000..321bb8746f --- /dev/null +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/DatacenterApiMockTest.java @@ -0,0 +1,86 @@ +/* + * 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.softlayer.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +import org.jclouds.softlayer.SoftLayerApi; +import org.jclouds.softlayer.internal.BaseSoftLayerMockTest; +import org.jclouds.softlayer.parse.DatacenterParseTest; +import org.jclouds.softlayer.parse.DatacentersParseTest; +import org.testng.annotations.Test; + +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; + +/** + * Mock tests for the {@link org.jclouds.softlayer.features.DatacenterApi} class. + */ +@Test(groups = "unit", testName = "DatacenterApiMockTest") +public class DatacenterApiMockTest extends BaseSoftLayerMockTest { + + public void testListDatacenters() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/datacenter_list.json"))); + DatacenterApi api = getDatacenterApi(server); + try { + assertEquals(api.listDatacenters(), new DatacentersParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Location_Datacenter/Datacenters?objectMask=locationAddress%3Bregions"); + } finally { + server.shutdown(); + } + } + + + public void testEmptyListDatacenters() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + DatacenterApi api = getDatacenterApi(server); + try { + assertTrue(api.listDatacenters().isEmpty()); + assertSent(server, "GET", "/SoftLayer_Location_Datacenter/Datacenters?objectMask=locationAddress%3Bregions"); + } finally { + server.shutdown(); + } + } + + public void testGetDatacenter() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/datacenter_get.json"))); + DatacenterApi api = getDatacenterApi(server); + try { + assertEquals(api.getDatacenter(265592), new DatacenterParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Location_Datacenter/265592?objectMask=locationAddress%3Bregions"); + } finally { + server.shutdown(); + } + } + + public void testGetNullDatacenter() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + DatacenterApi api = getDatacenterApi(server); + try { + assertNull(api.getDatacenter(265592)); + assertSent(server, "GET", "/SoftLayer_Location_Datacenter/265592?objectMask=locationAddress%3Bregions"); + } finally { + server.shutdown(); + } + } + + private DatacenterApi getDatacenterApi(MockWebServer server) { + return api(SoftLayerApi.class, server.getUrl("/").toString()).getDatacenterApi(); + } +} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/SoftwareDescriptionApiExpectTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/SoftwareDescriptionApiExpectTest.java deleted file mode 100644 index 524d703ff3..0000000000 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/SoftwareDescriptionApiExpectTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.softlayer.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.jclouds.softlayer.parse.GetAllObjectsResponseTest; -import org.testng.annotations.Test; - -import com.google.common.collect.Iterables; - -@Test(groups = "unit", testName = "SoftwareDescriptionApiExpectTest") -public class SoftwareDescriptionApiExpectTest extends BaseSoftLayerApiExpectTest { - - public void testGetAllObjectsWhenResponseIs2xx() { - - HttpRequest getAllObjectsRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Software_Description/getAllObjects?objectMask=id%3Bname%3Bversion%3BoperatingSystem%3BlongDescription%3BreferenceCode") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - HttpResponse getAllObjectsResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/software_description_list.json")).build(); - SoftwareDescriptionApi api = requestSendsResponse(getAllObjectsRequest, getAllObjectsResponse).getSoftwareDescriptionApi(); - assertEquals(api.getAllObjects(), new GetAllObjectsResponseTest().expected()); - } - - public void testGetAllObjectsWhenResponseIs4xx() { - - HttpRequest getAllObjectsRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Software_Description/getAllObjects?objectMask=id%3Bname%3Bversion%3BoperatingSystem%3BlongDescription%3BreferenceCode") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - HttpResponse getAllObjectsResponse = HttpResponse.builder().statusCode(404).build(); - SoftwareDescriptionApi api = requestSendsResponse(getAllObjectsRequest, getAllObjectsResponse).getSoftwareDescriptionApi(); - assertTrue(Iterables.isEmpty(api.getAllObjects())); - } - -} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/SoftwareDescriptionApiMockTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/SoftwareDescriptionApiMockTest.java new file mode 100644 index 0000000000..fe306e51d5 --- /dev/null +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/SoftwareDescriptionApiMockTest.java @@ -0,0 +1,48 @@ +/* + * 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.softlayer.features; + +import static org.testng.Assert.assertEquals; + +import org.jclouds.softlayer.SoftLayerApi; +import org.jclouds.softlayer.internal.BaseSoftLayerMockTest; +import org.jclouds.softlayer.parse.SoftwareDescriptionsParseTest; +import org.testng.annotations.Test; + +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; + +/** + * Mock tests for the {@link org.jclouds.softlayer.features.SoftwareDescriptionApi} class. + */ +@Test(groups = "unit", testName = "SoftwareDescriptionApiMockTest") +public class SoftwareDescriptionApiMockTest extends BaseSoftLayerMockTest { + + public void testGetAllObjects() throws Exception { + + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/software_description_list.json"))); + SoftwareDescriptionApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getSoftwareDescriptionApi(); + + try { + assertEquals(api.getAllObjects(), new SoftwareDescriptionsParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Software_Description/getAllObjects?objectMask=id%3Bname%3Bversion%3BoperatingSystem%3BlongDescription%3BreferenceCode"); + } finally { + server.shutdown(); + } + } + +} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestApiExpectTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestApiExpectTest.java deleted file mode 100644 index a629fc7dfd..0000000000 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestApiExpectTest.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * 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.softlayer.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - -import javax.ws.rs.core.MediaType; - -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.jclouds.softlayer.SoftLayerApi; -import org.jclouds.softlayer.domain.Datacenter; -import org.jclouds.softlayer.domain.OperatingSystem; -import org.jclouds.softlayer.domain.VirtualGuest; -import org.jclouds.softlayer.parse.CreateVirtualGuestResponseTest; -import org.jclouds.softlayer.parse.GetCreateObjectOptionsResponseTest; -import org.jclouds.softlayer.parse.GetVirtualGuestResponseTest; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableSet; - -@Test(groups = "unit", testName = "VirtualGuestApiExpectTest") -public class VirtualGuestApiExpectTest extends BaseSoftLayerApiExpectTest { - - public void testGetVirtualGuestWhenResponseIs2xx() { - - HttpRequest getVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/3001812/getObject?objectMask=id%3Bhostname%3Bdomain%3BfullyQualifiedDomainName%3BpowerState%3BmaxCpu%3BmaxMemory%3BstatusId%3BoperatingSystem.passwords%3BprimaryBackendIpAddress%3BprimaryIpAddress%3BactiveTransactionCount%3BblockDevices.diskImage%3Bdatacenter%3BtagReferences%3BprivateNetworkOnlyFlag%3BsshKeys") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getVirtualGuestResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/virtual_guest_get.json")).build(); - - SoftLayerApi api = requestSendsResponse(getVirtualGuest, getVirtualGuestResponse); - - assertEquals(api.getVirtualGuestApi().getVirtualGuest(3001812), - new GetVirtualGuestResponseTest().expected()); - } - - public void testGetVirtualGuestWhenResponseIs4xx() { - - HttpRequest getObjectRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/3001812/getObject?objectMask=id%3Bhostname%3Bdomain%3BfullyQualifiedDomainName%3BpowerState%3BmaxCpu%3BmaxMemory%3BstatusId%3BoperatingSystem.passwords%3BprimaryBackendIpAddress%3BprimaryIpAddress%3BactiveTransactionCount%3BblockDevices.diskImage%3Bdatacenter%3BtagReferences%3BprivateNetworkOnlyFlag%3BsshKeys") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getObjectResponse = HttpResponse.builder().statusCode(404).build(); - SoftLayerApi api = requestSendsResponse(getObjectRequest, getObjectResponse); - assertNull(api.getVirtualGuestApi().getVirtualGuest(3001812)); - } - - public void testCreateVirtualGuestWhenResponseIs2xx() { - - HttpRequest createVirtualGuest = HttpRequest.builder().method("POST") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==") - .payload(payloadFromResourceWithContentType("/virtual_guest_create.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse createVirtualGuestResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/virtual_guest_create_response.json")).build(); - - VirtualGuestApi api = requestSendsResponse(createVirtualGuest, createVirtualGuestResponse).getVirtualGuestApi(); - VirtualGuest virtualGuest = createVirtualGuest(); - VirtualGuest result = api.createVirtualGuest(virtualGuest); - assertEquals(result, new CreateVirtualGuestResponseTest().expected()); - } - - public void testDeleteVirtualGuestWhenResponseIs2xx() { - - HttpRequest deleteVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/deleteObject") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==") - .build(); - - HttpResponse deleteVirtualGuestResponse = HttpResponse.builder().statusCode(200).build(); - - VirtualGuestApi api = requestSendsResponse(deleteVirtualGuest, deleteVirtualGuestResponse).getVirtualGuestApi(); - assertTrue(api.deleteVirtualGuest(1301396)); - } - - public void testDeleteVirtualGuestWhenResponseIs4xx() { - - HttpRequest deleteVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/deleteObject") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==") - .build(); - - HttpResponse deleteVirtualGuestResponse = HttpResponse.builder().statusCode(404).build(); - VirtualGuestApi api = requestSendsResponse(deleteVirtualGuest, deleteVirtualGuestResponse).getVirtualGuestApi(); - assertFalse(api.deleteVirtualGuest(1301396)); - } - - private VirtualGuest createVirtualGuest() { - return VirtualGuest.builder() - .domain("example.com") - .hostname("host1") - .id(1301396) - .maxMemory(1024) - .startCpus(1) - .localDiskFlag(true) - .operatingSystem(OperatingSystem.builder().id("UBUNTU_LATEST") - .operatingSystemReferenceCode("UBUNTU_LATEST") - .build()) - .datacenter(Datacenter.builder().name("test").build()) - .build(); - } - - public void testGetCreateObjectOptionsWhenResponseIs2xx() { - - HttpRequest getVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/getCreateObjectOptions") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getVirtualGuestResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/container_virtual_guest_configuration.json")).build(); - - SoftLayerApi api = requestSendsResponse(getVirtualGuest, getVirtualGuestResponse); - - assertEquals(api.getVirtualGuestApi().getCreateObjectOptions(), - new GetCreateObjectOptionsResponseTest().expected()); - } - - public void testGetCreateObjectOptionsWhenResponseIs4xx() { - - HttpRequest getObjectRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/getCreateObjectOptions") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getObjectResponse = HttpResponse.builder().statusCode(404).build(); - SoftLayerApi api = requestSendsResponse(getObjectRequest, getObjectResponse); - assertNull(api.getVirtualGuestApi().getCreateObjectOptions()); - } - - public void testRebootHardVirtualGuestWhenResponseIs2xx() { - - HttpRequest rebootHardVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/rebootHard.json") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse rebootHardVirtualGuestResponse = HttpResponse.builder().statusCode(200).build(); - SoftLayerApi api = requestSendsResponse(rebootHardVirtualGuest, rebootHardVirtualGuestResponse); - api.getVirtualGuestApi().rebootHardVirtualGuest(1301396); - } - - public void testRebootHardVirtualGuestWhenResponseIs4xx() { - - HttpRequest rebootHardVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/rebootHard.json") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse rebootHardVirtualGuestResponse = HttpResponse.builder().statusCode(404).build(); - SoftLayerApi api = requestSendsResponse(rebootHardVirtualGuest, rebootHardVirtualGuestResponse); - api.getVirtualGuestApi().rebootHardVirtualGuest(1301396); - } - - public void testPauseVirtualGuestWhenResponseIs2xx() { - - HttpRequest pauseVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/pause.json") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse pauseVirtualGuestResponse = HttpResponse.builder().statusCode(200).build(); - SoftLayerApi api = requestSendsResponse(pauseVirtualGuest, pauseVirtualGuestResponse); - api.getVirtualGuestApi().pauseVirtualGuest(1301396); - } - - public void testPauseVirtualGuestWhenResponseIs4xx() { - - HttpRequest pauseVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/pause.json") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse pauseVirtualGuestResponse = HttpResponse.builder().statusCode(404).build(); - SoftLayerApi api = requestSendsResponse(pauseVirtualGuest, pauseVirtualGuestResponse); - api.getVirtualGuestApi().pauseVirtualGuest(1301396); - } - - public void testResumeVirtualGuestWhenResponseIs2xx() { - - HttpRequest resumeVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/resume.json") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse resumeVirtualGuestResponse = HttpResponse.builder().statusCode(200).build(); - SoftLayerApi api = requestSendsResponse(resumeVirtualGuest, resumeVirtualGuestResponse); - api.getVirtualGuestApi().resumeVirtualGuest(1301396); - } - - public void testResumeVirtualGuestWhenResponseIs4xx() { - - HttpRequest resumeVirtualGuest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/resume.json") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse resumeVirtualGuestResponse = HttpResponse.builder().statusCode(404).build(); - SoftLayerApi api = requestSendsResponse(resumeVirtualGuest, resumeVirtualGuestResponse); - api.getVirtualGuestApi().resumeVirtualGuest(1301396); - } - - public void testSetTagsOnVirtualGuestWhenResponseIs2xx() { - - HttpRequest setTagsOnVirtualGuest = HttpRequest.builder().method("POST") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/setTags") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==") - .payload(payloadFromResourceWithContentType("/virtual_guest_set_tags.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse setTagsOnVirtualGuestResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/virtual_guest_set_tags_response.json")).build(); - - SoftLayerApi api = requestSendsResponse(setTagsOnVirtualGuest, setTagsOnVirtualGuestResponse); - VirtualGuest virtualGuest = createVirtualGuest(); - assertTrue(api.getVirtualGuestApi().setTags(virtualGuest.getId(), ImmutableSet.of("test1", "test2", "test3"))); - } - - public void testSetNotesOnVirtualGuestWhenResponseIs2xx() { - - HttpRequest setNodesOnVirtualGuest = HttpRequest.builder().method("POST") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/1301396/editObject") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==") - .payload(payloadFromResourceWithContentType("/virtual_guest_set_notes.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse setNotesOnVirtualGuestResponse = HttpResponse.builder().statusCode(200) - .payload("true").build(); - - SoftLayerApi api = requestSendsResponse(setNodesOnVirtualGuest, setNotesOnVirtualGuestResponse); - VirtualGuest virtualGuest = createVirtualGuest(); - assertTrue(api.getVirtualGuestApi().setNotes(virtualGuest.getId(), "some notes")); - } -} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestApiMockTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestApiMockTest.java new file mode 100644 index 0000000000..853c5c98ec --- /dev/null +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestApiMockTest.java @@ -0,0 +1,234 @@ +/* + * 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.softlayer.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; +import org.jclouds.softlayer.SoftLayerApi; +import org.jclouds.softlayer.config.SoftLayerParserModule; +import org.jclouds.softlayer.domain.Datacenter; +import org.jclouds.softlayer.domain.OperatingSystem; +import org.jclouds.softlayer.domain.VirtualGuest; +import org.jclouds.softlayer.internal.BaseSoftLayerMockTest; +import org.jclouds.softlayer.parse.GetCreateObjectOptionsParseTest; +import org.jclouds.softlayer.parse.VirtualGuestParseTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; + +/** + * Mock tests for the {@link org.jclouds.softlayer.features.VirtualGuestApi} class. + */ +@Test(groups = "unit", testName = "VirtualGuestApiMockTest") +public class VirtualGuestApiMockTest extends BaseSoftLayerMockTest { + + public void testGetVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/virtual_guest_get.json"))); + VirtualGuestApi api = getVirtualGuestApi(server); + + try { + assertEquals(api.getVirtualGuest(3001812), new VirtualGuestParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/3001812/getObject?objectMask=id%3Bhostname%3Bdomain%3BfullyQualifiedDomainName%3BpowerState%3BmaxCpu%3BmaxMemory%3BstatusId%3BoperatingSystem.passwords%3BprimaryBackendIpAddress%3BprimaryIpAddress%3BactiveTransactionCount%3BblockDevices.diskImage%3Bdatacenter%3BtagReferences%3BprivateNetworkOnlyFlag%3BsshKeys"); + } finally { + server.shutdown(); + } + } + + public void testGetNullVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestApi api = getVirtualGuestApi(server); + try { + assertNull(api.getVirtualGuest(3001812)); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/3001812/getObject?objectMask=id%3Bhostname%3Bdomain%3BfullyQualifiedDomainName%3BpowerState%3BmaxCpu%3BmaxMemory%3BstatusId%3BoperatingSystem.passwords%3BprimaryBackendIpAddress%3BprimaryIpAddress%3BactiveTransactionCount%3BblockDevices.diskImage%3Bdatacenter%3BtagReferences%3BprivateNetworkOnlyFlag%3BsshKeys"); + } finally { + server.shutdown(); + } + } + + public void testCreateVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/virtual_guest_get.json"))); + VirtualGuestApi api = getVirtualGuestApi(server); + VirtualGuest virtualGuest = createVirtualGuest(); + try { + assertEquals(api.createVirtualGuest(virtualGuest), new VirtualGuestParseTest().expected()); + assertSent(server, "POST", "/SoftLayer_Virtual_Guest"); + } finally { + server.shutdown(); + } + } + + public void testDeleteVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse()); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + assertTrue(api.deleteVirtualGuest(1301396)); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/1301396/deleteObject"); + } finally { + server.shutdown(); + } + } + + public void testDeleteNonExistingVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + assertFalse(api.deleteVirtualGuest(1301396)); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/1301396/deleteObject"); + } finally { + server.shutdown(); + } + } + + public void testGetCreateObjectOptions() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/container_virtual_guest_configuration.json"))); + VirtualGuestApi api = getVirtualGuestApi(server); + try { + assertEquals(api.getCreateObjectOptions(), new GetCreateObjectOptionsParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/getCreateObjectOptions"); + } finally { + server.shutdown(); + } + } + + public void testGetNullCreateObjectOptions() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + assertNull(api.getCreateObjectOptions()); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/getCreateObjectOptions"); + } finally { + server.shutdown(); + } + } + + public void testRebootHardVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/datacenter_get.json"))); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + api.rebootHardVirtualGuest(1301396); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/1301396/rebootHard.json"); + } finally { + server.shutdown(); + } + } + + public void testRebootNonExistingVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + api.rebootHardVirtualGuest(1301396); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/1301396/rebootHard.json"); + } finally { + server.shutdown(); + } + } + + public void testPauseVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/datacenter_get.json"))); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + api.pauseVirtualGuest(1301396); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/1301396/pause.json"); + } finally { + server.shutdown(); + } + } + + public void testPauseNonExistingVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + api.pauseVirtualGuest(1301396); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/1301396/pause.json"); + } finally { + server.shutdown(); + } + } + + public void testResumeVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/datacenter_get.json"))); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + api.resumeVirtualGuest(1301396); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/1301396/resume.json"); + } finally { + server.shutdown(); + } + } + + public void testResumeNonExistingVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + try { + api.resumeVirtualGuest(1301396); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest/1301396/resume.json"); + } finally { + server.shutdown(); + } + } + + public void testSetTagsOnVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/virtual_guest_set_tags_response.json"))); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + VirtualGuest virtualGuest = createVirtualGuest(); + try { + assertTrue(api.setTags(virtualGuest.getId(), ImmutableSet.of("test1", "test2", "test3"))); + assertSent(server, "POST", "/SoftLayer_Virtual_Guest/1301396/setTags"); + } finally { + server.shutdown(); + } + } + + public void testSetTagsOnNonExistingVirtualGuest() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestApi api = api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestApi(); + VirtualGuest virtualGuest = createVirtualGuest(); + try { + assertFalse(api.setTags(virtualGuest.getId(), ImmutableSet.of("test1", "test2", "test3"))); + assertSent(server, "POST", "/SoftLayer_Virtual_Guest/1301396/setTags"); + } finally { + server.shutdown(); + } + } + + private VirtualGuest createVirtualGuest() { + return VirtualGuest.builder() + .domain("example.com") + .hostname("host1") + .id(1301396) + .maxMemory(1024) + .startCpus(1) + .localDiskFlag(true) + .operatingSystem(OperatingSystem.builder().id("UBUNTU_LATEST") + .operatingSystemReferenceCode("UBUNTU_LATEST") + .build()) + .datacenter(Datacenter.builder().name("test").build()) + .build(); + } + + private VirtualGuestApi getVirtualGuestApi(MockWebServer server) { + return api(SoftLayerApi.class, server.getUrl("/").toString(), new + JavaUrlHttpCommandExecutorServiceModule(), new SoftLayerParserModule()).getVirtualGuestApi(); + } + +} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApiExpectTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApiExpectTest.java deleted file mode 100644 index 2627841087..0000000000 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApiExpectTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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.softlayer.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.jclouds.softlayer.SoftLayerApi; -import org.jclouds.softlayer.parse.GetVirtualGuestBlockDeviceTemplateGroupResponseTest; -import org.jclouds.softlayer.parse.ListPublicImagesResponseTest; -import org.testng.annotations.Test; - -import com.google.common.collect.Iterables; - -@Test(groups = "unit", testName = "VirtualGuestBlockDeviceTemplateGroupApiExpectTest") -public class VirtualGuestBlockDeviceTemplateGroupApiExpectTest extends BaseSoftLayerApiExpectTest { - - public void testListPublicImagesWhenResponseIs2xx() { - - HttpRequest listPublicImagesRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest_Block_Device_Template_Group/getPublicImages?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse listPublicImagesResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/virtual_guest_block_device_template_group_get_public_images.json")).build(); - - SoftLayerApi api = requestSendsResponse(listPublicImagesRequest, listPublicImagesResponse); - - assertEquals(api.getVirtualGuestBlockDeviceTemplateGroupApi().getPublicImages(), - new ListPublicImagesResponseTest().expected()); - } - - public void testListPublicImagesWhenResponseIs4xx() { - - HttpRequest listPublicImagesRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest_Block_Device_Template_Group/getPublicImages?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse listPublicImagesResponse = HttpResponse.builder().statusCode(404).build(); - SoftLayerApi api = requestSendsResponse(listPublicImagesRequest, listPublicImagesResponse); - assertTrue(Iterables.isEmpty(api.getVirtualGuestBlockDeviceTemplateGroupApi().getPublicImages())); - } - - public void testGetObjectWhenResponseIs2xx() { - - HttpRequest getObject = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest_Block_Device_Template_Group/3001812/getObject?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getObjectResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/virtual_guest_block_device_template_group_get.json")).build(); - - SoftLayerApi api = requestSendsResponse(getObject, getObjectResponse); - - assertEquals(api.getVirtualGuestBlockDeviceTemplateGroupApi().getObject("3001812"), - new GetVirtualGuestBlockDeviceTemplateGroupResponseTest().expected()); - } - - public void testGetObjectWhenResponseIs4xx() { - - HttpRequest getObjectRequest = HttpRequest.builder().method("GET") - .endpoint("https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest_Block_Device_Template_Group/3001812/getObject?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build(); - - HttpResponse getObjectResponse = HttpResponse.builder().statusCode(404).build(); - SoftLayerApi api = requestSendsResponse(getObjectRequest, getObjectResponse); - assertNull(api.getVirtualGuestBlockDeviceTemplateGroupApi().getObject("3001812")); - } - -} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApiMockTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApiMockTest.java new file mode 100644 index 0000000000..3b6002e848 --- /dev/null +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestBlockDeviceTemplateGroupApiMockTest.java @@ -0,0 +1,86 @@ +/* + * 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.softlayer.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +import org.jclouds.softlayer.SoftLayerApi; +import org.jclouds.softlayer.internal.BaseSoftLayerMockTest; +import org.jclouds.softlayer.parse.PublicImagesParseTest; +import org.jclouds.softlayer.parse.VirtualGuestBlockDeviceTemplateGroupParseTest; +import org.testng.annotations.Test; + +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; + +/** + * Mock tests for the {@link VirtualGuestBlockDeviceTemplateGroupApi} class. + */ +@Test(groups = "unit", testName = "VirtualGuestBlockDeviceTemplateGroupApiMockTest") +public class VirtualGuestBlockDeviceTemplateGroupApiMockTest extends BaseSoftLayerMockTest { + + public void testListPublicImages() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/virtual_guest_block_device_template_group_get_public_images.json"))); + VirtualGuestBlockDeviceTemplateGroupApi api = getVirtualGuestBlockDeviceTemplateGroupApi(server); + try { + assertEquals(api.getPublicImages(), new PublicImagesParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest_Block_Device_Template_Group/getPublicImages?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription"); + } finally { + server.shutdown(); + } + } + + public void testEmptyListImages() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestBlockDeviceTemplateGroupApi api = getVirtualGuestBlockDeviceTemplateGroupApi(server); + try { + assertTrue(api.getPublicImages().isEmpty()); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest_Block_Device_Template_Group/getPublicImages?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription"); + } finally { + server.shutdown(); + } + } + + public void testGetObject() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setBody(payloadFromResource("/virtual_guest_block_device_template_group_get.json"))); + VirtualGuestBlockDeviceTemplateGroupApi api = getVirtualGuestBlockDeviceTemplateGroupApi(server); + try { + assertEquals(api.getObject("3001812"), new VirtualGuestBlockDeviceTemplateGroupParseTest().expected()); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest_Block_Device_Template_Group/3001812/getObject?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription"); + } finally { + server.shutdown(); + } + } + + public void testGetNullObject() throws Exception { + MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); + VirtualGuestBlockDeviceTemplateGroupApi api = getVirtualGuestBlockDeviceTemplateGroupApi(server); + try { + assertNull(api.getObject("3001812")); + assertSent(server, "GET", "/SoftLayer_Virtual_Guest_Block_Device_Template_Group/3001812/getObject?objectMask=children.blockDevices.diskImage.softwareReferences.softwareDescription"); + } finally { + server.shutdown(); + } + } + + private VirtualGuestBlockDeviceTemplateGroupApi getVirtualGuestBlockDeviceTemplateGroupApi(MockWebServer server) { + return api(SoftLayerApi.class, server.getUrl("/").toString()).getVirtualGuestBlockDeviceTemplateGroupApi(); + } + +} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/internal/BaseSoftLayerMockTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/internal/BaseSoftLayerMockTest.java new file mode 100644 index 0000000000..739a12de3d --- /dev/null +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/internal/BaseSoftLayerMockTest.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.softlayer.internal; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.jclouds.Constants.PROPERTY_CREDENTIAL; +import static org.jclouds.Constants.PROPERTY_IDENTITY; +import static org.jclouds.Constants.PROPERTY_API_VERSION; +import static org.jclouds.util.Strings2.toStringAndClose; +import java.io.IOException; +import java.util.Properties; + +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; + +import org.jclouds.http.BaseMockWebServerTest; +import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; +import org.jclouds.softlayer.SoftLayerApiMetadata; + +import com.google.common.base.Charsets; +import com.google.common.base.Throwables; +import com.google.inject.Module; +import com.squareup.okhttp.mockwebserver.MockWebServer; +import com.squareup.okhttp.mockwebserver.RecordedRequest; + +/** + * Base class for all SoftLayer mock tests. + */ +public class BaseSoftLayerMockTest extends BaseMockWebServerTest { + + @Override + protected void addOverrideProperties(Properties properties) { + properties.setProperty(PROPERTY_IDENTITY, "uuid"); + properties.setProperty(PROPERTY_CREDENTIAL, "apikey"); + properties.setProperty(PROPERTY_API_VERSION, "3"); + } + + @Override + protected Module createConnectionModule() { + return new JavaUrlHttpCommandExecutorServiceModule(); + } + + public byte[] payloadFromResource(String resource) { + try { + return toStringAndClose(getClass().getResourceAsStream(resource)).getBytes(Charsets.UTF_8); + } catch (IOException e) { + throw Throwables.propagate(e); + } + } + + protected RecordedRequest assertSent(MockWebServer server, String method, String path) throws InterruptedException { + RecordedRequest request = server.takeRequest(); + assertThat(request.getMethod()).isEqualTo(method); + assertThat(request.getPath()).isEqualTo("/v" + new SoftLayerApiMetadata().getVersion() + path); + assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.APPLICATION_JSON); + return request; + } + +} diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetDatacenterResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/DatacenterParseTest.java similarity index 96% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetDatacenterResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/DatacenterParseTest.java index e340707743..2aa5bf63e1 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetDatacenterResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/DatacenterParseTest.java @@ -28,7 +28,7 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @Test(groups = "unit") -public class GetDatacenterResponseTest extends BaseSoftLayerParseTest { +public class DatacenterParseTest extends BaseSoftLayerParseTest { @Override public String resource() { diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListDatacentersResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/DatacentersParseTest.java similarity index 97% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListDatacentersResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/DatacentersParseTest.java index 08832d6efe..c7ab6630de 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListDatacentersResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/DatacentersParseTest.java @@ -28,7 +28,7 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @Test(groups = "unit") -public class ListDatacentersResponseTest extends BaseSoftLayerParseTest> { +public class DatacentersParseTest extends BaseSoftLayerParseTest> { @Override public String resource() { diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetCreateObjectOptionsResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetCreateObjectOptionsParseTest.java similarity index 97% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetCreateObjectOptionsResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetCreateObjectOptionsParseTest.java index e25d08a8d1..1945c4f6ca 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetCreateObjectOptionsResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetCreateObjectOptionsParseTest.java @@ -30,7 +30,7 @@ import org.jclouds.softlayer.internal.BaseSoftLayerParseTest; import javax.ws.rs.Consumes; import javax.ws.rs.core.MediaType; -public class GetCreateObjectOptionsResponseTest extends BaseSoftLayerParseTest { +public class GetCreateObjectOptionsParseTest extends BaseSoftLayerParseTest { @Override public String resource() { diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListPublicImagesResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/PublicImagesParseTest.java similarity index 99% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListPublicImagesResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/PublicImagesParseTest.java index d91900b705..2d3a47f357 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListPublicImagesResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/PublicImagesParseTest.java @@ -32,7 +32,7 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @Test(groups = "unit") -public class ListPublicImagesResponseTest extends +public class PublicImagesParseTest extends BaseSoftLayerParseTest> { @Override diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetAllObjectsResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/SoftwareDescriptionsParseTest.java similarity index 95% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetAllObjectsResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/SoftwareDescriptionsParseTest.java index a9709b2063..a28988f962 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetAllObjectsResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/SoftwareDescriptionsParseTest.java @@ -28,7 +28,7 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @Test(groups = "unit") -public class GetAllObjectsResponseTest extends BaseSoftLayerParseTest> { +public class SoftwareDescriptionsParseTest extends BaseSoftLayerParseTest> { @Override public String resource() { diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestBlockDeviceTemplateGroupResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestBlockDeviceTemplateGroupParseTest.java similarity index 99% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestBlockDeviceTemplateGroupResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestBlockDeviceTemplateGroupParseTest.java index 70d202ea85..babdc88ba1 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestBlockDeviceTemplateGroupResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestBlockDeviceTemplateGroupParseTest.java @@ -31,7 +31,7 @@ import com.google.common.collect.ImmutableSet; @Test(groups = "unit") public class - GetVirtualGuestBlockDeviceTemplateGroupResponseTest extends + VirtualGuestBlockDeviceTemplateGroupParseTest extends BaseSoftLayerParseTest { @Override diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestBlockDeviceTemplateGroupsResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestBlockDeviceTemplateGroupsParseTest.java similarity index 99% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestBlockDeviceTemplateGroupsResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestBlockDeviceTemplateGroupsParseTest.java index ba214ad687..e8d5069f52 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestBlockDeviceTemplateGroupsResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestBlockDeviceTemplateGroupsParseTest.java @@ -33,7 +33,7 @@ import com.google.common.collect.ImmutableSet; @Test(groups = "unit") public class - GetVirtualGuestBlockDeviceTemplateGroupsResponseTest extends + VirtualGuestBlockDeviceTemplateGroupsParseTest extends BaseSoftLayerParseTest> { @Override diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestParseTest.java similarity index 96% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestParseTest.java index 97e2e0de22..dccdc62637 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/GetVirtualGuestResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestParseTest.java @@ -25,7 +25,7 @@ import org.jclouds.softlayer.internal.BaseSoftLayerParseTest; import org.testng.annotations.Test; @Test(groups = "unit") -public class GetVirtualGuestResponseTest extends BaseSoftLayerParseTest { +public class VirtualGuestParseTest extends BaseSoftLayerParseTest { @Override public String resource() { diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListVirtualGuestsResponseTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestsParseTest.java similarity index 96% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListVirtualGuestsResponseTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestsParseTest.java index 74508dcf80..792e08edde 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ListVirtualGuestsResponseTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/parse/VirtualGuestsParseTest.java @@ -29,7 +29,7 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @Test(groups = "unit") -public class ListVirtualGuestsResponseTest extends BaseSoftLayerParseTest> { +public class VirtualGuestsParseTest extends BaseSoftLayerParseTest> { @Override public String resource() {