PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/amps
Java | 210 lines | 164 code | 35 blank | 11 comment | 2 complexity | 75f2141aae1fe7dce8be22d641c84a2d MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.amps.product;
  2. import com.atlassian.maven.plugins.amps.DataSource;
  3. import com.atlassian.maven.plugins.amps.MavenContext;
  4. import com.atlassian.maven.plugins.amps.MavenGoals;
  5. import com.atlassian.maven.plugins.amps.Node;
  6. import com.atlassian.maven.plugins.amps.Product;
  7. import com.atlassian.maven.plugins.amps.product.manager.WebAppManager;
  8. import com.google.common.collect.ImmutableMap;
  9. import org.apache.maven.artifact.resolver.ArtifactResolver;
  10. import org.apache.maven.model.Build;
  11. import org.apache.maven.plugin.logging.Log;
  12. import org.apache.maven.project.MavenProject;
  13. import org.apache.maven.repository.RepositorySystem;
  14. import org.junit.Before;
  15. import org.junit.Rule;
  16. import org.junit.Test;
  17. import org.junit.rules.MethodRule;
  18. import org.junit.rules.TemporaryFolder;
  19. import org.mockito.ArgumentCaptor;
  20. import org.mockito.Mock;
  21. import org.mockito.junit.MockitoJUnit;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.List;
  25. import java.util.Map;
  26. import static com.atlassian.maven.plugins.amps.product.RefappProductHandler.ATLASSIAN_BUNDLED_PLUGINS_ZIP;
  27. import static java.util.Arrays.stream;
  28. import static java.util.Collections.emptyList;
  29. import static java.util.Collections.emptyMap;
  30. import static java.util.Collections.singletonList;
  31. import static org.apache.commons.lang3.JavaVersion.JAVA_9;
  32. import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast;
  33. import static org.hamcrest.MatcherAssert.assertThat;
  34. import static org.hamcrest.Matchers.allOf;
  35. import static org.hamcrest.Matchers.containsString;
  36. import static org.hamcrest.Matchers.equalTo;
  37. import static org.hamcrest.Matchers.hasEntry;
  38. import static org.hamcrest.Matchers.notNullValue;
  39. import static org.junit.Assert.assertFalse;
  40. import static org.junit.Assume.assumeTrue;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.verify;
  43. import static org.mockito.Mockito.when;
  44. @SuppressWarnings("unchecked")
  45. public class TestRefappProductHandler {
  46. private static final String JDBC_DRIVER_CLASS = "theDriver";
  47. private static final String JDBC_PASSWORD = "thePassword";
  48. private static final String JDBC_SCHEMA = "theSchema";
  49. private static final String JDBC_URL = "theUrl";
  50. private static final String JDBC_USER = "theUser";
  51. private static final String JDBC_VALIDATION_QUERY = "theValidationQuery";
  52. /*
  53. These properties and those in the next map down are defined in Refapp, see
  54. https://bitbucket.org/atlassian/atlassian-refapp/src/master/refapp-core/src/main/java/com/atlassian/plugin/refimpl/db/ConnectionProviderImpl.java
  55. */
  56. private static final Map<String, String> MANDATORY_JDBC_PROPERTIES = ImmutableMap.of(
  57. "refapp.jdbc.app.url", JDBC_URL,
  58. "refapp.jdbc.app.user", JDBC_USER,
  59. "refapp.jdbc.app.pass", JDBC_PASSWORD,
  60. "refapp.jdbc.driver.class.name", JDBC_DRIVER_CLASS,
  61. "refapp.jdbc.external", "true"
  62. );
  63. private static final Map<String, String> OPTIONAL_JDBC_PROPERTIES = ImmutableMap.of(
  64. "refapp.jdbc.app.schema", JDBC_SCHEMA,
  65. "refapp.jdbc.validation.query", JDBC_VALIDATION_QUERY
  66. );
  67. @Rule
  68. public final MethodRule mockitoRule = MockitoJUnit.rule();
  69. @Rule
  70. public final TemporaryFolder tempHome = new TemporaryFolder();
  71. @Mock
  72. private ArtifactResolver artifactResolver;
  73. @Mock
  74. private Build build;
  75. @Mock
  76. private MavenContext mavenContext;
  77. @Mock
  78. private MavenGoals mavenGoals;
  79. @Mock
  80. private MavenProject mavenProject;
  81. @Mock
  82. private Product product;
  83. @Mock
  84. private RepositorySystem repositorySystem;
  85. @Mock
  86. private WebAppManager webAppManager;
  87. // Can't use @InjectMocks because of method calls in the superclass' constructors
  88. private RefappProductHandler productHandler;
  89. @Before
  90. public void setUp() {
  91. final Log log = mock(Log.class);
  92. when(mavenContext.getLog()).thenReturn(log);
  93. when(mavenContext.getProject()).thenReturn(mavenProject);
  94. productHandler = new RefappProductHandler(
  95. mavenContext, mavenGoals, repositorySystem, artifactResolver, webAppManager);
  96. }
  97. @Test
  98. public void bundledPluginsLocationCorrectForDirectory() throws IOException {
  99. final File bundledPluginsDir = tempHome.newFolder(ATLASSIAN_BUNDLED_PLUGINS_ZIP.split("/"));
  100. assertBundledPluginPath(tempHome.getRoot(), bundledPluginsDir);
  101. }
  102. @Test
  103. public void bundledPluginsLocationCorrectForFallback() {
  104. final File bundledPluginsZip = new File(tempHome.getRoot(), ATLASSIAN_BUNDLED_PLUGINS_ZIP);
  105. assertBundledPluginPath(tempHome.getRoot(), bundledPluginsZip);
  106. }
  107. private void assertBundledPluginPath(final File appDir, final File expectedPath) {
  108. // Invoke
  109. final File bundledPluginPath = productHandler.getBundledPluginPath(product, appDir);
  110. // Check
  111. assertThat(bundledPluginPath, notNullValue());
  112. assertThat(expectedPath, equalTo(bundledPluginPath));
  113. }
  114. @Test
  115. public void getProductSpecificSystemProperties_whenNoDataSourceConfigured_shouldNotAddDbProperties() {
  116. final Map<String, String> systemProperties = assertDatabaseSystemProperties(emptyList(), emptyMap());
  117. assertFalse(systemProperties.keySet().stream()
  118. .anyMatch(key -> key.startsWith("refapp.jdbc.")));
  119. }
  120. @Test
  121. public void getProductSpecificSystemProperties_whenMinimalDataSourceConfigured_shouldAddDbProperties() {
  122. assertDatabaseSystemProperties(singletonList(mockDataSource(false)),
  123. MANDATORY_JDBC_PROPERTIES);
  124. }
  125. @Test
  126. public void getProductSpecificSystemProperties_whenMaximalDataSourceConfigured_shouldAddDbProperties() {
  127. assertDatabaseSystemProperties(singletonList(mockDataSource(true)),
  128. MANDATORY_JDBC_PROPERTIES, OPTIONAL_JDBC_PROPERTIES);
  129. }
  130. private static DataSource mockDataSource(final boolean withAdvancedOptions) {
  131. final DataSource dataSource = mock(DataSource.class);
  132. when(dataSource.getDriver()).thenReturn(JDBC_DRIVER_CLASS);
  133. when(dataSource.getUsername()).thenReturn(JDBC_USER);
  134. when(dataSource.getPassword()).thenReturn(JDBC_PASSWORD);
  135. when(dataSource.getUrl()).thenReturn(JDBC_URL);
  136. if (withAdvancedOptions) {
  137. when(dataSource.getSchema()).thenReturn(JDBC_SCHEMA);
  138. when(dataSource.getValidationQuery()).thenReturn(JDBC_VALIDATION_QUERY);
  139. } else {
  140. // Here we're testing that the production code ignores all types of blank value
  141. when(dataSource.getSchema()).thenReturn(null);
  142. when(dataSource.getValidationQuery()).thenReturn("");
  143. }
  144. return dataSource;
  145. }
  146. private Map<String, String> assertDatabaseSystemProperties(
  147. final List<DataSource> dataSources, final Map<String, String>... expectedDatabaseProperties) {
  148. // Arrange
  149. when(mavenProject.getBuild()).thenReturn(build);
  150. when(build.getDirectory()).thenReturn("theBuildDirectory");
  151. when(product.getDataHome()).thenReturn("theParent/theDataHome");
  152. when(product.getDataSources()).thenReturn(dataSources);
  153. when(product.getInstanceId()).thenReturn("theInstanceId");
  154. final Node node = mock(Node.class);
  155. when(product.getNodes()).thenReturn(singletonList(node));
  156. // Act
  157. final Map<String, String> systemProperties =
  158. productHandler.getProductSpecificSystemProperties(product, 0);
  159. // Assert
  160. stream(expectedDatabaseProperties)
  161. .flatMap(map -> map.entrySet().stream())
  162. .forEach(entry -> assertThat(systemProperties, hasEntry(entry.getKey(), entry.getValue())));
  163. return systemProperties;
  164. }
  165. @Test
  166. public void testFixJvmArgs() {
  167. assumeTrue(isJavaVersionAtLeast(JAVA_9));
  168. productHandler.fixJvmArgs(product);
  169. final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
  170. verify(product).setJvmArgs(captor.capture());
  171. assertThat(captor.getValue(), allOf(
  172. containsString("-Xmx512m"),
  173. containsString("--add-opens")));
  174. }
  175. }