Cleanup output-only Location object.
Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/f4af63fb Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/f4af63fb Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/f4af63fb Branch: refs/heads/master Commit: f4af63fb433a0b52cfc035aec89decdba764fd16 Parents: da40d63 Author: Adrian Cole <[email protected]> Authored: Mon Oct 20 08:45:21 2014 -0400 Committer: Adrian Cole <[email protected]> Committed: Mon Oct 20 13:26:54 2014 -0400 ---------------------------------------------------------------------- .../jclouds/azurecompute/domain/Location.java | 140 ++++++------------- .../azurecompute/xml/ListLocationsHandler.java | 35 ++--- .../azurecompute/xml/LocationHandler.java | 41 +++--- .../azurecompute/features/DiskApiLiveTest.java | 2 +- .../features/HostedServiceApiLiveTest.java | 2 +- .../azurecompute/features/ImageApiLiveTest.java | 2 +- .../features/LocationApiLiveTest.java | 34 ++--- .../features/LocationApiMockTest.java | 4 +- .../azurecompute/parse/ListLocationsTest.java | 79 ----------- .../xml/ListLocationsHandlerTest.java | 52 +++++++ 10 files changed, 141 insertions(+), 250 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Location.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Location.java b/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Location.java index 2b51a58..0949d46 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Location.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Location.java @@ -16,135 +16,73 @@ */ package org.jclouds.azurecompute.domain; -import com.google.common.base.MoreObjects; -import com.google.common.base.Objects; -import com.google.common.collect.ImmutableSet; -import java.util.Set; - +import static com.google.common.base.Objects.equal; import static com.google.common.base.Preconditions.checkNotNull; -/** - * - * A geographical region in which a service or storage account will be hosted. - * - * @see <a href="http://msdn.microsoft.com/en-us/library/gg441293" >api</a> - */ -public class Location { - public static Builder builder() { - return new Builder(); - } - - public Builder toBuilder() { - return builder().fromLocation(this); - } - - public static class Builder { - - private String name; - private String displayName; - private ImmutableSet.Builder<String> availableServices = ImmutableSet.<String> builder(); +import java.util.List; - /** - * @see Location#getName() - */ - public Builder name(String name) { - this.name = name; - return this; - } - - /** - * @see Location#getDisplayName() - */ - public Builder displayName(String displayName) { - this.displayName = displayName; - return this; - } - - /** - * @see Location#getAvailableServices() - */ - public Builder addAvailableService(String availableService) { - this.availableServices.add(checkNotNull(availableService, "availableService")); - return this; - } - - /** - * @see Location#getAvailableServices() - */ - public Builder availableServices(Iterable<String> availableServices) { - this.availableServices = ImmutableSet.<String> builder().addAll( - checkNotNull(availableServices, "availableServices")); - return this; - } +import com.google.common.base.Objects; - public Location build() { - return new Location(name, displayName, availableServices.build()); - } +/** A data center location that is valid for your subscription. */ +public final class Location { - public Builder fromLocation(Location in) { - return this.name(in.getName()).displayName(in.getDisplayName()).availableServices(in.getAvailableServices()); - } + /** The name of the data center location. Ex. {@code West Europe}. */ + public String name() { + return name; } - private final String name; - private final String displayName; - private final Set<String> availableServices; - - protected Location(String name, String displayName, Iterable<String> availableServices) { - this.name = checkNotNull(name, "name"); - this.displayName = checkNotNull(displayName, "displayName for %s", name); - this.availableServices = ImmutableSet.copyOf(checkNotNull(availableServices, "availableServices for %s", name)); + /** The localized name of the data center location. */ + public String displayName() { + return displayName; } - /** - * - * The name of a data center location that is valid for your subscription. For example: - * {@code West Europe} - */ - public String getName() { - return name; + /** Indicates the services available at this location. Ex. {@code Compute}. */ + public List<String> availableServices() { + return availableServices; } - /** - * The localized name of data center location. - */ - public String getDisplayName() { - return displayName; + public static Location create(String name, String displayName, List<String> availableServices) { + return new Location(name, displayName, availableServices); } - /** - * Indicates the services available at a location. - * - * Returned values are none, one, or both of the values listed below. - * - * Compute - * - * Storage - */ - public Set<String> getAvailableServices() { - return availableServices; + // TODO: Remove from here down with @AutoValue. + private Location(String name, String displayName, List<String> availableServices) { + this.name = checkNotNull(name, "name"); + this.displayName = checkNotNull(displayName, "displayName"); + this.availableServices = checkNotNull(availableServices, "availableServices"); } + private final String name; + private final String displayName; + private final List<String> availableServices; + @Override public int hashCode() { - return Objects.hashCode(name); + return Objects.hashCode(name, displayName, availableServices); } @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } Location other = (Location) obj; - return Objects.equal(this.name, other.name); + return equal(this.name, other.name) && + equal(this.displayName, other.displayName) && + equal(this.availableServices, other.availableServices); } @Override public String toString() { - return MoreObjects.toStringHelper(this).omitNullValues().add("name", name).add("displayName", displayName) - .add("availableServices", availableServices).toString(); + return Objects.toStringHelper(this) + .add("name", name) + .add("displayName", displayName) + .add("availableServices", availableServices).toString(); } } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListLocationsHandler.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListLocationsHandler.java b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListLocationsHandler.java index f257a6a..dc05df5 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListLocationsHandler.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListLocationsHandler.java @@ -16,46 +16,31 @@ */ package org.jclouds.azurecompute.xml; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableList.Builder; -import com.google.inject.Inject; import java.util.List; + import org.jclouds.azurecompute.domain.Location; import org.jclouds.http.functions.ParseSax; -import org.jclouds.util.SaxUtils; import org.xml.sax.Attributes; -import org.xml.sax.SAXException; - -public class ListLocationsHandler extends ParseSax.HandlerForGeneratedRequestWithResult<List<Location>> { - private final LocationHandler locationHandler; - - private Builder<Location> locations = ImmutableList.<Location> builder(); +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +public final class ListLocationsHandler extends ParseSax.HandlerForGeneratedRequestWithResult<List<Location>> { private boolean inLocation; + private final LocationHandler locationHandler = new LocationHandler(); + private final Builder<Location> locations = ImmutableList.builder(); - @Inject - public ListLocationsHandler(LocationHandler locationHandler) { - this.locationHandler = locationHandler; - } - - @Override - public List<Location> getResult() { + @Override public List<Location> getResult() { return locations.build(); } - @Override - public void startElement(String url, String name, String qName, Attributes attributes) throws SAXException { - if (SaxUtils.equalsOrSuffix(qName, "Location")) { + @Override public void startElement(String url, String name, String qName, Attributes attributes) { + if (qName.equals("Location")) { inLocation = true; } - if (inLocation) { - locationHandler.startElement(url, name, qName, attributes); - } } - @Override - public void endElement(String uri, String name, String qName) throws SAXException { + @Override public void endElement(String uri, String name, String qName) { if (qName.equals("Location")) { inLocation = false; locations.add(locationHandler.getResult()); http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/main/java/org/jclouds/azurecompute/xml/LocationHandler.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/LocationHandler.java b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/LocationHandler.java index 01d2b9e..a16505a 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/LocationHandler.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/LocationHandler.java @@ -16,42 +16,45 @@ */ package org.jclouds.azurecompute.xml; +import static org.jclouds.util.SaxUtils.currentOrNull; + +import java.util.List; + import org.jclouds.azurecompute.domain.Location; import org.jclouds.http.functions.ParseSax; -import org.jclouds.util.SaxUtils; -import org.xml.sax.SAXException; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; /** * @see <a href="http://msdn.microsoft.com/en-us/library/gg441293" >api</a> */ -public class LocationHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Location> { +final class LocationHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Location> { + private String name; + private String displayName; + private final List<String> availableServices = Lists.newArrayList(); - private StringBuilder currentText = new StringBuilder(); - private Location.Builder builder = Location.builder(); + private final StringBuilder currentText = new StringBuilder(); - @Override - public Location getResult() { - try { - return builder.build(); - } finally { - builder = Location.builder(); - } + @Override public Location getResult() { + Location result = Location.create(name, displayName, ImmutableList.copyOf(availableServices)); + name = displayName = null; // handler is called in a loop. + availableServices.clear(); + return result; } - @Override - public void endElement(String uri, String name, String qName) throws SAXException { + @Override public void endElement(String ignoredUri, String ignoredName, String qName) { if (qName.equals("Name")) { - builder.name(SaxUtils.currentOrNull(currentText)); + name = currentOrNull(currentText); } else if (qName.equals("DisplayName")) { - builder.displayName(SaxUtils.currentOrNull(currentText)); + displayName = currentOrNull(currentText); } else if (qName.equals("AvailableService")) { - builder.addAvailableService(SaxUtils.currentOrNull(currentText)); + availableServices.add(currentOrNull(currentText)); } currentText.setLength(0); } - @Override - public void characters(char ch[], int start, int length) { + @Override public void characters(char ch[], int start, int length) { currentText.append(ch, start, length); } } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiLiveTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiLiveTest.java index 53531de..4e8caba 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiLiveTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiLiveTest.java @@ -45,7 +45,7 @@ public class DiskApiLiveTest extends BaseAzureComputeApiLiveTest { locations = ImmutableSet.copyOf(transform(api.getLocationApi().list(), new Function<Location, String>() { public String apply(Location in) { - return in.getName(); + return in.name(); } })); images = ImmutableSet.copyOf(transform(api.getImageApi().list(), new Function<Image, String>() { http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java index da816d3..eb0eec8 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java @@ -54,7 +54,7 @@ public class HostedServiceApiLiveTest extends BaseAzureComputeApiLiveTest { public void setup() { super.setup(); // TODO: filter locations on those who have compute - location = Iterables.get(api.getLocationApi().list(), 0).getName(); + location = Iterables.get(api.getLocationApi().list(), 0).name(); operationSucceeded = retry(new Predicate<String>() { public boolean apply(String input) { return api.getOperationApi().get(input).getStatus() == Operation.Status.SUCCEEDED; http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/test/java/org/jclouds/azurecompute/features/ImageApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/features/ImageApiLiveTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/features/ImageApiLiveTest.java index 2b1aaf0..71d4deb 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/ImageApiLiveTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/ImageApiLiveTest.java @@ -43,7 +43,7 @@ public class ImageApiLiveTest extends BaseAzureComputeApiLiveTest { locations = ImmutableSet.copyOf(transform(api.getLocationApi().list(), new Function<Location, String>() { public String apply(Location in) { - return in.getName(); + return in.name(); } })); } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiLiveTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiLiveTest.java index 455ef11..d270442 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiLiveTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiLiveTest.java @@ -16,42 +16,34 @@ */ package org.jclouds.azurecompute.features; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.common.collect.Iterables; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + import java.util.Arrays; import java.util.List; + import org.jclouds.azurecompute.domain.Location; import org.jclouds.azurecompute.internal.BaseAzureComputeApiLiveTest; import org.testng.annotations.Test; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; - @Test(groups = "live", testName = "LocationApiLiveTest") public class LocationApiLiveTest extends BaseAzureComputeApiLiveTest { - @Test - protected void testList() { - List<Location> response = api().list(); + private static final List<String> KNOWN_SERVICES = Arrays + .asList("Compute", "Storage", "PersistentVMRole", "HighMemory"); - for (Location location : response) { + @Test public void testList() { + for (Location location : api().list()) { checkLocation(location); } - } - private Predicate<String> knownServices = Predicates - .in(Arrays.asList("Compute", "Storage", "PersistentVMRole", "HighMemory")); - private void checkLocation(Location location) { - checkNotNull(location.getName(), "Name cannot be null for a Location."); - checkNotNull(location.getDisplayName(), "DisplayName cannot be null for Location %s", location.getName()); - checkNotNull(location.getAvailableServices(), "AvailableServices cannot be null for Location %s", - location.getName()); - checkState(Iterables.all(location.getAvailableServices(), knownServices), - "AvailableServices in Location %s didn't match %s: %s", location.getName(), knownServices, - location.getAvailableServices()); + assertNotNull(location.name(), "Name cannot be null for a Location."); + assertNotNull(location.displayName(), "DisplayName cannot be null for: " + location); + assertNotNull(location.availableServices(), "AvailableServices cannot be null for: " + location.name()); + assertTrue(KNOWN_SERVICES.containsAll(location.availableServices()), + "AvailableServices in " + location + " didn't match: " + KNOWN_SERVICES); } private LocationApi api() { http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiMockTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiMockTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiMockTest.java index 695b54d..b476ed1 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiMockTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/LocationApiMockTest.java @@ -19,7 +19,7 @@ package org.jclouds.azurecompute.features; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockWebServer; import org.jclouds.azurecompute.internal.BaseAzureComputeApiMockTest; -import org.jclouds.azurecompute.parse.ListLocationsTest; +import org.jclouds.azurecompute.xml.ListLocationsHandlerTest; import org.testng.annotations.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -34,7 +34,7 @@ public class LocationApiMockTest extends BaseAzureComputeApiMockTest { try { LocationApi api = api(server.getUrl("/")).getLocationApi(); - assertThat(api.list()).containsExactlyElementsOf(ListLocationsTest.expected()); + assertThat(api.list()).containsExactlyElementsOf(ListLocationsHandlerTest.expected()); assertSent(server, "GET", "/locations"); } finally { http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ListLocationsTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ListLocationsTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ListLocationsTest.java deleted file mode 100644 index 5ec96b1..0000000 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ListLocationsTest.java +++ /dev/null @@ -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.azurecompute.parse; - -import com.google.common.collect.ImmutableList; -import java.io.InputStream; -import java.util.List; -import org.jclouds.azurecompute.domain.Location; -import org.jclouds.azurecompute.xml.ListLocationsHandler; -import org.jclouds.http.functions.BaseHandlerTest; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertEquals; - -@Test(groups = "unit", testName = "LocationsTest") -public class ListLocationsTest extends BaseHandlerTest { - - public void test() { - InputStream is = getClass().getResourceAsStream("/locations.xml"); - - List<Location> expected = expected(); - - ListLocationsHandler handler = injector.getInstance(ListLocationsHandler.class); - List<Location> result = factory.create(handler).parse(is); - - assertEquals(result.toString(), expected.toString()); - - } - - public static List<Location> expected() { - List<String> availableServices = ImmutableList.of("Compute", "Storage", "PersistentVMRole"); - return ImmutableList.<Location>builder() - .add(Location.builder() - .name("West US") - .displayName("West US") - .availableServices(availableServices) - .build()) - .add(Location.builder() - .name("East US") - .displayName("East US") - .availableServices(availableServices) - .build()) - .add(Location.builder() - .name("East Asia") - .displayName("East Asia") - .availableServices(availableServices) - .build()) - .add(Location.builder() - .name("Southeast Asia") - .displayName("Southeast Asia") - .availableServices(availableServices) - .build()) - .add(Location.builder() - .name("North Europe") - .displayName("North Europe") - .availableServices(availableServices) - .build()) - .add(Location.builder() - .name("West Europe") - .displayName("West Europe") - .availableServices(availableServices) - .build()).build(); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f4af63fb/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ListLocationsHandlerTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ListLocationsHandlerTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ListLocationsHandlerTest.java new file mode 100644 index 0000000..599d307 --- /dev/null +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ListLocationsHandlerTest.java @@ -0,0 +1,52 @@ +/* + * 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.azurecompute.xml; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.util.List; + +import org.jclouds.azurecompute.domain.Location; +import org.jclouds.http.functions.BaseHandlerTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +@Test(groups = "unit", testName = "ListLocationsHandlerTest") +public class ListLocationsHandlerTest extends BaseHandlerTest { + + public void test() { + InputStream is = getClass().getResourceAsStream("/locations.xml"); + List<Location> result = factory.create(new ListLocationsHandler()).parse(is); + + assertEquals(result, expected()); + } + + public static List<Location> expected() { + List<String> availableServices = ImmutableList.of("Compute", "Storage", "PersistentVMRole"); + return ImmutableList.of( // + Location.create("West US", "West US", availableServices), // + Location.create("East US", "East US", availableServices), // + Location.create("East Asia", "East Asia", availableServices), // + Location.create("Southeast Asia", "Southeast Asia", availableServices), // + Location.create("North Europe", "North Europe", availableServices), // + Location.create("West Europe", "West Europe", availableServices) // + ); + } + +}
