PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/testsuite/server-technical-testsuite/src/test/java/org/marvelution/jji/data/services/DefaultIssueLinkServiceIT.java

https://bitbucket.org/marvelution/jira-jenkins-integration
Java | 178 lines | 137 code | 26 blank | 15 comment | 0 complexity | 83b5dce4a9fd588687927e2433559e9d MD5 | raw file
  1. /*
  2. * Copyright (c) 2012-present Marvelution B.V.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.marvelution.jji.data.services;
  17. import java.net.*;
  18. import java.time.*;
  19. import java.util.ArrayList;
  20. import java.util.*;
  21. import java.util.concurrent.*;
  22. import org.marvelution.jji.api.events.*;
  23. import org.marvelution.jji.data.access.*;
  24. import org.marvelution.jji.data.services.api.*;
  25. import org.marvelution.jji.model.*;
  26. import org.marvelution.jji.test.data.*;
  27. import com.atlassian.jira.event.*;
  28. import com.atlassian.jira.event.issue.*;
  29. import com.atlassian.jira.event.type.*;
  30. import com.atlassian.jira.issue.*;
  31. import com.atlassian.jira.project.*;
  32. import com.google.inject.*;
  33. import com.google.inject.util.*;
  34. import net.java.ao.test.jdbc.*;
  35. import org.junit.jupiter.api.*;
  36. import org.junit.jupiter.api.extension.*;
  37. import static org.marvelution.jji.utils.KeyExtractor.*;
  38. import static java.util.Arrays.stream;
  39. import static java.util.Objects.*;
  40. import static java.util.stream.Collectors.*;
  41. import static org.assertj.core.api.Assertions.*;
  42. import static org.awaitility.Awaitility.*;
  43. import static org.mockito.Mockito.*;
  44. @ExtendWith(ActiveObjectsExtension.class)
  45. @Data(ModelDatabaseUpdater.class)
  46. class DefaultIssueLinkServiceIT
  47. extends AbstractBaseIssueLinkServiceIT<DefaultIssueLinkService>
  48. {
  49. private final ActiveObjectsExtension.EntityManagerContext entityManagerContext;
  50. private InMemoryEventPublisher eventPublisher;
  51. @Inject
  52. private IssueReferenceProvider issueReferenceProvider;
  53. private Set<IssueReference> issueReferences;
  54. public DefaultIssueLinkServiceIT(ActiveObjectsExtension.EntityManagerContext entityManagerContext)
  55. {
  56. this.entityManagerContext = entityManagerContext;
  57. }
  58. @Override
  59. public void configure(Binder binder)
  60. {
  61. eventPublisher = new InMemoryEventPublisher();
  62. binder.install(Modules.override(new DataAccessModule(entityManagerContext)).with(new org.marvelution.testing.inject.AbstractModule()
  63. {
  64. @Override
  65. protected void configure()
  66. {
  67. bindMock(IssueReferenceProvider.class, withSettings().lenient());
  68. }
  69. }));
  70. binder.install(Modules.override(new DataServicesModule()).with(new AbstractModule()
  71. {
  72. @Override
  73. protected void configure()
  74. {
  75. bind(EventPublisher.class).toInstance(eventPublisher);
  76. }
  77. }));
  78. }
  79. static class InMemoryEventPublisher
  80. implements EventPublisher
  81. {
  82. private final List<Object> events = new ArrayList<>();
  83. @Override
  84. public void publish(Object event)
  85. {
  86. events.add(event);
  87. }
  88. void assertEventPublished(Object event)
  89. {
  90. await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).contains(event));
  91. }
  92. }
  93. @Override
  94. protected <T> void assertEventPublished(
  95. EventPublisher eventPublisher,
  96. T event)
  97. {
  98. this.eventPublisher.assertEventPublished(event);
  99. }
  100. @BeforeEach
  101. @SuppressWarnings("unchecked")
  102. void setUpMocks()
  103. {
  104. issueReferences = new HashSet<>();
  105. when(issueReferenceProvider.getIssueReference(anyString())).then(invocation -> {
  106. String issueKey = invocation.getArgument(0);
  107. return issueReferences.stream().filter(reference -> reference.getIssueKey().equals(issueKey)).findFirst();
  108. });
  109. when(issueReferenceProvider.getIssueReferences(any(Set.class))).then(invocation -> {
  110. Set<String> issueKeys = invocation.getArgument(0);
  111. return issueReferences.stream().filter(reference -> issueKeys.contains(reference.getIssueKey())).collect(toSet());
  112. });
  113. doAnswer(invocation -> {
  114. IssueReference reference = invocation.getArgument(0);
  115. reference.setIssueUrl(URI.create("https://jira.example.com/browse/" + reference.getIssueKey()));
  116. return null;
  117. }).when(issueReferenceProvider).setIssueUrl(any(IssueReference.class));
  118. }
  119. @Override
  120. protected void setupIssueReference(String... issueKeys)
  121. {
  122. requireNonNull(issueKeys);
  123. stream(issueKeys).map(key -> new IssueReference().setIssueKey(key).setProjectKey(extractProjectKeyFromIssueKey(key)))
  124. .forEach(issueReferences::add);
  125. }
  126. @Test
  127. void testOnIssueEvent()
  128. {
  129. Build build = setupBuild(createBuild(job), new IssueReference().setIssueKey("PR-1").setProjectKey("PR"),
  130. new IssueReference().setIssueKey("PR-2").setProjectKey("PR"),
  131. new IssueReference().setIssueKey("PR-3").setProjectKey("PR"));
  132. assertThat(issueLinkService.getRelatedIssueKeys(build)).hasSize(3).containsOnly("PR-1", "PR-2", "PR-3");
  133. Issue issue = mock(Issue.class);
  134. when(issue.getKey()).thenReturn("PR-1");
  135. issueLinkService.onIssueEvent(new IssueEvent(issue, null, null, EventType.ISSUE_UPDATED_ID));
  136. assertThat(issueLinkService.getRelatedIssueKeys(build)).hasSize(3).containsOnly("PR-1", "PR-2", "PR-3");
  137. issueLinkService.onIssueEvent(new IssueEvent(issue, null, null, EventType.ISSUE_DELETED_ID));
  138. assertThat(issueLinkService.getRelatedIssueKeys(build)).hasSize(2).containsOnly("PR-2", "PR-3");
  139. }
  140. @Test
  141. void testOnProjectDeleted()
  142. {
  143. Build build = setupBuild(createBuild(job), new IssueReference().setIssueKey("PR-1").setProjectKey("PR"),
  144. new IssueReference().setIssueKey("PR-2").setProjectKey("PR"),
  145. new IssueReference().setIssueKey("OP-1").setProjectKey("OP"));
  146. assertThat(issueLinkService.getRelatedIssueKeys(build)).containsOnly("PR-1", "PR-2", "OP-1");
  147. Project project = mock(Project.class);
  148. when(project.getKey()).thenReturn("PR");
  149. issueLinkService.onProjectDeleted(new ProjectDeletedEvent(null, project));
  150. assertThat(issueLinkService.getRelatedIssueKeys(build)).containsOnly("OP-1");
  151. }
  152. }