Issue 695: Added Builder+Test for Nics

This commit is contained in:
Jason King 2011-11-21 17:15:40 +00:00
parent e2d7b66b0c
commit 7feefaff9e
3 changed files with 126 additions and 24 deletions

View File

@ -163,7 +163,7 @@ public class HardwareConfiguration extends BaseResource<HardwareConfiguration> {
private Disks virtualDisks = Disks.builder().build(); private Disks virtualDisks = Disks.builder().build();
@XmlElement(name = "Nics", required = false) @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) { 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(); this.actions = Actions.builder().actions(checkNotNull(actions, "actions")).build();

View File

@ -25,6 +25,8 @@ import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Wraps individual VirtualNic elements. * Wraps individual VirtualNic elements.
* Needed because parsing is done with JAXB and it does not handle Generic collections * Needed because parsing is done with JAXB and it does not handle Generic collections
@ -33,36 +35,78 @@ import java.util.Set;
*/ */
public class Nics { public class Nics {
private LinkedHashSet<VirtualNic> nics = Sets.newLinkedHashSet(); @SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
@XmlElement(name = "Nic") public Builder toBuilder() {
public void setVirtualNic(VirtualNic nic) { return new Builder().fromNics(this);
this.nics.add(nic); }
}
public Set<VirtualNic> getVirtualNics() { public static class Builder {
return Collections.unmodifiableSet(nics);
}
@Override private Set<VirtualNic> nics = Sets.newLinkedHashSet();
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Nics nics1 = (Nics) o; /**
* @see Nics#getVirtualNics()
*/
public Builder nics(Set<VirtualNic> nics) {
this.nics = Sets.newLinkedHashSet(checkNotNull(nics, "nics"));
return this;
}
if (!nics.equals(nics1.nics)) return false; public Builder addVirtualNic(VirtualNic nic) {
nics.add(checkNotNull(nic,"nic"));
return this;
}
return true; public Nics build() {
} return new Nics(nics);
}
@Override public Builder fromNics(Nics in) {
public int hashCode() { return nics(in.getVirtualNics());
return nics.hashCode(); }
} }
public String toString() { private Nics() {
return "["+ nics.toString()+"]"; //For JAXB and builder use
} }
private Nics(Set<VirtualNic> nics) {
this.nics = Sets.newLinkedHashSet(nics);
}
private LinkedHashSet<VirtualNic> nics = Sets.newLinkedHashSet();
@XmlElement(name = "Nic")
public void setVirtualNic(VirtualNic nic) {
this.nics.add(nic);
}
public Set<VirtualNic> getVirtualNics() {
return Collections.unmodifiableSet(nics);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Nics nics1 = (Nics) o;
if (!nics.equals(nics1.nics)) return false;
return true;
}
@Override
public int hashCode() {
return nics.hashCode();
}
public String toString() {
return "["+ nics.toString()+"]";
}
} }

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