diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Link.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Link.java index f72b0f94b1..c4a6e4b5af 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Link.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Link.java @@ -123,7 +123,7 @@ public class Link extends BaseNamedResource { @Override public Link build() { - return new Link(href, name, type, rel); + return new Link(href, type, name, rel); } public Builder fromLink(Link in) { diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Links.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Links.java index f016c873aa..f3b41f82b3 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Links.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Links.java @@ -25,6 +25,8 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; +import static com.google.common.base.Preconditions.checkNotNull; + /** * Wraps individual Link elements. * Needed because parsing is done with JAXB and it does not handle Generic collections @@ -32,36 +34,75 @@ import java.util.Set; */ public class Links { - private LinkedHashSet links = Sets.newLinkedHashSet(); + @SuppressWarnings("unchecked") + public static Builder builder() { + return new Builder(); + } - @XmlElement(name = "Link") - public void setLink(Link link) { - this.links.add(link); - } + public Builder toBuilder() { + return new Builder().fromLinks(this); + } - public Set getLinks() { - return Collections.unmodifiableSet(links); - } + public static class Builder { - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + private Set links = Sets.newLinkedHashSet(); - Links links1 = (Links) o; + /** + * @see Links#getLinks + */ + public Builder links(Set links) { + this.links = Sets.newLinkedHashSet(checkNotNull(links, "links")); + return this; + } - if (!links.equals(links1.links)) return false; + public Builder addLink(Link link) { + links.add(checkNotNull(link,"link")); + return this; + } - return true; - } + public Links build() { + return new Links(links); + } - @Override - public int hashCode() { - return links.hashCode(); - } + public Builder fromLinks(Links in) { + return links(in.getLinks()); + } + } - public String toString() { - return "["+ links.toString()+"]"; - } + @XmlElement(name = "Link") + private LinkedHashSet links = Sets.newLinkedHashSet(); + + private Links() { + //For JAXB + } + + private Links(Set links) { + this.links = Sets.newLinkedHashSet(links); + } + + public Set getLinks() { + return Collections.unmodifiableSet(links); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Links links1 = (Links) o; + + if (!links.equals(links1.links)) return false; + + return true; + } + + @Override + public int hashCode() { + return links.hashCode(); + } + + public String toString() { + return "["+ links.toString()+"]"; + } } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/vm/VirtualMachine.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/vm/VirtualMachine.java index affdb76981..5746011698 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/vm/VirtualMachine.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/vm/VirtualMachine.java @@ -62,7 +62,7 @@ public class VirtualMachine extends BaseNamedResource { public static class Builder extends BaseNamedResource.Builder { //TODO There are some more fields - private Links links = new Links(); + private Links links = Links.builder().build(); private Actions actions = Actions.builder().build(); private Tasks tasks = new Tasks(); private String description; @@ -81,8 +81,7 @@ public class VirtualMachine extends BaseNamedResource { * @see VirtualMachine#getLinks */ public Builder links(Set links) { - checkNotNull(links,"links"); - for(Link link:links) this.links.setLink(link); + this.links = Links.builder().links(checkNotNull(links,"links")).build(); return this; } @@ -269,7 +268,7 @@ public class VirtualMachine extends BaseNamedResource { } @XmlElement(name = "Links", required = true) - private Links links = new Links(); + private Links links = Links.builder().build(); @XmlElement(name = "Tasks", required = true) private Tasks tasks = new Tasks(); diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/LinksTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/LinksTest.java new file mode 100644 index 0000000000..72eb78bd09 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/LinksTest.java @@ -0,0 +1,58 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.tmrk.enterprisecloud.domain; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Set; + +import static org.jclouds.tmrk.enterprisecloud.domain.Link.Relationship; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +/** + * @author Jason King + */ +@Test(groups = "unit", testName = "LinksTest") +public class LinksTest { + + private Link link; + private Links links; + + @BeforeMethod() + public void setUp() throws URISyntaxException { + link = Link.builder().href(new URI("/1")).name("my link").type("test link").rel(Relationship.FIRST).build(); + links = Links.builder().addLink(link).build(); + + } + + @Test + public void testAddAction() throws URISyntaxException { + Link link2 = Link.builder().href(new URI("/2")).name("my link 2").type("test link").rel(Relationship.LAST).build(); + Links twoLinks = links.toBuilder().addLink(link2).build(); + Set linkSet = twoLinks.getLinks(); + + assertEquals(2, linkSet.size()); + assertTrue(linkSet.contains(link)); + assertTrue(linkSet.contains(link2)); + } +}