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,6 +35,49 @@ import java.util.Set;
*/ */
public class Nics { public class Nics {
@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);
}
private LinkedHashSet<VirtualNic> nics = Sets.newLinkedHashSet(); private LinkedHashSet<VirtualNic> nics = Sets.newLinkedHashSet();
@XmlElement(name = "Nic") @XmlElement(name = "Nic")
@ -64,5 +109,4 @@ public class Nics {
public String toString() { public String toString() {
return "["+ nics.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));
}
}