PageRenderTime 80ms CodeModel.GetById 51ms RepoModel.GetById 1ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/jql/context/TestAllTextClauseContextFactory.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 221 lines | 180 code | 38 blank | 3 comment | 0 complexity | ac87dbaab615e835222920320bf79e9d MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.jql.context;
  2. import com.atlassian.jira.issue.CustomFieldManager;
  3. import com.atlassian.jira.issue.customfields.CustomFieldSearcher;
  4. import com.atlassian.jira.issue.customfields.searchers.SimpleAllTextCustomFieldSearcherClauseHandler;
  5. import com.atlassian.jira.issue.fields.CustomField;
  6. import com.atlassian.jira.issue.search.ClauseNames;
  7. import com.atlassian.jira.issue.search.constants.SystemSearchConstants;
  8. import com.atlassian.jira.issue.search.managers.SearchHandlerManager;
  9. import com.atlassian.jira.jql.ClauseHandler;
  10. import com.atlassian.jira.user.ApplicationUser;
  11. import com.atlassian.jira.util.collect.CollectionBuilder;
  12. import com.atlassian.query.clause.TerminalClauseImpl;
  13. import com.atlassian.query.operand.SingleValueOperand;
  14. import com.atlassian.query.operator.Operator;
  15. import org.junit.After;
  16. import org.junit.Before;
  17. import org.junit.Test;
  18. import org.junit.runner.RunWith;
  19. import org.mockito.Mock;
  20. import org.mockito.runners.MockitoJUnitRunner;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.Set;
  24. import java.util.concurrent.atomic.AtomicBoolean;
  25. import static org.hamcrest.Matchers.contains;
  26. import static org.hamcrest.Matchers.containsInAnyOrder;
  27. import static org.hamcrest.Matchers.is;
  28. import static org.junit.Assert.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.verify;
  31. import static org.mockito.Mockito.when;
  32. /**
  33. * @since v4.0
  34. */
  35. @RunWith(MockitoJUnitRunner.class)
  36. public class TestAllTextClauseContextFactory {
  37. @Mock
  38. private CustomFieldManager customFieldManager;
  39. @Mock
  40. private SearchHandlerManager searchHandlerManager;
  41. @Mock
  42. private ContextSetUtil contextSetUtil;
  43. private AllTextClauseContextFactory factory;
  44. private ApplicationUser theUser = null;
  45. @Before
  46. public void setUp() throws Exception {
  47. factory = new AllTextClauseContextFactory(customFieldManager, searchHandlerManager, contextSetUtil);
  48. }
  49. @After
  50. public void tearDown() throws Exception {
  51. customFieldManager = null;
  52. searchHandlerManager = null;
  53. contextSetUtil = null;
  54. factory = null;
  55. theUser = null;
  56. }
  57. @Test
  58. public void testGetClauseContextMergesContextsFromFactories() throws Exception {
  59. final SingleValueOperand operand = new SingleValueOperand("test");
  60. final TerminalClauseImpl clause = new TerminalClauseImpl("text", Operator.LIKE, operand);
  61. final ClauseContext subContext1 = new ClauseContextImpl(Collections.<ProjectIssueTypeContext>singleton(new ProjectIssueTypeContextImpl(new ProjectContextImpl(10L), AllIssueTypesContext.INSTANCE)));
  62. final ClauseContext subContext2 = new ClauseContextImpl(Collections.<ProjectIssueTypeContext>singleton(new ProjectIssueTypeContextImpl(new ProjectContextImpl(50L), AllIssueTypesContext.INSTANCE)));
  63. final Set<ClauseContext> expectedSet = CollectionBuilder.newBuilder(subContext1, subContext2).asSet();
  64. final ClauseContextFactory subFactory1 = mock(ClauseContextFactory.class);
  65. when(subFactory1.getClauseContext(theUser, clause))
  66. .thenReturn(subContext1);
  67. final ClauseContextFactory subFactory2 = mock(ClauseContextFactory.class);
  68. when(subFactory2.getClauseContext(theUser, clause))
  69. .thenReturn(subContext2);
  70. factory = new AllTextClauseContextFactory(customFieldManager, searchHandlerManager, contextSetUtil) {
  71. @Override
  72. List<ClauseContextFactory> getFactories(final ApplicationUser searcher) {
  73. return CollectionBuilder.list(subFactory1, subFactory2);
  74. }
  75. };
  76. factory.getClauseContext(theUser, clause);
  77. verify(contextSetUtil).union(expectedSet);
  78. }
  79. @Test
  80. public void testGetAllSystemFieldFactories() throws Exception {
  81. final ClauseHandler commentHandler = mock(ClauseHandler.class);
  82. final ClauseContextFactory commentFactory = mock(ClauseContextFactory.class);
  83. when(searchHandlerManager.getClauseHandler(theUser, SystemSearchConstants.forComments().getJqlClauseNames().getPrimaryName()))
  84. .thenReturn(Collections.singletonList(commentHandler));
  85. when(commentHandler.getClauseContextFactory())
  86. .thenReturn(commentFactory);
  87. final ClauseHandler descriptionHandler = mock(ClauseHandler.class);
  88. final ClauseContextFactory descriptionFactory = mock(ClauseContextFactory.class);
  89. when(searchHandlerManager.getClauseHandler(theUser, SystemSearchConstants.forDescription().getJqlClauseNames().getPrimaryName()))
  90. .thenReturn(Collections.singletonList(descriptionHandler));
  91. when(descriptionHandler.getClauseContextFactory())
  92. .thenReturn(descriptionFactory);
  93. final ClauseHandler environmentHandler = mock(ClauseHandler.class);
  94. final ClauseContextFactory environmentFactory = mock(ClauseContextFactory.class);
  95. when(searchHandlerManager.getClauseHandler(theUser, SystemSearchConstants.forEnvironment().getJqlClauseNames().getPrimaryName()))
  96. .thenReturn(Collections.singletonList(environmentHandler));
  97. when(environmentHandler.getClauseContextFactory())
  98. .thenReturn(environmentFactory);
  99. final ClauseHandler summaryHandler = mock(ClauseHandler.class);
  100. final ClauseContextFactory summaryFactory = mock(ClauseContextFactory.class);
  101. when(searchHandlerManager.getClauseHandler(theUser, SystemSearchConstants.forSummary().getJqlClauseNames().getPrimaryName()))
  102. .thenReturn(Collections.singletonList(summaryHandler));
  103. when(summaryHandler.getClauseContextFactory())
  104. .thenReturn(summaryFactory);
  105. final List<ClauseContextFactory> result = factory.getAllSystemFieldFactories(theUser);
  106. assertThat(result, containsInAnyOrder(commentFactory, descriptionFactory, environmentFactory, summaryFactory));
  107. }
  108. @Test
  109. public void testGetAllCustomFieldFactories() throws Exception {
  110. final CustomField customFieldNullSearcher = mock(CustomField.class);
  111. when(customFieldNullSearcher.getCustomFieldSearcher())
  112. .thenReturn(null);
  113. final CustomFieldSearcher numberSearcher = mock(CustomFieldSearcher.class);
  114. final CustomField customFieldNonText = mock(CustomField.class);
  115. when(customFieldNonText.getCustomFieldSearcher())
  116. .thenReturn(numberSearcher);
  117. final ClauseHandler textHandler = mock(ClauseHandler.class);
  118. final ClauseContextFactory textFactory = mock(ClauseContextFactory.class);
  119. when(searchHandlerManager.getClauseHandler(theUser, "customfield_10000"))
  120. .thenReturn(Collections.singletonList(textHandler));
  121. when(textHandler.getClauseContextFactory())
  122. .thenReturn(textFactory);
  123. final CustomFieldSearcher freeTextSearcher = mock(CustomFieldSearcher.class);
  124. SimpleAllTextCustomFieldSearcherClauseHandler cfSupportsAllText = new SimpleAllTextCustomFieldSearcherClauseHandler(null, null, CollectionBuilder.newBuilder(Operator.LIKE).asSet(), null);
  125. when(freeTextSearcher.getCustomFieldSearcherClauseHandler()).thenReturn(cfSupportsAllText);
  126. final CustomField customField = mock(CustomField.class);
  127. when(customField.getCustomFieldSearcher())
  128. .thenReturn(freeTextSearcher);
  129. when(customField.getClauseNames())
  130. .thenReturn(new ClauseNames("customfield_10000"));
  131. when(customFieldManager.getCustomFieldObjects())
  132. .thenReturn(CollectionBuilder.list(customFieldNullSearcher, customFieldNonText, customField));
  133. final List<ClauseContextFactory> result = factory.getAllCustomFieldFactories(theUser);
  134. assertThat(result, contains(textFactory));
  135. }
  136. @Test
  137. public void testGetAllCustumFieldFactoriesSupportedOperators() throws Exception {
  138. final CustomFieldSearcher customFieldSearcher = mock(CustomFieldSearcher.class);
  139. final CustomField customFieldSupportsLIKEOoperator = mock(CustomField.class);
  140. when(customFieldSupportsLIKEOoperator.getCustomFieldSearcher())
  141. .thenReturn(customFieldSearcher);
  142. SimpleAllTextCustomFieldSearcherClauseHandler cfSupportsAllText = new SimpleAllTextCustomFieldSearcherClauseHandler(null, null, CollectionBuilder.newBuilder(Operator.LIKE).asSet(), null);
  143. when(customFieldSearcher.getCustomFieldSearcherClauseHandler()).thenReturn(cfSupportsAllText);
  144. when(customFieldSupportsLIKEOoperator.getClauseNames())
  145. .thenReturn(new ClauseNames("customfield_10000"));
  146. final ClauseHandler textHandler = mock(ClauseHandler.class);
  147. final ClauseContextFactory textFactory = mock(ClauseContextFactory.class);
  148. final CustomFieldSearcher customFieldSearcher1 = mock(CustomFieldSearcher.class);
  149. final CustomField customFieldDoesNotSupportLIKEOoperator = mock(CustomField.class);
  150. when(customFieldDoesNotSupportLIKEOoperator.getCustomFieldSearcher())
  151. .thenReturn(customFieldSearcher1);
  152. SimpleAllTextCustomFieldSearcherClauseHandler cfDoesNotSupportsAllText = new SimpleAllTextCustomFieldSearcherClauseHandler(null, null, CollectionBuilder.newBuilder(Operator.GREATER_THAN_EQUALS).asSet(), null);
  153. when(customFieldSearcher1.getCustomFieldSearcherClauseHandler()).thenReturn(cfDoesNotSupportsAllText);
  154. when(customFieldManager.getCustomFieldObjects())
  155. .thenReturn(CollectionBuilder.list(customFieldSupportsLIKEOoperator, customFieldDoesNotSupportLIKEOoperator));
  156. when(searchHandlerManager.getClauseHandler(theUser, "customfield_10000"))
  157. .thenReturn(Collections.singletonList(textHandler));
  158. when(textHandler.getClauseContextFactory())
  159. .thenReturn(textFactory);
  160. final List<ClauseContextFactory> result = factory.getAllCustomFieldFactories(theUser);
  161. assertThat(result, contains(textFactory));
  162. }
  163. @Test
  164. public void testGetFactoriesHappyPath() throws Exception {
  165. final AtomicBoolean systemCalled = new AtomicBoolean(false);
  166. final AtomicBoolean customCalled = new AtomicBoolean(false);
  167. factory = new AllTextClauseContextFactory(customFieldManager, searchHandlerManager, contextSetUtil) {
  168. @Override
  169. List<ClauseContextFactory> getAllSystemFieldFactories(final ApplicationUser searcher) {
  170. systemCalled.set(true);
  171. return Collections.emptyList();
  172. }
  173. @Override
  174. List<ClauseContextFactory> getAllCustomFieldFactories(final ApplicationUser user) {
  175. customCalled.set(true);
  176. return Collections.emptyList();
  177. }
  178. };
  179. factory.getFactories(theUser);
  180. assertThat("systemCalled", systemCalled.get(), is(true));
  181. assertThat("customCalled", customCalled.get(), is(true));
  182. }
  183. }