PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/amps-maven-plugin/src/test/java/com/atlassian/maven/plugins/amps/ProductTest.java

https://bitbucket.org/atlassian/amps
Java | 348 lines | 249 code | 63 blank | 36 comment | 0 complexity | dadd0cfd39560b559f27cd4b035268e6 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.amps;
  2. import org.apache.maven.plugin.logging.Log;
  3. import org.junit.Before;
  4. import org.junit.Rule;
  5. import org.junit.Test;
  6. import org.junit.rules.MethodRule;
  7. import org.mockito.Mock;
  8. import org.mockito.junit.MockitoJUnit;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Optional;
  12. import static java.util.Collections.emptyMap;
  13. import static java.util.Collections.singletonList;
  14. import static java.util.Optional.empty;
  15. import static org.hamcrest.MatcherAssert.assertThat;
  16. import static org.hamcrest.Matchers.hasEntry;
  17. import static org.hamcrest.Matchers.is;
  18. import static org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertThrows;
  21. import static org.mockito.Mockito.mock;
  22. import static org.mockito.Mockito.never;
  23. import static org.mockito.Mockito.verify;
  24. import static org.mockito.Mockito.when;
  25. public class ProductTest {
  26. @Rule
  27. public final MethodRule mockitoRule = MockitoJUnit.rule();
  28. private static final String OTHER_SHARED_HOME = "otherSharedHome";
  29. private static final String THIS_SHARED_HOME = "thisSharedHome";
  30. private static final int OTHER_PORT = 667;
  31. private static final int THIS_PORT = 666;
  32. private Product thisProduct;
  33. @Mock
  34. private Product otherProduct;
  35. @Before
  36. public void setUp() {
  37. thisProduct = new Product();
  38. }
  39. @Test
  40. public void mergingShouldPreserveSharedHomeWhenItExists() {
  41. thisProduct.setSharedHome(THIS_SHARED_HOME);
  42. assertEquals(THIS_SHARED_HOME, thisProduct.merge(otherProduct).getSharedHome());
  43. verify(otherProduct, never()).getSharedHome();
  44. }
  45. @Test
  46. public void mergingShouldSetOtherSharedHomeWhenItDoesNotExist() {
  47. when(otherProduct.getSharedHome()).thenReturn(OTHER_SHARED_HOME);
  48. assertNull(thisProduct.getSharedHome());
  49. assertEquals(OTHER_SHARED_HOME, thisProduct.merge(otherProduct).getSharedHome());
  50. }
  51. @Test
  52. public void mergingShouldPreserveAjpPortWhenItExists() {
  53. thisProduct.setAjpPort(THIS_PORT);
  54. assertEquals(THIS_PORT, thisProduct.merge(otherProduct).getAjpPort());
  55. verify(otherProduct, never()).getAjpPort();
  56. }
  57. @Test
  58. public void mergingShouldSetOtherAjpPortWhenItDoesNotExist() {
  59. when(otherProduct.getAjpPort()).thenReturn(OTHER_PORT);
  60. assertEquals(0, thisProduct.getAjpPort());
  61. assertEquals(OTHER_PORT, thisProduct.merge(otherProduct).getAjpPort());
  62. }
  63. @Test
  64. public void mergingShouldPreserveRmiPortWhenItExists() {
  65. thisProduct.setRmiPort(THIS_PORT);
  66. assertEquals(THIS_PORT, thisProduct.merge(otherProduct).getRmiPort());
  67. verify(otherProduct, never()).getRmiPort();
  68. }
  69. @Test
  70. public void mergingShouldSetOtherRmiPortWhenItDoesNotExist() {
  71. when(otherProduct.getRmiPort()).thenReturn(OTHER_PORT);
  72. assertEquals(0, thisProduct.getRmiPort());
  73. assertEquals(OTHER_PORT, thisProduct.merge(otherProduct).getRmiPort());
  74. }
  75. @Test
  76. public void getBaseUrl_whenGivenNonZeroPort_shouldReturnExpectedValue() {
  77. // Arrange
  78. thisProduct.setContextPath("theContextPath");
  79. thisProduct.setServer("theServer");
  80. // Act
  81. final String baseUrl = thisProduct.getBaseUrlForPort(42);
  82. // Assert
  83. assertThat(baseUrl, is("http://theServer:42/theContextPath"));
  84. }
  85. @Test
  86. public void defaultJvmArgs_whenJvmArgsAlreadySet_shouldDoNothing() {
  87. // Arrange
  88. final String originalJvmArgs = "theOriginalJvmArgs";
  89. thisProduct.setJvmArgs(originalJvmArgs);
  90. // Act
  91. thisProduct.defaultJvmArgs("newJvmArgs");
  92. // Assert
  93. assertThat(thisProduct.getJvmArgs(), is(originalJvmArgs));
  94. }
  95. @Test
  96. public void defaultJvmArgs_whenJvmArgsNotAlreadySet_shouldSetGivenArgs() {
  97. // Arrange
  98. thisProduct.setJvmArgs(null);
  99. final String newJvmArgs = "theNewJvmArgs";
  100. // Act
  101. thisProduct.defaultJvmArgs(newJvmArgs);
  102. // Assert
  103. assertThat(thisProduct.getJvmArgs(), is(newJvmArgs));
  104. }
  105. @Test
  106. public void setNodeDebugArgs_whenNodesNotSet_shouldThrowException() {
  107. // Arrange
  108. final Log log = mock(Log.class);
  109. thisProduct.setInstanceId("foo");
  110. // Act
  111. final IllegalStateException exception = assertThrows(IllegalStateException.class,
  112. () -> thisProduct.setNodeDebugArgs(true, log));
  113. // Assert
  114. assertThat(exception.getMessage(), is("No nodes set for instance 'foo'"));
  115. }
  116. @Test
  117. public void setNodeDebugArgs_whenTheyAreBlank_shouldSetThemBasedOnPortAndSuspend() {
  118. // Arrange
  119. final Log log = mock(Log.class);
  120. final int debugPort = 42;
  121. thisProduct.initialiseNodes();
  122. final Node node = thisProduct.getNodes().get(0);
  123. node.setDebugArgs(null);
  124. node.setJvmDebugPort(debugPort);
  125. // Act
  126. thisProduct.setNodeDebugArgs(true, log);
  127. // Assert
  128. assertThat(node.getDebugArgs(),
  129. is(" -Xdebug -Xrunjdwp:transport=dt_socket,address=42,suspend=y,server=y "));
  130. }
  131. @Test
  132. public void setNodeDebugArgs_whenTheyAreNotBlank_shouldLeaveThemIntact() {
  133. // Arrange
  134. final Log log = mock(Log.class);
  135. final int debugPort = 43;
  136. thisProduct.initialiseNodes();
  137. final Node node = thisProduct.getNodes().get(0);
  138. final String userConfiguredDebugArgs = "theUserConfiguredDebugArgs";
  139. node.setDebugArgs(userConfiguredDebugArgs);
  140. node.setJvmDebugPort(debugPort);
  141. // Act
  142. thisProduct.setNodeDebugArgs(false, log);
  143. // Assert
  144. assertThat(node.getDebugArgs(), is(userConfiguredDebugArgs));
  145. }
  146. @Test
  147. public void defaultJvmArgs_whenPreviousJvmArgsAreNotBlank_shouldLeaveThemUnchanged() {
  148. final String previousJvmArgs = " previousJvmArgs ";
  149. assertDefaultJvmArgs(previousJvmArgs, previousJvmArgs);
  150. }
  151. @Test
  152. public void defaultJvmArgs_whenPreviousJvmArgsAreNull_shouldApplyTheDefault() {
  153. assertDefaultJvmArgsAppliesDefault(null);
  154. }
  155. @Test
  156. public void defaultJvmArgs_whenPreviousJvmArgsAreEmpty_shouldApplyTheDefault() {
  157. assertDefaultJvmArgsAppliesDefault("");
  158. }
  159. @Test
  160. public void defaultJvmArgs_whenPreviousJvmArgsAreBlank_shouldApplyTheDefault() {
  161. assertDefaultJvmArgsAppliesDefault(" ");
  162. }
  163. private void assertDefaultJvmArgsAppliesDefault(final String previousJvmArgs) {
  164. assertDefaultJvmArgs(previousJvmArgs, " defaultJvmArgs ");
  165. }
  166. private void assertDefaultJvmArgs(final String previousJvmArgs, final String expectedJvmArgs) {
  167. // Arrange
  168. thisProduct.setJvmArgs(previousJvmArgs);
  169. // Act
  170. thisProduct.defaultJvmArgs(" defaultJvmArgs ");
  171. // Assert
  172. assertThat(thisProduct.getJvmArgs(), is(expectedJvmArgs));
  173. }
  174. @Test
  175. public void getSystemPropertiesForNode_whenNoPropertiesExist_shouldReturnEmptyMap() {
  176. // Arrange
  177. final Product product = new Product();
  178. final Node node = mock(Node.class);
  179. when(node.getSystemProperties()).thenReturn(emptyMap());
  180. product.setNodes(singletonList(node));
  181. // Act
  182. final Map<String, String> systemProperties = product.getSystemPropertiesForNode(0);
  183. // Assert
  184. assertThat(systemProperties, is(emptyMap()));
  185. }
  186. @Test
  187. public void getSystemPropertiesForNode_whenNodeClashesWithProduct_shouldGivePrecedenceToNode() {
  188. // Arrange
  189. final Product product = new Product();
  190. final Node node = mock(Node.class);
  191. final String unconflictedProductKey = "k1";
  192. final String conflictedKey = "k2";
  193. final String unconflictedNodeKey = "k3";
  194. final String unconflictedProductValue = "v1";
  195. final String conflictedProductValue = "v2";
  196. final String unconflictedNodeValue = "v3";
  197. final String conflictedNodeValue = "v4";
  198. final Map<String, Object> productProperties = new HashMap<>();
  199. productProperties.put(unconflictedProductKey, unconflictedProductValue);
  200. productProperties.put(conflictedKey, conflictedProductValue);
  201. product.setSystemPropertyVariables(productProperties);
  202. final Map<String, String> nodeProperties = new HashMap<>();
  203. nodeProperties.put(conflictedKey, conflictedNodeValue);
  204. nodeProperties.put(unconflictedNodeKey, unconflictedNodeValue);
  205. when(node.getSystemProperties()).thenReturn(nodeProperties);
  206. product.setNodes(singletonList(node));
  207. // Act
  208. final Map<String, String> systemProperties = product.getSystemPropertiesForNode(0);
  209. // Assert
  210. assertThat(systemProperties.size(), is(3));
  211. assertThat(systemProperties, hasEntry(unconflictedProductKey, unconflictedProductValue));
  212. assertThat(systemProperties, hasEntry(unconflictedNodeKey, unconflictedNodeValue));
  213. assertThat(systemProperties, hasEntry(conflictedKey, conflictedNodeValue));
  214. }
  215. @Test
  216. public void getWebPortForNode_whenWebPortIsZero_shouldThrowException() {
  217. // Arrange
  218. final Product product = new Product();
  219. final Node node = mock(Node.class);
  220. product.setNodes(singletonList(node));
  221. when(node.getWebPort()).thenReturn(0);
  222. // Act
  223. final IllegalStateException exception =
  224. assertThrows(IllegalStateException.class, () -> product.getWebPortForNode(0));
  225. // Assert
  226. assertThat(exception.getMessage(), is("Web port not set or resolved for node 0"));
  227. }
  228. @Test
  229. public void getWebPortForNode_whenWebPortIsSet_shouldReturnIt() {
  230. // Arrange
  231. final Product product = new Product();
  232. final Node node = mock(Node.class);
  233. product.setNodes(singletonList(node));
  234. final int mockedWebPort = 42;
  235. when(node.getWebPort()).thenReturn(mockedWebPort);
  236. // Act
  237. final int actualWebPort = product.getWebPortForNode(0);
  238. // Assert
  239. assertThat(actualWebPort, is(mockedWebPort));
  240. }
  241. @Test
  242. public void getNodes_whenOneNodeExists_shouldEnsureAllNonDebugPortsOfThatNodeAreSet() {
  243. // Arrange
  244. final Product product = new Product();
  245. final String instanceId = "theInstanceId";
  246. product.setInstanceId(instanceId);
  247. final Node node = mock(Node.class);
  248. product.setNodes(singletonList(node));
  249. // Act
  250. product.initialiseNodes();
  251. // Assert
  252. verify(node).ensureNonDebugPortsAreSet(instanceId);
  253. }
  254. private Optional<String> getUserConfiguredLicense(final String license) {
  255. final Product product = new Product();
  256. product.setLicense(license);
  257. return product.getUserConfiguredLicense();
  258. }
  259. @Test
  260. public void getEffectiveLicense_whenNoLicenseDetailsSet_shouldReturnEmpty() {
  261. assertThat(getUserConfiguredLicense(null), is(empty()));
  262. }
  263. @Test
  264. public void getEffectiveLicense_whenOnlyLicenseStringSet_shouldReturnIt() {
  265. final String license = "theLicense";
  266. assertThat(getUserConfiguredLicense(license), is(Optional.of(license)));
  267. }
  268. @Test
  269. public void defaultSystemProperty_shouldUpdateUnsetProperty() {
  270. final Map<String, Object> productProperties = new HashMap<>();
  271. thisProduct.setSystemPropertyVariables(productProperties);
  272. thisProduct.defaultSystemProperty("testKey", () -> "someValue");
  273. assertThat(thisProduct.getSystemPropertyVariables(),
  274. hasEntry("testKey", "someValue"));
  275. }
  276. @Test
  277. public void defaultSystemProperty_shouldNotUpdateSetProperty() {
  278. final Map<String, Object> productProperties = new HashMap<>();
  279. productProperties.put("testKey", "testValue");
  280. thisProduct.setSystemPropertyVariables(productProperties);
  281. thisProduct.defaultSystemProperty("testKey", () -> "someOtherValue");
  282. assertThat(thisProduct.getSystemPropertyVariables(),
  283. hasEntry("testKey", "testValue"));
  284. }
  285. }