From a9eecfad818eaddbf3048e44b5cd6567f10602f8 Mon Sep 17 00:00:00 2001 From: Daniel Broudy Date: Tue, 27 Jan 2015 16:47:16 -0800 Subject: [PATCH] Updated BackendServiceOptions to AutoValue + Builder --- .../options/BackendServiceOptions.java | 241 ++++++++---------- .../features/BackendServiceApiLiveTest.java | 29 +-- .../features/BackendServiceApiMockTest.java | 12 +- .../GlobalForwardingRuleApiLiveTest.java | 3 +- .../features/TargetHttpProxyApiLiveTest.java | 4 +- .../features/UrlMapApiLiveTest.java | 3 +- 6 files changed, 135 insertions(+), 157 deletions(-) diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/options/BackendServiceOptions.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/options/BackendServiceOptions.java index 60f1e4f102..0257882acc 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/options/BackendServiceOptions.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/options/BackendServiceOptions.java @@ -21,154 +21,135 @@ import java.util.List; import org.jclouds.googlecomputeengine.domain.BackendService.Backend; import org.jclouds.javax.annotation.Nullable; +import org.jclouds.json.SerializedNames; -public class BackendServiceOptions { +import com.google.auto.value.AutoValue; - private String name; - @Nullable private String description; - private List healthChecks; - private List backends; - private Integer timeoutSec; - private Integer port; - private String protocol; - private String fingerprint; - private String portName; +@AutoValue +public abstract class BackendServiceOptions { - /** - * Name of the BackendService resource. - * @return name, provided by the client. - */ - public String getName(){ - return name; + @Nullable public abstract String name(); + @Nullable public abstract String description(); + @Nullable public abstract List healthChecks(); + @Nullable public abstract List backends(); + @Nullable public abstract Integer timeoutSec(); + @Nullable public abstract Integer port(); + @Nullable public abstract String protocol(); + @Nullable public abstract String fingerprint(); + @Nullable public abstract String portName(); + + @SerializedNames({"name", "description", "healthChecks", "backends", "timeoutSec", + "port", "protocol", "fingerprint", "portName"}) + static BackendServiceOptions create(String name, String description, List healthChecks, + List backends, Integer timeoutSec, Integer port, String protocol, String fingerprint, String portName){ + return new AutoValue_BackendServiceOptions(name, description, healthChecks, + backends, timeoutSec, port, protocol, fingerprint, portName); } - /** - * @see BackendServiceOptions#getName() - */ - public BackendServiceOptions name(String name) { - this.name = name; - return this; + BackendServiceOptions(){ } - /** - * An optional textual description of the BackendService. - * @return description, provided by the client. - */ - public String getDescription(){ - return description; - } + public static class Builder { - /** - * @see BackendServiceOptions#getDescription() - */ - public BackendServiceOptions description(String description) { - this.description = description; - return this; - } + private String name; + private String description; + private List healthChecks; + private List backends; + private Integer timeoutSec; + private Integer port; + private String protocol; + private String fingerprint; + private String portName; - /** - * The list of {@link HttpHealthCheck#selfLink Links} to the HttpHealthCheck resource for health checking this BackendService. - * Currently at most one health check can be specified, and a health check is required. - */ - public List getHealthChecks() { - return healthChecks; - } + /** + * @param name, provided by the client. + * @param healthChecks The list of {@link HttpHealthCheck#selfLink Links} to the HttpHealthCheck + * resource for health checking this BackendService. + * Currently at most one health check can be specified, and a health check is required. + */ + public Builder(String name, List healthChecks){ + this.name = name; + this.healthChecks = healthChecks; + } - /** - * @see BackendServiceOptions#getHealthChecks() - */ - public BackendServiceOptions healthChecks(List healthChecks) { - this.healthChecks = healthChecks; - return this; - } + /** + * Empty builder for use when patching or updating and existing BackendService + * Otherwise use the other {@link #Builder(String, List) builder} + */ + public Builder(){ + } - /** - * The list of backends that serve this BackendService. - */ - public List getBackends() { - return backends; - } + /** + * An optional textual description of the BackendService. + */ + public Builder description(String description){ + this.description = description; + return this; + } - /** - * @see BackendServiceOptions#getBackends() - */ - public BackendServiceOptions backends(List backends){ - this.backends = backends; - return this; - } + /** + * HealthChecks - The list of {@link HttpHealthCheck#selfLink Links} to the HttpHealthCheck + * resource for health checking this BackendService. + * Currently at most one health check can be specified, and a health check is required. + */ + public Builder healthChecks(List healthChecks){ + this.healthChecks = healthChecks; + return this; + } - /** - * How many seconds to wait for the backend before considering it a failed request. - * Default is 30 seconds. - */ - public Integer getTimeoutSec() { - return timeoutSec; - } + /** + * The list of backends that serve this BackendService. + */ + public Builder backends(List backends){ + this.backends = backends; + return this; + } - /** - * @see BackendServiceOptions#getTimeoutSec() - */ - public BackendServiceOptions timeoutSec(Integer timeoutSec) { - this.timeoutSec = timeoutSec; - return this; - } + /** + * How many seconds to wait for the backend before considering it a failed request. + * Default is 30 seconds. + */ + public Builder timeoutSec(Integer timeoutSec) { + this.timeoutSec = timeoutSec; + return this; + } - /** - * The TCP port to connect on the backend. - * The default value is 80. - */ - public Integer getPort() { - return port; - } + /** + * The TCP port to connect on the backend. + * The default value is 80. + */ + public Builder port(Integer port) { + this.port = port; + return this; + } - /** - * @see BackendServiceOptions#getPort() - */ - public BackendServiceOptions port(Integer port) { - this.port = port; - return this; - } + /** + * The protocol for incoming requests. + */ + public Builder protocol(String protocol) { + this.protocol = protocol; + return this; + } + /** + * Fingerprint of this resource. A hash of the contents stored in this object. + * This field is used in optimistic locking. This field will be ignored when + * inserting a BackendService. An up-to-date fingerprint must be provided in + * order to update the BackendService. + */ + public Builder fingerprint(String fingerprint) { + this.fingerprint = fingerprint; + return this; + } - /** - * The protocol for incoming requests. - */ - public String getProtocol() { - return protocol; - } + public Builder portName(String portName) { + this.portName = portName; + return this; + } - /** - * @see BackendServiceOptions#getProtocol() - */ - public BackendServiceOptions protocol(String protocol) { - this.protocol = protocol; - return this; - } - - /** - * Fingerprint of this resource. A hash of the contents stored in this object. - * This field is used in optimistic locking. This field will be ignored when - * inserting a BackendService. An up-to-date fingerprint must be provided in - * order to update the BackendService. - */ - public String getFingerprint() { - return fingerprint; - } - - /** - * @see BackendServiceOptions#getFingerprint() - */ - public BackendServiceOptions fingerprint(String fingerprint) { - this.fingerprint = fingerprint; - return this; - } - - public String getPortName() { - return portName; - } - - public BackendServiceOptions portName(String portName) { - this.portName = portName; - return this; + public BackendServiceOptions build(){ + return create(name, description, healthChecks, + backends, timeoutSec, port, protocol, fingerprint, portName); + } } } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiLiveTest.java index c5eeee6754..9ed6ceb696 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiLiveTest.java @@ -47,7 +47,8 @@ public class BackendServiceApiLiveTest extends BaseGoogleComputeEngineApiLiveTes assertOperationDoneSuccessfully(api.httpHeathChecks().insert(BACKEND_SERVICE_HEALTH_CHECK_NAME)); List healthChecks = ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME)); - BackendServiceOptions b = new BackendServiceOptions().name(BACKEND_SERVICE_NAME).healthChecks(healthChecks); + + BackendServiceOptions b = new BackendServiceOptions.Builder(BACKEND_SERVICE_NAME, healthChecks).build(); assertOperationDoneSuccessfully(api().create(b)); } @@ -61,11 +62,10 @@ public class BackendServiceApiLiveTest extends BaseGoogleComputeEngineApiLiveTes @Test(groups = "live", dependsOnMethods = "testGetBackendService") public void testPatchBackendService() { String fingerprint = api().get(BACKEND_SERVICE_NAME).fingerprint(); - BackendServiceOptions backendService = new BackendServiceOptions() - .name(BACKEND_SERVICE_NAME) - .healthChecks(ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME))) + BackendServiceOptions backendService = new BackendServiceOptions.Builder(BACKEND_SERVICE_NAME, ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME))) .timeoutSec(10) - .fingerprint(fingerprint); + .fingerprint(fingerprint) + .build(); assertOperationDoneSuccessfully(api().update(BACKEND_SERVICE_NAME, backendService)); assertBackendServiceEquals(api().get(BACKEND_SERVICE_NAME), backendService); @@ -75,12 +75,11 @@ public class BackendServiceApiLiveTest extends BaseGoogleComputeEngineApiLiveTes public void testUpdateBackendService() { String fingerprint = api().get(BACKEND_SERVICE_NAME).fingerprint(); - BackendServiceOptions backendService = new BackendServiceOptions() - .name(BACKEND_SERVICE_NAME) - .healthChecks(ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME))) + BackendServiceOptions backendService = new BackendServiceOptions.Builder(BACKEND_SERVICE_NAME, ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME))) .timeoutSec(45) .port(8080) - .fingerprint(fingerprint); + .fingerprint(fingerprint) + .build(); assertOperationDoneSuccessfully(api().update(BACKEND_SERVICE_NAME, backendService)); assertBackendServiceEquals(api().get(BACKEND_SERVICE_NAME), @@ -119,13 +118,13 @@ public class BackendServiceApiLiveTest extends BaseGoogleComputeEngineApiLiveTes } private void assertBackendServiceEquals(BackendService result, BackendServiceOptions expected) { - assertEquals(result.name(), expected.getName()); - assertEquals(result.healthChecks(), expected.getHealthChecks()); - if (expected.getTimeoutSec() != null) { - org.testng.Assert.assertEquals(result.timeoutSec(), expected.getTimeoutSec().intValue()); + assertEquals(result.name(), expected.name()); + assertEquals(result.healthChecks(), expected.healthChecks()); + if (expected.timeoutSec() != null) { + org.testng.Assert.assertEquals(result.timeoutSec(), expected.timeoutSec().intValue()); } - if (expected.getPort() != null) { - org.testng.Assert.assertEquals(result.port(), expected.getPort().intValue()); + if (expected.port() != null) { + org.testng.Assert.assertEquals(result.port(), expected.port().intValue()); } } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiMockTest.java index 1cc8400478..fd9a639003 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiMockTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/BackendServiceApiMockTest.java @@ -57,11 +57,11 @@ public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTes List healthChecks = ImmutableList.of(URI.create(url("/projects/" + "myproject/global/httpHealthChecks/jclouds-test"))); - assertEquals(backendServiceApi().create( new BackendServiceOptions().name("jclouds-test") + assertEquals(backendServiceApi().create( new BackendServiceOptions.Builder("jclouds-test", healthChecks) .protocol("HTTP") .port(80) .timeoutSec(30) - .healthChecks(healthChecks)), + .build()), new ParseOperationTest().expected(url("/projects"))); assertSent(server, "POST", "/projects/party/global/backendServices", @@ -75,11 +75,11 @@ public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTes + "myproject/global/httpHealthChecks/jclouds-test"))); assertEquals(backendServiceApi().update("jclouds-test", - new BackendServiceOptions().name("jclouds-test") + new BackendServiceOptions.Builder("jclouds-test", healthChecks) .protocol("HTTP") .port(80) .timeoutSec(30) - .healthChecks(healthChecks)), + .build()), new ParseOperationTest().expected(url("/projects"))); assertSent(server, "PUT", "/projects/party/global/backendServices/jclouds-test", stringFromResource("/backend_service_insert.json")); @@ -92,11 +92,11 @@ public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTes + "myproject/global/httpHealthChecks/jclouds-test"))); assertEquals(backendServiceApi().patch("jclouds-test", - new BackendServiceOptions().name("jclouds-test") + new BackendServiceOptions.Builder("jclouds-test", healthChecks) .protocol("HTTP") .port(80) .timeoutSec(30) - .healthChecks(healthChecks)), + .build()), new ParseOperationTest().expected(url("/projects"))); assertSent(server, "PATCH", "/projects/party/global/backendServices/jclouds-test", stringFromResource("/backend_service_insert.json")); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiLiveTest.java index a3656a0c43..3aa012b317 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiLiveTest.java @@ -61,8 +61,7 @@ public class GlobalForwardingRuleApiLiveTest extends BaseGoogleComputeEngineApiL List healthChecks = ImmutableList.of(getHealthCheckUrl(GLOBAL_FORWARDING_RULE_HEALTH_CHECK_NAME)); - BackendServiceOptions b = new BackendServiceOptions().name(GLOBAL_FORWARDING_RULE_BACKEND_SERVICE_NAME) - .healthChecks(healthChecks); + BackendServiceOptions b = new BackendServiceOptions.Builder(GLOBAL_FORWARDING_RULE_BACKEND_SERVICE_NAME, healthChecks).build(); assertOperationDoneSuccessfully(api.backendServices() .create(b)); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiLiveTest.java index ef3abaeb43..be51627940 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetHttpProxyApiLiveTest.java @@ -52,8 +52,8 @@ public class TargetHttpProxyApiLiveTest extends BaseGoogleComputeEngineApiLiveTe assertOperationDoneSuccessfully(api.httpHeathChecks().insert(HEALTH_CHECK_NAME)); List healthChecks = ImmutableList.of(getHealthCheckUrl(HEALTH_CHECK_NAME)); - BackendServiceOptions b = new BackendServiceOptions().name(URL_MAP_DEFAULT_SERVICE_NAME) - .healthChecks(healthChecks); + BackendServiceOptions b = new BackendServiceOptions.Builder(URL_MAP_DEFAULT_SERVICE_NAME, healthChecks) + .build(); assertOperationDoneSuccessfully(api.backendServices().create(b)); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiLiveTest.java index f48e44a4d0..c5df2ffaa2 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/UrlMapApiLiveTest.java @@ -54,8 +54,7 @@ public class UrlMapApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { assertOperationDoneSuccessfully(api.httpHeathChecks().insert(HEALTH_CHECK_NAME)); List healthChecks = ImmutableList.of(getHealthCheckUrl(HEALTH_CHECK_NAME)); - BackendServiceOptions b = new BackendServiceOptions().name(URL_MAP_BACKEND_SERVICE_NAME) - .healthChecks(healthChecks); + BackendServiceOptions b = new BackendServiceOptions.Builder(URL_MAP_BACKEND_SERVICE_NAME, healthChecks).build(); assertOperationDoneSuccessfully(api.backendServices().create(b)); UrlMapOptions map = new UrlMapOptions.Builder().name(URL_MAP_NAME).description("simple url map")