PageRenderTime 28ms CodeModel.GetById 9ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/nl/bitbrains/nebu/common/topology/TestPhysicalRack.java

https://github.com/deltaforge/nebu-common-java
Java | 322 lines | 234 code | 47 blank | 41 comment | 4 complexity | ca52293a0c365084d20fba29cae0f19a MD5 | raw file
  1package nl.bitbrains.nebu.common.topology;
  2
  3import java.util.ArrayList;
  4
  5import junitparams.JUnitParamsRunner;
  6import junitparams.Parameters;
  7import nl.bitbrains.nebu.common.topology.PhysicalDataCenter;
  8import nl.bitbrains.nebu.common.topology.PhysicalHost;
  9import nl.bitbrains.nebu.common.topology.PhysicalRack;
 10import nl.bitbrains.nebu.common.topology.PhysicalRackBuilder;
 11import nl.bitbrains.nebu.common.topology.PhysicalStore;
 12
 13import org.junit.Assert;
 14import org.junit.Before;
 15import org.junit.Test;
 16import org.junit.runner.RunWith;
 17import org.mockito.Mockito;
 18
 19/**
 20 * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
 21 * 
 22 */
 23@RunWith(JUnitParamsRunner.class)
 24public class TestPhysicalRack {
 25
 26    private static final String rackID = "id";
 27    private static final String otherID = "other";
 28
 29    private PhysicalRack rack;
 30    private PhysicalRack cloneRack;
 31    private PhysicalRack otherRack;
 32
 33    @Before
 34    public void setUp() {
 35        this.rack = new PhysicalRackBuilder().withUuid(TestPhysicalRack.rackID).build();
 36        this.cloneRack = new PhysicalRackBuilder().withUuid(TestPhysicalRack.rackID).build();
 37        this.otherRack = new PhysicalRackBuilder().withUuid(TestPhysicalRack.otherID).build();
 38    }
 39
 40    public static PhysicalHost mockCPU(final String identifier) {
 41        final PhysicalHost cpu = Mockito.mock(PhysicalHost.class);
 42        Mockito.when(cpu.getUniqueIdentifier()).thenReturn(identifier);
 43        return cpu;
 44    }
 45
 46    public static PhysicalStore mockDisk(final String identifier) {
 47        final PhysicalStore store = Mockito.mock(PhysicalStore.class);
 48        Mockito.when(store.getUniqueIdentifier()).thenReturn(identifier);
 49        return store;
 50    }
 51
 52    @Test
 53    public void testEmptyConstructor() {
 54        Assert.assertEquals(TestPhysicalRack.rackID, this.rack.getUniqueIdentifier());
 55        Assert.assertNotNull(this.rack.getCPUs());
 56    }
 57
 58    @Test
 59    public void testIDIsRequired() {
 60        boolean caught = false;
 61        try {
 62            new PhysicalRackBuilder().build();
 63        } catch (final IllegalStateException e) {
 64            caught = true;
 65        }
 66        Assert.assertTrue(caught);
 67    }
 68
 69    @Test
 70    public void testListConstructorWithNullList() {
 71        Assert.assertTrue(this.rack.getCPUs().isEmpty());
 72    }
 73
 74    @Test
 75    public void testListConstructorWithNullEntry() {
 76        final ArrayList<PhysicalHost> cpuList = new ArrayList<PhysicalHost>();
 77        cpuList.add(null);
 78
 79        final PhysicalRackBuilder builder = new PhysicalRackBuilder();
 80        builder.withUuid("Rack");
 81        boolean caught = false;
 82        try {
 83            builder.withHosts(cpuList);
 84        } catch (final IllegalArgumentException e) {
 85            caught = true;
 86        }
 87        // Then
 88        Assert.assertTrue(caught);
 89    }
 90
 91    @Test
 92    public void testListConstructorWithCPUs() {
 93        final ArrayList<PhysicalHost> cpuList = new ArrayList<PhysicalHost>();
 94        final PhysicalHost mockedCpu = TestPhysicalRack.mockCPU("CPU_A");
 95        cpuList.add(mockedCpu);
 96
 97        // When
 98        final PhysicalRack rack = new PhysicalRackBuilder().withHosts(cpuList).withUuid("Rack")
 99                .build();
100        // Then
101        Assert.assertArrayEquals(cpuList.toArray(), rack.getCPUs().toArray());
102    }
103
104    @Test
105    public void testAddNullCPU() {
106        // When
107        this.rack.addCPU(null);
108        // Then
109        Assert.assertTrue(this.rack.getCPUs().isEmpty());
110    }
111
112    @Test
113    public void testAddNewCPU() {
114        final PhysicalHost mockedCpu = TestPhysicalRack.mockCPU("CPU_A");
115
116        // When
117        this.rack.addCPU(mockedCpu);
118        // Then
119        Assert.assertTrue(this.rack.getCPUs().contains(mockedCpu));
120    }
121
122    @Test
123    public void testAddExistingCPU() {
124        final PhysicalHost mockedCpu = TestPhysicalRack.mockCPU("CPU_A");
125        final ArrayList<PhysicalHost> cpuList = new ArrayList<PhysicalHost>();
126        cpuList.add(mockedCpu);
127        // When
128        final PhysicalRack rack = new PhysicalRackBuilder().withHosts(cpuList).withUuid("Rack")
129                .build();
130
131        // When
132        rack.addCPU(mockedCpu);
133        // Then
134        Assert.assertArrayEquals(cpuList.toArray(), rack.getCPUs().toArray());
135    }
136
137    @Test
138    public void testRemoveNullCPU() {
139        final PhysicalHost mockedCpu = TestPhysicalRack.mockCPU("CPU_A");
140        final ArrayList<PhysicalHost> cpuList = new ArrayList<PhysicalHost>();
141        cpuList.add(mockedCpu);
142        // When
143        final PhysicalRack rack = new PhysicalRackBuilder().withHosts(cpuList).withUuid("Rack")
144                .build();
145
146        // When
147        rack.removeCPU(null);
148        // Then
149        Assert.assertArrayEquals(cpuList.toArray(), rack.getCPUs().toArray());
150    }
151
152    @Test
153    public void testRemoveExistingCPU() {
154        final PhysicalHost mockedCpu = TestPhysicalRack.mockCPU("CPU_A");
155        final ArrayList<PhysicalHost> cpuList = new ArrayList<PhysicalHost>();
156        cpuList.add(mockedCpu);
157        // When
158        final PhysicalRack rack = new PhysicalRackBuilder().withHosts(cpuList).withUuid("Rack")
159                .build();
160
161        // When
162        rack.removeCPU(mockedCpu);
163        // Then
164        Assert.assertFalse(rack.getCPUs().contains(mockedCpu));
165    }
166
167    @Test
168    public void testRemoveNonExistingCPU() {
169        final PhysicalHost mockedCpu = TestPhysicalRack.mockCPU("CPU_A");
170        final PhysicalHost newCpu = TestPhysicalRack.mockCPU("CPU_B");
171        final ArrayList<PhysicalHost> cpuList = new ArrayList<PhysicalHost>();
172        cpuList.add(mockedCpu);
173        // When
174        final PhysicalRack rack = new PhysicalRackBuilder().withHosts(cpuList).withUuid("Rack")
175                .build();
176
177        // When
178        rack.removeCPU(newCpu);
179        // Then
180        Assert.assertArrayEquals(cpuList.toArray(), rack.getCPUs().toArray());
181    }
182
183    @Test
184    public void testAddNullDisk() {
185        // When
186        this.rack.addDisk(null);
187        // Then
188        Assert.assertTrue(this.rack.getDisks().isEmpty());
189    }
190
191    @Test
192    public void testAddNewDisk() {
193        final PhysicalStore mockedDisk = TestPhysicalRack.mockDisk("Disk_A");
194
195        // When
196        this.rack.addDisk(mockedDisk);
197        // Then
198        Assert.assertTrue(this.rack.getDisks().contains(mockedDisk));
199    }
200
201    @Test
202    public void testAddExistingDisk() {
203        final PhysicalStore mockedDisk = TestPhysicalRack.mockDisk("Disk_A");
204        final ArrayList<PhysicalStore> DiskList = new ArrayList<PhysicalStore>();
205        DiskList.add(mockedDisk);
206        // When
207        final PhysicalRack rack = new PhysicalRackBuilder().withDisks(DiskList).withUuid("Rack")
208                .build();
209
210        // When
211        rack.addDisk(mockedDisk);
212        // Then
213        Assert.assertArrayEquals(DiskList.toArray(), rack.getDisks().toArray());
214    }
215
216    @Test
217    public void testRemoveNullDisk() {
218        final PhysicalStore mockedDisk = TestPhysicalRack.mockDisk("Disk_A");
219        final ArrayList<PhysicalStore> DiskList = new ArrayList<PhysicalStore>();
220        DiskList.add(mockedDisk);
221        // When
222        final PhysicalRack rack = new PhysicalRackBuilder().withDisks(DiskList).withUuid("Rack")
223                .build();
224
225        // When
226        rack.removeDisk(null);
227        // Then
228        Assert.assertArrayEquals(DiskList.toArray(), rack.getDisks().toArray());
229    }
230
231    @Test
232    public void testRemoveExistingDisk() {
233        final PhysicalStore mockedDisk = TestPhysicalRack.mockDisk("Disk_A");
234        final ArrayList<PhysicalStore> DiskList = new ArrayList<PhysicalStore>();
235        DiskList.add(mockedDisk);
236        // When
237        final PhysicalRack rack = new PhysicalRackBuilder().withDisks(DiskList).withUuid("Rack")
238                .build();
239
240        // When
241        rack.removeDisk(mockedDisk);
242        // Then
243        Assert.assertFalse(rack.getDisks().contains(mockedDisk));
244    }
245
246    @Test
247    public void testRemoveNonExistingDisk() {
248        final PhysicalStore mockedDisk = TestPhysicalRack.mockDisk("Disk_A");
249        final PhysicalStore newDisk = TestPhysicalRack.mockDisk("Disk_B");
250        final ArrayList<PhysicalStore> DiskList = new ArrayList<PhysicalStore>();
251        DiskList.add(mockedDisk);
252        // When
253        final PhysicalRack rack = new PhysicalRackBuilder().withDisks(DiskList).withUuid("Rack")
254                .build();
255
256        // When
257        rack.removeDisk(newDisk);
258        // Then
259        Assert.assertArrayEquals(DiskList.toArray(), rack.getDisks().toArray());
260    }
261
262    @Test
263    public void testParent() {
264        final PhysicalDataCenter datacenter = Mockito.mock(PhysicalDataCenter.class);
265
266        // When
267        this.rack.setParent(datacenter);
268        // Then
269        Assert.assertSame(datacenter, this.rack.getParent());
270    }
271
272    @Test
273    public void testParentBuilder() {
274        final PhysicalDataCenter datacenter = Mockito.mock(PhysicalDataCenter.class);
275
276        this.rack = new PhysicalRackBuilder().withParent(datacenter)
277                .withUuid(TestPhysicalRack.rackID).build();
278
279        Assert.assertSame(datacenter, this.rack.getParent());
280    }
281
282    @SuppressWarnings("unused")
283    private Object[] equalsParams() {
284        this.setUp();
285        return JUnitParamsRunner.$(JUnitParamsRunner.$(this.rack, null, false),
286                                   JUnitParamsRunner.$(this.rack, this.cloneRack, true),
287                                   JUnitParamsRunner.$(this.cloneRack, this.rack, true),
288                                   JUnitParamsRunner.$(this.rack, this.otherRack, false),
289                                   JUnitParamsRunner.$(this.rack, 1, false),
290                                   JUnitParamsRunner.$(this.rack,
291                                                       this.rack.getUniqueIdentifier(),
292                                                       false));
293    }
294
295    @SuppressWarnings("unused")
296    private Object[] hashCodeParams() {
297        this.setUp();
298        return JUnitParamsRunner.$(JUnitParamsRunner.$(this.rack, this.cloneRack, true),
299                                   JUnitParamsRunner.$(this.rack, this.rack, true),
300                                   JUnitParamsRunner.$(this.rack, this.otherRack, false));
301    }
302
303    @Test
304    @Parameters(method = "equalsParams")
305    public void equalsTest(final Object a, final Object b, final boolean result) {
306        if (result) {
307            Assert.assertEquals(a, b);
308        } else {
309            Assert.assertNotEquals(a, b);
310        }
311    }
312
313    @Test
314    @Parameters(method = "hashCodeParams")
315    public void hashCodeTest(final Object a, final Object b, final boolean result) {
316        if (result) {
317            Assert.assertEquals(a.hashCode(), b.hashCode());
318        } else {
319            Assert.assertNotEquals(a.hashCode(), b.hashCode());
320        }
321    }
322}