Issue 695: Added builder for Tasks, test and removed setter

This commit is contained in:
Jason King 2011-11-21 17:48:19 +00:00
parent a1c0041650
commit c67072f5b5
3 changed files with 157 additions and 30 deletions

View File

@ -36,37 +36,77 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
@XmlRootElement(name = "Tasks") @XmlRootElement(name = "Tasks")
public class Tasks { public class Tasks {
//TODO: There is a total count field
private LinkedHashSet<Task> tasks = Sets.newLinkedHashSet();
@XmlElement(name = "Task") @SuppressWarnings("unchecked")
public void setTask(Task task) { public static Builder builder() {
checkNotNull(task,"task"); return new Builder();
tasks.add(task); }
}
public Set<Task> getTasks() { public Builder toBuilder() {
return Collections.unmodifiableSet(tasks); return new Builder().fromTasks(this);
} }
@Override public static class Builder {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tasks tasks1 = (Tasks) o; private Set<Task> tasks = Sets.newLinkedHashSet();
if (!tasks.equals(tasks1.tasks)) return false; /**
* @see Tasks#getTasks
*/
public Builder tasks(Set<Task> 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 Tasks build() {
public int hashCode() { return new Tasks(tasks);
return tasks.hashCode(); }
}
public String toString() { public Builder fromTasks(Tasks in) {
return "["+tasks.toString()+"]"; 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
@XmlElement(name = "Task")
private LinkedHashSet<Task> tasks = Sets.newLinkedHashSet();
public Set<Task> 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()+"]";
}
} }

View File

@ -64,7 +64,7 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
//TODO There are some more fields //TODO There are some more fields
private Links links = Links.builder().build(); private Links links = Links.builder().build();
private Actions actions = Actions.builder().build(); private Actions actions = Actions.builder().build();
private Tasks tasks = new Tasks(); private Tasks tasks = Tasks.builder().build();
private String description; private String description;
private VirtualMachineStatus status; private VirtualMachineStatus status;
@ -98,12 +98,10 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
* @see VirtualMachine#getTasks * @see VirtualMachine#getTasks
*/ */
public Builder tasks(Set<Task> tasks) { public Builder tasks(Set<Task> tasks) {
checkNotNull(tasks,"tasks"); this.tasks = Tasks.builder().tasks(checkNotNull(tasks,"tasks")).build();
for(Task task: tasks) this.tasks.setTask(task);
return this; return this;
} }
/** /**
* @see VirtualMachine#getDescription * @see VirtualMachine#getDescription
*/ */
@ -271,7 +269,7 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
private Links links = Links.builder().build(); private Links links = Links.builder().build();
@XmlElement(name = "Tasks", required = true) @XmlElement(name = "Tasks", required = true)
private Tasks tasks = new Tasks(); private Tasks tasks = Tasks.builder().build();
@XmlElement(name = "Actions", required = true) @XmlElement(name = "Actions", required = true)
private Actions actions = Actions.builder().build(); private Actions actions = Actions.builder().build();

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));
}
}