MockTests now support PATCH semantics

This commit is contained in:
Daniel Broudy 2014-12-15 14:59:50 -08:00 committed by Ignasi Barrera
parent e4cc7282d7
commit 4772587722
10 changed files with 57 additions and 277 deletions

View File

@ -98,6 +98,12 @@
<version>${jclouds.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-okhttp</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>

View File

@ -1,73 +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 javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.net.URI;
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.ParseOperationTest;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
@Test(groups = "unit")
public class BackendServiceApiExpectTest extends BaseGoogleComputeEngineExpectTest<GoogleComputeEngineApi> {
private static final String ENDPOINT_BASE = "https://www.googleapis.com/"
+ "compute/v1/projects/party/global/backendServices";
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 testPatchBackendServiceResponseIs2xx() throws IOException {
HttpRequest request = getBasicRequest().method("PATCH")
.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<URI> healthChecks = ImmutableList.of(URI.create("https://www.googleapis.com/compute/v1/projects/"
+ "myproject/global/httpHealthChecks/jclouds-test"));
assertEquals(api.patch("jclouds-test", new BackendServiceOptions().name("jclouds-test")
.protocol("HTTP")
.port(80)
.timeoutSec(30)
.healthChecks(healthChecks)),
new ParseOperationTest().expected());
}
}

View File

@ -85,6 +85,23 @@ public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTes
stringFromResource("/backend_service_insert.json"));
}
public void patch() throws Exception {
server.enqueue(jsonResponse("/operation.json"));
List<URI> healthChecks = ImmutableList.of(URI.create(url("/projects/"
+ "myproject/global/httpHealthChecks/jclouds-test")));
assertEquals(backendServiceApi().patch("jclouds-test",
new BackendServiceOptions().name("jclouds-test")
.protocol("HTTP")
.port(80)
.timeoutSec(30)
.healthChecks(healthChecks)),
new ParseOperationTest().expected(url("/projects")));
assertSent(server, "PATCH", "/projects/party/global/backendServices/jclouds-test",
stringFromResource("/backend_service_insert.json"));
}
public void delete() throws Exception {
server.enqueue(jsonResponse("/operation.json"));
@ -102,7 +119,6 @@ public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTes
assertSent(server, "DELETE", "/projects/party/global/backendServices/jclouds-test");
}
public void list() throws Exception {
server.enqueue(jsonResponse("/backend_service_list.json"));

View File

@ -1,64 +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 java.io.IOException;
import java.net.URI;
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.ParseOperationTest;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
@Test(groups = "unit", testName = "FirewallApiExpectTest")
public class FirewallApiExpectTest extends BaseGoogleComputeEngineExpectTest<GoogleComputeEngineApi> {
public void testPatchFirewallResponseIs2xx() throws IOException {
HttpRequest update = HttpRequest
.builder()
.method("PATCH")
.endpoint(BASE_URL + "/party/global/firewalls/myfw")
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + TOKEN)
.payload(payloadFromResource("/firewall_insert.json"))
.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.patch("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());
}
}

View File

@ -115,6 +115,24 @@ public class FirewallApiMockTest extends BaseGoogleComputeEngineApiMockTest {
stringFromResource("/firewall_insert.json"));
}
public void patch() 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().patch("myfw", options),
new ParseOperationTest().expected(url("/projects")));
assertSent(server, "PATCH", "/projects/party/global/firewalls/myfw",
stringFromResource("/firewall_insert.json"));
}
FirewallApi firewallApi(){
return api().firewalls();
}

View File

@ -1,55 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.googlecomputeengine.features;
import static org.testng.Assert.assertEquals;
import javax.ws.rs.core.MediaType;
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.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
@Test(groups = "unit", testName = "HttpHealthCheckApiExpectTest")
public class HttpHealthCheckApiExpectTest extends BaseGoogleComputeEngineExpectTest<GoogleComputeEngineApi> {
public void testPatchHttpHealthChecksResponseIs2xx() {
String healthCheckName = "http-health-check";
HttpRequest patch = HttpRequest
.builder()
.method("PATCH")
.endpoint(BASE_URL + "/party/global/httpHealthChecks/" + healthCheckName)
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + TOKEN)
.payload(payloadFromResourceWithContentType("/httphealthcheck_patch.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.Builder().timeoutSec(0).unhealthyThreshold(0).buildNoDefaults();
assertEquals(api.patch(healthCheckName, options), new ParseGlobalOperationTest().expected());
}
}

View File

@ -87,6 +87,18 @@ public class HttpHealthCheckApiMockTest extends BaseGoogleComputeEngineApiMockTe
stringFromResource("/httphealthcheck_insert.json"));
}
public void patch() throws Exception {
server.enqueue(jsonResponse("/global_operation.json"));
HttpHealthCheckCreationOptions options = new HttpHealthCheckCreationOptions.Builder()
.timeoutSec(0).unhealthyThreshold(0).buildNoDefaults();
assertEquals(httpHealthCheckApi().patch("http-health-check", options),
new ParseGlobalOperationTest().expected(url("/projects")));
assertSent(server, "PATCH", "/projects/party/global/httpHealthChecks/http-health-check",
stringFromResource("/httphealthcheck_patch.json"));
}
public void list() throws Exception {
server.enqueue(jsonResponse("/httphealthcheck_list.json"));

View File

@ -1,79 +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 java.io.IOException;
import java.net.URI;
import javax.ws.rs.core.MediaType;
import org.jclouds.googlecomputeengine.GoogleComputeEngineApi;
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.BaseGoogleComputeEngineExpectTest;
import org.jclouds.googlecomputeengine.options.UrlMapOptions;
import org.jclouds.googlecomputeengine.parse.ParseOperationTest;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
@Test(groups = "unit")
public class UrlMapApiExpectTest extends BaseGoogleComputeEngineExpectTest<GoogleComputeEngineApi> {
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();
HttpResponse response = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/operation.json"))
.build();
UrlMapApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE),
TOKEN_RESPONSE, request, response).urlMaps();
assertEquals(api.patch("jclouds-test", createBasicMap()), new ParseOperationTest().expected());
}
private UrlMapOptions createBasicMap() {
URI service = URI.create("https://www.googleapis.com/compute/v1/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);
}
}

View File

@ -70,9 +70,7 @@ public class UrlMapApiMockTest extends BaseGoogleComputeEngineApiMockTest {
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()),
@ -80,7 +78,7 @@ public class UrlMapApiMockTest extends BaseGoogleComputeEngineApiMockTest {
assertSent(server, "PATCH", "/projects/party/global/urlMaps/jclouds-test",
stringFromResource("/url_map_insert.json"));
}
*/
public void delete() throws Exception {
server.enqueue(jsonResponse("/operation.json"));

View File

@ -36,6 +36,7 @@ import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.okhttp.config.OkHttpCommandExecutorServiceModule;
import org.jclouds.googlecomputeengine.GoogleComputeEngineApi;
import org.jclouds.googlecomputeengine.GoogleComputeEngineProviderMetadata;
import org.testng.annotations.AfterMethod;
@ -49,7 +50,7 @@ import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
/**
* Tests need to run {@code singleThreaded = true) as otherwise tests will clash on the server field.
* Tests need to run {@code singleThreaded = true} as otherwise tests will clash on the server field.
* Sharing the server field means less code to write.
*/
public class BaseGoogleComputeEngineApiMockTest {
@ -80,7 +81,7 @@ public class BaseGoogleComputeEngineApiMockTest {
}
private final Set<Module> modules = ImmutableSet
.of(new ExecutorServiceModule(sameThreadExecutor()), GoogleComputeEngineTestModule.INSTANCE);
.of(new ExecutorServiceModule(sameThreadExecutor()), GoogleComputeEngineTestModule.INSTANCE, new OkHttpCommandExecutorServiceModule());
final AtomicInteger suffix = new AtomicInteger();