From e4cc7282d798ca14e9394a53506a7b5447b026fc Mon Sep 17 00:00:00 2001 From: Daniel Broudy Date: Tue, 9 Dec 2014 10:15:43 -0800 Subject: [PATCH] New MockTests: TargetHttpProxyApi, UrlMap, ZoneApi, FirewallApi --- .../features/FirewallApiExpectTest.java | 122 +------------ .../features/FirewallApiMockTest.java | 40 ++++ .../TargetHttpProxyApiExpectTest.java | 171 ------------------ .../features/TargetHttpProxyApiMockTest.java | 99 ++++++++++ .../features/UrlMapApiExpectTest.java | 159 ++-------------- .../features/UrlMapApiMockTest.java | 140 ++++++++++++++ .../features/ZoneApiExpectTest.java | 95 ---------- .../features/ZoneApiMockTest.java | 63 +++++++ .../parse/ParseTargetHttpProxyListTest.java | 15 +- .../parse/ParseTargetHttpProxyTest.java | 12 +- .../parse/ParseUrlMapListTest.java | 13 +- .../parse/ParseUrlMapTest.java | 9 +- .../parse/ParseUrlMapValidateTest.java | 9 +- .../parse/ParseZoneListTest.java | 9 +- .../parse/ParseZoneTest.java | 7 +- .../src/test/resources/firewall_insert.json | 23 ++- 16 files changed, 429 insertions(+), 557 deletions(-) delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiMockTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiMockTest.java delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ZoneApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ZoneApiMockTest.java 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 b1ec4dafb0..b5ed9a1a42 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 @@ -16,18 +16,10 @@ */ package org.jclouds.googlecomputeengine.features; -import static com.google.common.base.Joiner.on; -import static com.google.common.collect.Iterables.transform; -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 java.io.IOException; import java.net.URI; -import java.util.List; - -import javax.ws.rs.core.MediaType; import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; import org.jclouds.googlecomputeengine.domain.Firewall; @@ -36,119 +28,13 @@ import org.jclouds.googlecomputeengine.options.FirewallOptions; import org.jclouds.googlecomputeengine.parse.ParseOperationTest; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; -import org.jclouds.io.Payload; import org.testng.annotations.Test; -import com.google.common.base.Function; import com.google.common.collect.ImmutableList; @Test(groups = "unit", testName = "FirewallApiExpectTest") public class FirewallApiExpectTest extends BaseGoogleComputeEngineExpectTest { - public static final HttpRequest GET_FIREWALL_REQUEST = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/firewalls/jclouds-test") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static HttpResponse GET_FIREWALL_RESPONSE = HttpResponse.builder().statusCode(200) - .payload(staticPayloadFromResource("/firewall_get.json")).build(); - - public static Payload firewallPayloadFirewallOfName(String firewallName, - String networkName, - List sourceRanges, - List sourceTags, - List targetTags, - List portRanges) throws IOException { - Function addQuotes = new Function() { - @Override - public String apply(String input) { - return "\"" + input + "\""; - } - }; - - String ports = on(",").skipNulls().join(transform(portRanges, addQuotes)); - - Payload payload = newStringPayload( - format(toStringAndClose(FirewallApiExpectTest.class.getResourceAsStream("/firewall_insert.json")), - firewallName, - networkName, - on(",").skipNulls().join(transform(sourceRanges, addQuotes)), - on(",").skipNulls().join(transform(sourceTags, addQuotes)), - on(",").skipNulls().join(transform(targetTags, addQuotes)), - ports, - ports)); - payload.getContentMetadata().setContentType(MediaType.APPLICATION_JSON); - return payload; - } - - //TODO (broudy): convert to mock test and add description to test. - public void testInsertFirewallResponseIs2xx() throws IOException { - - HttpRequest request = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/global/firewalls") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(firewallPayloadFirewallOfName( - "myfw", - "default", - ImmutableList.of("10.0.1.0/32"), - ImmutableList.of("tag1"), - ImmutableList.of("tag2"), - ImmutableList.of("22", "23-24"))) - .build(); - - HttpResponse insertFirewallResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/operation.json")).build(); - - FirewallApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, insertFirewallResponse).firewalls(); - - assertEquals(api.createInNetwork("myfw", URI.create(BASE_URL + "/party/global/networks/default"), - new FirewallOptions() - .addAllowedRule(Firewall.Rule.create("tcp", ImmutableList.of("22", "23-24"))) - .addSourceTag("tag1") - .addSourceRange("10.0.1.0/32") - .addTargetTag("tag2")), new ParseOperationTest().expected()); - - } - - public void testUpdateFirewallResponseIs2xx() throws IOException { - HttpRequest update = HttpRequest - .builder() - .method("PUT") - .endpoint(BASE_URL + "/party/global/firewalls/myfw") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(firewallPayloadFirewallOfName( - "myfw", - "default", - ImmutableList.of("10.0.1.0/32"), - ImmutableList.of("tag1"), - ImmutableList.of("tag2"), - ImmutableList.of("22", "23-24"))) - .build(); - - HttpResponse updateFirewallResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/operation.json")).build(); - - FirewallApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, update, - updateFirewallResponse).firewalls(); - - assertEquals(api.update("myfw", - new FirewallOptions() - .name("myfw") - .network(URI.create(BASE_URL + "/party/global/networks/default")) - .addAllowedRule(Firewall.Rule.create("tcp", ImmutableList.of("22", "23-24"))) - .addSourceTag("tag1") - .addSourceRange("10.0.1.0/32") - .addTargetTag("tag2")), new ParseOperationTest().expected()); - } - public void testPatchFirewallResponseIs2xx() throws IOException { HttpRequest update = HttpRequest .builder() @@ -156,13 +42,7 @@ public class FirewallApiExpectTest extends BaseGoogleComputeEngineExpectTest { - - private static final String ENDPOINT_BASE = "https://www.googleapis.com/" - + "compute/v1/projects/party/global/targetHttpProxies"; - - private org.jclouds.http.HttpRequest.Builder> getBasicRequest() { - return HttpRequest.builder().addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN); - } - - private HttpResponse createResponse(String payloadFile) { - return HttpResponse.builder().statusCode(200) - .payload(payloadFromResource(payloadFile)) - .build(); - } - - public void testGetTargetHttpProxyResponseIs2xx() throws Exception { - HttpRequest request = getBasicRequest().method("GET") - .endpoint(ENDPOINT_BASE + "/jclouds-test").build(); - - HttpResponse response = createResponse("/target_http_proxy_get.json"); - - TargetHttpProxyApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, request, response).targetHttpProxies(); - - assertEquals(api.get("jclouds-test"), - new ParseTargetHttpProxyTest().expected()); - } - - public void testGetTargetHttpProxyResponseIs4xx() throws Exception { - HttpRequest request = getBasicRequest().method("GET") - .endpoint(ENDPOINT_BASE + "/jclouds-test").build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - TargetHttpProxyApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, request, response).targetHttpProxies(); - - assertNull(api.get("jclouds-test")); - } - - - - public void testInsertTargetHttpProxyResponseIs2xx() { - HttpRequest request = getBasicRequest().method("POST") - .endpoint(ENDPOINT_BASE) - .payload(payloadFromResourceWithContentType("/target_http_proxy_insert.json", - MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse response = createResponse("/operation.json"); - - TargetHttpProxyApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).targetHttpProxies(); - - URI urlMap = URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps/jclouds-test"); - assertEquals(api.create("jclouds-test", urlMap), new ParseOperationTest().expected()); - } - - public void testDeleteTargetHttpProxyResponseIs2xx() { - HttpRequest request = getBasicRequest().method("DELETE") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .build(); - - HttpResponse response = createResponse("/operation.json"); - - TargetHttpProxyApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).targetHttpProxies(); - - assertEquals(api.delete("jclouds-test"), - new ParseOperationTest().expected()); - } - - public void testDeleteTargetHttpProxyResponseIs4xx() { - HttpRequest request = getBasicRequest().method("DELETE") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - TargetHttpProxyApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).targetHttpProxies(); - - assertNull(api.delete("jclouds-test")); - } - - public void testSetUrlMapTargetHttpProxyResponseIs2xx() { - HttpRequest request = getBasicRequest().method("POST") - // setUrlMap uses a non-standard url pattern - .endpoint("https://www.googleapis.com/compute/v1/projects/party/targetHttpProxies" - + "/jclouds-test/setUrlMap") - .payload(payloadFromResourceWithContentType("/target_http_proxy_set_url_map.json", - MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse response = createResponse("/operation.json"); - - TargetHttpProxyApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).targetHttpProxies(); - - URI urlMap = URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps/jclouds-test"); - assertEquals(api.setUrlMap("jclouds-test", urlMap), new ParseOperationTest().expected()); - } - - public void testListTargetHttpProxiesResponseIs2xx() { - HttpRequest request = getBasicRequest().method("GET") - .endpoint(ENDPOINT_BASE) - .build(); - - HttpResponse response = createResponse("/target_http_proxy_list.json"); - - TargetHttpProxyApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, request, response).targetHttpProxies(); - - assertEquals(api.list().next(), - new ParseTargetHttpProxyListTest().expected()); - } - - public void listEmpty() { - HttpRequest list = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/targetHttpProxies") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - TargetHttpProxyApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, list, response).targetHttpProxies(); - - assertFalse(api.list().hasNext()); - } -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiMockTest.java new file mode 100644 index 0000000000..08d3e8577f --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiMockTest.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.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.parse.ParseOperationTest; +import org.jclouds.googlecomputeengine.parse.ParseTargetHttpProxyListTest; +import org.jclouds.googlecomputeengine.parse.ParseTargetHttpProxyTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "TargetHttpProxyApiMockTest", singleThreaded = true) +public class TargetHttpProxyApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception { + server.enqueue(jsonResponse("/target_http_proxy_get.json")); + + assertEquals(targetHttpProxyApi().get("jclouds-test"), + new ParseTargetHttpProxyTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/targetHttpProxies/jclouds-test"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(targetHttpProxyApi().get("jclouds-test")); + assertSent(server, "GET", "/projects/party/global/targetHttpProxies/jclouds-test"); + } + + public void insert() throws Exception { + server.enqueue(jsonResponse("/operation.json")); + + URI urlMap = URI.create(url("/projects/myproject/global/urlMaps/jclouds-test")); + assertEquals(targetHttpProxyApi().create("jclouds-test", urlMap), new ParseOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/global/targetHttpProxies", + stringFromResource("/target_http_proxy_insert.json")); + } + + public void delete() throws Exception { + server.enqueue(jsonResponse("/operation.json")); + + assertEquals(targetHttpProxyApi().delete("jclouds-test"), + new ParseOperationTest().expected(url("/projects"))); + assertSent(server, "DELETE", "/projects/party/global/targetHttpProxies/jclouds-test"); + } + + public void delete_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(targetHttpProxyApi().delete("jclouds-test")); + assertSent(server, "DELETE", "/projects/party/global/targetHttpProxies/jclouds-test"); + } + + public void setUrlMap() throws Exception { + server.enqueue(jsonResponse("/operation.json")); + + URI urlMap = URI.create(url("/projects/myproject/global/urlMaps/jclouds-test")); + assertEquals(targetHttpProxyApi().setUrlMap("jclouds-test", urlMap), new ParseOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/targetHttpProxies/jclouds-test/setUrlMap", + stringFromResource("/target_http_proxy_set_url_map.json")); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/target_http_proxy_list.json")); + + assertEquals(targetHttpProxyApi().list().next(), new ParseTargetHttpProxyListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/targetHttpProxies"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(targetHttpProxyApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/global/targetHttpProxies"); + } + + TargetHttpProxyApi targetHttpProxyApi() { + return api().targetHttpProxies(); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiExpectTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiExpectTest.java index e13dbf39e2..885ab88d98 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiExpectTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiExpectTest.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 java.io.IOException; import java.net.URI; @@ -33,9 +31,6 @@ import org.jclouds.googlecomputeengine.domain.UrlMap.PathMatcher.PathRule; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineExpectTest; import org.jclouds.googlecomputeengine.options.UrlMapOptions; import org.jclouds.googlecomputeengine.parse.ParseOperationTest; -import org.jclouds.googlecomputeengine.parse.ParseUrlMapListTest; -import org.jclouds.googlecomputeengine.parse.ParseUrlMapTest; -import org.jclouds.googlecomputeengine.parse.ParseUrlMapValidateTest; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.testng.annotations.Test; @@ -45,82 +40,20 @@ import com.google.common.collect.ImmutableList; @Test(groups = "unit") public class UrlMapApiExpectTest extends BaseGoogleComputeEngineExpectTest { - private static final String ENDPOINT_BASE = "https://www.googleapis.com/" - + "compute/v1/projects/party/global/urlMaps"; - private org.jclouds.http.HttpRequest.Builder> getBasicRequest() { - return HttpRequest.builder().addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN); - } + public void patch() throws IOException { + HttpRequest request = HttpRequest.builder() + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN) + .method("PATCH") + .endpoint("https://www.googleapis.com/compute/v1/projects" + + "/party/global/urlMaps/jclouds-test") + .payload(payloadFromResourceWithContentType("/url_map_insert.json", MediaType.APPLICATION_JSON)) + .build(); - private HttpResponse createResponse(String payloadFile) { - return HttpResponse.builder().statusCode(200) - .payload(payloadFromResource(payloadFile)) - .build(); - } - - public void testGetUrlMapResponseIs2xx() throws Exception { - HttpRequest request = getBasicRequest().method("GET") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .build(); - - HttpResponse response = createResponse("/url_map_get.json"); - - UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, request, response).urlMaps(); - - assertEquals(api.get("jclouds-test"), new ParseUrlMapTest().expected()); - } - - public void testGetUrlMapResponseIs4xx() throws Exception { - HttpRequest request = getBasicRequest().method("GET") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .build(); - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, request, response).urlMaps(); - - assertNull(api.get("jclouds-test")); - } - - public void testInsertUrlMapResponseIs2xx() throws IOException { - HttpRequest request = getBasicRequest().method("POST") - .endpoint(ENDPOINT_BASE) - .payload(payloadFromResourceWithContentType("/url_map_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse response = createResponse("/operation.json"); - - UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).urlMaps(); - - assertEquals(api.create(createBasicMap()), new ParseOperationTest().expected()); - - } - - public void testUpdateUrlMapResponseIs2xx() throws IOException { - HttpRequest request = getBasicRequest().method("PUT") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .payload(payloadFromResourceWithContentType("/url_map_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse response = createResponse("/operation.json"); - - UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, request, response).urlMaps(); - - assertEquals(api.update("jclouds-test", createBasicMap()), new ParseOperationTest().expected()); - } - - public void testPatchUrlMapResponseIs2xx() throws IOException { - HttpRequest request = getBasicRequest().method("PATCH") - .endpoint(ENDPOINT_BASE + "/jclouds-test") - .payload(payloadFromResourceWithContentType("/url_map_insert.json", MediaType.APPLICATION_JSON)) - .build(); - - HttpResponse response = createResponse("/operation.json"); + HttpResponse response = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/operation.json")) + .build(); UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), TOKEN_RESPONSE, request, response).urlMaps(); @@ -128,74 +61,6 @@ public class UrlMapApiExpectTest extends BaseGoogleComputeEngineExpectTest { - - public static final String ZONES_URL_PREFIX = BASE_URL + "/party/zones"; - - public static final HttpRequest GET_ZONE_REQ = HttpRequest - .builder() - .method("GET") - .endpoint(ZONES_URL_PREFIX + "/us-central1-a") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static final HttpRequest LIST_ZONES_REQ = HttpRequest - .builder() - .method("GET") - .endpoint(ZONES_URL_PREFIX) - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static final HttpResponse LIST_ZONES_SHORT_RESPONSE = HttpResponse.builder().statusCode(200) - .payload(staticPayloadFromResource("/zone_list_short.json")).build(); - - public static final HttpResponse LIST_ZONES_RESPONSE = HttpResponse.builder().statusCode(200) - .payload(staticPayloadFromResource("/zone_list.json")).build(); - - public void testGetZoneResponseIs2xx() throws Exception { - - - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/zone_get.json")).build(); - - ZoneApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, GET_ZONE_REQ, response).zones(); - - assertEquals(api.get("us-central1-a"), - new ParseZoneTest().expected()); - } - - public void testGetZoneResponseIs4xx() throws Exception { - - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - ZoneApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, GET_ZONE_REQ, response).zones(); - - assertNull(api.get("us-central1-a")); - } - - public void list() throws Exception { - ZoneApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_ZONES_REQ, LIST_ZONES_RESPONSE).zones(); - - assertEquals(api.list().next(), new ParseZoneListTest().expected()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - ZoneApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_ZONES_REQ, response).zones(); - - assertFalse(api.list().hasNext()); - } -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ZoneApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ZoneApiMockTest.java new file mode 100644 index 0000000000..cefecf43ad --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ZoneApiMockTest.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.AssertJUnit.assertNull; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.parse.ParseZoneListTest; +import org.jclouds.googlecomputeengine.parse.ParseZoneTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "ZoneApiMockTest", singleThreaded = true) +public class ZoneApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception { + server.enqueue(jsonResponse("/zone_get.json")); + + assertEquals(zoneApi().get("us-central1-a"), + new ParseZoneTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/zones/us-central1-a"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(zoneApi().get("us-central1-a")); + assertSent(server, "GET", "/projects/party/zones/us-central1-a"); + } + + public void list() throws Exception { + server.enqueue(jsonResponse("/zone_list.json")); + + assertEquals(zoneApi().list().next(), new ParseZoneListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/zones"); + } + + public void list_empty() throws Exception { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(zoneApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/zones"); + } + + ZoneApi zoneApi() { + return api().zones(); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseTargetHttpProxyListTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseTargetHttpProxyListTest.java index d4abfca953..4c0a3d2f20 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseTargetHttpProxyListTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseTargetHttpProxyListTest.java @@ -38,19 +38,22 @@ public class ParseTargetHttpProxyListTest extends BaseGoogleComputeEngineParseTe return "/target_http_proxy_list.json"; } - @Override - @Consumes(MediaType.APPLICATION_JSON) + @Override @Consumes(MediaType.APPLICATION_JSON) public ListPage expected() { + return expected(BASE_URL); + } + + @Consumes(MediaType.APPLICATION_JSON) + public ListPage expected(String baseUrl) { return ForwardingListPage.create( ImmutableList.of( - new ParseTargetHttpProxyTest().expected(), + new ParseTargetHttpProxyTest().expected(baseUrl), TargetHttpProxy.create("13050421646334304116", // id new SimpleDateFormatDateService().iso8601DateParse("2012-11-25T01:38:48.306"), // creationTimestamp - URI.create("https://www.googleapis" + - ".com/compute/v1/projects/myproject/global/targetHttpProxies/jclouds-test-2"), // selfLink + URI.create(baseUrl + "/myproject/global/targetHttpProxies/jclouds-test-2"), // selfLink "jclouds-test-2", // name "Simple proxy", // description - URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps/jclouds-test-2"))), // urlMap + URI.create(baseUrl + "/myproject/global/urlMaps/jclouds-test-2"))), // urlMap null // nextPageToken d ); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseTargetHttpProxyTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseTargetHttpProxyTest.java index 802ad4692e..47ce80b457 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseTargetHttpProxyTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseTargetHttpProxyTest.java @@ -34,14 +34,18 @@ public class ParseTargetHttpProxyTest extends BaseGoogleComputeEngineParseTest expected() { + return expected(BASE_URL); + } + + @Consumes(MediaType.APPLICATION_JSON) + public ListPage expected(String baseUrl) { return ForwardingListPage.create( - ImmutableList.of(new ParseUrlMapTest().expected(), + ImmutableList.of(new ParseUrlMapTest().expected(baseUrl), UrlMap.create("13741966667737398120", // id new SimpleDateFormatDateService().iso8601DateParse("2014-07-23T12:39:50.022-07:00"), // creationTimestamp - URI.create("https://www.googleapis" + - ".com/compute/v1/projects/myproject/global/urlMaps/jclouds-test-2"), // selfLink + URI.create(baseUrl + "/myproject/global/urlMaps/jclouds-test-2"), // selfLink "jclouds-test-2", // name "Basic url map", // description null, // hostRules null, // pathMatchers null, // urlMapTests - URI.create("https://www.googleapis.com/compute/v1/projects/" - + "myproject/global/backendServices/jclouds-test"), // defaultService + URI.create(baseUrl + "/myproject/global/backendServices/jclouds-test"), // defaultService "EDqhvJucpz4=")), // fingerprint null); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseUrlMapTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseUrlMapTest.java index e29b8dd6d4..f266f6ddb7 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseUrlMapTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseUrlMapTest.java @@ -44,7 +44,12 @@ public class ParseUrlMapTest extends BaseGoogleComputeEngineParseTest { @Override @Consumes(MediaType.APPLICATION_JSON) public UrlMap expected() { - URI service = URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/jclouds-test"); + return expected(BASE_URL); + } + + @Consumes(MediaType.APPLICATION_JSON) + public UrlMap expected(String baseUrl) { + URI service = URI.create(baseUrl + "/myproject/global/backendServices/jclouds-test"); List hostRules = ImmutableList.of(HostRule.create(null, // description ImmutableList.of("jclouds-test"), // hosts @@ -64,7 +69,7 @@ public class ParseUrlMapTest extends BaseGoogleComputeEngineParseTest { return UrlMap.create("13741966667737398119", // id new SimpleDateFormatDateService().iso8601DateParse("2014-07-23T12:39:50.022-07:00"), // creationTimestamp - URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps/jclouds-test"), // selfLink + URI.create(baseUrl + "/myproject/global/urlMaps/jclouds-test"), // selfLink "jclouds-test", // name "Sample url map", // description hostRules, // hostRules diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseUrlMapValidateTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseUrlMapValidateTest.java index 20380c0008..06fdc6b9d8 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseUrlMapValidateTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseUrlMapValidateTest.java @@ -39,13 +39,18 @@ public class ParseUrlMapValidateTest extends BaseGoogleComputeEngineParseTest expected() { - Zone zone1 = new ParseZoneTest().expected(); + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public ListPage expected(String baseUrl) { + Zone zone1 = new ParseZoneTest().expected(baseUrl); Zone zone2 = Zone.create( // "13024414164050619686", // id parse("2012-10-24T20:13:19.271"), // creationTimestamp - URI.create(BASE_URL + "/party/zones/us-central1-b"), // selfLink + URI.create(baseUrl + "/party/zones/us-central1-b"), // selfLink "us-central1-b", // name "us-central1-b", // description Zone.Status.UP, // status diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseZoneTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseZoneTest.java index 2a472c9356..7bfd66c08e 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseZoneTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseZoneTest.java @@ -38,10 +38,15 @@ public class ParseZoneTest extends BaseGoogleComputeEngineParseTest { @Override @Consumes(APPLICATION_JSON) public Zone expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public Zone expected(String baseUrl) { return Zone.create( // "13020128040171887099", // id parse("2012-10-19T16:42:54.131"), // creationTimestamp - URI.create(BASE_URL + "/party/zones/us-central1-a"), // selfLink + URI.create(baseUrl + "/party/zones/us-central1-a"), // selfLink "us-central1-a", // name "us-central1-a", // description Zone.Status.DOWN, // status diff --git a/providers/google-compute-engine/src/test/resources/firewall_insert.json b/providers/google-compute-engine/src/test/resources/firewall_insert.json index 8742a10ad3..6d29ef8ce3 100644 --- a/providers/google-compute-engine/src/test/resources/firewall_insert.json +++ b/providers/google-compute-engine/src/test/resources/firewall_insert.json @@ -1 +1,22 @@ -{"name":"%s","network":"https://www.googleapis.com/compute/v1/projects/party/global/networks/%s","sourceRanges":[%s],"sourceTags":[%s],"targetTags":[%s],"allowed":[{"IPProtocol":"tcp","ports":[%s]}]} \ No newline at end of file +{ + "name": "myfw", + "network": "https://www.googleapis.com/compute/v1/projects/party/global/networks/default", + "sourceRanges": [ + "10.0.1.0/32" + ], + "sourceTags": [ + "tag1" + ], + "targetTags": [ + "tag2" + ], + "allowed": [ + { + "IPProtocol": "tcp", + "ports": [ + "22", + "23-24" + ] + } + ] +} \ No newline at end of file