diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Tasks.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Tasks.java index 8c411d3701..f3d5224fe6 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Tasks.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/Tasks.java @@ -36,37 +36,77 @@ import static com.google.common.base.Preconditions.checkNotNull; */ @XmlRootElement(name = "Tasks") public class Tasks { - //TODO: There is a total count field - private LinkedHashSet tasks = Sets.newLinkedHashSet(); - @XmlElement(name = "Task") - public void setTask(Task task) { - checkNotNull(task,"task"); - tasks.add(task); - } + @SuppressWarnings("unchecked") + public static Builder builder() { + return new Builder(); + } - public Set getTasks() { - return Collections.unmodifiableSet(tasks); - } + public Builder toBuilder() { + return new Builder().fromTasks(this); + } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + public static class Builder { - Tasks tasks1 = (Tasks) o; + private Set tasks = Sets.newLinkedHashSet(); - if (!tasks.equals(tasks1.tasks)) return false; + /** + * @see Tasks#getTasks + */ + public Builder tasks(Set tasks) { + this.tasks = Sets.newLinkedHashSet(checkNotNull(tasks, "tasks")); + return this; + } - return true; - } + public Builder addTask(Task task) { + tasks.add(checkNotNull(task,"task")); + return this; + } - @Override - public int hashCode() { - return tasks.hashCode(); - } + public Tasks build() { + return new Tasks(tasks); + } - public String toString() { - return "["+tasks.toString()+"]"; - } + public Builder fromTasks(Tasks in) { + return tasks(in.getTasks()); + } + } + + private Tasks() { + //For JAXB and builder use + } + + private Tasks(Set tasks) { + this.tasks = Sets.newLinkedHashSet(tasks); + } + + //TODO: There is a total count field + + @XmlElement(name = "Task") + private LinkedHashSet tasks = Sets.newLinkedHashSet(); + + public Set getTasks() { + return Collections.unmodifiableSet(tasks); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Tasks tasks1 = (Tasks) o; + + if (!tasks.equals(tasks1.tasks)) return false; + + return true; + } + + @Override + public int hashCode() { + return tasks.hashCode(); + } + + public String toString() { + return "["+tasks.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 5746011698..1d66ba870c 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 @@ -64,7 +64,7 @@ public class VirtualMachine extends BaseNamedResource { //TODO There are some more fields private Links links = Links.builder().build(); private Actions actions = Actions.builder().build(); - private Tasks tasks = new Tasks(); + private Tasks tasks = Tasks.builder().build(); private String description; private VirtualMachineStatus status; @@ -98,12 +98,10 @@ public class VirtualMachine extends BaseNamedResource { * @see VirtualMachine#getTasks */ public Builder tasks(Set 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 */ @@ -271,7 +269,7 @@ public class VirtualMachine extends BaseNamedResource { 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 = Actions.builder().build(); diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/TasksTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/TasksTest.java new file mode 100644 index 0000000000..12b393fb45 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/TasksTest.java @@ -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 taskSet = twoTasks.getTasks(); + + assertEquals(2,taskSet.size()); + assertTrue(taskSet.contains(task)); + assertTrue(taskSet.contains(task2)); + } +}