mirror of https://github.com/apache/jclouds.git
Issue 695: Added VirtalMachines builder+test
This commit is contained in:
parent
0cada01d5e
commit
f823a4d9de
|
@ -313,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;
|
||||
|
@ -321,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() {
|
||||
|
|
|
@ -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,35 +37,74 @@ 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();
|
||||
}
|
||||
|
||||
@XmlElement(name = "VirtualMachine")
|
||||
void setVirtualMachine(VirtualMachine nic) {
|
||||
this.virtualMachines.add(nic);
|
||||
}
|
||||
public Builder toBuilder() {
|
||||
return new Builder().fromVirtualMachines(this);
|
||||
}
|
||||
|
||||
public Set<VirtualMachine> getVirtualMachines() {
|
||||
return Collections.unmodifiableSet(virtualMachines);
|
||||
}
|
||||
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<VirtualMachine> virtualMachines = Sets.newLinkedHashSet();
|
||||
|
||||
VirtualMachines vms1 = (VirtualMachines) o;
|
||||
/**
|
||||
* @see VirtualMachines#getVirtualMachines
|
||||
*/
|
||||
public Builder virtualMachines(Set<VirtualMachine> virtualMachines) {
|
||||
this.virtualMachines = Sets.newLinkedHashSet(checkNotNull(virtualMachines, "virtualMachines"));
|
||||
return this;
|
||||
}
|
||||
|
||||
if (!virtualMachines.equals(vms1.virtualMachines)) return false;
|
||||
public Builder addVirtualMachine(VirtualMachine virtualMachine) {
|
||||
virtualMachines.add(checkNotNull(virtualMachine,"virtualMachine"));
|
||||
return this;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
public VirtualMachines build() {
|
||||
return new VirtualMachines(virtualMachines);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return virtualMachines.hashCode();
|
||||
}
|
||||
public Builder fromVirtualMachines(VirtualMachines in) {
|
||||
return virtualMachines(in.getVirtualMachines());
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "["+ virtualMachines.toString()+"]";
|
||||
}
|
||||
private VirtualMachines() {
|
||||
//For JAXB and builder use
|
||||
}
|
||||
|
||||
private VirtualMachines(Set<VirtualMachine> virtualMachines) {
|
||||
this.virtualMachines = Sets.newLinkedHashSet(virtualMachines);
|
||||
}
|
||||
|
||||
@XmlElement(name = "VirtualMachine")
|
||||
private LinkedHashSet<VirtualMachine> virtualMachines = Sets.newLinkedHashSet();
|
||||
|
||||
public Set<VirtualMachine> getVirtualMachines() {
|
||||
return Collections.unmodifiableSet(virtualMachines);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
VirtualMachines vms1 = (VirtualMachines) o;
|
||||
|
||||
if (!virtualMachines.equals(vms1.virtualMachines)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return virtualMachines.hashCode();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "["+ virtualMachines.toString()+"]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue