New MockTests: TargetHttpProxyApi, UrlMap, ZoneApi, FirewallApi

This commit is contained in:
Daniel Broudy 2014-12-09 10:15:43 -08:00 committed by Ignasi Barrera
parent 3db5cef592
commit e4cc7282d7
16 changed files with 429 additions and 557 deletions

View File

@ -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<GoogleComputeEngineApi> {
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<String> sourceRanges,
List<String> sourceTags,
List<String> targetTags,
List<String> portRanges) throws IOException {
Function<String, String> addQuotes = new Function<String, String>() {
@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<Goo
.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")))
.payload(payloadFromResource("/firewall_insert.json"))
.build();
HttpResponse updateFirewallResponse = HttpResponse.builder().statusCode(200)

View File

@ -20,12 +20,18 @@ 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.domain.Firewall;
import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest;
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.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
@Test(groups = "unit", testName = "FirewallApiMockTest", singleThreaded = true)
public class FirewallApiMockTest extends BaseGoogleComputeEngineApiMockTest {
@ -75,6 +81,40 @@ public class FirewallApiMockTest extends BaseGoogleComputeEngineApiMockTest {
assertSent(server, "GET", "/projects/party/global/firewalls");
}
public void insert() throws Exception {
server.enqueue(jsonResponse("/operation.json"));
FirewallOptions options = new FirewallOptions()
.addAllowedRule(Firewall.Rule.create("tcp", ImmutableList.of("22", "23-24")))
.addSourceTag("tag1")
.addSourceRange("10.0.1.0/32")
.addTargetTag("tag2");
assertEquals(firewallApi().createInNetwork("myfw", URI.create(url("/projects/party/global/networks/default")), options),
new ParseOperationTest().expected(url("/projects")));
assertSent(server, "POST", "/projects/party/global/firewalls",
stringFromResource("/firewall_insert.json"));
}
public void update() throws Exception {
server.enqueue(jsonResponse("/operation.json"));
FirewallOptions options = new FirewallOptions()
.name("myfw")
.network(URI.create(url("/projects/party/global/networks/default")))
.addAllowedRule(Firewall.Rule.create("tcp", ImmutableList.of("22", "23-24")))
.addSourceTag("tag1")
.addSourceRange("10.0.1.0/32")
.addTargetTag("tag2");
assertEquals(firewallApi().update("myfw", options),
new ParseOperationTest().expected(url("/projects")));
assertSent(server, "PUT", "/projects/party/global/firewalls/myfw",
stringFromResource("/firewall_insert.json"));
}
FirewallApi firewallApi(){
return api().firewalls();
}

View File

@ -1,171 +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.parse.ParseOperationTest;
import org.jclouds.googlecomputeengine.parse.ParseTargetHttpProxyListTest;
import org.jclouds.googlecomputeengine.parse.ParseTargetHttpProxyTest;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
@Test(groups = "unit")
public class TargetHttpProxyApiExpectTest extends BaseGoogleComputeEngineExpectTest<GoogleComputeEngineApi> {
private static final String ENDPOINT_BASE = "https://www.googleapis.com/"
+ "compute/v1/projects/party/global/targetHttpProxies";
private org.jclouds.http.HttpRequest.Builder<? extends 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());
}
}

View File

@ -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();
}
}

View File

@ -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<GoogleComputeEngineApi> {
private static final String ENDPOINT_BASE = "https://www.googleapis.com/"
+ "compute/v1/projects/party/global/urlMaps";
private org.jclouds.http.HttpRequest.Builder<? extends 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<Googl
assertEquals(api.patch("jclouds-test", createBasicMap()), new ParseOperationTest().expected());
}
public void testDeleteUrlMapResponseIs2xx() {
HttpRequest request = getBasicRequest().method("DELETE")
.endpoint(ENDPOINT_BASE + "/jclouds-test")
.build();
HttpResponse response = createResponse("/operation.json");
UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE),
TOKEN_RESPONSE, request, response).urlMaps();
assertEquals(api.delete("jclouds-test"), new ParseOperationTest().expected());
}
public void testDeleteUrlMapResponseIs4xx() {
HttpRequest request = getBasicRequest().method("DELETE")
.endpoint(ENDPOINT_BASE + "/jclouds-test")
.build();
HttpResponse response = HttpResponse.builder().statusCode(404).build();
UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE),
TOKEN_RESPONSE, request, response).urlMaps();
assertNull(api.delete("jclouds-test"));
}
HttpRequest list = HttpRequest
.builder()
.method("GET")
.endpoint(ENDPOINT_BASE)
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + TOKEN).build();
public void list() {
HttpResponse response = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/url_map_list.json")).build();
UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE),
TOKEN_RESPONSE, list, response).urlMaps();
assertEquals(api.list().next(), new ParseUrlMapListTest().expected());
}
public void listEmpty() {
HttpResponse response = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/list_empty.json")).build();
UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE),
TOKEN_RESPONSE, list, response).urlMaps();
assertFalse(api.list().hasNext());
}
public void testValidateUrlMapsResponseIs2xx() {
HttpRequest request = getBasicRequest().method("POST")
.endpoint(ENDPOINT_BASE + "/jclouds-test/validate")
.payload(payloadFromResourceWithContentType("/url_map_validate_request.json",
MediaType.APPLICATION_JSON))
.build();
HttpResponse response = createResponse("/url_map_validate.json");
UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE),
TOKEN_RESPONSE, request, response).urlMaps();
assertEquals(api.validate("jclouds-test", createBasicMap()), new ParseUrlMapValidateTest().expected());
}
private UrlMapOptions createBasicMap() {
URI service = URI.create("https://www.googleapis.com/compute/v1/projects/"
+ "myproject/global/backendServices/jclouds-test");

View File

@ -0,0 +1,140 @@
/*
* 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.domain.UrlMap;
import org.jclouds.googlecomputeengine.domain.UrlMap.HostRule;
import org.jclouds.googlecomputeengine.domain.UrlMap.PathMatcher;
import org.jclouds.googlecomputeengine.domain.UrlMap.PathMatcher.PathRule;
import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest;
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.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
@Test(groups = "unit", testName = "UrlMapApiMockTest", singleThreaded = true)
public class UrlMapApiMockTest extends BaseGoogleComputeEngineApiMockTest {
public void get() throws Exception {
server.enqueue(jsonResponse("/url_map_get.json"));
assertEquals(urlMapApi().get("jclouds-test"), new ParseUrlMapTest().expected(url("/projects")));
assertSent(server, "GET", "/projects/party/global/urlMaps/jclouds-test");
}
public void get_4xx() throws Exception {
server.enqueue(response404());
assertNull(urlMapApi().get("jclouds-test"));
assertSent(server, "GET", "/projects/party/global/urlMaps/jclouds-test");
}
public void insert() throws Exception {
server.enqueue(jsonResponse("/operation.json"));
assertEquals(urlMapApi().create(createBasicMap()), new ParseOperationTest().expected(url("/projects")));
assertSent(server, "POST", "/projects/party/global/urlMaps",
stringFromResource("/url_map_insert.json"));
}
public void update() throws Exception {
server.enqueue(jsonResponse("/operation.json"));
assertEquals(urlMapApi().update("jclouds-test", createBasicMap()),
new ParseOperationTest().expected(url("/projects")));
assertSent(server, "PUT", "/projects/party/global/urlMaps/jclouds-test",
stringFromResource("/url_map_insert.json"));
}
/*
public void patch() throws Exception {
// PATCH not yet supported
server.enqueue(jsonResponse("/operation.json"));
assertEquals(urlMapApi().patch("jclouds-test", createBasicMap()),
new ParseOperationTest().expected(url("/projects")));
assertSent(server, "PATCH", "/projects/party/global/urlMaps/jclouds-test",
stringFromResource("/url_map_insert.json"));
}
*/
public void delete() throws Exception {
server.enqueue(jsonResponse("/operation.json"));
assertEquals(urlMapApi().delete("jclouds-test"),
new ParseOperationTest().expected(url("/projects")));
assertSent(server, "DELETE", "/projects/party/global/urlMaps/jclouds-test");
}
public void delete_4xx() throws Exception {
server.enqueue(response404());
assertNull(urlMapApi().delete("jclouds-test"));
assertSent(server, "DELETE", "/projects/party/global/urlMaps/jclouds-test");
}
public void list() throws Exception {
server.enqueue(jsonResponse("/url_map_list.json"));
assertEquals(urlMapApi().list().next(),
new ParseUrlMapListTest().expected(url("/projects")));
assertSent(server, "GET", "/projects/party/global/urlMaps");
}
public void list_empty() throws Exception {
server.enqueue(jsonResponse("/list_empty.json"));
assertFalse(urlMapApi().list().hasNext());
assertSent(server, "GET", "/projects/party/global/urlMaps");
}
public void validate() throws Exception {
server.enqueue(jsonResponse("/url_map_validate.json"));
assertEquals(urlMapApi().validate("jclouds-test", createBasicMap()),
new ParseUrlMapValidateTest().expected(url("/projects")));
assertSent(server, "POST", "/projects/party/global/urlMaps/jclouds-test/validate");
}
private UrlMapOptions createBasicMap() {
URI service = URI.create(url("/projects/myproject/global/backendServices/jclouds-test"));
return new UrlMapOptions().name("jclouds-test")
.description("Sample url map")
.hostRule(HostRule.create(null, ImmutableList.of("jclouds-test"), "path"))
.pathMatcher(PathMatcher.create("path",
null,
service,
ImmutableList.of(
PathRule.create(ImmutableList.of("/"),
service))))
.test(UrlMap.UrlMapTest.create(null, "jclouds-test", "/test/path", service))
.defaultService(service);
}
UrlMapApi urlMapApi() {
return api().urlMaps();
}
}

View File

@ -1,95 +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.ParseZoneListTest;
import org.jclouds.googlecomputeengine.parse.ParseZoneTest;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
@Test(groups = "unit", testName = "ZoneApiExpectTest")
public class ZoneApiExpectTest extends BaseGoogleComputeEngineExpectTest<GoogleComputeEngineApi> {
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());
}
}

View File

@ -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();
}
}

View File

@ -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<TargetHttpProxy> expected() {
return expected(BASE_URL);
}
@Consumes(MediaType.APPLICATION_JSON)
public ListPage<TargetHttpProxy> 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
);
}

View File

@ -34,14 +34,18 @@ public class ParseTargetHttpProxyTest extends BaseGoogleComputeEngineParseTest<T
return "/target_http_proxy_get.json";
}
@Override
@Consumes(MediaType.APPLICATION_JSON)
@Override @Consumes(MediaType.APPLICATION_JSON)
public TargetHttpProxy expected() {
return expected(BASE_URL);
}
@Consumes(MediaType.APPLICATION_JSON)
public TargetHttpProxy expected(String baseUrl) {
return TargetHttpProxy.create("13050421646334304115", // 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"), // selfLink
URI.create(baseUrl + "/myproject/global/targetHttpProxies/jclouds-test"), // selfLink
"jclouds-test", // name
null,
URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps/jclouds-test")); // urlMap
URI.create(baseUrl + "/myproject/global/urlMaps/jclouds-test")); // urlMap
}
}

View File

@ -41,19 +41,22 @@ public class ParseUrlMapListTest extends BaseGoogleComputeEngineParseTest<ListPa
@Override
@Consumes(MediaType.APPLICATION_JSON)
public ListPage<UrlMap> expected() {
return expected(BASE_URL);
}
@Consumes(MediaType.APPLICATION_JSON)
public ListPage<UrlMap> 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);
}

View File

@ -44,7 +44,12 @@ public class ParseUrlMapTest extends BaseGoogleComputeEngineParseTest<UrlMap> {
@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<HostRule> hostRules = ImmutableList.of(HostRule.create(null, // description
ImmutableList.of("jclouds-test"), // hosts
@ -64,7 +69,7 @@ public class ParseUrlMapTest extends BaseGoogleComputeEngineParseTest<UrlMap> {
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

View File

@ -39,13 +39,18 @@ public class ParseUrlMapValidateTest extends BaseGoogleComputeEngineParseTest<Ur
@Override
@Consumes(APPLICATION_JSON)
public UrlMapValidateResult expected() {
return expected(BASE_URL);
}
@Consumes(APPLICATION_JSON)
public UrlMapValidateResult expected(String baseUrl) {
return UrlMapValidateResult.create(false, // loadSucceded
ImmutableList.of("jclouds-test"), // loadError
false, // testPassed
ImmutableList.of(TestFailure.create("jclouds-test", // host
"/test/path", // path
URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/jclouds-test"), // expectedService
URI.create("https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/jclouds-test-2") //actualService
URI.create(baseUrl + "/myproject/global/backendServices/jclouds-test"), // expectedService
URI.create(baseUrl + "/myproject/global/backendServices/jclouds-test-2") //actualService
))); //testFailures)
}
}

View File

@ -40,11 +40,16 @@ public class ParseZoneListTest extends BaseGoogleComputeEngineParseTest<ListPage
@Override @Consumes(APPLICATION_JSON)
public ListPage<Zone> expected() {
Zone zone1 = new ParseZoneTest().expected();
return expected(BASE_URL);
}
@Consumes(APPLICATION_JSON)
public ListPage<Zone> 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

View File

@ -38,10 +38,15 @@ public class ParseZoneTest extends BaseGoogleComputeEngineParseTest<Zone> {
@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

View File

@ -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]}]}
{
"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"
]
}
]
}