PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-webresource-tests/src/test/java/it/com/atlassian/webresource/WrmRequireCallsTest.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 173 lines | 100 code | 19 blank | 54 comment | 0 complexity | 7cc9ee9f5e946676f8f10cc7ec57e0bc MD5 | raw file
  1. package it.com.atlassian.webresource;
  2. import it.com.atlassian.util.ConfigurationProperty;
  3. import it.com.atlassian.util.WebDriverTestUtil;
  4. import org.codehaus.jackson.type.TypeReference;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.junit.runners.Parameterized;
  9. import org.junit.runners.Parameterized.Parameters;
  10. import org.springframework.lang.NonNull;
  11. import java.util.Collection;
  12. import java.util.List;
  13. import static it.com.atlassian.util.JSONUtil.fileToEntity;
  14. import static it.com.atlassian.util.WebDriverTestUtil.findConsoleErrorMessages;
  15. import static it.com.atlassian.util.WebDriverTestUtil.getLoadedScriptsLog;
  16. import static java.util.Arrays.asList;
  17. import static java.util.Objects.requireNonNull;
  18. import static org.hamcrest.MatcherAssert.assertThat;
  19. import static org.hamcrest.Matchers.contains;
  20. import static org.hamcrest.Matchers.either;
  21. import static org.hamcrest.Matchers.emptyCollectionOf;
  22. import static org.hamcrest.Matchers.nullValue;
  23. /**
  24. * <p>
  25. * Asserts that various server-side and client-side calls to the WRM result in
  26. * resources being loaded in an expected order, at an expected time, and with expected properties.
  27. * </p>
  28. * <ul>
  29. * <li>
  30. * Production assets for the tests are stored in
  31. * <a href="filepath:../../../../../../main/resources">src/main/resources</a>.
  32. * </li>
  33. * <li>
  34. * Expected values are stored in JSON files at
  35. * <a href="filepath:../../../../../resources/test-cases/wrm-require-calls">test/resources/test-cases/wrm-require-calls</a>
  36. * </li>
  37. * </ul>
  38. * <p>
  39. * Each test is run using both Soy and Velocity, so that we demonstrate the APIs are callable from
  40. * supported templating languages.
  41. * </p>
  42. */
  43. @RunWith(Parameterized.class)
  44. public class WrmRequireCallsTest {
  45. private final String servletBaseUrl;
  46. private final String configurationFilePath;
  47. public WrmRequireCallsTest(@NonNull final String servletBaseUrl, @NonNull final String configurationFilePath) {
  48. this.servletBaseUrl = requireNonNull(servletBaseUrl, "The servlet base url is mandatory.");
  49. this.configurationFilePath = requireNonNull(configurationFilePath, "The configuration file path is mandatory.");
  50. }
  51. /**
  52. * Configure all the possible values used for the tests.
  53. * @return The collection containing the parameter for the {@link WrmRequireCallsTest} constructor.
  54. */
  55. @Parameters(name = "servletBaseUrl: {0}, configurationFilePath: {1}")
  56. public static Collection<Object[]> configure() {
  57. return asList(new Object[][] {
  58. {
  59. "/plugins/servlet/resources/soy",
  60. "/test-cases/wrm-require-calls/soy"
  61. },
  62. {
  63. "/plugins/servlet/resources/velocity",
  64. "/test-cases/wrm-require-calls/velocity"
  65. }
  66. });
  67. }
  68. @Before
  69. public void setUp() {
  70. ConfigurationProperty.clearAll();
  71. }
  72. @Test
  73. public void when_LoadRequiredScriptsAfterPage_Then_ShouldLoadRequiredScriptsAfterPageScripts() {
  74. // when
  75. navigate("/wrmRequire");
  76. // then
  77. testDependencies("/wrm-require/expected.json");
  78. }
  79. @Test
  80. public void when_LoadRequireLazilyAfterRequire_Then_ShouldLoadInOrder() {
  81. // when
  82. navigate("/wrmRequireLazilyAfterRequire");
  83. // then
  84. testDependencies("/wrm-require-lazily-after-require/expected.json");
  85. }
  86. @Test
  87. public void when_LoadRequireLazilyAfterInline_Then_ShouldLoadInOrder() {
  88. // when
  89. navigate("/wrmRequireLazilyAfterInline");
  90. // then
  91. testDependencies("/wrm-require-lazily-after-inline/expected.json");
  92. }
  93. @Test
  94. public void given_conditionalDiamondDependencyGraph_when_allSidesTrue_and_requiringInline_Then_LeftLoadsInline() {
  95. // when
  96. navigate("/wrmRequireInline?left&right&bottom");
  97. // then
  98. testDependencies("/wrm-require-inline/expected.json");
  99. }
  100. @Test
  101. // PLUGWEB-631
  102. public void given_conditionalDiamondDependencyGraph_when_allSidesTrue_then_BottomLoadsEagerly() {
  103. // when
  104. navigate("/wrmConditionalDiamond?left=&right=&bottom=");
  105. // then
  106. testDependencies("/wrm-conditional-diamond/expected-left-right-bottom.json");
  107. }
  108. @Test
  109. // PLUGWEB-631
  110. public void given_conditionalDiamondDependencyGraph_when_leftFalse_then_BottomLoadsLazily() {
  111. // when
  112. navigate("/wrmConditionalDiamond?right=&bottom=");
  113. // then
  114. testDependencies("/wrm-conditional-diamond/expected-right-bottom.json");
  115. }
  116. /**
  117. * Builds the configuration file full path based on a base path.
  118. *
  119. * @param configurationFilePath The configuration file sub-path.
  120. * @return The final configuration file path.
  121. */
  122. private String buildConfigurationFilePath(@NonNull final String configurationFilePath) {
  123. requireNonNull(configurationFilePath, "The configuration file sub-path is mandatory");
  124. return this.configurationFilePath + configurationFilePath;
  125. }
  126. /**
  127. * Perform the navigation to the page according to the defined path.
  128. *
  129. * @param pagePath The path of the page to be visited.
  130. */
  131. private void navigate(final String pagePath) {
  132. requireNonNull(pagePath, "The page path is mandatory");
  133. WebDriverTestUtil.navigate(servletBaseUrl, pagePath);
  134. }
  135. /**
  136. * Tests if the expected dependencies where added correctly to the path.
  137. *
  138. * @param expectedFilePath The expected file configuration.
  139. */
  140. private void testDependencies(final String expectedFilePath) {
  141. requireNonNull(expectedFilePath, "The expected file path is mandatory");
  142. final List<String> actualErrors = findConsoleErrorMessages();
  143. assertThat("No javascript errors should be thrown or error messages logged",
  144. actualErrors,
  145. either(emptyCollectionOf(String.class)).or(nullValue())
  146. );
  147. final String configurationFileFullPath = buildConfigurationFilePath(expectedFilePath);
  148. final List<String> expectedDependencies = fileToEntity(configurationFileFullPath, new ListTypeReference());
  149. final List<String> actualDependencies = (List<String>) getLoadedScriptsLog(expectedDependencies.size());
  150. assertThat(actualDependencies, contains(expectedDependencies.toArray()));
  151. }
  152. private static class ListTypeReference extends TypeReference<List<String>> {
  153. }
  154. }