PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/plugin/jql/function/TestRemoteLinkByGlobalIdFunction.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 258 lines | 221 code | 33 blank | 4 comment | 5 complexity | 8c43ab9f91fd04888ce051db8cf8d68d MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.plugin.jql.function;
  2. import com.atlassian.jira.exception.GetException;
  3. import com.atlassian.jira.issue.link.DefaultRemoteIssueLinkManager;
  4. import com.atlassian.jira.issue.link.IssueLinkManager;
  5. import com.atlassian.jira.issue.link.RemoteIssueLink;
  6. import com.atlassian.jira.issue.link.RemoteIssueLinkManager;
  7. import com.atlassian.jira.issue.link.TestDefaultRemoteIssueLinkManager;
  8. import com.atlassian.jira.jql.operand.QueryLiteral;
  9. import com.atlassian.jira.jql.query.QueryCreationContext;
  10. import com.atlassian.jira.jql.query.QueryCreationContextImpl;
  11. import com.atlassian.jira.mock.i18n.MockI18nHelper;
  12. import com.atlassian.jira.user.ApplicationUser;
  13. import com.atlassian.jira.util.MessageSet;
  14. import com.atlassian.query.clause.TerminalClauseImpl;
  15. import com.atlassian.query.operand.FunctionOperand;
  16. import com.atlassian.query.operator.Operator;
  17. import com.google.common.base.Function;
  18. import com.google.common.collect.Collections2;
  19. import com.google.common.collect.ContiguousSet;
  20. import com.google.common.collect.DiscreteDomain;
  21. import com.google.common.collect.ImmutableList;
  22. import com.google.common.collect.Iterables;
  23. import com.google.common.collect.Lists;
  24. import com.google.common.collect.Range;
  25. import org.apache.commons.collections.CollectionUtils;
  26. import org.hamcrest.Description;
  27. import org.hamcrest.TypeSafeMatcher;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.mockito.InjectMocks;
  32. import org.mockito.Mock;
  33. import org.mockito.runners.MockitoJUnitRunner;
  34. import javax.annotation.Nullable;
  35. import java.util.Collection;
  36. import java.util.List;
  37. import static com.atlassian.jira.plugin.jql.function.RemoteLinksByGlobalIdFunction.FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID;
  38. import static org.hamcrest.Matchers.equalTo;
  39. import static org.hamcrest.Matchers.hasItem;
  40. import static org.hamcrest.Matchers.hasItems;
  41. import static org.hamcrest.Matchers.hasSize;
  42. import static org.junit.Assert.assertFalse;
  43. import static org.junit.Assert.assertThat;
  44. import static org.mockito.Matchers.argThat;
  45. import static org.mockito.Mockito.when;
  46. /**
  47. * @since v6.1
  48. */
  49. @RunWith(MockitoJUnitRunner.class)
  50. public class TestRemoteLinkByGlobalIdFunction {
  51. public static final String SAMPLE_GLOBAL_ID = "a-global-id";
  52. public static final String SAMPLE_GLOBAL_ID2 = "another-global-id";
  53. // mocks for injections
  54. @Mock
  55. private IssueLinkManager issueLinkManager;
  56. @Mock
  57. private RemoteIssueLinkManager remoteIssueLinkManager;
  58. @InjectMocks
  59. private RemoteLinksByGlobalIdFunction function;
  60. @Mock
  61. private JqlFunctionModuleDescriptor moduleDescriptor;
  62. private MockI18nHelper i18nHelper;
  63. @Mock
  64. private ApplicationUser user;
  65. private QueryCreationContext queryCreationContext;
  66. @Before
  67. public void setUp() throws Exception {
  68. i18nHelper = new MockI18nHelper();
  69. when(moduleDescriptor.getI18nBean()).thenReturn(i18nHelper);
  70. function.init(moduleDescriptor);
  71. queryCreationContext = new QueryCreationContextImpl((ApplicationUser) null);
  72. }
  73. @Test
  74. public void testValidate() {
  75. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID, SAMPLE_GLOBAL_ID);
  76. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  77. when(issueLinkManager.isLinkingEnabled()).thenReturn(true);
  78. final MessageSet result = function.validate(user, operand, clause);
  79. assertFalse(result.hasAnyMessages());
  80. }
  81. @Test
  82. public void testValidateLinkingDisabled() {
  83. i18nHelper.stubWith("jira.jql.function.issue.linking.disabled", "disabled " + FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID);
  84. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID);
  85. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  86. when(issueLinkManager.isLinkingEnabled()).thenReturn(false);
  87. final MessageSet result = function.validate(user, operand, clause);
  88. assertThat(result.getErrorMessages(), hasSize(1));
  89. assertThat(result.getErrorMessages(), hasItem("disabled " + FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID));
  90. }
  91. @Test
  92. public void testValidateNoArguments() {
  93. i18nHelper.stubWith("jira.jql.function.arg.incorrect.range", "error msg");
  94. i18nHelper.stubWith("jira.jql.function.remote.link.by.global.id.incorrect.usage", "usage");
  95. when(issueLinkManager.isLinkingEnabled()).thenReturn(true);
  96. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID);
  97. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  98. final MessageSet result = function.validate(user, operand, clause);
  99. assertThat(result.getErrorMessages(), hasSize(1));
  100. assertThat(result.getErrorMessages(), hasItem("error msg usage"));
  101. }
  102. @Test
  103. public void testValidateMoreThanMaxArguments() {
  104. i18nHelper.stubWith("jira.jql.function.arg.incorrect.range", "error msg");
  105. i18nHelper.stubWith("jira.jql.function.remote.link.by.global.id.incorrect.usage", "usage");
  106. when(issueLinkManager.isLinkingEnabled()).thenReturn(true);
  107. final Collection<String> arguments = Collections2.transform(
  108. ContiguousSet.create(Range.closedOpen(0, DefaultRemoteIssueLinkManager.MAX_GLOBAL_ID_LIST_SIZE_FOR_FIND + 1), DiscreteDomain.integers()),
  109. new Function<Integer, String>() {
  110. @Override
  111. public String apply(@Nullable final Integer input) {
  112. return SAMPLE_GLOBAL_ID + "-" + input;
  113. }
  114. });
  115. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID, arguments);
  116. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  117. final MessageSet result = function.validate(user, operand, clause);
  118. assertThat(result.getErrorMessages(), hasSize(1));
  119. assertThat(result.getErrorMessages(), hasItem("error msg usage"));
  120. }
  121. @Test
  122. public void testValidateMaxArguments() {
  123. i18nHelper.stubWith("jira.jql.function.arg.incorrect.range", "error msg");
  124. i18nHelper.stubWith("jira.jql.function.remote.link.by.global.id.incorrect.usage", "usage");
  125. when(issueLinkManager.isLinkingEnabled()).thenReturn(true);
  126. final Collection<String> arguments = Collections2.transform(ContiguousSet.create(Range.closedOpen(0, DefaultRemoteIssueLinkManager.MAX_GLOBAL_ID_LIST_SIZE_FOR_FIND), DiscreteDomain.integers()),
  127. new Function<Integer, String>() {
  128. @Override
  129. public String apply(@Nullable final Integer input) {
  130. return SAMPLE_GLOBAL_ID + "-" + input;
  131. }
  132. });
  133. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID, arguments);
  134. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  135. final MessageSet result = function.validate(user, operand, clause);
  136. assertThat(result.getErrorMessages(), hasSize(0));
  137. }
  138. @Test
  139. public void testGetValues() throws Exception {
  140. when(issueLinkManager.isLinkingEnabled()).thenReturn(true);
  141. final List<RemoteIssueLink> links = mockLinksForGlobalId(SAMPLE_GLOBAL_ID, 10001L, 10002L, 10003L);
  142. when(remoteIssueLinkManager.findRemoteIssueLinksByGlobalIds(ImmutableList.of(SAMPLE_GLOBAL_ID))).thenReturn(links);
  143. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID, SAMPLE_GLOBAL_ID);
  144. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  145. final List<QueryLiteral> values = function.getValues(queryCreationContext, operand, clause);
  146. assertThat(values, hasSize(links.size()));
  147. for (int i = 0; i < links.size(); i++) {
  148. assertThat(values.get(i).getLongValue(), equalTo(links.get(i).getIssueId()));
  149. }
  150. }
  151. @Test
  152. public void testGetValuesMultipleArguments() throws Exception {
  153. when(issueLinkManager.isLinkingEnabled()).thenReturn(true);
  154. final List<RemoteIssueLink> links1 = mockLinksForGlobalId(SAMPLE_GLOBAL_ID, 10001L, 10003L, 10005L);
  155. final List<RemoteIssueLink> links2 = mockLinksForGlobalId(SAMPLE_GLOBAL_ID2, 10002L, 10004L, 10006L);
  156. final List<RemoteIssueLink> links = ImmutableList.copyOf(Iterables.concat(links1, links2));
  157. when(remoteIssueLinkManager.findRemoteIssueLinksByGlobalIds(eqCollection(ImmutableList.of(SAMPLE_GLOBAL_ID, SAMPLE_GLOBAL_ID2))))
  158. .thenReturn(links);
  159. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID, SAMPLE_GLOBAL_ID, SAMPLE_GLOBAL_ID2);
  160. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  161. final List<QueryLiteral> values = function.getValues(queryCreationContext, operand, clause);
  162. assertThat(values, hasSize(links.size()));
  163. for (int i = 0; i < links.size(); i++) {
  164. assertThat(values.get(i).getLongValue(), equalTo(links.get(i).getIssueId()));
  165. }
  166. }
  167. @Test
  168. public void testGetValuesMultipleArgumentsOverlap() throws Exception {
  169. when(issueLinkManager.isLinkingEnabled()).thenReturn(true);
  170. final List<RemoteIssueLink> links1 = mockLinksForGlobalId(SAMPLE_GLOBAL_ID, 10001L, 10003L, 10005L);
  171. final List<RemoteIssueLink> links2 = mockLinksForGlobalId(SAMPLE_GLOBAL_ID2, 10002L, 10003L, 10006L);
  172. final List<RemoteIssueLink> links = ImmutableList.copyOf(Iterables.concat(links1, links2));
  173. when(remoteIssueLinkManager.findRemoteIssueLinksByGlobalIds(eqCollection(ImmutableList.of(SAMPLE_GLOBAL_ID, SAMPLE_GLOBAL_ID2))))
  174. .thenReturn(links);
  175. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID, SAMPLE_GLOBAL_ID, SAMPLE_GLOBAL_ID2);
  176. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  177. final List<QueryLiteral> values = function.getValues(queryCreationContext, operand, clause);
  178. assertThat(values, hasSize(5));
  179. final List<Long> issueIds = Lists.transform(values, new Function<QueryLiteral, Long>() {
  180. @Override
  181. public Long apply(final QueryLiteral literal) {
  182. return literal.getLongValue();
  183. }
  184. });
  185. assertThat(issueIds, hasItems(10001L, 10002L, 10003L, 10005L, 10006L));
  186. }
  187. @Test
  188. public void testGetValuesLinkingDisabled() throws Exception {
  189. when(issueLinkManager.isLinkingEnabled()).thenReturn(false);
  190. mockLinksForGlobalId(SAMPLE_GLOBAL_ID, 10001L, 10002L, 10003L);
  191. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID, SAMPLE_GLOBAL_ID);
  192. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  193. final List<QueryLiteral> values = function.getValues(queryCreationContext, operand, clause);
  194. assertThat(values, hasSize(0));
  195. }
  196. @Test
  197. public void testGetValuesNoArguments() throws Exception {
  198. when(issueLinkManager.isLinkingEnabled()).thenReturn(true);
  199. final FunctionOperand operand = new FunctionOperand(FUNCTION_REMOTE_LINKS_BY_GLOBAL_ID);
  200. final TerminalClauseImpl clause = new TerminalClauseImpl("issue", Operator.IN, operand);
  201. mockLinksForGlobalId(SAMPLE_GLOBAL_ID, 10001L, 10002L, 10003L);
  202. final List<QueryLiteral> values = function.getValues(queryCreationContext, operand, clause);
  203. assertThat(values, hasSize(0));
  204. }
  205. private List<RemoteIssueLink> mockLinksForGlobalId(final String globalId, final Long... issueIds) throws GetException {
  206. final ImmutableList<Long> issueIdList = ImmutableList.copyOf(issueIds);
  207. return ImmutableList.copyOf(Lists.transform(issueIdList, new Function<Long, RemoteIssueLink>() {
  208. @Override
  209. public RemoteIssueLink apply(@Nullable final Long issueId) {
  210. return TestDefaultRemoteIssueLinkManager.populatedBuilder(issueId).globalId(globalId).build();
  211. }
  212. }));
  213. }
  214. private <T> Collection<T> eqCollection(final Collection<T> expectedItems) {
  215. return argThat(new TypeSafeMatcher<Collection<T>>() {
  216. @Override
  217. protected boolean matchesSafely(final Collection<T> items) {
  218. return expectedItems.size() == items.size() && CollectionUtils.intersection(items, expectedItems).size() == items.size();
  219. }
  220. @Override
  221. public void describeTo(final Description description) {
  222. }
  223. });
  224. }
  225. }