Merge pull request #183 from jsonking/695-1

Issue 695: Created builders for collection wrapper classes. Removed setters and annotation postiton.
This commit is contained in:
Jason King 2011-11-22 02:15:52 -08:00
commit f5fb1e6ee2
15 changed files with 658 additions and 116 deletions

View File

@ -68,7 +68,7 @@ public class Actions {
}
}
public Actions() {
private Actions() {
//For JAXB and builder use
}

View File

@ -123,7 +123,7 @@ public class Link extends BaseNamedResource<Link> {
@Override
public Link build() {
return new Link(href, name, type, rel);
return new Link(href, type, name, rel);
}
public Builder fromLink(Link in) {

View File

@ -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,11 +34,50 @@ import java.util.Set;
*/
public class Links {
private LinkedHashSet<Link> links = Sets.newLinkedHashSet();
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromLinks(this);
}
public static class Builder {
private Set<Link> links = Sets.newLinkedHashSet();
/**
* @see Links#getLinks
*/
public Builder links(Set<Link> links) {
this.links = Sets.newLinkedHashSet(checkNotNull(links, "links"));
return this;
}
public Builder addLink(Link link) {
links.add(checkNotNull(link,"link"));
return this;
}
public Links build() {
return new Links(links);
}
public Builder fromLinks(Links in) {
return links(in.getLinks());
}
}
@XmlElement(name = "Link")
public void setLink(Link link) {
this.links.add(link);
private LinkedHashSet<Link> links = Sets.newLinkedHashSet();
private Links() {
//For JAXB
}
private Links(Set<Link> links) {
this.links = Sets.newLinkedHashSet(links);
}
public Set<Link> getLinks() {

View File

@ -36,14 +36,54 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
@XmlRootElement(name = "Tasks")
public class Tasks {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromTasks(this);
}
public static class Builder {
private Set<Task> tasks = Sets.newLinkedHashSet();
/**
* @see Tasks#getTasks
*/
public Builder tasks(Set<Task> tasks) {
this.tasks = Sets.newLinkedHashSet(checkNotNull(tasks, "tasks"));
return this;
}
public Builder addTask(Task task) {
tasks.add(checkNotNull(task,"task"));
return this;
}
public Tasks build() {
return new Tasks(tasks);
}
public Builder fromTasks(Tasks in) {
return tasks(in.getTasks());
}
}
private Tasks() {
//For JAXB and builder use
}
private Tasks(Set<Task> tasks) {
this.tasks = Sets.newLinkedHashSet(tasks);
}
//TODO: There is a total count field
private LinkedHashSet<Task> tasks = Sets.newLinkedHashSet();
@XmlElement(name = "Task")
public void setTask(Task task) {
checkNotNull(task,"task");
tasks.add(task);
}
private LinkedHashSet<Task> tasks = Sets.newLinkedHashSet();
public Set<Task> getTasks() {
return Collections.unmodifiableSet(tasks);

View File

@ -35,9 +35,52 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public class Disks {
private LinkedHashSet<VirtualDisk> disks = Sets.newLinkedHashSet();
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromDisks(this);
}
public static class Builder {
private Set<VirtualDisk> disks = Sets.newLinkedHashSet();
/**
* @see Disks#getVirtualDisks()
*/
public Builder disks(Set<VirtualDisk> disks) {
this.disks = Sets.newLinkedHashSet(checkNotNull(disks, "disks"));
return this;
}
public Builder addDisk(VirtualDisk disk) {
disks.add(checkNotNull(disk,"disk"));
return this;
}
public Disks build() {
return new Disks(disks);
}
public Builder fromDisks(Disks in) {
return disks(in.getVirtualDisks());
}
}
@XmlElement(name = "Disk")
private LinkedHashSet<VirtualDisk> disks = Sets.newLinkedHashSet();
private Disks() {
//For JAXB and builder use
}
private Disks(Set<VirtualDisk> disks) {
this.disks = Sets.newLinkedHashSet(disks);
}
public void setVirtualDisk(VirtualDisk disk) {
checkNotNull(disk,"disk");
this.disks.add(disk);

View File

@ -151,7 +151,7 @@ public class HardwareConfiguration extends BaseResource<HardwareConfiguration> {
}
@XmlElement(name = "Actions", required = false)
private Actions actions = new Actions();
private Actions actions = Actions.builder().build();
@XmlElement(name = "ProcessorCount", required = true)
private int processorCount;
@ -160,16 +160,15 @@ public class HardwareConfiguration extends BaseResource<HardwareConfiguration> {
private Memory memory;
@XmlElement(name = "Disks", required = false)
private Disks virtualDisks = new Disks();
private Disks virtualDisks = Disks.builder().build();
@XmlElement(name = "Nics", required = false)
private Nics virtualNics = new Nics();
private Nics virtualNics = Nics.builder().build();
public HardwareConfiguration(Set<Action> actions, int processorCount, @Nullable Memory memory, Set<VirtualDisk> virtualDisks, Set<VirtualNic> virtualNics) {
this.actions = Actions.builder().actions(checkNotNull(actions, "actions")).build();
for( VirtualDisk disk: checkNotNull(virtualDisks, "virtualDisks")) this.virtualDisks.setVirtualDisk(disk);
for( VirtualNic virtualNic: checkNotNull(virtualNics, "virtualNics")) this.virtualNics.setVirtualNic(virtualNic);
this.virtualDisks = Disks.builder().disks(checkNotNull(virtualDisks,"virtualDisks")).build();
this.virtualNics = Nics.builder().nics(checkNotNull(virtualNics, "virtualNics")).build();
this.processorCount = processorCount;
this.memory = memory;
}

View File

@ -37,7 +37,7 @@ public class AssignedIpAddresses extends BaseResource<AssignedIpAddresses> {
//TODO links
@XmlElement(name = "Actions", required = true)
private Actions actions = new Actions();
private Actions actions = Actions.builder().build();
@XmlElement(name = "Networks", required = true)
private DeviceNetworks networks = new DeviceNetworks();

View File

@ -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 VirtualNic elements.
* Needed because parsing is done with JAXB and it does not handle Generic collections
@ -33,12 +35,51 @@ import java.util.Set;
*/
public class Nics {
private LinkedHashSet<VirtualNic> nics = Sets.newLinkedHashSet();
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromNics(this);
}
public static class Builder {
private Set<VirtualNic> nics = Sets.newLinkedHashSet();
/**
* @see Nics#getVirtualNics()
*/
public Builder nics(Set<VirtualNic> nics) {
this.nics = Sets.newLinkedHashSet(checkNotNull(nics, "nics"));
return this;
}
public Builder addVirtualNic(VirtualNic nic) {
nics.add(checkNotNull(nic,"nic"));
return this;
}
public Nics build() {
return new Nics(nics);
}
public Builder fromNics(Nics in) {
return nics(in.getVirtualNics());
}
}
private Nics() {
//For JAXB and builder use
}
private Nics(Set<VirtualNic> nics) {
this.nics = Sets.newLinkedHashSet(nics);
}
@XmlElement(name = "Nic")
public void setVirtualNic(VirtualNic nic) {
this.nics.add(nic);
}
private LinkedHashSet<VirtualNic> nics = Sets.newLinkedHashSet();
public Set<VirtualNic> getVirtualNics() {
return Collections.unmodifiableSet(nics);
@ -64,5 +105,4 @@ public class Nics {
public String toString() {
return "["+ nics.toString()+"]";
}
}

View File

@ -62,9 +62,9 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
public static class Builder extends BaseNamedResource.Builder<VirtualMachine> {
//TODO There are some more fields
private Links links = new Links();
private Actions actions = new Actions();
private Tasks tasks = new Tasks();
private Links links = Links.builder().build();
private Actions actions = Actions.builder().build();
private Tasks tasks = Tasks.builder().build();
private String description;
private VirtualMachineStatus status;
@ -81,8 +81,7 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
* @see VirtualMachine#getLinks
*/
public Builder links(Set<Link> links) {
checkNotNull(links,"links");
for(Link link:links) this.links.setLink(link);
this.links = Links.builder().links(checkNotNull(links,"links")).build();
return this;
}
@ -99,12 +98,10 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
* @see VirtualMachine#getTasks
*/
public Builder tasks(Set<Task> tasks) {
checkNotNull(tasks,"tasks");
for(Task task: tasks) this.tasks.setTask(task);
this.tasks = Tasks.builder().tasks(checkNotNull(tasks,"tasks")).build();
return this;
}
/**
* @see VirtualMachine#getDescription
*/
@ -269,13 +266,13 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
}
@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();
private Tasks tasks = Tasks.builder().build();
@XmlElement(name = "Actions", required = true)
private Actions actions = new Actions();
private Actions actions = Actions.builder().build();
@XmlElement(name = "Description", required = true)
private String description;
@ -316,6 +313,7 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
this.tasks = checkNotNull(tasks, "tasks");
this.actions = checkNotNull(actions, "actions");
this.status = checkNotNull(status, "status");
this.ipAddresses = checkNotNull(ipAddresses, "ipAddresses");
this.layout = layout;
this.poweredOn = poweredOn;
@ -324,7 +322,6 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
this.customizationPending = customizationPending;
this.operatingSystem = operatingSystem;
this.hardwareConfiguation = hardwareConfiguration;
this.ipAddresses = checkNotNull(ipAddresses, "ipAddresses");
}
protected VirtualMachine() {

View File

@ -26,6 +26,8 @@ import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Wraps individual VirtualMachine elements.
* Needed because parsing is done with JAXB and it does not handle Generic collections
@ -35,12 +37,51 @@ import java.util.Set;
@XmlRootElement(name="VirtualMachines")
public class VirtualMachines {
private LinkedHashSet<VirtualMachine> virtualMachines = Sets.newLinkedHashSet();
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromVirtualMachines(this);
}
public static class Builder {
private Set<VirtualMachine> virtualMachines = Sets.newLinkedHashSet();
/**
* @see VirtualMachines#getVirtualMachines
*/
public Builder virtualMachines(Set<VirtualMachine> virtualMachines) {
this.virtualMachines = Sets.newLinkedHashSet(checkNotNull(virtualMachines, "virtualMachines"));
return this;
}
public Builder addVirtualMachine(VirtualMachine virtualMachine) {
virtualMachines.add(checkNotNull(virtualMachine,"virtualMachine"));
return this;
}
public VirtualMachines build() {
return new VirtualMachines(virtualMachines);
}
public Builder fromVirtualMachines(VirtualMachines in) {
return virtualMachines(in.getVirtualMachines());
}
}
private VirtualMachines() {
//For JAXB and builder use
}
private VirtualMachines(Set<VirtualMachine> virtualMachines) {
this.virtualMachines = Sets.newLinkedHashSet(virtualMachines);
}
@XmlElement(name = "VirtualMachine")
void setVirtualMachine(VirtualMachine nic) {
this.virtualMachines.add(nic);
}
private LinkedHashSet<VirtualMachine> virtualMachines = Sets.newLinkedHashSet();
public Set<VirtualMachine> getVirtualMachines() {
return Collections.unmodifiableSet(virtualMachines);

View File

@ -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<Link> linkSet = twoLinks.getLinks();
assertEquals(2, linkSet.size());
assertTrue(linkSet.contains(link));
assertTrue(linkSet.contains(link2));
}
}

View File

@ -0,0 +1,89 @@
/**
* 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.Date;
import java.util.Set;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* @author Jason King
*/
@Test(groups = "unit", testName = "TasksTest")
public class TasksTest {
private Task task;
private Tasks tasks;
@BeforeMethod()
public void setUp() throws URISyntaxException {
task = Task
.builder()
.href(URI.create("/tasks/1002"))
.type("application/vnd.tmrk.cloud.task")
.operation("test task")
.status(Task.Status.ERROR)
.impactedItem(
NamedResource.builder().href(URI.create("/item/1")).name("sample item 1")
.type("application/vnd.tmrk.cloud.nodeService").build())
.startTime(new Date())
.completedTime(new Date())
.notes("Some notes about the operation.")
.errorMessage("sample error message 1 here")
.initiatedBy(
NamedResource.builder().href(URI.create("/users/1")).name("User 1")
.type("application/vnd.tmrk.cloud.admin.user").build()).build();
tasks = Tasks.builder().addTask(task).build();
}
@Test
public void testAddAction() throws URISyntaxException {
Task task2 = Task
.builder()
.href(URI.create("/tasks/1003"))
.type("application/vnd.tmrk.cloud.task")
.operation("test task 2")
.status(Task.Status.ERROR)
.impactedItem(
NamedResource.builder().href(URI.create("/item/2")).name("sample item 2")
.type("application/vnd.tmrk.cloud.nodeService").build())
.startTime(new Date())
.completedTime(new Date())
.notes("Some notes about the operation.")
.errorMessage("sample error message 2 here")
.initiatedBy(
NamedResource.builder().href(URI.create("/users/2")).name("User 2")
.type("application/vnd.tmrk.cloud.admin.user").build()).build();
Tasks twoTasks = tasks.toBuilder().addTask(task2).build();
Set<Task> taskSet = twoTasks.getTasks();
assertEquals(2,taskSet.size());
assertTrue(taskSet.contains(task));
assertTrue(taskSet.contains(task2));
}
}

View File

@ -0,0 +1,56 @@
/**
* 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.hardware;
import org.jclouds.tmrk.enterprisecloud.domain.Size;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.URISyntaxException;
import java.util.Set;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* @author Jason King
*/
@Test(groups = "unit", testName = "DisksTest")
public class DisksTest {
private VirtualDisk disk;
private Disks disks;
@BeforeMethod()
public void setUp() throws URISyntaxException {
disk = VirtualDisk.builder().index(0).name("test disk").size(Size.builder().value(1).unit("GB").build()).build();
disks = Disks.builder().addDisk(disk).build();
}
@Test
public void testAddDisk() throws URISyntaxException {
VirtualDisk disk2 = VirtualDisk.builder().index(1).name("test disk 1").size(Size.builder().value(1).unit("GB").build()).build();
Disks twoDisks = disks.toBuilder().addDisk(disk2).build();
Set<VirtualDisk> virtualDisks = twoDisks.getVirtualDisks();
assertEquals(2, virtualDisks.size());
assertTrue(virtualDisks.contains(disk));
assertTrue(virtualDisks.contains(disk2));
}
}

View File

@ -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.network;
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.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* @author Jason King
*/
@Test(groups = "unit", testName = "NicsTest")
public class NicsTest {
private NetworkReference networkReference;
private VirtualNic nic;
private Nics nics;
@BeforeMethod()
public void setUp() throws URISyntaxException {
networkReference = NetworkReference.builder().href(new URI("/netref")).type("a network ref").name("my network ref").build();
nic = VirtualNic.builder().macAddress("aa:bb").name("my nic").network(networkReference).unitNumber(1).build();
nics = Nics.builder().addVirtualNic(nic).build();
}
@Test
public void testAddDisk() throws URISyntaxException {
VirtualNic nic2 = VirtualNic.builder().macAddress("aa:cc").name("my nic 2").network(networkReference).unitNumber(2).build();
Nics twoNics = nics.toBuilder().addVirtualNic(nic2).build();
Set<VirtualNic> nicSet = twoNics.getVirtualNics();
assertEquals(2, nicSet.size());
assertTrue(nicSet.contains(nic));
assertTrue(nicSet.contains(nic2));
}
}

View File

@ -0,0 +1,80 @@
/**
* 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.vm;
import org.jclouds.tmrk.enterprisecloud.domain.Actions;
import org.jclouds.tmrk.enterprisecloud.domain.Links;
import org.jclouds.tmrk.enterprisecloud.domain.Tasks;
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.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* @author Jason King
*/
@Test(groups = "unit", testName = "ActionsTest")
public class VirtualMachinesTest {
private VirtualMachine virtualMachine;
private VirtualMachines virtualMachines;
@BeforeMethod()
public void setUp() throws URISyntaxException {
virtualMachine = VirtualMachine.builder()
.description("This is a test VM")
.name("Test VM")
.href(new URI("/test"))
.type("Test VM")
.actions(Actions.builder().build().getActions())
.links(Links.builder().build().getLinks())
.tasks(Tasks.builder().build().getTasks())
.status(VirtualMachine.VirtualMachineStatus.NOT_DEPLOYED)
.ipAddresses(new VirtualMachineIpAddresses())
.build();
virtualMachines = VirtualMachines.builder().addVirtualMachine(virtualMachine).build();
}
@Test
public void testAddAction() throws URISyntaxException {
VirtualMachine virtualMachine2 = VirtualMachine.builder()
.description("This is a test VM 2")
.name("Test VM 2")
.href(new URI("/test/2"))
.type("Test VM 2")
.actions(Actions.builder().build().getActions())
.links(Links.builder().build().getLinks())
.tasks(Tasks.builder().build().getTasks())
.status(VirtualMachine.VirtualMachineStatus.NOT_DEPLOYED)
.ipAddresses(new VirtualMachineIpAddresses())
.build();
VirtualMachines twoVirtualMachines = virtualMachines.toBuilder().addVirtualMachine(virtualMachine2).build();
Set<VirtualMachine> virtualMachineSet = twoVirtualMachines.getVirtualMachines();
assertEquals(2, virtualMachineSet.size());
assertTrue(virtualMachineSet.contains(virtualMachine));
assertTrue(virtualMachineSet.contains(virtualMachine2));
}
}