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

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

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 180 lines | 135 code | 29 blank | 16 comment | 3 complexity | 929c38101e62cebcd42f4539187e6e2b MD5 | raw file
  1. package com.atlassian.plugin.webresource.integration.content;
  2. import com.atlassian.plugin.webresource.integration.TestCase;
  3. import com.atlassian.plugin.webresource.integration.fixtures.BroadFeatures;
  4. import com.atlassian.plugin.webresource.integration.stub.WebResource;
  5. import com.atlassian.webresource.api.assembler.resource.ResourcePhase;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.hamcrest.Description;
  8. import org.hamcrest.Matcher;
  9. import org.hamcrest.TypeSafeMatcher;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import org.junit.runner.RunWith;
  13. import org.junit.runners.Parameterized;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Collection;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Set;
  20. import java.util.function.Predicate;
  21. import java.util.stream.Collectors;
  22. import static com.atlassian.plugin.webresource.util.DuplicateContentMatcher.doesNotDuplicateSuperbatchContents;
  23. import static java.util.Arrays.asList;
  24. import static org.hamcrest.MatcherAssert.assertThat;
  25. import static org.hamcrest.Matchers.anyOf;
  26. import static org.hamcrest.Matchers.containsString;
  27. import static org.hamcrest.Matchers.not;
  28. import static org.mockito.Mockito.doReturn;
  29. @RunWith(Parameterized.class)
  30. public class TestContentInclusionWithSuperbatch extends TestCase {
  31. private static final boolean[] possibleContextBatchingValues = new boolean[]{true, false};
  32. private static final boolean[] possibleWebresourceBatchingValues = new boolean[]{true, false};
  33. private final boolean contextBatchingEnabled;
  34. private final boolean webresourceBatchingEnabled;
  35. private final ResourcePhase superbatchPhase;
  36. public TestContentInclusionWithSuperbatch(
  37. final boolean contextBatchingEnabled,
  38. final boolean webresourceBatchingEnabled,
  39. final ResourcePhase superbatchPhase) {
  40. this.contextBatchingEnabled = contextBatchingEnabled;
  41. this.webresourceBatchingEnabled = webresourceBatchingEnabled;
  42. this.superbatchPhase = superbatchPhase;
  43. }
  44. @Parameterized.Parameters(name = "superbatch-phase: {2}, context-batching: {0}, webresource-batching: {1}")
  45. public static Collection<Object[]> configure() {
  46. final Collection<Object[]> values = new ArrayList<>();
  47. for (final ResourcePhase phase : ResourcePhase.values()) {
  48. for (final boolean contextBatching : possibleContextBatchingValues) {
  49. for (final boolean webresourceBatching : possibleWebresourceBatchingValues) {
  50. values.add(new Object[]{contextBatching, webresourceBatching, phase});
  51. }
  52. }
  53. }
  54. return values;
  55. }
  56. /**
  57. *
  58. */
  59. @Before
  60. public void setUp() throws Exception {
  61. BroadFeatures.configure(
  62. wr.configure(config -> {
  63. doReturn(contextBatchingEnabled).when(config).isContextBatchingEnabled();
  64. doReturn(webresourceBatchingEnabled).when(config).isWebResourceBatchingEnabled();
  65. doReturn(true).when(config).isSuperBatchingEnabled();
  66. doReturn(false).when(config).isPerformanceTrackingEnabled();
  67. }).addToSuperbatch(
  68. "plugin.key:feature-04",
  69. "plugin.key:feature-05",
  70. "plugin.key:feature-14",
  71. "plugin.key:feature-15",
  72. "plugin.key:feature-24",
  73. "plugin.key:feature-25"
  74. )
  75. );
  76. wr.getPageBuilderService().assembler().resources().requireSuperbatch(superbatchPhase);
  77. }
  78. @Test
  79. public void given_nothingRequested_when_1NcontextRequested_then_shouldOnlyServe1NContentOnce() {
  80. // when
  81. wr.requireContext("1N");
  82. final WebResource.DrainResult result = wr.drain();
  83. final String content = wr.getContent(result);
  84. // then
  85. assertThat(content, doesNotDuplicateSuperbatchContents());
  86. }
  87. @Test
  88. public void given_nothingRequested_when_1Nand2NcontextRequested_then_shouldOnlyServeContentOnce() {
  89. // when
  90. wr.requireContext("1N");
  91. wr.requireContext("2N");
  92. final WebResource.DrainResult result = wr.drain();
  93. final String content = wr.getContent(result);
  94. // then
  95. assertThat(content, doesNotDuplicateSuperbatchContents());
  96. }
  97. @Test
  98. public void given_nothingRequested_when_1NContextRequestedMultipleTimes_then_shouldOnlyServeContentOnce() {
  99. // given
  100. final StringBuffer sb = new StringBuffer();
  101. // when
  102. wr.requireContext("1N");
  103. sb.append(wr.getContent(wr.drain()));
  104. wr.requireContext("1N");
  105. sb.append(wr.getContent(wr.drain()));
  106. wr.requireContext("1N");
  107. sb.append(wr.getContent(wr.drain()));
  108. final String content = sb.toString();
  109. // then
  110. assertThat(content, doesNotDuplicateSuperbatchContents());
  111. }
  112. @Test
  113. public void given_nothingRequested_when_AllContextsRequestedOverMultipleDrains_then_shouldOnlyServeContentOnce() {
  114. // given
  115. final StringBuffer sb = new StringBuffer();
  116. // when
  117. wr.requireContext("0N");
  118. sb.append(wr.getContent(wr.drain()));
  119. wr.requireContext("1N");
  120. sb.append(wr.getContent(wr.drain()));
  121. wr.requireContext("2N");
  122. sb.append(wr.getContent(wr.drain()));
  123. final String content = sb.toString();
  124. // then
  125. assertThat(content, doesNotDuplicateSuperbatchContents());
  126. }
  127. @Test
  128. public void given_0NcontextAlreadyRequested_when_1Nand2NcontextRequested_then_shouldOnlyServeContentOnce() {
  129. //given
  130. wr.excludeContext("0N");
  131. // when
  132. wr.requireContext("1N");
  133. wr.requireContext("2N");
  134. final WebResource.DrainResult result = wr.drain();
  135. final String content = wr.getContent(result);
  136. // then
  137. assertThat(content, doesNotDuplicateSuperbatchContents());
  138. assertThat(content, not(anyOf(
  139. containsString("content of feature-00.js"),
  140. containsString("content of feature-01.js"),
  141. containsString("content of feature-02.js"),
  142. containsString("content of feature-03.js"),
  143. containsString("content of feature-04.js"),
  144. containsString("content of feature-05.js"),
  145. containsString("content of feature-06.js"),
  146. containsString("content of feature-07.js"),
  147. containsString("content of feature-08.js"),
  148. containsString("content of feature-09.js")
  149. )));
  150. }
  151. }