/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

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