Cleanup output-only Disk 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/da40d636 Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/da40d636 Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/da40d636 Branch: refs/heads/master Commit: da40d636bbfee47d07ef6ffefc9a22c3367dd45b Parents: caf57cc Author: Adrian Cole <[email protected]> Authored: Sun Oct 19 10:34:30 2014 -0400 Committer: Adrian Cole <[email protected]> Committed: Mon Oct 20 13:26:53 2014 -0400 ---------------------------------------------------------------------- .../org/jclouds/azurecompute/domain/Disk.java | 446 +++++-------------- .../azurecompute/xml/AttachmentHandler.java | 36 +- .../jclouds/azurecompute/xml/DiskHandler.java | 151 ++++--- .../azurecompute/xml/ListDisksHandler.java | 35 +- .../azurecompute/features/DiskApiLiveTest.java | 74 ++- .../azurecompute/features/DiskApiMockTest.java | 19 +- .../azurecompute/parse/ListDisksTest.java | 71 --- .../azurecompute/xml/ListDisksHandlerTest.java | 67 +++ azurecompute/src/test/resources/disks.xml | 6 +- 9 files changed, 332 insertions(+), 573 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/da40d636/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Disk.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Disk.java b/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Disk.java index d257709..6590330 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Disk.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Disk.java @@ -16,103 +16,56 @@ */ package org.jclouds.azurecompute.domain; -import com.google.common.base.MoreObjects; -import com.google.common.base.MoreObjects.ToStringHelper; -import com.google.common.base.Objects; -import com.google.common.base.Optional; +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Objects.toStringHelper; +import static com.google.common.base.Preconditions.checkNotNull; + import java.net.URI; -import static com.google.common.base.Preconditions.checkNotNull; +import org.jclouds.javax.annotation.Nullable; + +import com.google.common.base.Objects; /** - * disk in the image repository + * A disk in the image repository. * * @see <a href="http://msdn.microsoft.com/en-us/library/jj157176" >api</a> */ -public class Disk { - public static class Attachment { - - public static Builder builder() { - return new Builder(); +public final class Disk { + public static final class Attachment { + /** The deployment in which the disk is being used. */ + public String deployment() { + return deployment; } - public Builder toBuilder() { - return builder().fromAttachment(this); + /** The hosted service in which the disk is being used. */ + public String hostedService() { + return hostedService; } - public static class Builder { - - private String hostedService; - private String deployment; - private String role; - - /** - * @see Attachment#getHostedService() - */ - public Builder hostedService(String hostedService) { - this.hostedService = hostedService; - return this; - } - - /** - * @see Attachment#getDeployment() - */ - public Builder deployment(String deployment) { - this.deployment = deployment; - return this; - } - - /** - * @see Attachment#getRole() - */ - public Builder role(String role) { - this.role = role; - return this; - } - - public Attachment build() { - return new Attachment(hostedService, deployment, role); - } - - public Builder fromAttachment(Attachment in) { - return this.hostedService(in.hostedService).deployment(in.deployment).role(in.role); - } + /** The virtual machine that the disk is attached to. */ + public String virtualMachine() { + return virtualMachine; } - private final String hostedService; - private final String deployment; - private final String role; + public static Attachment create(String hostedService, String deployment, String virtualMachine) { + return new Attachment(hostedService, deployment, virtualMachine); + } - private Attachment(String hostedService, String deployment, String role) { + // TODO: Remove from here down with @AutoValue. + private Attachment(String hostedService, String deployment, String virtualMachine) { this.hostedService = checkNotNull(hostedService, "hostedService"); this.deployment = checkNotNull(deployment, "deployment"); - this.role = checkNotNull(role, "role"); - } - - /** - * The deployment in which the disk is being used. - */ - public String getDeployment() { - return deployment; - } - - /** - * The hosted service in which the disk is being used. - */ - public String getHostedService() { - return hostedService; + this.virtualMachine = checkNotNull(virtualMachine, "virtualMachine"); } - /** - * The virtual machine that the disk is attached to. - */ - public String getRole() { - return role; - } + private final String hostedService; + private final String deployment; + private final String virtualMachine; @Override public int hashCode() { - return Objects.hashCode(hostedService, deployment, role); + return Objects.hashCode(hostedService, deployment, virtualMachine); } @Override @@ -127,316 +80,157 @@ public class Disk { return false; } Attachment other = (Attachment) obj; - return Objects.equal(this.hostedService, other.hostedService) && Objects - .equal(this.deployment, other.deployment) && Objects.equal(this.role, other.role); + return equal(this.hostedService, other.hostedService) && + equal(this.deployment, other.deployment) && + equal(this.virtualMachine, other.virtualMachine); } @Override public String toString() { - return MoreObjects.toStringHelper(this).omitNullValues().add("deployment", hostedService).add("role", role) - .toString(); - } - - } - - public static Builder builder() { - return new Builder(); - } - - public Builder toBuilder() { - return new Builder().fromHostedService(this); - } - - public static class Builder { - - private Optional<Attachment> attachedTo = Optional.absent(); - private OSType os; - private String name; - private Optional<Integer> logicalSizeInGB = Optional.absent(); - private Optional<String> description = Optional.absent(); - private Optional<String> location = Optional.absent(); - private Optional<String> affinityGroup = Optional.absent(); - private Optional<URI> mediaLink = Optional.absent(); - private Optional<String> sourceImage = Optional.absent(); - private Optional<String> label = Optional.absent(); - private boolean hasOperatingSystem; - private boolean isCorrupted; - - /** - * @see Disk#getAttachedTo() - */ - public Builder attachedTo(Attachment attachedTo) { - this.attachedTo = Optional.fromNullable(attachedTo); - return this; - } - - /** - * @see Disk#getOS() - */ - public Builder os(OSType os) { - this.os = os; - return this; - } - - /** - * @see Disk#getName() - */ - public Builder name(String name) { - this.name = name; - return this; - } - - /** - * @see Disk#getDescription() - */ - public Builder description(String description) { - this.description = Optional.fromNullable(description); - return this; - } - - /** - * @see Disk#getLogicalSizeInGB() - */ - public Builder logicalSizeInGB(Integer logicalSizeInGB) { - this.logicalSizeInGB = Optional.fromNullable(logicalSizeInGB); - return this; - } - - /** - * @see Disk#getLocation() - */ - public Builder location(String location) { - this.location = Optional.fromNullable(location); - return this; - } - - /** - * @see Disk#getAffinityGroup() - */ - public Builder affinityGroup(String affinityGroup) { - this.affinityGroup = Optional.fromNullable(affinityGroup); - return this; - } - - /** - * @see Disk#getMediaLink() - */ - public Builder mediaLink(URI mediaLink) { - this.mediaLink = Optional.fromNullable(mediaLink); - return this; - } - - /** - * @see Disk#getSourceImage() - */ - public Builder sourceImage(String sourceImage) { - this.sourceImage = Optional.fromNullable(sourceImage); - return this; - } - - /** - * @see Disk#getLabel() - */ - public Builder label(String label) { - this.label = Optional.fromNullable(label); - return this; - } - - /** - * @see Disk#hasOperatingSystem() - */ - public Builder hasOperatingSystem(boolean hasOperatingSystem) { - this.hasOperatingSystem = hasOperatingSystem; - return this; - } - - /** - * @see Disk#isCorrupted() - */ - public Builder isCorrupted(boolean isCorrupted) { - this.isCorrupted = isCorrupted; - return this; - } - - public Disk build() { - return new Disk(attachedTo, os, name, logicalSizeInGB, description, location, affinityGroup, mediaLink, - sourceImage, label, hasOperatingSystem, isCorrupted); - } - - public Builder fromHostedService(Disk in) { - return this.attachedTo(in.attachedTo.orNull()).os(in.getOS()).name(in.getName()) - .logicalSizeInGB(in.getLogicalSizeInGB().orNull()).description(in.getDescription().orNull()) - .location(in.getLocation().orNull()).affinityGroup(in.getAffinityGroup().orNull()) - .mediaLink(in.getMediaLink().orNull()).sourceImage(in.getSourceImage().orNull()) - .label(in.getLabel().orNull()).hasOperatingSystem(in.hasOperatingSystem).isCorrupted(in.isCorrupted); + return Objects.toStringHelper(this) + .add("hostedService", hostedService) + .add("deployment", deployment) + .add("virtualMachine", virtualMachine).toString(); } - } - - private final Optional<Attachment> attachedTo; - private final OSType os; - private final String name; - private final Optional<Integer> logicalSizeInGB; - private final Optional<String> description; - private final Optional<String> location; - private final Optional<String> affinityGroup; - private final Optional<URI> mediaLink; - private final Optional<String> sourceImage; - private final Optional<String> label; - private final boolean hasOperatingSystem; - private final boolean isCorrupted; - - private Disk(Optional<Attachment> attachedTo, OSType os, String name, Optional<Integer> logicalSizeInGB, - Optional<String> description, Optional<String> location, Optional<String> affinityGroup, - Optional<URI> mediaLink, Optional<String> sourceImage, Optional<String> label, boolean hasOperatingSystem, - boolean isCorrupted) { - this.name = checkNotNull(name, "name"); - this.attachedTo = checkNotNull(attachedTo, "attachedTo for %s", name); - this.logicalSizeInGB = checkNotNull(logicalSizeInGB, "logicalSizeInGB for %s", name); - this.description = checkNotNull(description, "description for %s", name); - this.os = checkNotNull(os, "os for %s", name); - this.location = checkNotNull(location, "location for %s", name); - this.affinityGroup = checkNotNull(affinityGroup, "affinityGroup for %s", name); - this.mediaLink = checkNotNull(mediaLink, "mediaLink for %s", name); - this.sourceImage = checkNotNull(sourceImage, "sourceImage for %s", name); - this.label = checkNotNull(label, "label for %s", name); - this.hasOperatingSystem = hasOperatingSystem; - this.isCorrupted = isCorrupted; - } - - /** - * Contains properties that specify a virtual machine that currently using the disk. A disk - * cannot be deleted as long as it is attached to a virtual machine. - */ - public Optional<Attachment> getAttachedTo() { - return attachedTo; - } - /** - * The operating system type of the OS image. - */ - public OSType getOS() { - return os; } /** * The name of the disk. This is the name that is used when creating one or more virtual machines * using the disk. */ - public String getName() { + public String name() { return name; } /** - * The size, in GB, of the image. + * The geo-location of the disk in Windows Azure, if the disk is not + * associated with an affinity group. If a location has been specified, the AffinityGroup element + * is not returned. */ - public Optional<Integer> getLogicalSizeInGB() { - return logicalSizeInGB; + @Nullable public String location() { + return location; } /** - * The description for the image. + * The affinity group with which this disk is associated, if any. If the service is + * associated with an affinity group, the Location element is not returned. */ - public Optional<String> getDescription() { - return description; + @Nullable public String affinityGroup() { + return affinityGroup; } - /** - * The geo-location in which this media is located. The Location value is derived from storage - * account that contains the blob in which the media is located. If the storage account belongs - * to an affinity group the value is absent. - */ - public Optional<String> getLocation() { - return location; + @Nullable public String description() { + return description; } - /** - * The affinity in which the media is located. The AffinityGroup value is derived from storage - * account that contains the blob in which the media is located. If the storage account does not - * belong to an affinity group the value is absent. - */ - public Optional<String> getAffinityGroup() { - return affinityGroup; + /** The operating system type of the OS image, or null if a data disk. */ + @Nullable public OSType os() { + return os; } /** - * The location of the blob in the blob store in which the media for the disk is located. The + * The location of the blob in the blob store in which the media for the image is located. The * blob location belongs to a storage account in the subscription specified by the * <subscription-id> value in the operation call. * * Example: * - * http://example.blob.core.windows.net/disks/mydisk.vhd + * http://example.blob.core.windows.net/disks/myimage.vhd */ - public Optional<URI> getMediaLink() { + @Nullable public URI mediaLink() { return mediaLink; } - /** - * The name of the OS Image from which the disk was created. This property is populated - * automatically when a disk is created from an OS image by calling the Add Role, Create - * Deployment, or Provision Disk operations. - */ - public Optional<String> getSourceImage() { - return sourceImage; + @Nullable public Integer logicalSizeInGB() { + return logicalSizeInGB; } /** - * The description of the image. + * Contains properties that specify a virtual machine that currently using the disk. A disk + * cannot be deleted as long as it is attached to a virtual machine. */ - public Optional<String> getLabel() { - return label; + @Nullable public Attachment attachedTo() { + return attachedTo; } /** - * Returns whether this disk contains operation system. Only disks that have an operating system - * installed can be mounted as an OS Drive. + * The name of the OS Image from which the disk was created. This property is populated + * automatically when a disk is created from an OS image by calling the Add Role, Create + * Deployment, or Provision Disk operations. */ - public boolean hasOperatingSystem() { - return hasOperatingSystem; + @Nullable public String sourceImage() { + return sourceImage; } - /** - * Returns whether there is a consistency failure detected with this disk. If a disk fails the - * consistency check, you delete any virtual machines using it, delete the disk, and inspect the - * blob media to see if the content is intact. You can then reregister the media in the blob as a - * disk. - */ - public boolean isCorrupted() { - return isCorrupted; + public static Disk create(String name, String location, String affinityGroup, String description, + OSType os, URI mediaLink, Integer logicalSizeInGB, Attachment attachedTo, String sourceImage) { + return new Disk(name, location, affinityGroup, description, os, mediaLink, logicalSizeInGB, attachedTo, + sourceImage); } - @Override - public int hashCode() { - return Objects.hashCode(name); + // TODO: Remove from here down with @AutoValue. + private Disk(String name, String location, String affinityGroup, String description, OSType os, URI mediaLink, + Integer logicalSizeInGB, Attachment attachedTo, String sourceImage) { + this.name = checkNotNull(name, "name"); + this.location = location; + this.affinityGroup = affinityGroup; + this.description = description; + this.os = os; + this.mediaLink = mediaLink; + this.logicalSizeInGB = checkNotNull(logicalSizeInGB, "logicalSizeInGB of %s", name); + this.attachedTo = attachedTo; + this.sourceImage = sourceImage; } + private final String name; + private final String location; + private final String affinityGroup; + private final String description; + private final OSType os; + private final URI mediaLink; + private final Integer logicalSizeInGB; + private final Attachment attachedTo; + private final String sourceImage; + @Override - public boolean equals(Object obj) { - if (this == obj) { + public boolean equals(Object object) { + if (this == object) { return true; } - if (obj == null) { + if (object instanceof Disk) { + Disk that = Disk.class.cast(object); + return equal(name, that.name) && + equal(location, that.location) && + equal(affinityGroup, that.affinityGroup) && + equal(description, that.description) && + equal(os, that.os) && + equal(mediaLink, that.mediaLink) && + equal(logicalSizeInGB, that.logicalSizeInGB) && + equal(attachedTo, that.attachedTo) && + equal(sourceImage, that.sourceImage); + } else { return false; } - if (getClass() != obj.getClass()) { - return false; - } - Disk other = (Disk) obj; - return Objects.equal(this.name, other.name); } @Override - public String toString() { - return string().toString(); + public int hashCode() { + return Objects.hashCode(name, location, affinityGroup, description, os, mediaLink, logicalSizeInGB, + attachedTo, sourceImage); } - private ToStringHelper string() { - return MoreObjects.toStringHelper(this).omitNullValues().add("os", os).add("name", name) - .add("attachedTo", attachedTo.orNull()).add("logicalSizeInGB", logicalSizeInGB.orNull()) - .add("description", description).add("location", location.orNull()) - .add("affinityGroup", affinityGroup.orNull()).add("mediaLink", mediaLink.orNull()) - .add("sourceImage", sourceImage.orNull()).add("label", label.orNull()) - .add("hasOperatingSystem", hasOperatingSystem).add("isCorrupted", isCorrupted); + @Override + public String toString() { + return toStringHelper(this) + .add("name", name) + .add("location", location) + .add("affinityGroup", affinityGroup) + .add("description", description) + .add("os", os) + .add("mediaLink", mediaLink) + .add("logicalSizeInGB", logicalSizeInGB) + .add("attachedTo", attachedTo) + .add("sourceImage", sourceImage).toString(); } - } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/da40d636/azurecompute/src/main/java/org/jclouds/azurecompute/xml/AttachmentHandler.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/AttachmentHandler.java b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/AttachmentHandler.java index 648cf26..ccbe9c8 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/AttachmentHandler.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/AttachmentHandler.java @@ -16,43 +16,39 @@ */ package org.jclouds.azurecompute.xml; +import static org.jclouds.util.SaxUtils.currentOrNull; + import org.jclouds.azurecompute.domain.Disk.Attachment; import org.jclouds.http.functions.ParseSax; -import org.jclouds.util.SaxUtils; -import org.xml.sax.SAXException; /** * @see <a href="http://msdn.microsoft.com/en-us/library/jj157176" >api</a> */ -public class AttachmentHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Attachment> { +final class AttachmentHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Attachment> { + private String hostedService; + private String deployment; + private String virtualMachine; - private StringBuilder currentText = new StringBuilder(); - private Attachment.Builder builder = Attachment.builder(); + private final StringBuilder currentText = new StringBuilder(); - @Override - public Attachment getResult() { - try { - return builder.build(); - } finally { - builder = Attachment.builder(); - } + @Override public Attachment getResult() { + Attachment result = Attachment.create(hostedService, deployment, virtualMachine); + hostedService = deployment = virtualMachine = null; // handler could be called in a loop. + 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("HostedServiceName")) { - builder.hostedService(SaxUtils.currentOrNull(currentText)); + hostedService = currentOrNull(currentText); } else if (qName.equals("DeploymentName")) { - builder.deployment(SaxUtils.currentOrNull(currentText)); + deployment = currentOrNull(currentText); } else if (qName.equals("RoleName")) { - builder.role(SaxUtils.currentOrNull(currentText)); + virtualMachine = 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/da40d636/azurecompute/src/main/java/org/jclouds/azurecompute/xml/DiskHandler.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/DiskHandler.java b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/DiskHandler.java index b592f95..0cc62c6 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/DiskHandler.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/DiskHandler.java @@ -16,92 +16,97 @@ */ package org.jclouds.azurecompute.xml; +import static org.jclouds.util.SaxUtils.currentOrNull; + import java.net.URI; -import javax.inject.Inject; + import org.jclouds.azurecompute.domain.Disk; +import org.jclouds.azurecompute.domain.Disk.Attachment; import org.jclouds.azurecompute.domain.OSType; import org.jclouds.http.functions.ParseSax; import org.xml.sax.Attributes; -import org.xml.sax.SAXException; - -import static org.jclouds.util.SaxUtils.currentOrNull; -import static org.jclouds.util.SaxUtils.equalsOrSuffix; /** * @see <a href="http://msdn.microsoft.com/en-us/library/jj157176" >api</a> */ -public class DiskHandler extends - ParseSax.HandlerForGeneratedRequestWithResult<Disk> { - - private final AttachmentHandler attachmentHandler; - - @Inject - private DiskHandler(AttachmentHandler attachmentHandler) { - this.attachmentHandler = attachmentHandler; - } +final class DiskHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Disk> { + private String name; + private String location; + private String affinityGroup; + private String description; + private OSType os; + private URI mediaLink; + private Integer logicalSizeInGB; + private Attachment attachedTo; + private String sourceImage; - private StringBuilder currentText = new StringBuilder(); - private Disk.Builder builder = Disk.builder(); + private boolean inAttachment; + private final AttachmentHandler attachmentHandler = new AttachmentHandler(); + private final StringBuilder currentText = new StringBuilder(); - private boolean inAttachment; + @Override public Disk getResult() { + Disk result = Disk.create(name, location, affinityGroup, description, os, mediaLink, logicalSizeInGB, + attachedTo, sourceImage); + resetState(); // handler is called in a loop. + return result; + } - @Override - public Disk getResult() { - try { - return builder.build(); - } finally { - builder = Disk.builder(); - } - } + private void resetState() { + name = location = affinityGroup = description = sourceImage = null; + os = null; + mediaLink = null; + logicalSizeInGB = null; + attachedTo = null; + } - @Override - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - if (equalsOrSuffix(qName, "AttachedTo")) { - inAttachment = true; - } - } + @Override public void startElement(String uri, String localName, String qName, Attributes attributes) { + if (qName.equals("AttachedTo")) { + inAttachment = true; + } + } - @Override - public void endElement(String uri, String name, String qName) - throws SAXException { - if (equalsOrSuffix(qName, "AttachedTo")) { - builder.attachedTo(attachmentHandler.getResult()); - inAttachment = false; - } else if (inAttachment) { - attachmentHandler.endElement(uri, name, qName); - } else if (equalsOrSuffix(qName, "OS")) { - builder.os(OSType.fromValue(currentOrNull(currentText))); - } else if (equalsOrSuffix(qName, "Name")) { - builder.name(currentOrNull(currentText)); - } else if (equalsOrSuffix(qName, "LogicalDiskSizeInGB")) { - String gb = currentOrNull(currentText); - if (gb != null) - builder.logicalSizeInGB(Integer.parseInt(gb)); - } else if (equalsOrSuffix(qName, "Description")) { - builder.description(currentOrNull(currentText)); - } else if (equalsOrSuffix(qName, "Location")) { - builder.location(currentOrNull(currentText)); - } else if (equalsOrSuffix(qName, "AffinityGroup")) { - builder.affinityGroup(currentOrNull(currentText)); - } else if (equalsOrSuffix(qName, "MediaLink")) { - String link = currentOrNull(currentText); - if (link != null) - builder.mediaLink(URI.create(link)); - } else if (equalsOrSuffix(qName, "SourceImageName")) { - builder.sourceImage(currentOrNull(currentText)); - } else if (equalsOrSuffix(qName, "Label")) { - builder.label(currentOrNull(currentText)); - } - currentText.setLength(0); - } + @Override public void endElement(String ignoredUri, String ignoredName, String qName) { + if (qName.equals("AttachedTo")) { + attachedTo = attachmentHandler.getResult(); + inAttachment = false; + } else if (inAttachment) { + attachmentHandler.endElement(ignoredUri, ignoredName, qName); + } else if (qName.equals("OS")) { + String osText = currentOrNull(currentText); + if (osText != null && osText.toUpperCase().equals("NULL")) { + os = null; + } else { + os = OSType.fromValue(currentOrNull(currentText)); + } + } else if (qName.equals("Name")) { + name = currentOrNull(currentText); + } else if (qName.equals("LogicalDiskSizeInGB")) { + String gb = currentOrNull(currentText); + if (gb != null) { + logicalSizeInGB = Integer.parseInt(gb); + } + } else if (qName.equals("Description")) { + description = currentOrNull(currentText); + } else if (qName.equals("Location")) { + location = currentOrNull(currentText); + } else if (qName.equals("AffinityGroup")) { + affinityGroup = currentOrNull(currentText); + } else if (qName.equals("MediaLink")) { + String link = currentOrNull(currentText); + if (link != null) { + mediaLink = URI.create(link); + } + } else if (qName.equals("SourceImageName")) { + sourceImage = currentOrNull(currentText); + } + currentText.setLength(0); + } - @Override - public void characters(char ch[], int start, int length) { - if (inAttachment) { - attachmentHandler.characters(ch, start, length); - } else { - currentText.append(ch, start, length); - } - } + @Override public void characters(char ch[], int start, int length) { + if (inAttachment) { + attachmentHandler.characters(ch, start, length); + } else { + currentText.append(ch, start, length); + } + } } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/da40d636/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListDisksHandler.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListDisksHandler.java b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListDisksHandler.java index fb6ea13..acdc7b5 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListDisksHandler.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ListDisksHandler.java @@ -16,37 +16,26 @@ */ 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.Disk; import org.jclouds.http.functions.ParseSax; -import org.jclouds.util.SaxUtils; import org.xml.sax.Attributes; -import org.xml.sax.SAXException; - -public class ListDisksHandler extends ParseSax.HandlerForGeneratedRequestWithResult<List<Disk>> { - - private final DiskHandler diskHandler; - private Builder<Disk> disks = ImmutableList.<Disk> builder(); +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +public final class ListDisksHandler extends ParseSax.HandlerForGeneratedRequestWithResult<List<Disk>> { private boolean inDisk; + private final DiskHandler diskHandler = new DiskHandler(); + private final Builder<Disk> disks = ImmutableList.builder(); - @Inject - public ListDisksHandler(final DiskHandler diskHandler) { - this.diskHandler = diskHandler; - } - - @Override - public List<Disk> getResult() { + @Override public List<Disk> getResult() { return disks.build(); } - @Override - public void startElement(String url, String name, String qName, Attributes attributes) throws SAXException { - if (SaxUtils.equalsOrSuffix(qName, "Disk")) { + @Override public void startElement(String url, String name, String qName, Attributes attributes) { + if (qName.equals("Disk")) { inDisk = true; } if (inDisk) { @@ -54,8 +43,7 @@ public class ListDisksHandler extends ParseSax.HandlerForGeneratedRequestWithRes } } - @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("Disk")) { inDisk = false; disks.add(diskHandler.getResult()); @@ -64,8 +52,7 @@ public class ListDisksHandler extends ParseSax.HandlerForGeneratedRequestWithRes } } - @Override - public void characters(char ch[], int start, int length) { + @Override public void characters(char ch[], int start, int length) { if (inDisk) { diskHandler.characters(ch, start, length); } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/da40d636/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 478b1b3..53531de 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiLiveTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiLiveTest.java @@ -16,9 +16,11 @@ */ package org.jclouds.azurecompute.features; -import com.google.common.base.Function; -import com.google.common.collect.ImmutableSet; -import java.util.List; +import static com.google.common.collect.Iterables.transform; +import static org.testng.Assert.assertNotEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + import org.jclouds.azurecompute.domain.Disk; import org.jclouds.azurecompute.domain.Image; import org.jclouds.azurecompute.domain.Location; @@ -27,10 +29,8 @@ import org.jclouds.azurecompute.internal.BaseAzureComputeApiLiveTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.collect.Iterables.transform; -import static org.testng.Assert.assertNotEquals; -import static org.testng.Assert.assertTrue; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; @Test(groups = "live", testName = "DiskApiLiveTest") public class DiskApiLiveTest extends BaseAzureComputeApiLiveTest { @@ -44,73 +44,51 @@ public class DiskApiLiveTest extends BaseAzureComputeApiLiveTest { locations = ImmutableSet.copyOf(transform(api.getLocationApi().list(), new Function<Location, String>() { - @Override public String apply(Location in) { return in.getName(); } })); images = ImmutableSet.copyOf(transform(api.getImageApi().list(), new Function<Image, String>() { - @Override public String apply(Image in) { return in.name(); } })); } - @Test - protected void testList() { - List<Disk> response = api().list(); - - for (Disk disk : response) { + public void testList() { + for (Disk disk : api().list()) { checkDisk(disk); } } private void checkDisk(Disk disk) { - checkNotNull(disk.getName(), "Name cannot be null for Disk %s", disk.getLabel()); - checkNotNull(disk.getOS(), "OS cannot be null for Disk: %s", disk); - assertNotEquals(disk.getOS(), OSType.UNRECOGNIZED, "Status cannot be UNRECOGNIZED for Disk: " + disk); + assertNull(disk.name(), "Name cannot be null for: " + disk); + assertNull(disk.os(), "OS cannot be null for: " + disk); + assertNotEquals(disk.os(), OSType.UNRECOGNIZED, "Status cannot be UNRECOGNIZED for: " + disk); - checkNotNull(disk.getAttachedTo(), "While AttachedTo can be null for Disk, its Optional wrapper cannot: %s", disk); - if (disk.getAttachedTo().isPresent()) { + if (disk.attachedTo() != null) { // TODO: verify you can lookup the role } - checkNotNull(disk.getLogicalSizeInGB(), - "While LogicalSizeInGB can be null for Disk, its Optional wrapper cannot: %s", disk); - - if (disk.getLogicalSizeInGB().isPresent()) - assertTrue(disk.getLogicalSizeInGB().get() > 0, "LogicalSizeInGB should be positive, if set" + disk.toString()); - - checkNotNull(disk.getMediaLink(), "While MediaLink can be null for Disk, its Optional wrapper cannot: %s", disk); - - if (disk.getMediaLink().isPresent()) - assertTrue(ImmutableSet.of("http", "https").contains(disk.getMediaLink().get().getScheme()), - "MediaLink should be an http(s) url" + disk.toString()); - - checkNotNull(disk.getLabel(), "While Label can be null for Disk, its Optional wrapper cannot: %s", - disk); + if (disk.logicalSizeInGB() != null) { + assertTrue(disk.logicalSizeInGB() > 0, "LogicalSizeInGB should be positive, if set" + disk); + } - checkNotNull(disk.getDescription(), "While Description can be null for Disk, its Optional wrapper cannot: %s", - disk); + if (disk.mediaLink() != null) { + assertTrue(ImmutableSet.of("http", "https").contains(disk.mediaLink().getScheme()), + "MediaLink should be an http(s) url" + disk); + } - checkNotNull(disk.getLocation(), "While Location can be null for Disk, its Optional wrapper cannot: %s", disk); - if (disk.getLocation().isPresent()) { - assertTrue(locations.contains(disk.getLocation().get()), - "Location not in " + locations + " :" + disk.toString()); + if (disk.location() != null) { + assertTrue(locations.contains(disk.location()), "Location not in " + locations + " :" + disk); } - checkNotNull(disk.getSourceImage(), "While SourceImage can be null for Disk, its Optional wrapper cannot: %s", - disk); - if (disk.getSourceImage().isPresent()) { - assertTrue(images.contains(disk.getSourceImage().get()), - "SourceImage not in " + images + " :" + disk.toString()); + if (disk.sourceImage() != null) { + assertTrue(images.contains(disk.sourceImage()), "SourceImage not in " + images + " :" + disk); } - checkNotNull(disk.getAffinityGroup(), - "While AffinityGroup can be null for Disk, its Optional wrapper cannot: %s", disk); - if (disk.getAffinityGroup().isPresent()) { - // TODO: list getAffinityGroups and check if there + if (disk.affinityGroup() != null) { + // TODO: list affinityGroups and check if there } } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/da40d636/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiMockTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiMockTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiMockTest.java index c6fab15..656428c 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiMockTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/DiskApiMockTest.java @@ -16,13 +16,16 @@ */ package org.jclouds.azurecompute.features; -import com.squareup.okhttp.mockwebserver.MockResponse; -import com.squareup.okhttp.mockwebserver.MockWebServer; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + import org.jclouds.azurecompute.internal.BaseAzureComputeApiMockTest; -import org.jclouds.azurecompute.parse.ListDisksTest; +import org.jclouds.azurecompute.xml.ListDisksHandlerTest; import org.testng.annotations.Test; -import static org.assertj.core.api.Assertions.assertThat; +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; @Test(groups = "unit", testName = "DiskApiMockTest") public class DiskApiMockTest extends BaseAzureComputeApiMockTest { @@ -34,7 +37,7 @@ public class DiskApiMockTest extends BaseAzureComputeApiMockTest { try { DiskApi api = api(server.getUrl("/")).getDiskApi(); - assertThat(api.list()).containsExactlyElementsOf(ListDisksTest.expected()); + assertEquals(api.list(), ListDisksHandlerTest.expected()); assertSent(server, "GET", "/services/disks"); } finally { @@ -49,7 +52,7 @@ public class DiskApiMockTest extends BaseAzureComputeApiMockTest { try { DiskApi api = api(server.getUrl("/")).getDiskApi(); - assertThat(api.list()).isEmpty(); + assertTrue(api.list().isEmpty()); assertSent(server, "GET", "/services/disks"); } finally { @@ -64,7 +67,7 @@ public class DiskApiMockTest extends BaseAzureComputeApiMockTest { try { DiskApi api = api(server.getUrl("/")).getDiskApi(); - assertThat(api.delete("my-disk")).isEqualTo("request-1"); + assertEquals(api.delete("my-disk"), "request-1"); assertSent(server, "DELETE", "/services/disks/my-disk"); } finally { @@ -79,7 +82,7 @@ public class DiskApiMockTest extends BaseAzureComputeApiMockTest { try { DiskApi api = api(server.getUrl("/")).getDiskApi(); - assertThat(api.delete("my-disk")).isNull(); + assertNull(api.delete("my-disk")); assertSent(server, "DELETE", "/services/disks/my-disk"); } finally { http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/da40d636/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ListDisksTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ListDisksTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ListDisksTest.java deleted file mode 100644 index 390b007..0000000 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ListDisksTest.java +++ /dev/null @@ -1,71 +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.ImmutableSet; -import java.io.InputStream; -import java.net.URI; -import java.util.List; -import java.util.Set; -import org.jclouds.azurecompute.domain.Disk; -import org.jclouds.azurecompute.domain.Disk.Attachment; -import org.jclouds.azurecompute.domain.OSType; -import org.jclouds.azurecompute.xml.ListDisksHandler; -import org.jclouds.http.functions.BaseHandlerTest; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertEquals; - -@Test(groups = "unit", testName = "ListDisksTest") -public class ListDisksTest extends BaseHandlerTest { - - public void test() { - InputStream is = getClass().getResourceAsStream("/disks.xml"); - - Set<Disk> expected = expected(); - - ListDisksHandler handler = injector.getInstance(ListDisksHandler.class); - List<Disk> result = factory.create(handler).parse(is); - - assertEquals(result.toString(), expected.toString()); - - } - - public static Set<Disk> expected() { - - return ImmutableSet.<Disk>builder() - .add(Disk.builder() - .os(OSType.LINUX) - .location("West Europe") - .logicalSizeInGB(30) - .mediaLink(URI.create("http://neotysbucket1.blob.core.windows.net/vhds/testimage2-testimage2-2012-08-17.vhd")) - .name("testimage2-testimage2-0-20120817095145") - .sourceImage("OpenLogic__OpenLogic-CentOS-62-20120531-en-us-30GB.vhd") - .build()) - .add(Disk.builder() - .attachedTo(Attachment.builder().deployment("neotysss").hostedService("neotysss").role("neotysss").build()) - .os(OSType.WINDOWS) - .location("West Europe") - .logicalSizeInGB(30) - .mediaLink(URI.create("http://portalvhds0g7xhnq2x7t21.blob.core.windows.net/disks/neotysss/MSFT__Win2K8R2SP1-120612-1520-121206-01-en-us-30GB.vhd")) - .name("neotysss-neotysss-0-20120824091357") - .sourceImage("MSFT__Win2K8R2SP1-120612-1520-121206-01-en-us-30GB.vhd") - .build()) - .build(); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/da40d636/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ListDisksHandlerTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ListDisksHandlerTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ListDisksHandlerTest.java new file mode 100644 index 0000000..f263567 --- /dev/null +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ListDisksHandlerTest.java @@ -0,0 +1,67 @@ +/* + * 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.net.URI; +import java.util.List; + +import org.jclouds.azurecompute.domain.Disk; +import org.jclouds.azurecompute.domain.Disk.Attachment; +import org.jclouds.azurecompute.domain.OSType; +import org.jclouds.http.functions.BaseHandlerTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +@Test(groups = "unit", testName = "ListDisksHandlerTest") +public class ListDisksHandlerTest extends BaseHandlerTest { + + public void test() { + InputStream is = getClass().getResourceAsStream("/disks.xml"); + List<Disk> result = factory.create(new ListDisksHandler()).parse(is); + + assertEquals(result, expected()); + } + + public static List<Disk> expected() { + return ImmutableList.of( // + Disk.create( // + "testimage2-testimage2-0-20120817095145", // name + "West Europe", // location + null, // affinityGroup + null, //description + OSType.LINUX, // os + URI.create("http://blobs/vhds/testimage2-testimage2-2012-08-17.vhd"), // mediaLink + 30, // logicalSizeInGB + null, // attachedTo + "OpenLogic__OpenLogic-CentOS-62-20120531-en-us-30GB.vhd" // sourceImage + ), Disk.create( // + "neotysss-neotysss-0-20120824091357", // name + "West Europe", // location + null, // affinityGroup + null, //description + OSType.WINDOWS, // os + URI.create("http://blobs/disks/neotysss/MSFT__Win2K8R2SP1-ABCD-en-us-30GB.vhd"), // mediaLink + 30, // logicalSizeInGB + Attachment.create("neotysss", "neotysss", "neotysss"), // attachedTo + "MSFT__Win2K8R2SP1-ABCD-en-us-30GB.vhd" // sourceImage + )); + } +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/da40d636/azurecompute/src/test/resources/disks.xml ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/resources/disks.xml b/azurecompute/src/test/resources/disks.xml index afae399..2e9b453 100644 --- a/azurecompute/src/test/resources/disks.xml +++ b/azurecompute/src/test/resources/disks.xml @@ -3,7 +3,7 @@ <OS>Linux</OS> <Location>West Europe</Location> <LogicalDiskSizeInGB>30</LogicalDiskSizeInGB> - <MediaLink>http://neotysbucket1.blob.core.windows.net/vhds/testimage2-testimage2-2012-08-17.vhd</MediaLink> + <MediaLink>http://blobs/vhds/testimage2-testimage2-2012-08-17.vhd</MediaLink> <Name>testimage2-testimage2-0-20120817095145</Name> <SourceImageName>OpenLogic__OpenLogic-CentOS-62-20120531-en-us-30GB.vhd</SourceImageName> </Disk> @@ -16,8 +16,8 @@ <OS>Windows</OS> <Location>West Europe</Location> <LogicalDiskSizeInGB>30</LogicalDiskSizeInGB> - <MediaLink>http://portalvhds0g7xhnq2x7t21.blob.core.windows.net/disks/neotysss/MSFT__Win2K8R2SP1-120612-1520-121206-01-en-us-30GB.vhd</MediaLink> + <MediaLink>http://blobs/disks/neotysss/MSFT__Win2K8R2SP1-ABCD-en-us-30GB.vhd</MediaLink> <Name>neotysss-neotysss-0-20120824091357</Name> - <SourceImageName>MSFT__Win2K8R2SP1-120612-1520-121206-01-en-us-30GB.vhd</SourceImageName> + <SourceImageName>MSFT__Win2K8R2SP1-ABCD-en-us-30GB.vhd</SourceImageName> </Disk> </Disks>
