From 81d410a5880a1249ac203e3be40b5da8e380f2ca Mon Sep 17 00:00:00 2001 From: Daniel Broudy Date: Tue, 25 Nov 2014 13:43:54 -0800 Subject: [PATCH] Converting tests to Mock --- .../features/AddressApiExpectTest.java | 149 -------- .../features/AddressApiMockTest.java | 87 +++++ .../features/BackendServiceApiExpectTest.java | 146 -------- .../features/BackendServiceApiMockTest.java | 136 ++++++++ .../features/DiskApiExpectTest.java | 217 ------------ .../features/DiskApiMockTest.java | 128 +++++++ .../features/DiskTypeApiExpectTest.java | 101 ------ .../features/DiskTypeApiMockTest.java | 63 ++++ .../features/FirewallApiExpectTest.java | 90 ----- .../features/FirewallApiMockTest.java | 82 +++++ .../features/ForwardingRuleApiExpectTest.java | 177 ---------- .../features/ForwardingRuleApiMockTest.java | 104 ++++++ .../GlobalForwardingRuleApiExpectTest.java | 177 ---------- .../GlobalForwardingRuleApiMockTest.java | 106 ++++++ .../HttpHealthCheckApiExpectTest.java | 141 +------- .../features/HttpHealthCheckApiMockTest.java | 109 ++++++ .../features/ImageApiMockTest.java | 12 +- .../features/InstanceApiExpectTest.java | 321 ------------------ .../features/InstanceApiMockTest.java | 162 ++++++++- .../features/MachineTypeApiExpectTest.java | 109 ------ .../features/MachineTypeApiMockTest.java | 63 ++++ .../parse/ParseAddressListTest.java | 11 +- .../parse/ParseAddressTest.java | 11 +- .../ParseBackendServiceGetHealthTest.java | 8 +- .../parse/ParseBackendServiceListTest.java | 11 +- .../parse/ParseBackendServiceTest.java | 12 +- .../parse/ParseDiskListTest.java | 7 +- .../parse/ParseDiskTest.java | 11 +- .../parse/ParseFirewallListTest.java | 11 +- .../parse/ParseFirewallTest.java | 9 +- .../parse/ParseForwardingRuleListTest.java | 7 +- .../parse/ParseForwardingRuleTest.java | 10 +- .../parse/ParseGlobalOperationTest.java | 9 +- .../parse/ParseHttpHealthCheckListTest.java | 11 +- .../parse/ParseHttpHealthCheckTest.java | 7 +- .../parse/ParseInstanceListTest.java | 7 +- .../parse/ParseInstanceTest.java | 17 +- .../parse/ParseMachineTypeListTest.java | 11 +- .../parse/ParseMachineTypeTest.java | 7 +- .../parse/ParseRegionOperationTest.java | 11 +- .../src/test/resources/address_get.json | 1 - .../resources/machinetype_list_central1b.json | 41 --- .../machinetype_list_central1b_empty.json | 6 - 43 files changed, 1175 insertions(+), 1741 deletions(-) delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/AddressApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/AddressApiMockTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiMockTest.java delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskApiMockTest.java delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskTypeApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskTypeApiMockTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiMockTest.java delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiMockTest.java delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiMockTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/HttpHealthCheckApiMockTest.java delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiExpectTest.java delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/MachineTypeApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/MachineTypeApiMockTest.java delete mode 100644 providers/google-compute-engine/src/test/resources/machinetype_list_central1b.json delete mode 100644 providers/google-compute-engine/src/test/resources/machinetype_list_central1b_empty.json diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/AddressApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/AddressApiExpectTest.java deleted file mode 100644 index 5feeef1f3d..0000000000 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/AddressApiExpectTest.java +++ /dev/null @@ -1,149 +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.googlecomputeengine.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.AssertJUnit.assertNull; - -import javax.ws.rs.core.MediaType; - -import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; -import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; -import org.jclouds.googlecomputeengine.parse.ParseAddressListTest; -import org.jclouds.googlecomputeengine.parse.ParseAddressTest; -import org.jclouds.googlecomputeengine.parse.ParseRegionOperationTest; -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.testng.annotations.Test; - -@Test(groups = "unit", testName = "AddressApiExpectTest") -public class AddressApiExpectTest extends BaseGoogleComputeEngineExpectTest { - - public void testGetAddressResponseIs2xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/regions/us-central1/addresses/test-ip1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/address_get.json")).build(); - - AddressApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).addressesInRegion("us-central1"); - - assertEquals(api.get("test-ip1"), new ParseAddressTest().expected()); - } - - public void testGetAddressResponseIs4xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/regions/us-central1/addresses/test-ip1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - AddressApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).addressesInRegion("us-central1"); - - assertNull(api.get("test-ip1")); - } - - public void testInsertAddressResponseIs2xx() { - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/regions/us-central1/addresses") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/address_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertAddressResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/region_operation.json")).build(); - - AddressApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, - insertAddressResponse).addressesInRegion("us-central1"); - - assertEquals(api.create("test-ip1"), new ParseRegionOperationTest().expected()); - } - - public void testDeleteAddressResponseIs2xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/regions/us-central1/addresses/test-ip1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/region_operation.json")).build(); - - AddressApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).addressesInRegion("us-central1"); - - assertEquals(api.delete("test-ip1"), new ParseRegionOperationTest().expected()); - } - - public void testDeleteAddressResponseIs4xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/regions/us-central1/addresses/test-ip1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(404).build(); - - AddressApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).addressesInRegion("us-central1"); - - assertNull(api.delete("test-ip1")); - } - - HttpRequest list = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/regions/us-central1/addresses") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public void list() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/address_list.json")).build(); - - AddressApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).addressesInRegion("us-central1"); - - assertEquals(api.list().next(), new ParseAddressListTest().expected()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - AddressApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).addressesInRegion("us-central1"); - - assertFalse(api.list().hasNext()); - } -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/AddressApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/AddressApiMockTest.java new file mode 100644 index 0000000000..3903048287 --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/AddressApiMockTest.java @@ -0,0 +1,87 @@ +/* + * 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.AssertJUnit.assertNull; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.parse.ParseAddressListTest; +import org.jclouds.googlecomputeengine.parse.ParseAddressTest; +import org.jclouds.googlecomputeengine.parse.ParseRegionOperationTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "AddressApiExpectTest", singleThreaded = true) +public class AddressApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception{ + server.enqueue(jsonResponse("/address_get.json")); + + assertEquals(addressApi().get("test-ip1"), new ParseAddressTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/regions/us-central1/addresses/test-ip1"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(addressApi().get("test-ip1")); + assertSent(server, "GET", "/projects/party/regions/us-central1/addresses/test-ip1"); + } + + public void insert() throws Exception { + server.enqueue(jsonResponse("/region_operation.json")); + + assertEquals(addressApi().create("test-ip1"), + new ParseRegionOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/regions/us-central1/addresses", + stringFromResource("/address_insert.json")); + } + + public void delete() throws Exception { + server.enqueue(jsonResponse("/region_operation.json")); + + assertEquals(addressApi().delete("test-ip1"), + new ParseRegionOperationTest().expected(url("/projects"))); + assertSent(server, "DELETE", "/projects/party/regions/us-central1/addresses/test-ip1"); + } + + public void delete_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(addressApi().delete("test-ip1")); + assertSent(server, "DELETE", "/projects/party/regions/us-central1/addresses/test-ip1"); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/address_list.json")); + + assertEquals(addressApi().list().next(), new ParseAddressListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/regions/us-central1/addresses"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(addressApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/regions/us-central1/addresses"); + } + + AddressApi addressApi(){ + return api().addressesInRegion("us-central1"); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiExpectTest.java index 2e034957ee..79fd5f75a0 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiExpectTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiExpectTest.java @@ -18,8 +18,6 @@ package org.jclouds.googlecomputeengine.features; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.AssertJUnit.assertNull; import java.io.IOException; import java.net.URI; @@ -28,9 +26,6 @@ import java.util.List; import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; import org.jclouds.googlecomputeengine.options.BackendServiceOptions; -import org.jclouds.googlecomputeengine.parse.ParseBackendServiceGetHealthTest; -import org.jclouds.googlecomputeengine.parse.ParseBackendServiceListTest; -import org.jclouds.googlecomputeengine.parse.ParseBackendServiceTest; import org.jclouds.googlecomputeengine.parse.ParseOperationTest; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; @@ -55,75 +50,6 @@ public class BackendServiceApiExpectTest extends BaseGoogleComputeEngineExpectTe .build(); } - public void testGetBackendServiceResponseIs2xx() throws Exception { - HttpRequest request = getBasicRequest().method("GET") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .build(); - HttpResponse response = createResponse("/backend_service_get.json"); - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, request, response) - .backendServices(); - - assertEquals(api.get("jclouds-test"), new ParseBackendServiceTest().expected()); - } - - public void testGetBackendServiceResponseIs4xx() throws Exception { - HttpRequest request = getBasicRequest().method("GET") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, request, response) - .backendServices(); - - assertNull(api.get("jclouds-test")); - } - - public void testInsertBackendServiceResponseIs2xx() throws IOException { - HttpRequest request = getBasicRequest().method("POST") - .endpoint(ENDPOINT_BASE) - .payload(payloadFromResourceWithContentType("/backend_service_insert.json", - APPLICATION_JSON)) - .build(); - HttpResponse response = createResponse("/operation.json"); - - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).backendServices(); - - List healthChecks = ImmutableList.of(URI.create("https://www.googleapis.com/compute/v1/projects/" - + "myproject/global/httpHealthChecks/jclouds-test")); - assertEquals(api.create( new BackendServiceOptions().name("jclouds-test") - .protocol("HTTP") - .port(80) - .timeoutSec(30) - .healthChecks(healthChecks)), - new ParseOperationTest().expected()); - - } - - public void testUpdateBackendServiceResponseIs2xx() throws IOException { - HttpRequest request = getBasicRequest().method("PUT") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .payload(payloadFromResourceWithContentType("/backend_service_insert.json", - APPLICATION_JSON)) - .build(); - HttpResponse response = createResponse("/operation.json"); - - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).backendServices(); - - List healthChecks = ImmutableList.of(URI.create("https://www.googleapis.com/compute/v1/projects/" - + "myproject/global/httpHealthChecks/jclouds-test")); - assertEquals(api.update("jclouds-test", new BackendServiceOptions().name("jclouds-test") - .protocol("HTTP") - .port(80) - .timeoutSec(30) - .healthChecks(healthChecks)), - new ParseOperationTest().expected()); - } - public void testPatchBackendServiceResponseIs2xx() throws IOException { HttpRequest request = getBasicRequest().method("PATCH") .endpoint(ENDPOINT_BASE + "/jclouds-test") @@ -144,76 +70,4 @@ public class BackendServiceApiExpectTest extends BaseGoogleComputeEngineExpectTe .healthChecks(healthChecks)), new ParseOperationTest().expected()); } - - public void testDeleteBackendServiceResponseIs2xx() { - HttpRequest request = getBasicRequest().method("DELETE") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .build(); - HttpResponse response = createResponse("/operation.json"); - - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).backendServices(); - - assertEquals(api.delete("jclouds-test"), new ParseOperationTest().expected()); - } - - public void testDeleteBackendServiceResponseIs4xx() { - HttpRequest request = getBasicRequest().method("DELETE") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).backendServices(); - - assertNull(api.delete("jclouds-test")); - } - - public void testListBackendServiceResponseIs2xx() { - HttpRequest request = getBasicRequest().method("GET") - .endpoint(ENDPOINT_BASE) - .build(); - HttpResponse response = createResponse("/backend_service_list.json"); - - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, request, response).backendServices(); - - assertEquals(api.list().next(), new ParseBackendServiceListTest().expected()); - } - - public void listEmpty() { - HttpRequest list = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/backendServices") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).backendServices(); - - assertFalse(api.list().hasNext()); - } - - public void testGetHealthResponseIs2xx() throws IOException { - HttpRequest request = getBasicRequest().method("POST") - .endpoint(ENDPOINT_BASE - + "/jclouds-test/getHealth") - .payload(payloadFromResource("/backend_service_get_health_request.json")) - .build(); - HttpResponse response = createResponse("/backend_service_get_health.json"); - - BackendServiceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).backendServices(); - - URI group = URI.create("https://www.googleapis.com/resourceviews/v1beta1/" - + "projects/myproject/zones/us-central1-a/" - + "resourceViews/jclouds-test"); - assertEquals(api.getHealth("jclouds-test", group), new ParseBackendServiceGetHealthTest().expected()); - - } } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiMockTest.java new file mode 100644 index 0000000000..0434401105 --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiMockTest.java @@ -0,0 +1,136 @@ +/* + * 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.AssertJUnit.assertNull; + +import java.net.URI; +import java.util.List; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.options.BackendServiceOptions; +import org.jclouds.googlecomputeengine.parse.ParseBackendServiceGetHealthTest; +import org.jclouds.googlecomputeengine.parse.ParseBackendServiceListTest; +import org.jclouds.googlecomputeengine.parse.ParseBackendServiceTest; +import org.jclouds.googlecomputeengine.parse.ParseOperationTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +@Test(groups = "unit", testName = "BackendServiceApiMockTest", singleThreaded = true) +public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception { + server.enqueue(jsonResponse("/backend_service_get.json")); + + assertEquals(backendServiceApi().get("jclouds-test"), + new ParseBackendServiceTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/backendServices/jclouds-test"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(backendServiceApi().get("jclouds-test")); + assertSent(server, "GET", "/projects/party/global/backendServices/jclouds-test"); + } + + public void insert() throws Exception { + server.enqueue(jsonResponse("/operation.json")); + + List healthChecks = ImmutableList.of(URI.create(url("/projects/" + + "myproject/global/httpHealthChecks/jclouds-test"))); + + assertEquals(backendServiceApi().create( new BackendServiceOptions().name("jclouds-test") + .protocol("HTTP") + .port(80) + .timeoutSec(30) + .healthChecks(healthChecks)), + new ParseOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/global/backendServices", + stringFromResource("/backend_service_insert.json")); + } + + public void update() throws Exception { + server.enqueue(jsonResponse("/operation.json")); + + List healthChecks = ImmutableList.of(URI.create(url("/projects/" + + "myproject/global/httpHealthChecks/jclouds-test"))); + + assertEquals(backendServiceApi().update("jclouds-test", + new BackendServiceOptions().name("jclouds-test") + .protocol("HTTP") + .port(80) + .timeoutSec(30) + .healthChecks(healthChecks)), + new ParseOperationTest().expected(url("/projects"))); + assertSent(server, "PUT", "/projects/party/global/backendServices/jclouds-test", + stringFromResource("/backend_service_insert.json")); + } + + public void delete() throws Exception { + server.enqueue(jsonResponse("/operation.json")); + + assertEquals(backendServiceApi().delete("jclouds-test"), + new ParseOperationTest().expected(url("/projects"))); + + assertSent(server, "DELETE", "/projects/party/global/backendServices/jclouds-test"); + } + + public void delete_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(backendServiceApi().delete("jclouds-test")); + + assertSent(server, "DELETE", "/projects/party/global/backendServices/jclouds-test"); + } + + + public void list() throws Exception { + server.enqueue(jsonResponse("/backend_service_list.json")); + + assertEquals(backendServiceApi().list().next(), new ParseBackendServiceListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/backendServices"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(backendServiceApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/global/backendServices"); + } + + public void getHealth() throws Exception { + server.enqueue(jsonResponse("/backend_service_get_health.json")); + + URI group = URI.create("https://www.googleapis.com/resourceviews/v1beta1/" + + "projects/myproject/zones/us-central1-a/" + + "resourceViews/jclouds-test"); + assertEquals(backendServiceApi().getHealth("jclouds-test", group), + new ParseBackendServiceGetHealthTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/global/backendServices/jclouds-test/getHealth", + stringFromResource("/backend_service_get_health_request.json")); + } + + public BackendServiceApi backendServiceApi(){ + return api().backendServices(); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskApiExpectTest.java deleted file mode 100644 index e3807bc87a..0000000000 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskApiExpectTest.java +++ /dev/null @@ -1,217 +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.googlecomputeengine.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.AssertJUnit.assertNull; - -import java.net.URI; - -import javax.ws.rs.core.MediaType; - -import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; -import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; -import org.jclouds.googlecomputeengine.options.DiskCreationOptions; -import org.jclouds.googlecomputeengine.parse.ParseDiskListTest; -import org.jclouds.googlecomputeengine.parse.ParseDiskTest; -import org.jclouds.googlecomputeengine.parse.ParseZoneOperationTest; -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.testng.annotations.Test; - -@Test(groups = "unit", testName = "DiskApiExpectTest") -public class DiskApiExpectTest extends BaseGoogleComputeEngineExpectTest { - public static final String IMAGE_URL = BASE_URL + "/party/zones/us-central1-a/images/foo"; - public static final String SSD_URL = BASE_URL + "/party/zones/us-central1-a/diskTypes/pd-ssd"; - - public void testGetDiskResponseIs2xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks/testimage1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/disk_get.json")).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).disksInZone("us-central1-a"); - - assertEquals(api.get("testimage1"), new ParseDiskTest().expected()); - } - - public void testGetDiskResponseIs4xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks/testimage1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).disksInZone("us-central1-a"); - - assertNull(api.get("testimage1")); - } - - public void testInsertDiskResponseIs2xx() { - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/disk_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertDiskResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, - insertDiskResponse).disksInZone("us-central1-a"); - - assertEquals(api.create("testimage1", 1), new ParseZoneOperationTest().expected()); - } - - public void testInsertDiskFromImageResponseIs2xx() { - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/disk_insert_sourceImage.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertDiskResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, - insertDiskResponse).disksInZone("us-central1-a"); - - DiskCreationOptions diskCreationOptions = new DiskCreationOptions().sourceImage(URI.create(IMAGE_URL)); - assertEquals(api.create("testimage1", 1, diskCreationOptions), new ParseZoneOperationTest().expected()); - } - - public void testInsertDiskSSDResponseIs2xx(){ - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/disk_insert_ssd.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertDiskResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, - insertDiskResponse).disksInZone("us-central1-a"); - - DiskCreationOptions diskCreationOptions = new DiskCreationOptions().type(URI.create(SSD_URL)); - assertEquals(api.create("testimage1", 1, diskCreationOptions), new ParseZoneOperationTest().expected()); - } - - public void testCreateSnapshotResponseIs2xx() { - HttpRequest createSnapshotRequest = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks/testimage1/createSnapshot") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/disk_create_snapshot.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse createSnapshotResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, createSnapshotRequest, - createSnapshotResponse).disksInZone("us-central1-a"); - - assertEquals(api.createSnapshot("testimage1", "test-snap"), new ParseZoneOperationTest().expected()); - } - - public void testDeleteDiskResponseIs2xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks/testimage1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).disksInZone("us-central1-a"); - - assertEquals(api.delete("testimage1"), - new ParseZoneOperationTest().expected()); - } - - public void testDeleteDiskResponseIs4xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks/testimage1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(404).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).disksInZone("us-central1-a"); - - assertNull(api.delete("testimage1")); - } - - HttpRequest list = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/disks") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public void list() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/disk_list.json")).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).disksInZone("us-central1-a"); - - assertEquals(api.list().next(), new ParseDiskListTest().expected()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - DiskApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).disksInZone("us-central1-a"); - - assertFalse(api.list().hasNext()); - } -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskApiMockTest.java new file mode 100644 index 0000000000..7633a03aae --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskApiMockTest.java @@ -0,0 +1,128 @@ +/* + * 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.AssertJUnit.assertNull; + +import java.net.URI; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.options.DiskCreationOptions; +import org.jclouds.googlecomputeengine.parse.ParseDiskListTest; +import org.jclouds.googlecomputeengine.parse.ParseDiskTest; +import org.jclouds.googlecomputeengine.parse.ParseZoneOperationTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "DiskApiMockTest", singleThreaded = true) +public class DiskApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public static final String IMAGE_URL = "/projects/party/zones/us-central1-a/images/foo"; + public static final String SSD_URL = "/projects/party/zones/us-central1-a/diskTypes/pd-ssd"; + + public void get() throws Exception { + server.enqueue(jsonResponse("/disk_get.json")); + + assertEquals(diskApi().get("testimage1"), new ParseDiskTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/disks/testimage1"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(diskApi().get("testimage1")); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/disks/testimage1"); + } + + public void insert() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(diskApi().create("testimage1", 1), + new ParseZoneOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/zones/us-central1-a/disks", + stringFromResource("/disk_insert.json")); + } + + public void insertFromImage() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + DiskCreationOptions diskCreationOptions = new DiskCreationOptions() + .sourceImage(URI.create(url(IMAGE_URL))); + assertEquals(diskApi().create("testimage1", 1, diskCreationOptions), + new ParseZoneOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/zones/us-central1-a/disks", + stringFromResource("/disk_insert_sourceImage.json")); + } + + public void insertFromSSD() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + DiskCreationOptions diskCreationOptions = new DiskCreationOptions().type(URI.create(url(SSD_URL))); + assertEquals(diskApi().create("testimage1", 1, diskCreationOptions), + new ParseZoneOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/zones/us-central1-a/disks", + stringFromResource("/disk_insert_ssd.json")); + } + + public void creatSnapshot() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(diskApi().createSnapshot("testimage1", "test-snap"), + new ParseZoneOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/zones/us-central1-a/disks/testimage1/createSnapshot", + stringFromResource("/disk_create_snapshot.json")); + } + + public void delete() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(diskApi().delete("testimage1"), + new ParseZoneOperationTest().expected(url("/projects"))); + + assertSent(server, "DELETE", "/projects/party/zones/us-central1-a/disks/testimage1"); + } + + public void delete_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(diskApi().delete("testimage1")); + assertSent(server, "DELETE", "/projects/party/zones/us-central1-a/disks/testimage1"); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/disk_list.json")); + + assertEquals(diskApi().list().next(), new ParseDiskListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/disks"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(diskApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/disks"); + } + + public DiskApi diskApi(){ + return api().disksInZone("us-central1-a"); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskTypeApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskTypeApiExpectTest.java deleted file mode 100644 index 37ece626e9..0000000000 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskTypeApiExpectTest.java +++ /dev/null @@ -1,101 +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.googlecomputeengine.features; - -import static org.jclouds.googlecomputeengine.options.ListOptions.Builder.maxResults; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNull; - -import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; -import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; -import org.jclouds.googlecomputeengine.parse.ParseDiskTypeListTest; -import org.jclouds.googlecomputeengine.parse.ParseDiskTypeTest; -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.testng.annotations.Test; - -@Test(groups = "unit", testName = "DiskTypeApiExpectTest") -public class DiskTypeApiExpectTest extends BaseGoogleComputeEngineExpectTest { - - public static final HttpRequest LIST_DISK_TYPES_REQUEST = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/diskTypes") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static final HttpResponse LIST_DISK_TYPES_RESPONSE = HttpResponse.builder() - .statusCode(200) - .payload(staticPayloadFromResource("/disktype_list.json")) - .build(); - - public void testGetDiskTypeResponseIs2xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/diskTypes/pd-standard") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/disktype.json")).build(); - - DiskTypeApi diskTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).diskTypesInZone("us-central1-a"); - - assertEquals(diskTypeApi.get("pd-standard"), - new ParseDiskTypeTest().expected()); - } - - public void testGetDiskTypeResponseIs4xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/diskTypes/pd-standard") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - DiskTypeApi diskTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).diskTypesInZone("us-central1-a"); - - assertNull(diskTypeApi.get("pd-standard")); - } - - public void list() throws Exception { - - DiskTypeApi diskTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_DISK_TYPES_REQUEST, LIST_DISK_TYPES_RESPONSE).diskTypesInZone("us-central1-a"); - - assertEquals(diskTypeApi.list().next(), new ParseDiskTypeListTest().expected()); - } - - public void listWithOptionsEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - HttpRequest listRequestWithOptions = LIST_DISK_TYPES_REQUEST.toBuilder() - .endpoint(LIST_DISK_TYPES_REQUEST.getEndpoint() + "?maxResults=1").build(); - - DiskTypeApi diskTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, listRequestWithOptions, response).diskTypesInZone("us-central1-a"); - - assertFalse(diskTypeApi.list(maxResults(1)).hasNext()); - } -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskTypeApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskTypeApiMockTest.java new file mode 100644 index 0000000000..07b8cf234e --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/DiskTypeApiMockTest.java @@ -0,0 +1,63 @@ +/* + * 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.parse.ParseDiskTypeListTest; +import org.jclouds.googlecomputeengine.parse.ParseDiskTypeTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "DiskTypeApiMockTest", singleThreaded = true) +public class DiskTypeApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception { + server.enqueue(jsonResponse("/disktype.json")); + + assertEquals(diskTypeApi().get("pd-standard"), + new ParseDiskTypeTest().expected()); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/diskTypes/pd-standard"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(diskTypeApi().get("pd-standard")); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/diskTypes/pd-standard"); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/disktype_list.json")); + + assertEquals(diskTypeApi().list().next(), new ParseDiskTypeListTest().expected()); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/diskTypes"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(diskTypeApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/diskTypes"); + } + + public DiskTypeApi diskTypeApi() { + return api().diskTypesInZone("us-central1-a"); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiExpectTest.java index 96dd7b2602..cc92e23e76 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiExpectTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiExpectTest.java @@ -22,8 +22,6 @@ import static java.lang.String.format; import static org.jclouds.io.Payloads.newStringPayload; import static org.jclouds.util.Strings2.toStringAndClose; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.AssertJUnit.assertNull; import java.io.IOException; import java.net.URI; @@ -35,8 +33,6 @@ import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; import org.jclouds.googlecomputeengine.domain.Firewall; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; import org.jclouds.googlecomputeengine.options.FirewallOptions; -import org.jclouds.googlecomputeengine.parse.ParseFirewallListTest; -import org.jclouds.googlecomputeengine.parse.ParseFirewallTest; import org.jclouds.googlecomputeengine.parse.ParseOperationTest; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; @@ -59,14 +55,6 @@ public class FirewallApiExpectTest extends BaseGoogleComputeEngineExpectTest sourceRanges, @@ -95,23 +83,6 @@ public class FirewallApiExpectTest extends BaseGoogleComputeEngineExpectTest { - - public void testGetForwardingRuleResponseIs2xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/regions/us-central1/forwardingRules/test-forwarding-rule") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/forwardingrule_get.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).forwardingRulesInRegion("us-central1"); - - assertEquals(api.get("test-forwarding-rule"), new ParseForwardingRuleTest().expected()); - } - - public void testGetForwardingRuleResponseIs4xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/regions/us-central1/forwardingRules/test-forwarding-rule") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).forwardingRulesInRegion("us-central1"); - - assertNull(api.get("test-forwarding-rule")); - } - - public void testInsertForwardingRuleResponseIs2xx() { - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/regions/us-central1/forwardingRules") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/forwardingrule_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertForwardingRuleResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/region_operation.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, - insertForwardingRuleResponse).forwardingRulesInRegion("us-central1"); - - ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() - .target(URI.create(BASE_URL + "/party/regions/europe-west1/targetPools/test-target-pool")); - assertEquals(api.create("test-forwarding-rule", forwardingRuleCreationOptions), - new ParseRegionOperationTest().expected()); - } - - public void testDeleteForwardingRuleResponseIs2xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/regions/us-central1/forwardingRules/test-forwarding-rule") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/region_operation.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).forwardingRulesInRegion("us-central1"); - - assertEquals(api.delete("test-forwarding-rule"), new ParseRegionOperationTest().expected()); - } - - public void testDeleteForwardingRuleResponseIs4xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/regions/us-central1/forwardingRules/test-targetPool") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(404).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).forwardingRulesInRegion("us-central1"); - - assertNull(api.delete("test-targetPool")); - } - - HttpRequest list = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/regions/us-central1/forwardingRules") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public void list() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/forwardingrule_list.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).forwardingRulesInRegion("us-central1"); - - assertEquals(api.list().next(), new ParseForwardingRuleListTest().expected()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).forwardingRulesInRegion("us-central1"); - - assertFalse(api.list().hasNext()); - } - - public void testSetTargetForwardingRuleResponseIs2xx(){ - String ruleName = "testForwardingRule"; - HttpRequest setTarget = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/regions/us-central1/forwardingRules/" + ruleName + "/setTarget") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/forwardingrule_set_target.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse setTargetResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/region_operation.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, setTarget, setTargetResponse).forwardingRulesInRegion("us-central1"); - - URI newTarget = URI.create(BASE_URL + "/party/regions/europe-west1/targetPools/test-target-pool"); - assertEquals(api.setTarget(ruleName, newTarget), new ParseRegionOperationTest().expected()); - } - -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiMockTest.java new file mode 100644 index 0000000000..f66a14181e --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiMockTest.java @@ -0,0 +1,104 @@ +/* + * 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.AssertJUnit.assertNull; + +import java.net.URI; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.options.ForwardingRuleCreationOptions; +import org.jclouds.googlecomputeengine.parse.ParseForwardingRuleListTest; +import org.jclouds.googlecomputeengine.parse.ParseForwardingRuleTest; +import org.jclouds.googlecomputeengine.parse.ParseRegionOperationTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "ForwardingRuleApiMockTest", singleThreaded = true) +public class ForwardingRuleApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception { + server.enqueue(jsonResponse("/forwardingrule_get.json")); + + assertEquals(forwardingRuleApi().get("test-forwarding-rule"), + new ParseForwardingRuleTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/regions/us-central1/forwardingRules/test-forwarding-rule"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(forwardingRuleApi().get("test-forwarding-rule")); + assertSent(server, "GET", "/projects/party/regions/us-central1/forwardingRules/test-forwarding-rule"); + } + + public void insert() throws Exception { + server.enqueue(jsonResponse("/region_operation.json")); + + ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() + .target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))); + assertEquals(forwardingRuleApi().create("test-forwarding-rule", forwardingRuleCreationOptions), + new ParseRegionOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/regions/us-central1/forwardingRules", + stringFromResource("/forwardingrule_insert.json")); + } + + public void delete() throws Exception { + server.enqueue(jsonResponse("/region_operation.json")); + + assertEquals(forwardingRuleApi().delete("test-forwarding-rule"), + new ParseRegionOperationTest().expected(url("/projects"))); + assertSent(server, "DELETE", "/projects/party/regions/us-central1/forwardingRules/test-forwarding-rule"); + } + + public void delete_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(forwardingRuleApi().delete("test-forwarding-rule")); + assertSent(server, "DELETE", "/projects/party/regions/us-central1/forwardingRules/test-forwarding-rule"); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/forwardingrule_list.json")); + + assertEquals(forwardingRuleApi().list().next(), new ParseForwardingRuleListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/regions/us-central1/forwardingRules"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(forwardingRuleApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/regions/us-central1/forwardingRules"); + } + + public void setTarget() throws Exception { + server.enqueue(jsonResponse("/region_operation.json")); + + URI newTarget = URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool")); + assertEquals(forwardingRuleApi().setTarget("testForwardingRule", newTarget), new ParseRegionOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/regions/us-central1/forwardingRules/testForwardingRule/setTarget", + stringFromResource("/forwardingrule_set_target.json")); + } + + ForwardingRuleApi forwardingRuleApi() { + return api().forwardingRulesInRegion("us-central1"); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiExpectTest.java deleted file mode 100644 index 1adcd5e62c..0000000000 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiExpectTest.java +++ /dev/null @@ -1,177 +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.googlecomputeengine.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.AssertJUnit.assertNull; - -import java.net.URI; - -import javax.ws.rs.core.MediaType; - -import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; -import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; -import org.jclouds.googlecomputeengine.options.ForwardingRuleCreationOptions; -import org.jclouds.googlecomputeengine.parse.ParseForwardingRuleListTest; -import org.jclouds.googlecomputeengine.parse.ParseForwardingRuleTest; -import org.jclouds.googlecomputeengine.parse.ParseRegionOperationTest; -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.testng.annotations.Test; - -@Test(groups = "unit", testName = "ForwardingRuleApiExpectTest") -public class GlobalForwardingRuleApiExpectTest extends BaseGoogleComputeEngineExpectTest { - - public void testGetForwardingRuleResponseIs2xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/forwardingRules/test-forwarding-rule") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/forwardingrule_get.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).globalForwardingRules(); - - assertEquals(api.get("test-forwarding-rule"), new ParseForwardingRuleTest().expected()); - } - - public void testGetForwardingRuleResponseIs4xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/forwardingRules/test-forwarding-rule") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).globalForwardingRules(); - - assertNull(api.get("test-forwarding-rule")); - } - - public void testInsertForwardingRuleResponseIs2xx() { - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/global/forwardingRules") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/forwardingrule_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertForwardingRuleResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/region_operation.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, - insertForwardingRuleResponse).globalForwardingRules(); - - ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() - .target(URI.create(BASE_URL + "/party/regions/europe-west1/targetPools/test-target-pool")); - assertEquals(api.create("test-forwarding-rule", forwardingRuleCreationOptions), - new ParseRegionOperationTest().expected()); - } - - public void testDeleteForwardingRuleResponseIs2xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/global/forwardingRules/test-forwarding-rule") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/region_operation.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).globalForwardingRules(); - - assertEquals(api.delete("test-forwarding-rule"), new ParseRegionOperationTest().expected()); - } - - public void testDeleteForwardingRuleResponseIs4xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/global/forwardingRules/test-targetPool") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(404).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).globalForwardingRules(); - - assertNull(api.delete("test-targetPool")); - } - - HttpRequest list = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/forwardingRules") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public void list() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/forwardingrule_list.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).globalForwardingRules(); - - assertEquals(api.list().next(), new ParseForwardingRuleListTest().expected()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).globalForwardingRules(); - - assertFalse(api.list().hasNext()); - } - - public void testSetTargetForwardingRuleResponseIs2xx(){ - String ruleName = "testForwardingRule"; - HttpRequest setTarget = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/global/forwardingRules/" + ruleName + "/setTarget") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/forwardingrule_set_target.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse setTargetResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/region_operation.json")).build(); - - ForwardingRuleApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, setTarget, setTargetResponse).globalForwardingRules(); - - URI newTarget = URI.create(BASE_URL + "/party/regions/europe-west1/targetPools/test-target-pool"); - assertEquals(api.setTarget(ruleName, newTarget), new ParseRegionOperationTest().expected()); - } - -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiMockTest.java new file mode 100644 index 0000000000..8c0d31820d --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiMockTest.java @@ -0,0 +1,106 @@ +/* + * 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.AssertJUnit.assertNull; + +import java.net.URI; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.options.ForwardingRuleCreationOptions; +import org.jclouds.googlecomputeengine.parse.ParseForwardingRuleListTest; +import org.jclouds.googlecomputeengine.parse.ParseForwardingRuleTest; +import org.jclouds.googlecomputeengine.parse.ParseRegionOperationTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "GlobalForwardingRuleApiMockTest", singleThreaded = true) +public class GlobalForwardingRuleApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception { + server.enqueue(jsonResponse("/forwardingrule_get.json")); + + assertEquals(globalForwardingRuleApi().get("test-forwarding-rule"), + new ParseForwardingRuleTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/forwardingRules/test-forwarding-rule"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(globalForwardingRuleApi().get("test-forwarding-rule")); + assertSent(server, "GET", "/projects/party/global/forwardingRules/test-forwarding-rule"); + } + + public void insert() throws Exception { + server.enqueue(jsonResponse("/region_operation.json")); + + ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() + .target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))); + + assertEquals(globalForwardingRuleApi().create("test-forwarding-rule", forwardingRuleCreationOptions), + new ParseRegionOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/global/forwardingRules", + stringFromResource("/forwardingrule_insert.json")); + } + + public void delete() throws Exception { + server.enqueue(jsonResponse("/region_operation.json")); + + assertEquals(globalForwardingRuleApi().delete("test-forwarding-rule"), + new ParseRegionOperationTest().expected(url("/projects"))); + assertSent(server, "DELETE", "/projects/party/global/forwardingRules/test-forwarding-rule"); + } + + public void delete_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(globalForwardingRuleApi().delete("test-forwarding-rule")); + assertSent(server, "DELETE", "/projects/party/global/forwardingRules/test-forwarding-rule"); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/forwardingrule_list.json")); + + assertEquals(globalForwardingRuleApi().list().next(), new ParseForwardingRuleListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/forwardingRules"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(globalForwardingRuleApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/global/forwardingRules"); + } + + public void setTarget() throws Exception { + server.enqueue(jsonResponse("/region_operation.json")); + + URI newTarget = URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool")); + assertEquals(globalForwardingRuleApi().setTarget("testForwardingRule", newTarget), + new ParseRegionOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/global/forwardingRules/testForwardingRule/setTarget", + stringFromResource("/forwardingrule_set_target.json")); + } + + ForwardingRuleApi globalForwardingRuleApi(){ + return api().globalForwardingRules(); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/HttpHealthCheckApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/HttpHealthCheckApiExpectTest.java index ad915ca693..1e1549a80e 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/HttpHealthCheckApiExpectTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/HttpHealthCheckApiExpectTest.java @@ -17,8 +17,6 @@ package org.jclouds.googlecomputeengine.features; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.AssertJUnit.assertNull; import javax.ws.rs.core.MediaType; @@ -26,8 +24,6 @@ import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; import org.jclouds.googlecomputeengine.options.HttpHealthCheckCreationOptions; import org.jclouds.googlecomputeengine.parse.ParseGlobalOperationTest; -import org.jclouds.googlecomputeengine.parse.ParseHttpHealthCheckListTest; -import org.jclouds.googlecomputeengine.parse.ParseHttpHealthCheckTest; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.testng.annotations.Test; @@ -35,121 +31,6 @@ import org.testng.annotations.Test; @Test(groups = "unit", testName = "HttpHealthCheckApiExpectTest") public class HttpHealthCheckApiExpectTest extends BaseGoogleComputeEngineExpectTest { - public void testGetHttpHealthCheckResponseIs2xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/httpHealthChecks/http-health-check-api-live-test") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/httphealthcheck_get.json")).build(); - - HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).httpHeathChecks(); - - assertEquals(api.get("http-health-check-api-live-test"), - new ParseHttpHealthCheckTest().expected()); - } - - public void testGetHttpHealthCheckResponseIs4xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/httpHealthChecks/http-health-check-test") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).httpHeathChecks(); - - assertNull(api.get("http-health-check-test")); - } - - public void testInsertHttpHealthCheckResponseIs2xx() { - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/global/httpHealthChecks") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/httphealthcheck_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertHttpHealthCheckResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/global_operation.json")).build(); - - HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, - insertHttpHealthCheckResponse).httpHeathChecks(); - HttpHealthCheckCreationOptions options = new HttpHealthCheckCreationOptions().timeoutSec(0).unhealthyThreshold(0); - assertEquals(api.insert("http-health-check", options), new ParseGlobalOperationTest().expected()); - } - - public void testDeleteHttpHealthCheckResponseIs2xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/global/httpHealthChecks/http-health-check") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/global_operation.json")).build(); - - HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).httpHeathChecks(); - - assertEquals(api.delete("http-health-check"), - new ParseGlobalOperationTest().expected()); - } - - public void testDeleteHttpHealthCheckResponseIs4xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/global/httpHealthChecks/http-health-check") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(404).build(); - - HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).httpHeathChecks(); - - assertNull(api.delete("http-health-check")); - } - - HttpRequest list = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/httpHealthChecks") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public void list() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/httphealthcheck_list.json")).build(); - - HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).httpHeathChecks(); - - assertEquals(api.list().next(), new ParseHttpHealthCheckListTest().expected()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).httpHeathChecks(); - - assertFalse(api.list().hasNext()); - } - public void testPatchHttpHealthChecksResponseIs2xx() { String healthCheckName = "http-health-check"; HttpRequest patch = HttpRequest @@ -163,7 +44,7 @@ public class HttpHealthCheckApiExpectTest extends BaseGoogleComputeEngineExpectT HttpResponse insertHttpHealthCheckResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromResource("/global_operation.json")).build(); - + HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), TOKEN_RESPONSE, patch, insertHttpHealthCheckResponse).httpHeathChecks(); @@ -171,24 +52,4 @@ public class HttpHealthCheckApiExpectTest extends BaseGoogleComputeEngineExpectT assertEquals(api.patch(healthCheckName, options), new ParseGlobalOperationTest().expected()); } - public void testUpdateHttpHealthChecksResponseIs2xx() { - String healthCheckName = "http-health-check"; - HttpRequest patch = HttpRequest - .builder() - .method("PUT") - .endpoint(BASE_URL + "/party/global/httpHealthChecks/" + healthCheckName) - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/httphealthcheck_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertHttpHealthCheckResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/global_operation.json")).build(); - - HttpHealthCheckApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, patch, - insertHttpHealthCheckResponse).httpHeathChecks(); - HttpHealthCheckCreationOptions options = new HttpHealthCheckCreationOptions().timeoutSec(0).unhealthyThreshold(0); - assertEquals(api.update(healthCheckName, options), new ParseGlobalOperationTest().expected()); - } } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/HttpHealthCheckApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/HttpHealthCheckApiMockTest.java new file mode 100644 index 0000000000..35b4e03778 --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/HttpHealthCheckApiMockTest.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.AssertJUnit.assertNull; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.options.HttpHealthCheckCreationOptions; +import org.jclouds.googlecomputeengine.parse.ParseGlobalOperationTest; +import org.jclouds.googlecomputeengine.parse.ParseHttpHealthCheckListTest; +import org.jclouds.googlecomputeengine.parse.ParseHttpHealthCheckTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "HttpHealthCheckApiMockTest", singleThreaded = true) +public class HttpHealthCheckApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception{ + server.enqueue(jsonResponse("/httphealthcheck_get.json")); + + assertEquals(httpHealthCheckApi().get("http-health-check-api-live-test"), + new ParseHttpHealthCheckTest().expected(url("/projects"))); + + assertSent(server, "GET", "/projects/party/global/httpHealthChecks/http-health-check-api-live-test"); + } + + public void get_4xx() throws Exception{ + server.enqueue(response404()); + + assertNull(httpHealthCheckApi().get("http-health-check-api-live-test")); + assertSent(server, "GET", "/projects/party/global/httpHealthChecks/http-health-check-api-live-test"); + } + + public void insert() throws Exception { + server.enqueue(jsonResponse("/global_operation.json")); + + HttpHealthCheckCreationOptions options = new HttpHealthCheckCreationOptions() + .timeoutSec(0).unhealthyThreshold(0); + assertEquals(httpHealthCheckApi().insert("http-health-check", options), + new ParseGlobalOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/global/httpHealthChecks", + stringFromResource("/httphealthcheck_insert.json")); + } + + public void delete() throws Exception { + server.enqueue(jsonResponse("/global_operation.json")); + + assertEquals(httpHealthCheckApi().delete("http-health-check"), + new ParseGlobalOperationTest().expected(url("/projects"))); + + assertSent(server, "DELETE", "/projects/party/global/httpHealthChecks/http-health-check"); + } + + public void delete_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(httpHealthCheckApi().delete("http-health-check")); + + assertSent(server, "DELETE", "/projects/party/global/httpHealthChecks/http-health-check"); + } + + public void update() throws Exception { + server.enqueue(jsonResponse("/global_operation.json")); + + HttpHealthCheckCreationOptions options = new HttpHealthCheckCreationOptions() + .timeoutSec(0).unhealthyThreshold(0); + assertEquals(httpHealthCheckApi().update("http-health-check", options), + new ParseGlobalOperationTest().expected(url("/projects"))); + + assertSent(server, "PUT", "/projects/party/global/httpHealthChecks/http-health-check", + stringFromResource("/httphealthcheck_insert.json")); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/httphealthcheck_list.json")); + + assertEquals(httpHealthCheckApi().list().next(), + new ParseHttpHealthCheckListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/httpHealthChecks"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(httpHealthCheckApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/global/httpHealthChecks"); + } + + + HttpHealthCheckApi httpHealthCheckApi() { + return api().httpHeathChecks(); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiMockTest.java index 7fd62c50c7..ccdc039870 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiMockTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiMockTest.java @@ -78,35 +78,35 @@ public class ImageApiMockTest extends BaseGoogleComputeEngineApiMockTest { assertSent(server, "DELETE", "/projects/party/global/images/centos-6-2-v20120326"); } - public void list() throws InterruptedException { + public void list() throws Exception { server.enqueue(jsonResponse("/image_list.json")); assertEquals(imageApi().list().next(), new ParseImageListTest().expected(url("/projects"))); assertSent(server, "GET", "/projects/party/global/images"); } - public void list_empty() throws InterruptedException { + public void list_empty() throws Exception { server.enqueue(jsonResponse("/list_empty.json")); assertFalse(imageApi().list().hasNext()); assertSent(server, "GET", "/projects/party/global/images"); } - public void listInProject() throws InterruptedException { + public void listInProject() throws Exception { server.enqueue(jsonResponse("/image_list.json")); assertEquals(imageApi().listInProject("centos-cloud").next(), new ParseImageListTest().expected(url("/projects"))); assertSent(server, "GET", "/projects/centos-cloud/global/images"); } - public void listInProject_empty() throws InterruptedException { + public void listInProject_empty() throws Exception { server.enqueue(jsonResponse("/list_empty.json")); assertFalse(imageApi().listInProject("centos-cloud").hasNext()); assertSent(server, "GET", "/projects/centos-cloud/global/images"); } - public void createImageFromPd_2xx() throws InterruptedException{ + public void createImageFromPd_2xx() throws Exception{ server.enqueue(jsonResponse("/operation.json")); assertEquals(imageApi().createFromDisk("my-image", url("/projects/party/zones/us-central1-a/disks/mydisk")), @@ -114,7 +114,7 @@ public class ImageApiMockTest extends BaseGoogleComputeEngineApiMockTest { assertSent(server, "POST", "/projects/party/global/images", stringFromResource("/image_insert_from_pd.json")); } - public void deprecateImage_2xx() throws InterruptedException{ + public void deprecateImage_2xx() throws Exception{ String imageName = "test-image"; server.enqueue(jsonResponse("/operation.json")); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiExpectTest.java deleted file mode 100644 index 17423206cb..0000000000 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiExpectTest.java +++ /dev/null @@ -1,321 +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.googlecomputeengine.features; - -import static org.jclouds.googlecomputeengine.features.ProjectApiExpectTest.GET_PROJECT_REQUEST; -import static org.jclouds.googlecomputeengine.features.ProjectApiExpectTest.GET_PROJECT_RESPONSE; -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 java.net.URI; -import java.util.Arrays; - -import javax.ws.rs.core.MediaType; - -import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; -import org.jclouds.googlecomputeengine.domain.AttachDisk; -import org.jclouds.googlecomputeengine.domain.Metadata; -import org.jclouds.googlecomputeengine.domain.NewInstance; -import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; -import org.jclouds.googlecomputeengine.parse.ParseInstanceSerialOutputTest; -import org.jclouds.googlecomputeengine.parse.ParseInstanceTest; -import org.jclouds.googlecomputeengine.parse.ParseZoneOperationTest; -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; - -@Test(groups = "unit", testName = "InstanceApiExpectTest") -public class InstanceApiExpectTest extends BaseGoogleComputeEngineExpectTest { - - public static final HttpRequest GET_INSTANCE_REQUEST = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static final HttpResponse GET_INSTANCE_RESPONSE = HttpResponse.builder().statusCode(200) - .payload(staticPayloadFromResource("/instance_get.json")).build(); - - public static final HttpRequest LIST_INSTANCES_REQUEST = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static final HttpResponse LIST_INSTANCES_RESPONSE = HttpResponse.builder().statusCode(200) - .payload(staticPayloadFromResource("/instance_list.json")).build(); - - public static final HttpResponse CREATE_INSTANCE_RESPONSE = HttpResponse.builder().statusCode(200) - .payload(staticPayloadFromResource("/zone_operation.json")).build(); - - public void testGetInstanceResponseIs2xx() throws Exception { - - InstanceApi api = requestsSendResponses( - requestForScopes(COMPUTE_READONLY_SCOPE), TOKEN_RESPONSE, - GET_INSTANCE_REQUEST, GET_INSTANCE_RESPONSE).instancesInZone("us-central1-a"); - - assertEquals(api.get("test-1"), new ParseInstanceTest().expected()); - } - - public void testGetInstanceResponseIs4xx() throws Exception { - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, GET_INSTANCE_REQUEST, response).instancesInZone("us-central1-a"); - - assertNull(api.get("test-1")); - } - - public void testGetInstanceSerialPortOutput() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1/serialPort") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/instance_serial_port.json")).build(); - - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).instancesInZone("us-central1-a"); - - assertEquals(api.getSerialPortOutput("test-1"), new ParseInstanceSerialOutputTest().expected()); - } - - public void testInsertInstanceResponseIs2xxNoOptions() { - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/instance_insert_simple.json", MediaType.APPLICATION_JSON)) - .build(); - - InstanceApi api = requestsSendResponses(ImmutableMap.of(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, GET_PROJECT_REQUEST, GET_PROJECT_RESPONSE, - requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, - CREATE_INSTANCE_RESPONSE)).instancesInZone("us-central1-a"); - - NewInstance newInstance = NewInstance.create( - "test-1", // name - URI.create(BASE_URL + "/party/zones/us-central1-a/machineTypes/n1-standard-1"), // machineType - URI.create(BASE_URL + "/party/global/networks/default"), // network - URI.create(BASE_URL + "/party/global/images/centos-6-2-v20120326") // sourceImage - ); - - assertEquals(api.create(newInstance), new ParseZoneOperationTest().expected()); - } - - public void testInsertInstanceResponseIs2xxAllOptions() { - HttpRequest insert = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/instance_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse insertInstanceResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - InstanceApi api = requestsSendResponses(ImmutableMap.of(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, GET_PROJECT_REQUEST, GET_PROJECT_RESPONSE, - requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, insert, insertInstanceResponse)).instancesInZone("us-central1-a"); - - NewInstance newInstance = NewInstance.create( - "test-1", // name - URI.create(BASE_URL + "/party/zones/us-central1-a/machineTypes/n1-standard-1"), // machineType - URI.create(BASE_URL + "/party/global/networks/default"), // network - Arrays.asList(AttachDisk.existingBootDisk(URI.create(BASE_URL + "/party/zones/us-central1-a/disks/test"))), - "desc" // description - ); - - newInstance.metadata().put("aKey", "aValue"); - assertEquals(api.create(newInstance), new ParseZoneOperationTest().expected()); - } - - public void testDeleteInstanceResponseIs2xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).instancesInZone("us-central1-a"); - - assertEquals(api.delete("test-1"), new ParseZoneOperationTest().expected()); - } - - public void testDeleteInstanceResponseIs4xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(404).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).instancesInZone("us-central1-a"); - - assertNull(api.delete("test-1")); - } - - public void list() { - InstanceApi api = requestsSendResponses( - requestForScopes(COMPUTE_READONLY_SCOPE), TOKEN_RESPONSE, - LIST_INSTANCES_REQUEST, LIST_INSTANCES_RESPONSE).instancesInZone("us-central1-a"); - - assertTrue(api.list().hasNext()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_INSTANCES_REQUEST, response).instancesInZone("us-central1-a"); - - assertFalse(api.list().hasNext()); - } - - public void testSetInstanceMetadataResponseIs2xx() { - HttpRequest setMetadata = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1/setMetadata") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/instance_set_metadata.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse setMetadataResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, setMetadata, setMetadataResponse).instancesInZone("us-central1-a"); - - assertEquals(api.setMetadata("test-1", Metadata.create("efgh").put("foo", "bar")), - new ParseZoneOperationTest().expected()); - } - - public void testSetInstanceTagsResponseIs2xx() { - HttpRequest setTags = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1/setTags") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/instance_set_tags.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse setTagsResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, setTags, setTagsResponse).instancesInZone("us-central1-a"); - - assertEquals(api.setTags("test-1", ImmutableList.of("foo", "bar"), "efgh"), - new ParseZoneOperationTest().expected()); - } - - public void testResetInstanceResponseIs2xx() { - HttpRequest reset = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1/reset") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse resetResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, reset, resetResponse).instancesInZone("us-central1-a"); - - assertEquals(api.reset("test-1"), - new ParseZoneOperationTest().expected()); - } - - public void testAttachDiskResponseIs2xx() { - HttpRequest attach = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1/attachDisk") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResourceWithContentType("/instance_attach_disk.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse attachResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, attach, attachResponse).instancesInZone("us-central1-a"); - - assertEquals(api.attachDisk("test-1", - AttachDisk.create(AttachDisk.Type.PERSISTENT, - AttachDisk.Mode.READ_ONLY, - URI.create(BASE_URL + "/party/zones/us-central1-a/disks/testimage1"), - null, - false, - null, - true)), - new ParseZoneOperationTest().expected()); - } - - public void testDetachDiskResponseIs2xx() { - HttpRequest detach = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/zones/us-central1-a/instances/test-1/detachDisk?deviceName=test-disk-1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .build(); - - HttpResponse detachResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_operation.json")).build(); - - InstanceApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, detach, detachResponse).instancesInZone("us-central1-a"); - - assertEquals(api.detachDisk("test-1", "test-disk-1"), - new ParseZoneOperationTest().expected()); - } -} - - diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java index 56cc5f91df..45abb946de 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java @@ -17,25 +17,165 @@ package org.jclouds.googlecomputeengine.features; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; -import org.jclouds.googlecomputeengine.domain.Operation; +import java.net.URI; +import java.util.Arrays; + +import org.jclouds.googlecomputeengine.domain.AttachDisk; +import org.jclouds.googlecomputeengine.domain.Metadata; +import org.jclouds.googlecomputeengine.domain.NewInstance; import org.jclouds.googlecomputeengine.domain.Instance.Scheduling.OnHostMaintenance; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.parse.ParseInstanceListTest; +import org.jclouds.googlecomputeengine.parse.ParseInstanceSerialOutputTest; +import org.jclouds.googlecomputeengine.parse.ParseInstanceTest; import org.jclouds.googlecomputeengine.parse.ParseZoneOperationTest; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; + @Test(groups = "unit", testName = "InstanceApiMockTest", singleThreaded = true) public class InstanceApiMockTest extends BaseGoogleComputeEngineApiMockTest { - public void setDiskAutoDeleteResponseIs2xx() throws Exception { + public void get() throws Exception { + server.enqueue(jsonResponse("/instance_get.json")); + + assertEquals(instanceApi().get("test-1"), new ParseInstanceTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/instances/test-1"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(instanceApi().get("test-1")); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/instances/test-1"); + } + + public void getInstanceSerialPortOutput() throws Exception { + server.enqueue(jsonResponse("/instance_serial_port.json")); + + assertEquals(instanceApi().getSerialPortOutput("test-1"), + new ParseInstanceSerialOutputTest().expected()); + + assertSent(server, "GET", "/projects/party/zones/us-central1-a/instances/test-1/serialPort"); + } + + public void insert_noOptions() throws Exception { server.enqueue(jsonResponse("/zone_operation.json")); - InstanceApi instanceApi = api().instancesInZone("us-central1-a"); + NewInstance newInstance = NewInstance.create( + "test-1", // name + URI.create(url("/projects/party/zones/us-central1-a/machineTypes/n1-standard-1")), // machineType + URI.create(url("/projects/party/global/networks/default")), // network + URI.create(url("/projects/party/global/images/centos-6-2-v20120326")) // sourceImage + ); - Operation o = instanceApi.setDiskAutoDelete("test-1", "test-disk-1", true); - int port = server.getPort(); - // Endpoint is different for URIs such as zone and selfLink. - assertEquals(o, new ParseZoneOperationTest().expected("http://localhost:" + port + "/projects")); + assertEquals(instanceApi().create(newInstance), new ParseZoneOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances", + stringFromResource("/instance_insert_simple.json")); + } + + public void insert_allOptions() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + NewInstance newInstance = NewInstance.create( + "test-1", // name + URI.create(url("/projects/party/zones/us-central1-a/machineTypes/n1-standard-1")), // machineType + URI.create(url("/projects/party/global/networks/default")), // network + Arrays.asList(AttachDisk.existingBootDisk(URI.create(url("/projects/party/zones/us-central1-a/disks/test")))), + "desc" // description + ); + + newInstance.metadata().put("aKey", "aValue"); + assertEquals(instanceApi().create(newInstance), new ParseZoneOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances", + stringFromResource("/instance_insert.json")); + } + + public void delete() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(instanceApi().delete("test-1"), + new ParseZoneOperationTest().expected(url("/projects"))); + assertSent(server, "DELETE", "/projects/party/zones/us-central1-a/instances/test-1"); + } + + public void delete_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(instanceApi().delete("test-1")); + assertSent(server, "DELETE", "/projects/party/zones/us-central1-a/instances/test-1"); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/instance_list.json")); + + assertEquals(instanceApi().list().next(), new ParseInstanceListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/instances"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(instanceApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/instances"); + } + + public void setMetadata() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(instanceApi().setMetadata("test-1", Metadata.create("efgh").put("foo", "bar")), + new ParseZoneOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances/test-1/setMetadata", + stringFromResource("/instance_set_metadata.json")); + } + + public void setTags() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(instanceApi().setTags("test-1", ImmutableList.of("foo", "bar"), "efgh"), + new ParseZoneOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances/test-1/setTags", + stringFromResource("/instance_set_tags.json")); + } + + public void reset() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(instanceApi().reset("test-1"), + new ParseZoneOperationTest().expected(url("/projects"))); + + assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances/test-1/reset"); + } + + public void attachDisk() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(instanceApi().attachDisk("test-1", + AttachDisk.create(AttachDisk.Type.PERSISTENT, + AttachDisk.Mode.READ_ONLY, + URI.create(url("/projects/party/zones/us-central1-a/disks/testimage1")), + null, false, null, true)), + new ParseZoneOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances/test-1/attachDisk", + stringFromResource("/instance_attach_disk.json")); + } + + public void detatchDisk() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(instanceApi().detachDisk("test-1", "test-disk-1"), + new ParseZoneOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances/test-1/detachDisk?deviceName=test-disk-1"); + } + + public void setDiskAutoDelete() throws Exception { + server.enqueue(jsonResponse("/zone_operation.json")); + + assertEquals(instanceApi().setDiskAutoDelete("test-1", "test-disk-1", true), + new ParseZoneOperationTest().expected(url("/projects"))); assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances/test-1/setDiskAutoDelete" + "?deviceName=test-disk-1&autoDelete=true"); @@ -44,12 +184,14 @@ public class InstanceApiMockTest extends BaseGoogleComputeEngineApiMockTest { public void setScheduling() throws Exception { server.enqueue(jsonResponse("/zone_operation.json")); - InstanceApi instanceApi = api().instancesInZone("us-central1-a"); - - assertEquals(instanceApi.setScheduling("test-1", OnHostMaintenance.TERMINATE, true), + assertEquals(instanceApi().setScheduling("test-1", OnHostMaintenance.TERMINATE, true), new ParseZoneOperationTest().expected(url("/projects"))); assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances/test-1/setScheduling", "{\"onHostMaintenance\": \"TERMINATE\",\"automaticRestart\": true}"); } + + InstanceApi instanceApi(){ + return api().instancesInZone("us-central1-a"); + } } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/MachineTypeApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/MachineTypeApiExpectTest.java deleted file mode 100644 index 01be4c344c..0000000000 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/MachineTypeApiExpectTest.java +++ /dev/null @@ -1,109 +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.googlecomputeengine.features; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNull; - -import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; -import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; -import org.jclouds.googlecomputeengine.parse.ParseMachineTypeListTest; -import org.jclouds.googlecomputeengine.parse.ParseMachineTypeTest; -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.testng.annotations.Test; - -@Test(groups = "unit", testName = "MachineTypeApiExpectTest") -public class MachineTypeApiExpectTest extends BaseGoogleComputeEngineExpectTest { - - public static final HttpRequest LIST_MACHINE_TYPES_REQUEST = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/machineTypes") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static final HttpResponse LIST_MACHINE_TYPES_RESPONSE = HttpResponse.builder() - .statusCode(200) - .payload(staticPayloadFromResource("/machinetype_list.json")) - .build(); - - public static final HttpRequest LIST_CENTRAL1B_MACHINE_TYPES_REQUEST = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-b/machineTypes") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static final HttpResponse LIST_CENTRAL1B_MACHINE_TYPES_RESPONSE = HttpResponse.builder() - .statusCode(200) - .payload(staticPayloadFromResource("/machinetype_list_central1b.json")) - .build(); - - public void testGetMachineTypeResponseIs2xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/machineTypes/n1-standard-1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/machinetype.json")).build(); - - MachineTypeApi machineTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).machineTypesInZone("us-central1-a"); - - assertEquals(machineTypeApi.get("n1-standard-1"), new ParseMachineTypeTest().expected()); - } - - public void testGetMachineTypeResponseIs4xx() throws Exception { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/zones/us-central1-a/machineTypes/n1-standard-1") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - MachineTypeApi machineTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).machineTypesInZone("us-central1-a"); - - assertNull(machineTypeApi.get("n1-standard-1")); - } - - public void list() throws Exception { - - MachineTypeApi machineTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_MACHINE_TYPES_REQUEST, LIST_MACHINE_TYPES_RESPONSE).machineTypesInZone( - "us-central1-a"); - - assertEquals(machineTypeApi.list().next(), new ParseMachineTypeListTest().expected()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - MachineTypeApi machineTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), TOKEN_RESPONSE, - LIST_MACHINE_TYPES_REQUEST, response).machineTypesInZone("us-central1-a"); - - assertFalse(machineTypeApi.list().hasNext()); - } -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/MachineTypeApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/MachineTypeApiMockTest.java new file mode 100644 index 0000000000..058cf57288 --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/MachineTypeApiMockTest.java @@ -0,0 +1,63 @@ +/* + * 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.parse.ParseMachineTypeListTest; +import org.jclouds.googlecomputeengine.parse.ParseMachineTypeTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "MachineTypeApiMockTest", singleThreaded = true) +public class MachineTypeApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception{ + server.enqueue(jsonResponse("/machinetype.json")); + + assertEquals(machineTypeApi().get("n1-standard-1"), + new ParseMachineTypeTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/machineTypes/n1-standard-1"); + } + + public void get_4xx() throws Exception{ + server.enqueue(response404()); + + assertNull(machineTypeApi().get("n1-standard-1")); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/machineTypes/n1-standard-1"); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/machinetype_list.json")); + + assertEquals(machineTypeApi().list().next(), new ParseMachineTypeListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/machineTypes"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(machineTypeApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/zones/us-central1-a/machineTypes"); + } + + MachineTypeApi machineTypeApi() { + return api().machineTypesInZone("us-central1-a"); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseAddressListTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseAddressListTest.java index 1908914705..50412116b2 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseAddressListTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseAddressListTest.java @@ -40,15 +40,20 @@ public class ParseAddressListTest extends BaseGoogleComputeEngineParseTest expected() { - Address address1 = new ParseAddressTest().expected(); + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage
expected(String baseUrl) { + Address address1 = new ParseAddressTest().expected(baseUrl); Address address2 = Address.create( // "4881363978908129158", // id - URI.create(BASE_URL + "/party/regions/us-central1/addresses/test-ip2"), // selfLink + URI.create(baseUrl + "/party/regions/us-central1/addresses/test-ip2"), // selfLink "test-ip2", // name "", // description "RESERVED", // status null, // user - URI.create(BASE_URL + "/party/regions/us-central1"), // region + URI.create(baseUrl + "/party/regions/us-central1"), // region "173.255.118.115" // address ); return ForwardingListPage.create( // diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseAddressTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseAddressTest.java index 28feb2e20d..3dd748fe3a 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseAddressTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseAddressTest.java @@ -36,14 +36,19 @@ public class ParseAddressTest extends BaseGoogleComputeEngineParseTest
@Override @Consumes(APPLICATION_JSON) public Address expected() { - return Address.create( // + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public Address expected(String baseUrl) { + return Address.create( //d "4439373783165447583", // id - URI.create(BASE_URL + "/party/regions/us-central1/addresses/test-ip1"), // selfLink + URI.create(baseUrl + "/party/regions/us-central1/addresses/test-ip1"), // selfLink "test-ip1", // name "", // description "RESERVED", // status null, // user - URI.create(BASE_URL + "/party/regions/us-central1"), // region + URI.create(baseUrl + "/party/regions/us-central1"), // region "173.255.115.190" // address ); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceGetHealthTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceGetHealthTest.java index b131b43d94..5eab5291e0 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceGetHealthTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceGetHealthTest.java @@ -39,8 +39,12 @@ public class ParseBackendServiceGetHealthTest extends BaseGoogleComputeEnginePar @Override @Consumes(APPLICATION_JSON) public BackendServiceGroupHealth expected() { - URI uri = URI.create("https://www.googleapis.com/compute/v1/projects/" - + "myproject/zones/us-central1-a/instances/" + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public BackendServiceGroupHealth expected(String baseUrl) { + URI uri = URI.create(baseUrl + "/myproject/zones/us-central1-a/instances/" + "jclouds-test"); return BackendServiceGroupHealth.create( ImmutableList.of(HealthStatus.create( diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceListTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceListTest.java index 93975575df..7bd0b4a44b 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceListTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceListTest.java @@ -42,16 +42,21 @@ public class ParseBackendServiceListTest extends BaseGoogleComputeEngineParseTes @Override @Consumes(APPLICATION_JSON) public ListPage expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage expected(String baseUrl) { return ForwardingListPage.create( ImmutableList.of( - new ParseBackendServiceTest().expected(), + new ParseBackendServiceTest().expected(baseUrl), BackendService.create("12862241067393040785", //id new SimpleDateFormatDateService().iso8601DateParse("2012-04-13T03:05:04.365"), //creationTimestamp, - URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/jclouds-test-2"), //selfLink, + URI.create(baseUrl + "/myproject/global/backendServices/jclouds-test-2"), //selfLink, "jclouds-test-2", //name, "Backend Service 2", //description null, // backends, - ImmutableList.of(URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/httpHealthChecks/jclouds-test")), //healthChecks, + ImmutableList.of(URI.create(baseUrl + "/myproject/global/httpHealthChecks/jclouds-test")), //healthChecks, 45, //timeoutSec, 80, //port, "HTTP", //protocol, diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceTest.java index 004a9eccba..51a63f4e16 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseBackendServiceTest.java @@ -41,12 +41,12 @@ public class ParseBackendServiceTest extends BaseGoogleComputeEngineParseTest expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage expected(String baseUrl) { return ForwardingListPage.create( // - ImmutableList.of(new ParseDiskTest().expected()), // items + ImmutableList.of(new ParseDiskTest().expected(baseUrl)), // items null // nextPageToken ); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseDiskTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseDiskTest.java index 67689ab432..9ce74492f1 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseDiskTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseDiskTest.java @@ -36,15 +36,20 @@ public class ParseDiskTest extends BaseGoogleComputeEngineParseTest { @Override @Consumes(APPLICATION_JSON) public Disk expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public Disk expected(String baseUrl){ return Disk.create( // "13050421646334304115", // id - URI.create(BASE_URL + "/party/zones/us-central1-a"), // zone + URI.create(baseUrl + "/party/zones/us-central1-a"), // zone "READY", // status "testimage1", // name null, // description 1, // sizeGb - URI.create(BASE_URL + "/party/zones/us-central1-a/disks/testimage1"), // selfLink - URI.create(BASE_URL + "/studied-point-720/zones/us-central1-a/diskTypes/pd-standard") // type + URI.create(baseUrl + "/party/zones/us-central1-a/disks/testimage1"), // selfLink + URI.create(baseUrl + "/studied-point-720/zones/us-central1-a/diskTypes/pd-standard") // type ); } } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java index 8b02a82da6..e6a7f00778 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java @@ -40,13 +40,18 @@ public class ParseFirewallListTest extends BaseGoogleComputeEngineParseTest expected() { - Firewall firewall1 = new ParseFirewallTest().expected(); + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage expected(String baseUrl) { + Firewall firewall1 = new ParseFirewallTest().expected(baseUrl); Firewall firewall2 = Firewall.create( // "12862241067393040785", // id - URI.create(BASE_URL + "/google/global/firewalls/default-ssh"), // selfLink + URI.create(baseUrl + "/google/global/firewalls/default-ssh"), // selfLink "default-ssh", // name "SSH allowed from anywhere", // description - URI.create(BASE_URL + "/google/global/networks/default"), // network + URI.create(baseUrl + "/google/global/networks/default"), // network ImmutableList.of("0.0.0.0/0"), // sourceRanges null, // sourceTags null, // targetTags diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java index e4219a7e39..4ac6bc1a1b 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java @@ -39,12 +39,17 @@ public class ParseFirewallTest extends BaseGoogleComputeEngineParseTest expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage expected(String baseUrl) { return ForwardingListPage.create( // - ImmutableList.of(new ParseForwardingRuleTest().expected()), // items + ImmutableList.of(new ParseForwardingRuleTest().expected(baseUrl)), // items null // nextPageToken ); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseForwardingRuleTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseForwardingRuleTest.java index 7b09dc654c..ce21f9b717 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseForwardingRuleTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseForwardingRuleTest.java @@ -37,17 +37,21 @@ public class ParseForwardingRuleTest extends BaseGoogleComputeEngineParseTest expected() { - HttpHealthCheck healthCheck1 = new ParseHttpHealthCheckTest().expected(); + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage expected(String baseUrl) { + HttpHealthCheck healthCheck1 = new ParseHttpHealthCheckTest().expected(baseUrl); HttpHealthCheck healthCheck2 = HttpHealthCheck.create( // "1035854271083519643", // id - URI.create(BASE_URL + "/party-gce/global/httpHealthChecks/myname-andrea-kmzmi1bh-http-health-check"), + URI.create(baseUrl + "/party-gce/global/httpHealthChecks/myname-andrea-kmzmi1bh-http-health-check"), // selfLink "myname-andrea-kmzmi1bh-http-health-check", // name null, // description @@ -57,7 +62,7 @@ public class ParseHttpHealthCheckListTest extends BaseGoogleComputeEngineParseTe ); HttpHealthCheck healthCheck3 = HttpHealthCheck.create( // "7006563292274658743", // id - URI.create(BASE_URL + "/party-gce/global/httpHealthChecks/myname-andrea-zk7gadwq-http-health-check"), + URI.create(baseUrl + "/party-gce/global/httpHealthChecks/myname-andrea-zk7gadwq-http-health-check"), // selfLink "myname-andrea-zk7gadwq-http-health-check", // name null, // description diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseHttpHealthCheckTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseHttpHealthCheckTest.java index 43ccb600de..62138b5260 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseHttpHealthCheckTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseHttpHealthCheckTest.java @@ -36,9 +36,14 @@ public class ParseHttpHealthCheckTest extends BaseGoogleComputeEngineParseTest expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage expected(String baseUrl) { return ForwardingListPage.create( // - ImmutableList.of(new ParseInstanceTest().expected()), // items + ImmutableList.of(new ParseInstanceTest().expected(baseUrl)), // items null // nextPageToken ); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java index e34c1495ad..f786a5c47c 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java @@ -44,19 +44,24 @@ public class ParseInstanceTest extends BaseGoogleComputeEngineParseTest expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage expected(String baseUrl) { MachineType machineType1 = MachineType.create( // "4618642685664990776", // id - URI.create(BASE_URL + "/party/zones/us-central1-a/machineTypes/f1-micro"), // selfLink + URI.create(baseUrl + "/party/zones/us-central1-a/machineTypes/f1-micro"), // selfLink "f1-micro", // name "1 vCPU (shared physical core) and 0.6 GB RAM", // description 1, // guestCpus @@ -55,7 +60,7 @@ public class ParseMachineTypeListTest extends BaseGoogleComputeEngineParseTest