PageRenderTime 30ms CodeModel.GetById 15ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/deltaforge/nebu-common-java
Java | 202 lines | 165 code | 33 blank | 4 comment | 2 complexity | 2d4354a4e50a942bb21a19c7ad504246 MD5 | raw file
  1package nl.bitbrains.nebu.common.topology;
  2
  3import java.util.List;
  4
  5import nl.bitbrains.nebu.common.topology.PhysicalDataCenter;
  6import nl.bitbrains.nebu.common.topology.PhysicalHost;
  7import nl.bitbrains.nebu.common.topology.PhysicalRack;
  8import nl.bitbrains.nebu.common.topology.PhysicalRoot;
  9import nl.bitbrains.nebu.common.topology.PhysicalRootBuilder;
 10import nl.bitbrains.nebu.common.topology.PhysicalTopology;
 11
 12import org.junit.Before;
 13import org.junit.Test;
 14import org.junit.runner.RunWith;
 15import org.mockito.Matchers;
 16import org.mockito.Mock;
 17import org.mockito.Mockito;
 18import org.mockito.MockitoAnnotations;
 19import org.powermock.api.mockito.PowerMockito;
 20import org.powermock.core.classloader.annotations.PrepareForTest;
 21import org.powermock.modules.junit4.PowerMockRunner;
 22
 23/**
 24 * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
 25 * 
 26 */
 27@RunWith(PowerMockRunner.class)
 28@PrepareForTest({ PhysicalRoot.class, PhysicalDataCenter.class, PhysicalRack.class,
 29        PhysicalHost.class, PhysicalTopology.class })
 30public class TestPhysicalTopologyCopyConstructorAndMergeFunctions {
 31
 32    private PhysicalRoot root;
 33    private PhysicalRoot root2;
 34
 35    @Mock
 36    private PhysicalTopology one;
 37    @Mock
 38    private PhysicalTopology two;
 39
 40    @Mock
 41    private PhysicalTopology clonedOne;
 42    @Mock
 43    private PhysicalTopology clonedTwo;
 44
 45    private PhysicalRoot createdRoot;
 46    private PhysicalDataCenter createdDC;
 47    private PhysicalRack createdRack;
 48    private PhysicalHost createdCPU;
 49
 50    private final int numDC = 2;
 51    private final int numRack = 3;
 52    private final int numCpu = 4;
 53
 54    @Before
 55    public void setUp() throws Exception {
 56        MockitoAnnotations.initMocks(this);
 57
 58        this.createdRoot = PowerMockito.mock(PhysicalRoot.class);
 59        this.createdDC = PowerMockito.mock(PhysicalDataCenter.class);
 60        this.createdRack = PowerMockito.mock(PhysicalRack.class);
 61        this.createdCPU = PowerMockito.mock(PhysicalHost.class);
 62    }
 63
 64    private void setUpForCopyConstructor() throws Exception {
 65        this.root = PowerMockito.mock(PhysicalRoot.class);
 66        this.setUpConstructorMocks();
 67        Mockito.when(this.root.getUniqueIdentifier()).thenReturn(TestPhysicalTopology.rootID);
 68    }
 69
 70    private void setUpConstructorMocks() throws Exception {
 71        PowerMockito.whenNew(PhysicalRoot.class).withParameterTypes(PhysicalRoot.class)
 72                .withArguments(Matchers.any(PhysicalRoot.class)).thenReturn(this.createdRoot);
 73        PowerMockito.whenNew(PhysicalDataCenter.class).withParameterTypes(PhysicalDataCenter.class)
 74                .withArguments(Matchers.any(PhysicalDataCenter.class)).thenReturn(this.createdDC);
 75        PowerMockito.whenNew(PhysicalRack.class).withParameterTypes(PhysicalRack.class)
 76                .withArguments(Matchers.any(PhysicalRack.class)).thenReturn(this.createdRack);
 77        PowerMockito.whenNew(PhysicalHost.class).withParameterTypes(PhysicalHost.class)
 78                .withArguments(Matchers.any(PhysicalHost.class)).thenReturn(this.createdCPU);
 79    }
 80
 81    private void setUpForMergeFunctions() throws Exception {
 82        this.root = new PhysicalRootBuilder().withUuid("one").build();
 83        if (this.root2 == null) {
 84            this.root2 = new PhysicalRootBuilder().withUuid("one").build();
 85        }
 86        Mockito.when(this.one.getRoot()).thenReturn(this.root);
 87        Mockito.when(this.two.getRoot()).thenReturn(this.root2);
 88
 89        PowerMockito.whenNew(PhysicalTopology.class).withArguments(this.one)
 90                .thenReturn(this.clonedOne);
 91        PowerMockito.whenNew(PhysicalTopology.class).withArguments(this.two)
 92                .thenReturn(this.clonedTwo);
 93        PowerMockito.when(this.clonedOne.getRoot()).thenReturn(this.createdRoot);
 94
 95        this.setUpConstructorMocks();
 96    }
 97
 98    @Test
 99    public void testEmptyTreeCopy() throws Exception {
100        this.setUpForCopyConstructor();
101        final PhysicalTopology origin = new PhysicalTopology(this.root);
102        final PhysicalTopology copy = new PhysicalTopology(origin);
103        PowerMockito.verifyNew(PhysicalRoot.class);
104    }
105
106    @Test
107    public void testTreeWithDataCenters() throws Exception {
108        this.setUpForCopyConstructor();
109        final int numDC = 2;
110        final List<PhysicalDataCenter> dc = TestPhysicalTopology.mockDataCentersForTree(numDC);
111        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
112        final PhysicalTopology origin = new PhysicalTopology(this.root);
113        final PhysicalTopology copy = new PhysicalTopology(origin);
114
115        PowerMockito.verifyNew(PhysicalDataCenter.class, Mockito.times(numDC));
116    }
117
118    @Test
119    public void testTreeWithRacks() throws Exception {
120        this.setUpForCopyConstructor();
121        final int numDC = 2;
122        final int numRacks = 3;
123        final List<PhysicalDataCenter> dc = TestPhysicalTopology.mockDataCentersForTree(numDC);
124        final List<PhysicalRack> rack = TestPhysicalTopology.mockRacksForTree(dc, numRacks);
125        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
126        final PhysicalTopology origin = new PhysicalTopology(this.root);
127        final PhysicalTopology copy = new PhysicalTopology(origin);
128
129        PowerMockito.verifyNew(PhysicalRack.class, Mockito.times(numDC * numRacks));
130    }
131
132    @Test
133    public void testTreeWithCPUs() throws Exception {
134        this.setUpForCopyConstructor();
135        final int numDC = 2;
136        final int numRacks = 3;
137        final int numCpus = 4;
138        final List<PhysicalDataCenter> dc = TestPhysicalTopology.mockDataCentersForTree(numDC);
139        final List<PhysicalRack> rack = TestPhysicalTopology.mockRacksForTree(dc, numRacks);
140        final List<PhysicalHost> cpu = TestPhysicalTopology.mockCPUsForTree(rack, numCpus);
141        Mockito.when(this.root.getDataCenters()).thenReturn(dc);
142        final PhysicalTopology origin = new PhysicalTopology(this.root);
143        final PhysicalTopology copy = new PhysicalTopology(origin);
144
145        PowerMockito.verifyNew(PhysicalHost.class, Mockito.times(numDC * numRacks * numCpus));
146    }
147
148    @Test
149    public void testMergeTreeDifferentRootsVerifyRootsAreUsed() throws Exception {
150        this.root2 = new PhysicalRootBuilder().withUuid("two").build();
151        this.setUpForMergeFunctions();
152
153        final PhysicalTopology merged = PhysicalTopology.mergeTree(this.one, this.two);
154        Mockito.verify(this.one).getRoot();
155        Mockito.verify(this.two).getRoot();
156    }
157
158    @Test
159    public void testMergeTreeDifferentRootsVerifyOnlyFirstArgumentIsCopied() throws Exception {
160        this.root2 = new PhysicalRootBuilder().withUuid("two").build();
161        this.setUpForMergeFunctions();
162
163        final PhysicalTopology merged = PhysicalTopology.mergeTree(this.one, this.two);
164        PowerMockito.verifyNew(PhysicalTopology.class, Mockito.times(1)).withArguments(this.one);
165        PowerMockito.verifyNew(PhysicalTopology.class, Mockito.times(0)).withArguments(this.two);
166    }
167
168    @Test
169    public void testMergeTreeSameRootsNoCenters() throws Exception {
170        this.setUpForMergeFunctions();
171        final PhysicalTopology merged = PhysicalTopology.mergeTree(this.one, this.two);
172
173        PowerMockito.verifyNew(PhysicalTopology.class, Mockito.times(1)).withArguments(this.one);
174        PowerMockito.verifyNew(PhysicalTopology.class, Mockito.times(0)).withArguments(this.two);
175    }
176
177    @Test
178    public void testMergeTreeSameRootsCentersInOne() throws Exception {
179        this.setUpForMergeFunctions();
180        final List<PhysicalDataCenter> dcs = TestPhysicalTopology
181                .mockDataCentersForTree(this.numDC);
182        Mockito.when(this.one.getDataCenters()).thenReturn(dcs);
183        final PhysicalTopology merged = PhysicalTopology.mergeTree(this.one, this.two);
184
185        PowerMockito.verifyNew(PhysicalTopology.class, Mockito.times(1)).withArguments(this.one);
186        PowerMockito.verifyNew(PhysicalTopology.class, Mockito.times(0)).withArguments(this.two);
187    }
188
189    @Test
190    public void testMergeTreeSameRootsCentersInTwo() throws Exception {
191        this.setUpForMergeFunctions();
192        final List<PhysicalDataCenter> dcs = TestPhysicalTopology
193                .mockDataCentersForTree(this.numDC);
194        Mockito.when(this.two.getDataCenters()).thenReturn(dcs);
195        final PhysicalTopology merged = PhysicalTopology.mergeTree(this.one, this.two);
196
197        PowerMockito.verifyNew(PhysicalTopology.class, Mockito.times(1)).withArguments(this.one);
198        PowerMockito.verifyNew(PhysicalTopology.class, Mockito.times(0)).withArguments(this.two);
199        PowerMockito.verifyNew(PhysicalDataCenter.class, Mockito.times(this.numDC))
200                .withArguments(Matchers.any(PhysicalDataCenter.class));
201    }
202}