PageRenderTime 57ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-webresource/src/test/java/com/atlassian/plugin/webresource/impl/http/ContextBatchResponseCachingTest.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 133 lines | 110 code | 23 blank | 0 comment | 0 complexity | 7097c3da5c786f84f57f40931ee0cb85 MD5 | raw file
  1. package com.atlassian.plugin.webresource.impl.http;
  2. import com.atlassian.plugin.webresource.impl.support.http.Request;
  3. import com.atlassian.plugin.webresource.impl.support.http.Response;
  4. import com.google.common.collect.ImmutableMap;
  5. import org.junit.Before;
  6. import org.junit.Ignore;
  7. import org.junit.Rule;
  8. import org.junit.Test;
  9. import org.junit.contrib.java.lang.system.RestoreSystemProperties;
  10. import org.junit.rules.TestRule;
  11. import org.junit.runner.RunWith;
  12. import org.mockito.Answers;
  13. import org.mockito.junit.MockitoJUnitRunner;
  14. import java.io.ByteArrayOutputStream;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.stream.Collectors;
  18. import static com.atlassian.plugin.webresource.TestUtils.cacheableRequestParams;
  19. import static com.atlassian.plugin.webresource.impl.config.Config.ENABLE_BUNDLE_HASH_VALIDATION;
  20. import static org.hamcrest.CoreMatchers.equalTo;
  21. import static org.junit.Assert.assertThat;
  22. import static org.mockito.ArgumentMatchers.any;
  23. import static org.mockito.ArgumentMatchers.eq;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.times;
  26. import static org.mockito.Mockito.verify;
  27. import static org.mockito.Mockito.when;
  28. @RunWith(MockitoJUnitRunner.class)
  29. public class ContextBatchResponseCachingTest extends AbstractResponseCachingTest {
  30. @Rule
  31. public final TestRule restoreSystemPropertiesRule = new RestoreSystemProperties();
  32. @Before
  33. public void setUp() {
  34. System.setProperty(ENABLE_BUNDLE_HASH_VALIDATION, Boolean.FALSE.toString());
  35. }
  36. @Test
  37. public void differentRoutesHaveSeparateCacheKeys() throws Exception {
  38. Router router = globals.getRouter();
  39. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/AAA/batch.js", cacheableRequestParams()), mockResponse());
  40. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/BBB/batch.js", cacheableRequestParams()), mockResponse());
  41. verify(cache, times(2)).cache(eq("http"), cacheKeyCaptor.capture(), any(), any());
  42. List<String> uniqueKeysUsed = cacheKeyCaptor.getAllValues().stream().distinct().collect(Collectors.toList());
  43. assertThat(String.format("Unique cache keys found: %s", uniqueKeysUsed), uniqueKeysUsed.size(), equalTo(2));
  44. }
  45. @Test
  46. public void differentResourceTypesHaveSeparateCacheKeys() throws Exception {
  47. Router router = globals.getRouter();
  48. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/AAA/batch.js", cacheableRequestParams()), mockResponse());
  49. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/css/AAA/batch.css", cacheableRequestParams()), mockResponse());
  50. verify(cache, times(2)).cache(eq("http"), cacheKeyCaptor.capture(), any(), any());
  51. List<String> uniqueKeysUsed = cacheKeyCaptor.getAllValues().stream().distinct().collect(Collectors.toList());
  52. assertThat(String.format("Unique cache keys found: %s", uniqueKeysUsed), uniqueKeysUsed.size(), equalTo(2));
  53. }
  54. @Test
  55. public void identicalRoutesHaveSameCacheKey() throws Exception {
  56. Router router = globals.getRouter();
  57. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/AAA/batch.js", cacheableRequestParams()), mockResponse());
  58. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/AAA/batch.js", cacheableRequestParams()), mockResponse());
  59. verify(cache, times(2)).cache(eq("http"), cacheKeyCaptor.capture(), any(), any());
  60. List<String> uniqueKeysUsed = cacheKeyCaptor.getAllValues().stream().distinct().collect(Collectors.toList());
  61. assertThat(String.format("Unique cache keys found: %s", uniqueKeysUsed), uniqueKeysUsed.size(), equalTo(1));
  62. }
  63. @Test
  64. @Ignore("PLUGWEB-640 - Confluence rely on order significance. Can be changed in WRM v6.")
  65. public void contextInclusionsAndExclusionsAreCommutative() throws Exception {
  66. Router router = globals.getRouter();
  67. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/A,B/batch.js", cacheableRequestParams()), mockResponse());
  68. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/B,A/batch.js", cacheableRequestParams()), mockResponse());
  69. verify(cache, times(2)).cache(eq("http"), cacheKeyCaptor.capture(), any(), any());
  70. List<String> uniqueKeysUsed = cacheKeyCaptor.getAllValues().stream().distinct().collect(Collectors.toList());
  71. assertThat(String.format("Unique cache keys found: %s", uniqueKeysUsed), uniqueKeysUsed.size(), equalTo(1));
  72. }
  73. @Test
  74. public void contextInclusionsAndExclusionsAreCollapsed() throws Exception {
  75. Router router = globals.getRouter();
  76. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/B/batch.js", cacheableRequestParams()), mockResponse());
  77. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/A,B,-A/batch.js", cacheableRequestParams()), mockResponse());
  78. verify(cache, times(2)).cache(eq("http"), cacheKeyCaptor.capture(), any(), any());
  79. List<String> uniqueKeysUsed = cacheKeyCaptor.getAllValues().stream().distinct().collect(Collectors.toList());
  80. assertThat(String.format("Unique cache keys found: %s", uniqueKeysUsed), uniqueKeysUsed.size(), equalTo(1));
  81. }
  82. @Test
  83. public void urlParametersAreCommutative() throws Exception {
  84. Router router = globals.getRouter();
  85. Map<String, String> params1 = cacheableRequestParams(ImmutableMap.of("one", "1", "two", "2", "three", "3", "four", "4", "five", "5"));
  86. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/AAA/batch.js", params1), mockResponse());
  87. Map<String, String> params2 = cacheableRequestParams(ImmutableMap.of("five", "5", "four", "4", "three", "3", "two", "2", "one", "1"));
  88. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/AAA/batch.js", params2), mockResponse());
  89. verify(cache, times(2)).cache(eq("http"), cacheKeyCaptor.capture(), any(), any());
  90. List<String> uniqueKeysUsed = cacheKeyCaptor.getAllValues().stream().distinct().collect(Collectors.toList());
  91. assertThat(String.format("Unique cache keys found: %s", uniqueKeysUsed), uniqueKeysUsed.size(), equalTo(1));
  92. }
  93. @Test
  94. public void sourceMapUsesDifferentCache() throws Exception {
  95. when(globals.getConfig().isSourceMapEnabled()).thenReturn(true);
  96. Router router = globals.getRouter();
  97. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/A,B,C,D/batch.js", cacheableRequestParams()), mockResponse());
  98. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/js/A,B,C,D/batch.js.map", cacheableRequestParams()), mockResponse());
  99. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/css/A,B,C,D/batch.css", cacheableRequestParams()), mockResponse());
  100. router.dispatch(new Request(globals, "/s/1/1/1/contextbatch/css/A,B,C,D/batch.css.map", cacheableRequestParams()), mockResponse());
  101. verify(cache, times(4)).cache(eq("http"), cacheKeyCaptor.capture(), any(), any());
  102. List<String> uniqueKeysUsed = cacheKeyCaptor.getAllValues().stream().distinct().collect(Collectors.toList());
  103. assertThat(String.format("Unique cache keys found: %s", uniqueKeysUsed), uniqueKeysUsed.size(), equalTo(4));
  104. }
  105. private Response mockResponse() {
  106. Response r = mock(Response.class, Answers.RETURNS_DEEP_STUBS);
  107. when(r.getOutputStream()).thenReturn(new ByteArrayOutputStream());
  108. return r;
  109. }
  110. }