PageRenderTime 69ms CodeModel.GetById 11ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://github.com/deltaforge/nebu-common-java
Java | 544 lines | 414 code | 54 blank | 76 comment | 7 complexity | 6739f8da3d28520861ec3f72bbc02793 MD5 | raw file
  1package nl.bitbrains.nebu.common.topology;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5import java.util.NoSuchElementException;
  6
  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.PhysicalRoot;
 11import nl.bitbrains.nebu.common.topology.PhysicalStore;
 12import nl.bitbrains.nebu.common.topology.PhysicalTopology;
 13
 14import org.junit.Assert;
 15import org.junit.Before;
 16import org.junit.Test;
 17import org.junit.runner.RunWith;
 18import org.mockito.Mockito;
 19import org.mockito.MockitoAnnotations;
 20import org.powermock.api.mockito.PowerMockito;
 21import org.powermock.core.classloader.annotations.PrepareForTest;
 22import org.powermock.modules.junit4.PowerMockRunner;
 23
 24/**
 25 * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
 26 * 
 27 */
 28@RunWith(PowerMockRunner.class)
 29@PrepareForTest({ PhysicalRoot.class, PhysicalDataCenter.class, PhysicalRack.class,
 30        PhysicalStore.class, PhysicalHost.class })
 31public class TestPhysicalTopology {
 32    PhysicalRoot root;
 33
 34    static final String rootID = "Root";
 35
 36    @Before
 37    public void setUp() throws Exception {
 38        MockitoAnnotations.initMocks(this);
 39        this.root = TestPhysicalTopology.newRootMock(TestPhysicalTopology.rootID);
 40    }
 41
 42    public static PhysicalRoot newRootMock(final String rootID) {
 43        final PhysicalRoot res = PowerMockito.mock(PhysicalRoot.class);
 44        Mockito.when(res.getUniqueIdentifier()).thenReturn(rootID);
 45        return res;
 46    }
 47
 48    public static PhysicalDataCenter mockDataCenter(final String identifier) {
 49        final PhysicalDataCenter dc = PowerMockito.mock(PhysicalDataCenter.class);
 50        Mockito.when(dc.getUniqueIdentifier()).thenReturn(identifier);
 51        return dc;
 52    }
 53
 54    public static PhysicalRack mockRack(final String identifier) {
 55        final PhysicalRack rack = PowerMockito.mock(PhysicalRack.class);
 56        Mockito.when(rack.getUniqueIdentifier()).thenReturn(identifier);
 57        return rack;
 58    }
 59
 60    public static PhysicalHost mockCPU(final String identifier) {
 61        final PhysicalHost cpu = PowerMockito.mock(PhysicalHost.class);
 62        Mockito.when(cpu.getUniqueIdentifier()).thenReturn(identifier);
 63        return cpu;
 64    }
 65
 66    public static PhysicalStore mockStore(final String identifier) {
 67        final PhysicalStore store = PowerMockito.mock(PhysicalStore.class);
 68        Mockito.when(store.getUniqueIdentifier()).thenReturn(identifier);
 69        return store;
 70    }
 71
 72    public static List<PhysicalDataCenter> mockDataCentersForTree(final int dataCenters) {
 73        final ArrayList<PhysicalDataCenter> dcList = new ArrayList<PhysicalDataCenter>();
 74        for (int i = 0; i < dataCenters; i++) {
 75            final PhysicalDataCenter newDC = TestPhysicalTopology.mockDataCenter("ID_" + i);
 76            dcList.add(newDC);
 77        }
 78        return dcList;
 79    }
 80
 81    public static List<PhysicalRack> mockRacksForTree(final List<PhysicalDataCenter> dataCenters,
 82            final int racksPerDataCenter) {
 83        final ArrayList<PhysicalRack> rackList = new ArrayList<PhysicalRack>();
 84        for (final PhysicalDataCenter dc : dataCenters) {
 85            final ArrayList<PhysicalRack> dcRackList = new ArrayList<PhysicalRack>();
 86            for (int i = 0; i < racksPerDataCenter; i++) {
 87                final PhysicalRack rack = TestPhysicalTopology.mockRack(dc.getUniqueIdentifier()
 88                        + "_" + i);
 89                rackList.add(rack);
 90                dcRackList.add(rack);
 91            }
 92            Mockito.when(dc.getRacks()).thenReturn(dcRackList);
 93        }
 94        return rackList;
 95    }
 96
 97    public static List<PhysicalHost> mockCPUsForTree(final List<PhysicalRack> racks,
 98            final int cpusPerRack) {
 99        final ArrayList<PhysicalHost> cpuList = new ArrayList<PhysicalHost>();
100        for (final PhysicalRack rack : racks) {
101            final ArrayList<PhysicalHost> rackCPUList = new ArrayList<PhysicalHost>();
102            for (int i = 0; i < cpusPerRack; i++) {
103                final PhysicalHost cpu = TestPhysicalTopology.mockCPU(rack.getUniqueIdentifier()
104                        + "_" + i);
105                cpuList.add(cpu);
106                rackCPUList.add(cpu);
107            }
108            Mockito.when(rack.getCPUs()).thenReturn(rackCPUList);
109        }
110        return cpuList;
111    }
112
113    public static List<PhysicalStore> mockStoresForTree(final List<PhysicalRack> racks,
114            final int storesPerRack) {
115        final ArrayList<PhysicalStore> storeList = new ArrayList<PhysicalStore>();
116        for (final PhysicalRack rack : racks) {
117            final ArrayList<PhysicalStore> rackStoreList = new ArrayList<PhysicalStore>();
118            for (int i = 0; i < storesPerRack; i++) {
119                final PhysicalStore store = TestPhysicalTopology.mockStore(rack
120                        .getUniqueIdentifier() + "_" + i);
121                storeList.add(store);
122                rackStoreList.add(store);
123            }
124            Mockito.when(rack.getDisks()).thenReturn(rackStoreList);
125        }
126        return storeList;
127    }
128
129    @Test
130    public void testConstructor() {
131        // When
132        final PhysicalTopology topology = new PhysicalTopology(this.root);
133        // Then
134        Assert.assertEquals(topology.getRoot(), this.root);
135    }
136
137    @Test
138    public void testGetDataCenters() {
139        final List<PhysicalDataCenter> dcList = TestPhysicalTopology.mockDataCentersForTree(3);
140        // When
141        final PhysicalTopology topology = new PhysicalTopology(this.root);
142        Mockito.when(this.root.getDataCenters()).thenReturn(dcList);
143        // Then
144        Assert.assertEquals(dcList, topology.getDataCenters());
145    }
146
147    @Test
148    public void testGetRacks() {
149        final List<PhysicalDataCenter> dcList = TestPhysicalTopology.mockDataCentersForTree(3);
150        final List<PhysicalRack> rackList = TestPhysicalTopology.mockRacksForTree(dcList, 3);
151        // When
152        final PhysicalTopology topology = new PhysicalTopology(this.root);
153        Mockito.when(this.root.getDataCenters()).thenReturn(dcList);
154        // Then
155        Assert.assertEquals(rackList, topology.getRacks());
156    }
157
158    @Test
159    public void testGetCPUs() {
160        final List<PhysicalDataCenter> dcList = TestPhysicalTopology.mockDataCentersForTree(3);
161        final List<PhysicalRack> rackList = TestPhysicalTopology.mockRacksForTree(dcList, 3);
162        final List<PhysicalHost> cpuList = TestPhysicalTopology.mockCPUsForTree(rackList, 3);
163        // When
164        final PhysicalTopology topology = new PhysicalTopology(this.root);
165        Mockito.when(this.root.getDataCenters()).thenReturn(dcList);
166        // Then
167        Assert.assertEquals(cpuList, topology.getCPUs());
168    }
169
170    @Test
171    public void testGetStores() {
172        final List<PhysicalDataCenter> dcList = TestPhysicalTopology.mockDataCentersForTree(3);
173        final List<PhysicalRack> rackList = TestPhysicalTopology.mockRacksForTree(dcList, 3);
174        final List<PhysicalStore> storeList = TestPhysicalTopology.mockStoresForTree(rackList, 3);
175        final List<PhysicalHost> hostList = TestPhysicalTopology.mockCPUsForTree(rackList, 1);
176
177        final PhysicalStore extraStore = TestPhysicalTopology.mockStore("extra");
178        final List<PhysicalStore> extraStoreList = new ArrayList<PhysicalStore>();
179        extraStoreList.add(extraStore);
180        Mockito.when(hostList.get(0).getDisks()).thenReturn(extraStoreList);
181        storeList.add(extraStore);
182        // When
183        final PhysicalTopology topology = new PhysicalTopology(this.root);
184        Mockito.when(this.root.getDataCenters()).thenReturn(dcList);
185        // Then
186        Assert.assertEquals(storeList, topology.getStores());
187    }
188
189    @Test
190    public void testAddDataCenterConsistency() {
191        final PhysicalTopology topology = new PhysicalTopology(this.root);
192        final PhysicalDataCenter dc = TestPhysicalTopology.mockDataCenter("DC_A");
193        // When
194        topology.addDataCenter(dc);
195        // Then
196        Mockito.verify(this.root).addDataCenter(dc);
197        Mockito.verify(dc).setParent(this.root);
198    }
199
200    @Test
201    public void testRemoveDataCenterConsistency() {
202        final PhysicalTopology topology = new PhysicalTopology(this.root);
203        final PhysicalDataCenter dc = TestPhysicalTopology.mockDataCenter("DC_A");
204        // When
205        topology.removeDataCenter(dc);
206        // Then
207        Mockito.verify(this.root).removeDataCenter(dc);
208        Mockito.verify(dc).setParent(null);
209    }
210
211    @Test(expected = IllegalArgumentException.class)
212    public void testAddNullDataCenter() {
213        final PhysicalTopology topology = new PhysicalTopology(this.root);
214        // When
215        topology.addDataCenter(null);
216        // Then
217        // ... nothing needs to happen, but no Exceptions may be thrown
218        Assert.fail();
219    }
220
221    @Test
222    public void testAddRackConsistency() {
223        final PhysicalTopology topology = new PhysicalTopology(this.root);
224        final PhysicalDataCenter dc = TestPhysicalTopology.mockDataCenter("DC_A");
225        final PhysicalRack rack = TestPhysicalTopology.mockRack("Rack_A");
226        // When
227        topology.addRackToDataCenter(rack, dc);
228        // Then
229        Mockito.verify(dc).addRack(rack);
230        Mockito.verify(rack).setParent(dc);
231    }
232
233    @Test
234    public void testRemoveRackConsistency() {
235        final PhysicalTopology topology = new PhysicalTopology(this.root);
236        final PhysicalDataCenter dc = TestPhysicalTopology.mockDataCenter("DC_A");
237        final PhysicalRack rack = TestPhysicalTopology.mockRack("Rack_A");
238        // When
239        topology.removeRackFromDataCenter(rack, dc);
240        // Then
241        Mockito.verify(dc).removeRack(rack);
242        Mockito.verify(rack).setParent(null);
243    }
244
245    @Test(expected = IllegalArgumentException.class)
246    public void testAddNullRack() {
247        final PhysicalTopology topology = new PhysicalTopology(this.root);
248        final PhysicalDataCenter dc = TestPhysicalTopology.mockDataCenter("DC_A");
249        // When
250        topology.addRackToDataCenter(null, dc);
251        // Then
252        // ... an Exception should have been thrown
253        Assert.fail();
254    }
255
256    @Test(expected = IllegalArgumentException.class)
257    public void testAddRackToNull() {
258        final PhysicalTopology topology = new PhysicalTopology(this.root);
259        final PhysicalRack rack = TestPhysicalTopology.mockRack("Rack_A");
260        // When
261        topology.addRackToDataCenter(rack, null);
262        // Then
263        // ... an Exception should have been thrown
264        Assert.fail();
265    }
266
267    @Test
268    public void testAddCPUConsistency() {
269        final PhysicalTopology topology = new PhysicalTopology(this.root);
270        final PhysicalRack rack = TestPhysicalTopology.mockRack("Rack_A");
271        final PhysicalHost cpu = TestPhysicalTopology.mockCPU("CPU_A");
272        // When
273        topology.addCPUToRack(cpu, rack);
274        // Then
275        Mockito.verify(rack).addCPU(cpu);
276        Mockito.verify(cpu).setParent(rack);
277    }
278
279    @Test
280    public void testRemoveCPUConsistency() {
281        final PhysicalTopology topology = new PhysicalTopology(this.root);
282        final PhysicalRack rack = TestPhysicalTopology.mockRack("Rack_A");
283        final PhysicalHost cpu = TestPhysicalTopology.mockCPU("CPU_A");
284        // When
285        topology.removeCPUFromRack(cpu, rack);
286        // Then
287        Mockito.verify(rack).removeCPU(cpu);
288        Mockito.verify(cpu).setParent(null);
289    }
290
291    @Test(expected = IllegalArgumentException.class)
292    public void testAddNullCPU() {
293        final PhysicalTopology topology = new PhysicalTopology(this.root);
294        final PhysicalRack rack = TestPhysicalTopology.mockRack("Rack_A");
295        // When
296        topology.addCPUToRack(null, rack);
297        // Then
298        // ... an Exception should have been thrown
299        Assert.fail();
300    }
301
302    @Test(expected = IllegalArgumentException.class)
303    public void testAddCPUToNull() {
304        final PhysicalTopology topology = new PhysicalTopology(this.root);
305        final PhysicalHost cpu = TestPhysicalTopology.mockCPU("CPUA");
306        // When
307        topology.addCPUToRack(cpu, null);
308        // Then
309        // ... an Exception should have been thrown
310        Assert.fail();
311    }
312
313    @Test
314    public void testAddDiskToRackConsistency() {
315        final PhysicalTopology topology = new PhysicalTopology(this.root);
316        final PhysicalRack rack = TestPhysicalTopology.mockRack("Rack_A");
317        final PhysicalStore store = TestPhysicalTopology.mockStore("STORE_A");
318        // When
319        topology.addDiskToRack(store, rack);
320        // Then
321        Mockito.verify(rack).addDisk(store);
322        Mockito.verify(store).setParent(rack);
323    }
324
325    @Test
326    public void testRemoveDiskFromRackConsistency() {
327        final PhysicalTopology topology = new PhysicalTopology(this.root);
328        final PhysicalRack rack = TestPhysicalTopology.mockRack("Rack_A");
329        final PhysicalStore store = TestPhysicalTopology.mockStore("STORE_A");
330        // When
331        topology.removeDiskFromRack(store, rack);
332        // Then
333        Mockito.verify(rack).removeDisk(store);
334        Mockito.verify(store).setParent(null);
335    }
336
337    @Test(expected = IllegalArgumentException.class)
338    public void testAddNullDiskToRack() {
339        final PhysicalTopology topology = new PhysicalTopology(this.root);
340        final PhysicalRack rack = TestPhysicalTopology.mockRack("STORE_A");
341        // When
342        topology.addDiskToRack(null, rack);
343        // Then
344        // ... an Exception should have been thrown
345        Assert.fail();
346    }
347
348    @Test(expected = IllegalArgumentException.class)
349    public void testAddDiskToRackToNull() {
350        final PhysicalTopology topology = new PhysicalTopology(this.root);
351        final PhysicalStore store = TestPhysicalTopology.mockStore("STORE_A");
352        // When
353        topology.addDiskToRack(store, null);
354        // Then
355        // ... an Exception should have been thrown
356        Assert.fail();
357    }
358
359    @Test
360    public void testAddDiskToHostConsistency() {
361        final PhysicalTopology topology = new PhysicalTopology(this.root);
362        final PhysicalHost host = TestPhysicalTopology.mockCPU("Host_A");
363        final PhysicalStore store = TestPhysicalTopology.mockStore("STORE_A");
364        // When
365        topology.addDiskToHost(store, host);
366        // Then
367        Mockito.verify(host).addDisk(store);
368        Mockito.verify(store).setParent(host);
369    }
370
371    @Test
372    public void testRemoveDiskFromHostConsistency() {
373        final PhysicalTopology topology = new PhysicalTopology(this.root);
374        final PhysicalHost host = TestPhysicalTopology.mockCPU("Host_A");
375        final PhysicalStore store = TestPhysicalTopology.mockStore("STORE_A");
376        // When
377        topology.removeDiskFromHost(store, host);
378        // Then
379        Mockito.verify(host).removeDisk(store);
380        Mockito.verify(store).setParent(null);
381    }
382
383    @Test(expected = IllegalArgumentException.class)
384    public void testAddNullDiskToHost() {
385        final PhysicalTopology topology = new PhysicalTopology(this.root);
386        final PhysicalHost host = TestPhysicalTopology.mockCPU("Host_A");
387        // When
388        topology.addDiskToHost(null, host);
389        // Then
390        // ... an Exception should have been thrown
391        Assert.fail();
392    }
393
394    @Test(expected = IllegalArgumentException.class)
395    public void testAddDiskToHostToNull() {
396        final PhysicalTopology topology = new PhysicalTopology(this.root);
397        final PhysicalStore store = TestPhysicalTopology.mockStore("STORE_A");
398        // When
399        topology.addDiskToHost(store, null);
400        // Then
401        // ... an Exception should have been thrown
402        Assert.fail();
403    }
404
405    @Test
406    public void testNotEqualsNull() {
407        // When
408        final PhysicalTopology topology = new PhysicalTopology(this.root);
409        // Then
410        Assert.assertNotEquals(topology, null);
411    }
412
413    @Test
414    public void testEqualsSelf() {
415        // When
416        final PhysicalTopology topology = new PhysicalTopology(this.root);
417        // Then
418        Assert.assertEquals(topology, topology);
419    }
420
421    @Test
422    public void testObjectEqualsCopy() {
423        final PhysicalTopology topology = new PhysicalTopology(this.root);
424        // When
425        final PhysicalTopology topologyCopy = new PhysicalTopology(this.root);
426        // Then
427        Assert.assertEquals(topologyCopy, topology);
428    }
429
430    @Test
431    public void testCopyEqualsObject() {
432        final PhysicalTopology topology = new PhysicalTopology(this.root);
433        // When
434        final PhysicalTopology topologyCopy = new PhysicalTopology(this.root);
435        // Then
436        Assert.assertEquals(topology, topologyCopy);
437    }
438
439    @Test
440    public void testNotEqualsOther() {
441        final PhysicalTopology topology = new PhysicalTopology(this.root);
442        final PhysicalRoot otherRoot = Mockito.mock(PhysicalRoot.class);
443        Mockito.when(otherRoot.getUniqueIdentifier()).thenReturn(TestPhysicalTopology.rootID + "_");
444        // When
445        final PhysicalTopology other = new PhysicalTopology(otherRoot);
446        // Then
447        Assert.assertNotEquals(other, topology);
448    }
449
450    @Test
451    public void testHashCodeConsistency() {
452        // When
453        final PhysicalTopology topology = new PhysicalTopology(this.root);
454        // Then
455        Assert.assertEquals(topology.hashCode(), topology.hashCode());
456    }
457
458    @Test
459    public void testHashCodeCopy() {
460        // When
461        final PhysicalTopology topology = new PhysicalTopology(this.root);
462        final PhysicalTopology topologyCopy = new PhysicalTopology(this.root);
463        // Then
464        Assert.assertEquals(topology.hashCode(), topologyCopy.hashCode());
465    }
466
467    @Test
468    public void testHashCodeDifferentID() {
469        final PhysicalRoot otherRoot = Mockito.mock(PhysicalRoot.class);
470        Mockito.when(otherRoot.getUniqueIdentifier()).thenReturn(TestPhysicalTopology.rootID + "_");
471        // When
472        final PhysicalTopology topology = new PhysicalTopology(this.root);
473        final PhysicalTopology otherTopology = new PhysicalTopology(otherRoot);
474        Assert.assertNotEquals(topology.hashCode(), otherTopology.hashCode());
475    }
476
477    @Test
478    public void testHasCPUByIDEmptyTreeNotFound() {
479        final PhysicalTopology topology = new PhysicalTopology(this.root);
480        final List<PhysicalDataCenter> dc = new ArrayList<PhysicalDataCenter>();
481        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
482        Assert.assertFalse(topology.hasCPUByID("this does not exist"));
483    }
484
485    @Test
486    public void testHasCPUByIDFilledTreeNotFound() {
487        final PhysicalTopology topology = new PhysicalTopology(this.root);
488        final List<PhysicalDataCenter> dc = TestPhysicalTopology.mockDataCentersForTree(1);
489        final List<PhysicalRack> racks = TestPhysicalTopology.mockRacksForTree(dc, 2);
490        final List<PhysicalHost> hosts = TestPhysicalTopology.mockCPUsForTree(racks, 2);
491        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
492        Assert.assertFalse(topology.hasCPUByID("this does not exist"));
493    }
494
495    @Test
496    public void testHasCPUByIDFilledTreeFound() {
497        final PhysicalTopology topology = new PhysicalTopology(this.root);
498        final List<PhysicalDataCenter> dc = TestPhysicalTopology.mockDataCentersForTree(1);
499        final List<PhysicalRack> racks = TestPhysicalTopology.mockRacksForTree(dc, 2);
500        final List<PhysicalHost> hosts = TestPhysicalTopology.mockCPUsForTree(racks, 2);
501        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
502        Assert.assertTrue(topology.hasCPUByID("ID_0_1_1"));
503    }
504
505    @Test
506    public void testGetCPUByIDEmptyTreeNotFound() {
507        final PhysicalTopology topology = new PhysicalTopology(this.root);
508        final List<PhysicalDataCenter> dc = new ArrayList<PhysicalDataCenter>();
509        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
510        boolean caught = false;
511        try {
512            topology.getCPUByID("this does not exist");
513        } catch (final NoSuchElementException e) {
514            caught = true;
515        }
516        Assert.assertTrue(caught);
517    }
518
519    @Test
520    public void testGetCPUByIDFilledTreeNotFound() {
521        final PhysicalTopology topology = new PhysicalTopology(this.root);
522        final List<PhysicalDataCenter> dc = TestPhysicalTopology.mockDataCentersForTree(1);
523        final List<PhysicalRack> racks = TestPhysicalTopology.mockRacksForTree(dc, 2);
524        final List<PhysicalHost> hosts = TestPhysicalTopology.mockCPUsForTree(racks, 2);
525        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
526        boolean caught = false;
527        try {
528            topology.getCPUByID("this does not exist");
529        } catch (final NoSuchElementException e) {
530            caught = true;
531        }
532        Assert.assertTrue(caught);
533    }
534
535    @Test
536    public void testGetCPUByIDFilledTreeFound() {
537        final PhysicalTopology topology = new PhysicalTopology(this.root);
538        final List<PhysicalDataCenter> dc = TestPhysicalTopology.mockDataCentersForTree(1);
539        final List<PhysicalRack> racks = TestPhysicalTopology.mockRacksForTree(dc, 2);
540        final List<PhysicalHost> hosts = TestPhysicalTopology.mockCPUsForTree(racks, 2);
541        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
542        Assert.assertNotNull(topology.getCPUByID("ID_0_1_1"));
543    }
544}