PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-webresource/src/test/java/com/atlassian/plugin/webresource/integration/content/TestContentInclusionOnConditionalDiamondGraph.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 265 lines | 175 code | 40 blank | 50 comment | 0 complexity | 9eaa69d2255a79c5883e53152df01b46 MD5 | raw file
  1. package com.atlassian.plugin.webresource.integration.content;
  2. import com.atlassian.plugin.webresource.condition.SimpleUrlReadingCondition;
  3. import com.atlassian.plugin.webresource.integration.TestCase;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.junit.runners.Parameterized;
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.List;
  11. import java.util.stream.Collectors;
  12. import static com.google.common.collect.Lists.newArrayList;
  13. import static java.util.Arrays.asList;
  14. import static org.hamcrest.MatcherAssert.assertThat;
  15. import static org.hamcrest.Matchers.stringContainsInOrder;
  16. import static org.mockito.Mockito.doReturn;
  17. // PLUGWEB-648
  18. @RunWith(Parameterized.class)
  19. public class TestContentInclusionOnConditionalDiamondGraph extends TestCase {
  20. private static boolean featureAEnabled;
  21. private final boolean contextBatchingEnabled;
  22. private final boolean webresourceBatchingEnabled;
  23. private List<String> urls;
  24. @Parameterized.Parameters(name = "context-batching: {0}, webresource-batching: {1}")
  25. public static Collection<Object[]> configure() {
  26. return asList(new Object[][]{
  27. {
  28. true,
  29. true
  30. },
  31. {
  32. true,
  33. false
  34. },
  35. {
  36. false,
  37. true
  38. },
  39. {
  40. false,
  41. false
  42. }
  43. });
  44. }
  45. public TestContentInclusionOnConditionalDiamondGraph(final boolean contextBatchingEnabled, final boolean webresourceBatchingEnabled) {
  46. this.contextBatchingEnabled = contextBatchingEnabled;
  47. this.webresourceBatchingEnabled = webresourceBatchingEnabled;
  48. }
  49. /**
  50. * <p>
  51. * Prepares five web-resources: `feature-a`, `feature-b`, `feature-s`, `shared-lib`, and `deep-lib`.
  52. * <p>
  53. * Their relationships are as follows:
  54. * </p>
  55. * <ul>
  56. * <li>Feature A is in the `foo` context</li>
  57. * <li>Feature B is in the `bar` context</li>
  58. * <li>Features A and B depend on `shared-lib`</li>
  59. * <li>`shared-lib` depends on `deep-lib`</li>
  60. * <li>Feature A is conditionally enabled.</li>
  61. * </ul>
  62. * <p>
  63. * This results in a scenario where `shared-lib` will sometimes be needed in
  64. * the `foo` context (via `feature-a`), but always needed in the `bar` context.
  65. * </p>
  66. * <p>
  67. * When sub-batch generation or other ordering requirements dictate that `foo`
  68. * should load before `bar`, we have a scenario where `shared-lib` can jump
  69. * between two context batches.
  70. * </p>
  71. * <p>
  72. * Note: Feature S is added to both `foo` and `bar` contexts
  73. * to ensure there's at least one unconditional overlap, which should
  74. * guarantee the second URL generated has an explicit exclusion in it.
  75. * </p>
  76. *
  77. * @see <a href="https://ecosystem.atlassian.net/browse/PLUGWEB-624">PLUGWEB-624</a>
  78. * @see <a href="https://ecosystem.atlassian.net/browse/PLUGWEB-648">PLUGWEB-648</a>
  79. */
  80. @Before
  81. public void setUp() throws Exception {
  82. wr.configure(config -> {
  83. doReturn(contextBatchingEnabled).when(config).isContextBatchingEnabled();
  84. doReturn(webresourceBatchingEnabled).when(config).isWebResourceBatchingEnabled();
  85. doReturn(false).when(config).isSuperBatchingEnabled();
  86. doReturn(false).when(config).isPerformanceTrackingEnabled();
  87. })
  88. .plugin("plugin.key")
  89. .webResource("feature-a")
  90. .context("foo")
  91. .condition(FeatureACondition.class)
  92. .dependency(":shared-lib")
  93. .resource("feature-a.js")
  94. .webResource("feature-b")
  95. .context("bar")
  96. .dependency(":shared-lib")
  97. .resource("feature-b.js")
  98. .webResource("feature-s")
  99. .context("foo")
  100. .context("bar")
  101. .resource("feature-s.js")
  102. .webResource("shared-lib")
  103. .dependency(":deep-lib")
  104. .webResource("deep-lib")
  105. .resource("lib.js")
  106. .end();
  107. urls = new ArrayList<>();
  108. }
  109. @Test
  110. public void given_featureAEnabled_when_ContextSubBatchRequested_then_BundlehashIsConsistent() {
  111. // given
  112. featureAEnabled = true;
  113. // when
  114. wr.requireContext("foo");
  115. urls.addAll(wr.paths());
  116. wr.requireContext("bar");
  117. urls.addAll(wr.paths());
  118. final String content = getAllContentInOrder(urls);
  119. // then
  120. assertThat(content, stringContainsInOrder(newArrayList(
  121. "content of lib.js",
  122. "content of feature-a.js",
  123. "content of feature-s.js",
  124. "content of feature-b.js"
  125. )));
  126. }
  127. @Test
  128. public void given_featureADisabled_when_ContextSubBatchRequested_then_BundlehashIsConsistent() {
  129. // given
  130. featureAEnabled = false;
  131. // when
  132. wr.requireContext("foo");
  133. urls.addAll(wr.paths());
  134. wr.requireContext("bar");
  135. urls.addAll(wr.paths());
  136. final String content = getAllContentInOrder(urls);
  137. // then
  138. assertThat(content, stringContainsInOrder(newArrayList(
  139. "content of feature-s.js",
  140. "content of lib.js",
  141. "content of feature-b.js"
  142. )));
  143. }
  144. @Test
  145. public void given_featureAEnabled_when_FooAndBarRequested_then_BundlehashIsConsistent() {
  146. // given
  147. featureAEnabled = true;
  148. // when
  149. wr.requireContext("foo");
  150. wr.requireContext("bar");
  151. urls.addAll(wr.paths());
  152. final String content = getAllContentInOrder(urls);
  153. // then
  154. assertThat(content, stringContainsInOrder(newArrayList(
  155. "content of lib.js",
  156. "content of feature-a.js",
  157. "content of feature-s.js",
  158. "content of feature-b.js"
  159. )));
  160. }
  161. @Test
  162. public void given_featureADisabled_when_FooAndBarRequested_then_BundlehashIsConsistent() {
  163. // given
  164. featureAEnabled = false;
  165. // when
  166. wr.requireContext("foo");
  167. wr.requireContext("bar");
  168. urls.addAll(wr.paths());
  169. final String content = getAllContentInOrder(urls);
  170. // then
  171. assertThat(content, stringContainsInOrder(newArrayList(
  172. "content of feature-s.js",
  173. "content of lib.js",
  174. "content of feature-b.js"
  175. )));
  176. }
  177. @Test
  178. public void given_featureAEnabled_when_BarAndFooRequested_then_BundlehashIsConsistent() {
  179. // given
  180. featureAEnabled = true;
  181. // when
  182. wr.requireContext("bar");
  183. wr.requireContext("foo");
  184. urls.addAll(wr.paths());
  185. final String content = getAllContentInOrder(urls);
  186. // then
  187. assertThat(content, stringContainsInOrder(newArrayList(
  188. "content of lib.js",
  189. "content of feature-b.js",
  190. "content of feature-s.js",
  191. "content of feature-a.js"
  192. )));
  193. }
  194. @Test
  195. public void given_featureADisabled_when_BarAndFooRequested_then_BundlehashIsConsistent() {
  196. // given
  197. featureAEnabled = false;
  198. // when
  199. wr.requireContext("bar");
  200. wr.requireContext("foo");
  201. urls.addAll(wr.paths());
  202. final String content = getAllContentInOrder(urls);
  203. // then
  204. assertThat(content, stringContainsInOrder(newArrayList(
  205. "content of lib.js",
  206. "content of feature-b.js",
  207. "content of feature-s.js"
  208. )));
  209. }
  210. private String getAllContentInOrder(final List<String> urls) {
  211. return urls.stream()
  212. .map(wr::getContent)
  213. .collect(Collectors.joining("\n"));
  214. }
  215. public static class FeatureACondition extends SimpleUrlReadingCondition {
  216. @Override
  217. protected boolean isConditionTrue() {
  218. return featureAEnabled;
  219. }
  220. @Override
  221. protected String queryKey() {
  222. return "feature-a";
  223. }
  224. }
  225. }