/atlassian-plugins-webresource/src/test/java/com/atlassian/plugin/webresource/integration/content/TestContentInclusionOnConditionalDiamondGraph.java
Java | 265 lines | 175 code | 40 blank | 50 comment | 0 complexity | 9eaa69d2255a79c5883e53152df01b46 MD5 | raw file
- package com.atlassian.plugin.webresource.integration.content;
- import com.atlassian.plugin.webresource.condition.SimpleUrlReadingCondition;
- import com.atlassian.plugin.webresource.integration.TestCase;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.junit.runners.Parameterized;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import java.util.stream.Collectors;
- import static com.google.common.collect.Lists.newArrayList;
- import static java.util.Arrays.asList;
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.hamcrest.Matchers.stringContainsInOrder;
- import static org.mockito.Mockito.doReturn;
- // PLUGWEB-648
- @RunWith(Parameterized.class)
- public class TestContentInclusionOnConditionalDiamondGraph extends TestCase {
- private static boolean featureAEnabled;
- private final boolean contextBatchingEnabled;
- private final boolean webresourceBatchingEnabled;
- private List<String> urls;
- @Parameterized.Parameters(name = "context-batching: {0}, webresource-batching: {1}")
- public static Collection<Object[]> configure() {
- return asList(new Object[][]{
- {
- true,
- true
- },
- {
- true,
- false
- },
- {
- false,
- true
- },
- {
- false,
- false
- }
- });
- }
- public TestContentInclusionOnConditionalDiamondGraph(final boolean contextBatchingEnabled, final boolean webresourceBatchingEnabled) {
- this.contextBatchingEnabled = contextBatchingEnabled;
- this.webresourceBatchingEnabled = webresourceBatchingEnabled;
- }
- /**
- * <p>
- * Prepares five web-resources: `feature-a`, `feature-b`, `feature-s`, `shared-lib`, and `deep-lib`.
- * <p>
- * Their relationships are as follows:
- * </p>
- * <ul>
- * <li>Feature A is in the `foo` context</li>
- * <li>Feature B is in the `bar` context</li>
- * <li>Features A and B depend on `shared-lib`</li>
- * <li>`shared-lib` depends on `deep-lib`</li>
- * <li>Feature A is conditionally enabled.</li>
- * </ul>
- * <p>
- * This results in a scenario where `shared-lib` will sometimes be needed in
- * the `foo` context (via `feature-a`), but always needed in the `bar` context.
- * </p>
- * <p>
- * When sub-batch generation or other ordering requirements dictate that `foo`
- * should load before `bar`, we have a scenario where `shared-lib` can jump
- * between two context batches.
- * </p>
- * <p>
- * Note: Feature S is added to both `foo` and `bar` contexts
- * to ensure there's at least one unconditional overlap, which should
- * guarantee the second URL generated has an explicit exclusion in it.
- * </p>
- *
- * @see <a href="https://ecosystem.atlassian.net/browse/PLUGWEB-624">PLUGWEB-624</a>
- * @see <a href="https://ecosystem.atlassian.net/browse/PLUGWEB-648">PLUGWEB-648</a>
- */
- @Before
- public void setUp() throws Exception {
- wr.configure(config -> {
- doReturn(contextBatchingEnabled).when(config).isContextBatchingEnabled();
- doReturn(webresourceBatchingEnabled).when(config).isWebResourceBatchingEnabled();
- doReturn(false).when(config).isSuperBatchingEnabled();
- doReturn(false).when(config).isPerformanceTrackingEnabled();
- })
- .plugin("plugin.key")
- .webResource("feature-a")
- .context("foo")
- .condition(FeatureACondition.class)
- .dependency(":shared-lib")
- .resource("feature-a.js")
- .webResource("feature-b")
- .context("bar")
- .dependency(":shared-lib")
- .resource("feature-b.js")
- .webResource("feature-s")
- .context("foo")
- .context("bar")
- .resource("feature-s.js")
- .webResource("shared-lib")
- .dependency(":deep-lib")
- .webResource("deep-lib")
- .resource("lib.js")
- .end();
- urls = new ArrayList<>();
- }
- @Test
- public void given_featureAEnabled_when_ContextSubBatchRequested_then_BundlehashIsConsistent() {
- // given
- featureAEnabled = true;
- // when
- wr.requireContext("foo");
- urls.addAll(wr.paths());
- wr.requireContext("bar");
- urls.addAll(wr.paths());
- final String content = getAllContentInOrder(urls);
- // then
- assertThat(content, stringContainsInOrder(newArrayList(
- "content of lib.js",
- "content of feature-a.js",
- "content of feature-s.js",
- "content of feature-b.js"
- )));
- }
- @Test
- public void given_featureADisabled_when_ContextSubBatchRequested_then_BundlehashIsConsistent() {
- // given
- featureAEnabled = false;
- // when
- wr.requireContext("foo");
- urls.addAll(wr.paths());
- wr.requireContext("bar");
- urls.addAll(wr.paths());
- final String content = getAllContentInOrder(urls);
- // then
- assertThat(content, stringContainsInOrder(newArrayList(
- "content of feature-s.js",
- "content of lib.js",
- "content of feature-b.js"
- )));
- }
- @Test
- public void given_featureAEnabled_when_FooAndBarRequested_then_BundlehashIsConsistent() {
- // given
- featureAEnabled = true;
- // when
- wr.requireContext("foo");
- wr.requireContext("bar");
- urls.addAll(wr.paths());
- final String content = getAllContentInOrder(urls);
- // then
- assertThat(content, stringContainsInOrder(newArrayList(
- "content of lib.js",
- "content of feature-a.js",
- "content of feature-s.js",
- "content of feature-b.js"
- )));
- }
- @Test
- public void given_featureADisabled_when_FooAndBarRequested_then_BundlehashIsConsistent() {
- // given
- featureAEnabled = false;
- // when
- wr.requireContext("foo");
- wr.requireContext("bar");
- urls.addAll(wr.paths());
- final String content = getAllContentInOrder(urls);
- // then
- assertThat(content, stringContainsInOrder(newArrayList(
- "content of feature-s.js",
- "content of lib.js",
- "content of feature-b.js"
- )));
- }
- @Test
- public void given_featureAEnabled_when_BarAndFooRequested_then_BundlehashIsConsistent() {
- // given
- featureAEnabled = true;
- // when
- wr.requireContext("bar");
- wr.requireContext("foo");
- urls.addAll(wr.paths());
- final String content = getAllContentInOrder(urls);
- // then
- assertThat(content, stringContainsInOrder(newArrayList(
- "content of lib.js",
- "content of feature-b.js",
- "content of feature-s.js",
- "content of feature-a.js"
- )));
- }
- @Test
- public void given_featureADisabled_when_BarAndFooRequested_then_BundlehashIsConsistent() {
- // given
- featureAEnabled = false;
- // when
- wr.requireContext("bar");
- wr.requireContext("foo");
- urls.addAll(wr.paths());
- final String content = getAllContentInOrder(urls);
- // then
- assertThat(content, stringContainsInOrder(newArrayList(
- "content of lib.js",
- "content of feature-b.js",
- "content of feature-s.js"
- )));
- }
- private String getAllContentInOrder(final List<String> urls) {
- return urls.stream()
- .map(wr::getContent)
- .collect(Collectors.joining("\n"));
- }
- public static class FeatureACondition extends SimpleUrlReadingCondition {
- @Override
- protected boolean isConditionTrue() {
- return featureAEnabled;
- }
- @Override
- protected String queryKey() {
- return "feature-a";
- }
- }
- }