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

/atlassian-plugins-webresource/src/test/java/com/atlassian/plugin/webresource/graph/DependencyGraphTest.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 179 lines | 112 code | 20 blank | 47 comment | 3 complexity | c710356df153072f4c30930731ea9067 MD5 | raw file
  1. package com.atlassian.plugin.webresource.graph;
  2. import com.atlassian.plugin.webresource.ResourceBatchingConfiguration;
  3. import com.atlassian.plugin.webresource.mocks.WebResourceModuleDescriptorMock;
  4. import com.atlassian.plugin.webresource.models.Requestable;
  5. import com.atlassian.plugin.webresource.models.WebResourceContextKey;
  6. import com.atlassian.plugin.webresource.models.WebResourceKey;
  7. import org.codehaus.jackson.type.TypeReference;
  8. import org.junit.Test;
  9. import java.util.Collection;
  10. import static com.atlassian.util.JSONUtil.fileToEntity;
  11. import static java.lang.String.format;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.mockito.Mockito.mock;
  16. public class DependencyGraphTest {
  17. private static final String FEATURE_PATTERN_NAME = "feature_%s.js";
  18. private static final String FEATURE_DEPENDENCY_PATTERN_NAME = "feature_%s.%s.js";
  19. private static final int ROOT_NODE_INDEX = 0;
  20. private static final String TEST_CASE_FOLDER = "/test-cases/graph/%s";
  21. @Test
  22. public void when_requestingSomethingNotInGraph_Then_hasDependencyReturnsFalse() {
  23. //given
  24. final DependencyGraph<Requestable> originalGraph = buildArtificialGraph(10, 10);
  25. //when
  26. final String requestableKey = format(FEATURE_PATTERN_NAME, "abcd");
  27. final boolean result = originalGraph.hasDependency(new WebResourceKey(requestableKey));
  28. // then
  29. assertFalse(result);
  30. }
  31. @Test
  32. public void when_ThereIsBigDependencyGraph_And_SourceRequestableKeyIsRoot_Then_ReturnEntireDependencyGraph() {
  33. //given
  34. final DependencyGraph<Requestable> originalGraph = buildArtificialGraph(1000, 1000);
  35. //when
  36. final String rootRequestableKey = format(FEATURE_PATTERN_NAME, ROOT_NODE_INDEX);
  37. final DependencyGraph<Requestable> actualGraph = originalGraph.findDependencySubGraphByRequestableKey(new WebResourceKey(rootRequestableKey));
  38. //then
  39. assertEquals(originalGraph, actualGraph);
  40. }
  41. @Test
  42. public void when_ThereIsMatchingDependantsForSourceRequestableKey_Then_ReturnDependantsSubGraph() {
  43. //given
  44. final DependencyGraph<Requestable> fullResourceDependencyGraph = buildGraphFromFile("when-there-is-matching-for-source-vertex/full-resource-dependency-graph.json");
  45. //when
  46. final DependencyGraph<Requestable> actualResourceDependantSubGraph = fullResourceDependencyGraph.findDependantsSubGraphByKey(new WebResourceKey("atlassian-plugins-webresource-parent.refapp-integration-test:r6"));
  47. //then
  48. final DependencyGraph<Requestable> expectedResourceDependantSubGraph = buildGraphFromFile("when-there-is-matching-for-source-vertex/expected-resource-dependant-sub-graph.json");
  49. assertEquals(expectedResourceDependantSubGraph, actualResourceDependantSubGraph);
  50. }
  51. @Test
  52. public void when_ThereIsMatchingDependantsForSourceRequestableKeyWithMultipleEdges_Then_ReturnDependantsSubGraph() {
  53. //given
  54. final DependencyGraph<Requestable> fullResourceDependencyGraph = buildGraphFromFile("when-there-is-matching-for-source-vertex-with-mult-edges/full-resource-dependency-graph.json");
  55. //when
  56. final DependencyGraph<Requestable> actualResourceDependantSubGraph = fullResourceDependencyGraph.findDependantsSubGraphByKey(new WebResourceKey("atlassian-plugins-webresource-parent.refapp-integration-test:r4"));
  57. //then
  58. final DependencyGraph<Requestable> expectedResourceDependantSubGraph = buildGraphFromFile("when-there-is-matching-for-source-vertex-with-mult-edges/expected-resource-dependant-sub-graph.json");
  59. assertEquals(expectedResourceDependantSubGraph, actualResourceDependantSubGraph);
  60. }
  61. @Test
  62. public void when_ThereIsCycleDependencyInGraph_Then_ReturnCyclicSubGraph() {
  63. //given
  64. final DependencyGraph<Requestable> fullResourceDependencyGraph = buildGraphFromFile("when-there-is-cyclic-dependency/full-resource-dependency-graph.json");
  65. //when
  66. final DependencyGraph<Requestable> actualCyclicSubGraph = fullResourceDependencyGraph.findCyclicSubGraphByVertex(new WebResourceKey("atlassian-plugins-webresource-parent.refapp-integration-test:r1"));
  67. //then
  68. final DependencyGraph<Requestable> expectedCyclicSubGraph = buildGraphFromFile("when-there-is-cyclic-dependency/expected-resource-dependant-sub-graph.json");
  69. assertEquals(expectedCyclicSubGraph, actualCyclicSubGraph);
  70. }
  71. @Test
  72. public void when_ThereIsIntersectionBetweenTwoSubGraphs_Then_ReturnIntersectionGraph() {
  73. //given
  74. final DependencyGraph<Requestable> fullResourceDependencyGraph = buildGraphFromFile("when-there-is-intersection-between-two-sub-graphs/full-resource-dependency-graph.json");
  75. //when
  76. final DependencyGraph<Requestable> actualIntersectionSubGraph = fullResourceDependencyGraph.findIntersectionSubGraph(new WebResourceKey("atlassian-plugins-webresource-parent.refapp-integration-test:r6"), new WebResourceKey("atlassian-plugins-webresource-parent.refapp-integration-test:r4"));
  77. //then
  78. final DependencyGraph<Requestable> expectedIntersectionSubGraph = buildGraphFromFile("when-there-is-intersection-between-two-sub-graphs/expected-resource-dependant-sub-graph.json");
  79. assertEquals(expectedIntersectionSubGraph, actualIntersectionSubGraph);
  80. }
  81. @Test
  82. public void given_ThereAreMutlipleWebResourcesInAContext_Then_hasDependencyReturnsTrueForFeatureContext() {
  83. // given
  84. final DependencyGraph<Requestable> fullResourceDependencyGraph = buildGraphFromFile("given-multiple-web-resources-in-a-context/full-resource-dependency-graph.json");
  85. //when
  86. final boolean result = fullResourceDependencyGraph.hasDependency(new WebResourceContextKey("features"));
  87. // then
  88. assertTrue(result);
  89. }
  90. @Test
  91. public void given_ThereAreMutlipleWebResourcesInAContext_when_FeatureContextRequested_Then_ReturnGraphForAllFeatures() {
  92. // given
  93. final DependencyGraph<Requestable> fullResourceDependencyGraph = buildGraphFromFile("given-multiple-web-resources-in-a-context/full-resource-dependency-graph.json");
  94. //when
  95. final DependencyGraph<Requestable> actualResultSubGraph = fullResourceDependencyGraph.findDependencySubGraphByRequestableKey(new WebResourceContextKey("features"));
  96. // then
  97. final DependencyGraph<Requestable> expectedResultSubGraph = buildGraphFromFile("given-multiple-web-resources-in-a-context/expected-feature-context-graph.json");
  98. assertEquals(expectedResultSubGraph, actualResultSubGraph);
  99. }
  100. @Test
  101. public void given_ThereAreMutlipleWebResourcesInAContext_when_NorthContextRequested_Then_ReturnGraphForNorthernWebResources() {
  102. // given
  103. final DependencyGraph<Requestable> fullResourceDependencyGraph = buildGraphFromFile("given-multiple-web-resources-in-a-context/full-resource-dependency-graph.json");
  104. //when
  105. final DependencyGraph<Requestable> actualResultSubGraph = fullResourceDependencyGraph.findDependencySubGraphByRequestableKey(new WebResourceContextKey("north"));
  106. // then
  107. final DependencyGraph<Requestable> expectedResultSubGraph = buildGraphFromFile("given-multiple-web-resources-in-a-context/expected-north-context-graph.json");
  108. assertEquals(expectedResultSubGraph, actualResultSubGraph);
  109. }
  110. /**
  111. * Build a certain graph with random values based on a max height and max width.
  112. *
  113. * @param maxHeight The graph max height.
  114. * @param maxWidth The graph max width.
  115. * @return The created dependency graph.
  116. */
  117. private DependencyGraph<Requestable> buildArtificialGraph(final int maxHeight, final int maxWidth) {
  118. final DependencyGraphBuilder builder = DependencyGraph.builder();
  119. for (int width = 0; width < maxWidth; width++) {
  120. //Linking the previous vertex with the current one.
  121. final String sourceVertex = format(FEATURE_PATTERN_NAME, width);
  122. final String targetVertex = format(FEATURE_PATTERN_NAME, width + 1);
  123. builder.addWebResourceDependency(new WebResourceKey(sourceVertex), targetVertex);
  124. //Creating the height of the graph based on a origin vertex.
  125. for (int height = 0; height < maxHeight; height++) {
  126. final String sourceDependencyVertex = format(FEATURE_DEPENDENCY_PATTERN_NAME, width, height);
  127. final String targetDependencyVertex = format(FEATURE_DEPENDENCY_PATTERN_NAME, width, height + 1);
  128. builder.addWebResourceDependency(new WebResourceKey(sourceDependencyVertex), targetDependencyVertex);
  129. }
  130. //Linking all the base vertexes with the root vertex.
  131. final String sourceDependencyVertex = format(FEATURE_PATTERN_NAME, width + "." + ROOT_NODE_INDEX);
  132. builder.addWebResourceDependency(new WebResourceKey(sourceVertex), sourceDependencyVertex);
  133. }
  134. return builder.build();
  135. }
  136. private DependencyGraph<Requestable> buildGraphFromFile(final String filePath) {
  137. return buildGraphFromFile(filePath, mock(ResourceBatchingConfiguration.class));
  138. }
  139. /**
  140. * Build the dependency relation between two resources based on a configuration file.
  141. *
  142. * @param filePath The file path used to build the entire graph.
  143. * @param resourceBatchingConfiguration The resource batch configuration.
  144. * @return The built resource dependency graph.
  145. */
  146. private DependencyGraph<Requestable> buildGraphFromFile(final String filePath,
  147. final ResourceBatchingConfiguration resourceBatchingConfiguration) {
  148. final DependencyGraphBuilder builder = DependencyGraph.builder();
  149. for (final WebResourceModuleDescriptorMock resource : fileToEntity(format(TEST_CASE_FOLDER, filePath), new ResourceCollectionTypeReference())) {
  150. builder.addDependencies(resource.toWebResourceModuleDescriptor());
  151. }
  152. return builder.build();
  153. }
  154. /**
  155. * Represents a resource dependency collection type used to extract information from a JSON file.
  156. */
  157. private static class ResourceCollectionTypeReference extends TypeReference<Collection<WebResourceModuleDescriptorMock>> {
  158. }
  159. }