PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/issue/util/TestDocumentIssueAggregateTimeTrackingCalculator.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 221 lines | 186 code | 34 blank | 1 comment | 2 complexity | a4e7058087db4f52ba5cd6f62e91516b MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.issue.util;
  2. import com.atlassian.jira.component.ComponentAccessor;
  3. import com.atlassian.jira.issue.Issue;
  4. import com.atlassian.jira.issue.fields.FieldManager;
  5. import com.atlassian.jira.issue.index.DocumentConstants;
  6. import com.atlassian.jira.issue.search.LuceneFieldSorter;
  7. import com.atlassian.jira.issue.search.SearchProvider;
  8. import com.atlassian.jira.issue.search.SearchProviderFactory;
  9. import com.atlassian.jira.jql.builder.JqlClauseBuilder;
  10. import com.atlassian.jira.mock.component.MockComponentWorker;
  11. import com.atlassian.jira.mock.issue.MockIssue;
  12. import com.atlassian.jira.security.JiraAuthenticationContext;
  13. import com.atlassian.query.clause.Clause;
  14. import com.atlassian.query.clause.TerminalClause;
  15. import com.atlassian.query.clause.TerminalClauseImpl;
  16. import com.atlassian.query.operand.SingleValueOperand;
  17. import com.atlassian.query.operator.Operator;
  18. import com.mockobjects.dynamic.Mock;
  19. import com.mockobjects.dynamic.P;
  20. import org.apache.lucene.document.Document;
  21. import org.apache.lucene.document.FieldSelectorResult;
  22. import org.apache.lucene.queryParser.ParseException;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import java.util.Comparator;
  26. import static org.junit.Assert.assertEquals;
  27. import static org.junit.Assert.assertNotNull;
  28. import static org.junit.Assert.fail;
  29. public class TestDocumentIssueAggregateTimeTrackingCalculator {
  30. private static final Long ZERO_ESTIMATE = 0L;
  31. private static final Long ONE_ESTIMATE = 1L;
  32. private static final Long BIG_ESTIMATE = (long) 99999;
  33. private static final int INVOKED_COUNT = 10;
  34. @Before
  35. public void setUp() throws Exception {
  36. ComponentAccessor.initialiseWorker(new MockComponentWorker());
  37. }
  38. @Test
  39. public void testNullIssue() {
  40. // does this even make sense
  41. AggregateTimeTrackingCalculator calculator = new DocumentIssueAggregateTimeTrackingCalculator(null, null, null, null) {
  42. LuceneFieldSorter getSorter(FieldManager fieldManager, String field) {
  43. return null;
  44. }
  45. };
  46. try {
  47. calculator.getAggregates(null);
  48. fail("Should have thrown a IllegalArgumentException");
  49. } catch (IllegalArgumentException iae) {
  50. }
  51. }
  52. @Test
  53. public void testSubTaskParentIssue() {
  54. Issue issue = getIssue(ZERO_ESTIMATE, ONE_ESTIMATE, BIG_ESTIMATE, true);
  55. AggregateTimeTrackingCalculator calculator = new DocumentIssueAggregateTimeTrackingCalculator(null, null, null, null) {
  56. LuceneFieldSorter getSorter(FieldManager fieldManager, String field) {
  57. return null;
  58. }
  59. };
  60. AggregateTimeTrackingBean bean = calculator.getAggregates(issue);
  61. assertNotNull(bean);
  62. assertEquals(ZERO_ESTIMATE, bean.getRemainingEstimate());
  63. assertEquals(ONE_ESTIMATE, bean.getOriginalEstimate());
  64. assertEquals(BIG_ESTIMATE, bean.getTimeSpent());
  65. }
  66. @Test
  67. public void testHitCollectorAdditionViaCollectCall() {
  68. AggregateTimeTrackingBean bean = new AggregateTimeTrackingBean(null, null, null, 0);
  69. final LuceneFieldSorter remainingEstimateSorter = getLFS(DocumentConstants.ISSUE_TIME_ESTIMATE_CURR, (long) 33);
  70. final LuceneFieldSorter originalEstimateSorter = getLFS(DocumentConstants.ISSUE_TIME_ESTIMATE_ORIG, (long) 66);
  71. final LuceneFieldSorter timeSpentSorter = getLFS(DocumentConstants.ISSUE_TIME_SPENT, (long) 99);
  72. DocumentIssueAggregateTimeTrackingCalculator.AggregateHitCollector hitCollector = new DocumentIssueAggregateTimeTrackingCalculator.AggregateHitCollector(null, bean, remainingEstimateSorter, originalEstimateSorter, timeSpentSorter) {
  73. Long getValueFromDocument(Document d, LuceneFieldSorter sorter) {
  74. return 42L;
  75. }
  76. };
  77. for (int i = 0; i < INVOKED_COUNT; i++) {
  78. hitCollector.collect(null);
  79. }
  80. assertEquals(new Long(42 * INVOKED_COUNT), bean.getOriginalEstimate());
  81. assertEquals(new Long(42 * INVOKED_COUNT), bean.getRemainingEstimate());
  82. assertEquals(new Long(42 * INVOKED_COUNT), bean.getTimeSpent());
  83. assertEquals(INVOKED_COUNT, hitCollector.getInvocationCount());
  84. }
  85. @Test
  86. public void testHitCollectorAdditionViaCollectCallWithSorters() {
  87. AggregateTimeTrackingBean bean = new AggregateTimeTrackingBean(null, null, null, 0);
  88. final LuceneFieldSorter remainingEstimateSorter = getLFS(DocumentConstants.ISSUE_TIME_ESTIMATE_CURR, (long) 33);
  89. final LuceneFieldSorter originalEstimateSorter = getLFS(DocumentConstants.ISSUE_TIME_ESTIMATE_ORIG, (long) 66);
  90. final LuceneFieldSorter timeSpentSorter = getLFS(DocumentConstants.ISSUE_TIME_SPENT, (long) 99);
  91. DocumentIssueAggregateTimeTrackingCalculator.AggregateHitCollector hitCollector = new DocumentIssueAggregateTimeTrackingCalculator.AggregateHitCollector(null, bean, remainingEstimateSorter, originalEstimateSorter, timeSpentSorter) {
  92. String getRawDocumentValue(Document d, String documentConstant) {
  93. return null;
  94. }
  95. };
  96. for (int i = 0; i < INVOKED_COUNT; i++) {
  97. hitCollector.collect(null);
  98. }
  99. assertEquals(new Long(66 * INVOKED_COUNT), bean.getOriginalEstimate());
  100. assertEquals(new Long(33 * INVOKED_COUNT), bean.getRemainingEstimate());
  101. assertEquals(new Long(99 * INVOKED_COUNT), bean.getTimeSpent());
  102. assertEquals(INVOKED_COUNT, hitCollector.getInvocationCount());
  103. }
  104. @Test
  105. public void testHitCollectorFieldSelectorCorrect() {
  106. AggregateTimeTrackingBean bean = new AggregateTimeTrackingBean(null, null, null, 0);
  107. final LuceneFieldSorter remainingEstimateSorter = getLFS(DocumentConstants.ISSUE_TIME_ESTIMATE_CURR, 33L);
  108. final LuceneFieldSorter originalEstimateSorter = getLFS(DocumentConstants.ISSUE_TIME_ESTIMATE_ORIG, 66L);
  109. final LuceneFieldSorter timeSpentSorter = getLFS(DocumentConstants.ISSUE_TIME_SPENT, 99L);
  110. DocumentIssueAggregateTimeTrackingCalculator.AggregateHitCollector hitCollector = new DocumentIssueAggregateTimeTrackingCalculator.AggregateHitCollector(null, bean, remainingEstimateSorter, originalEstimateSorter, timeSpentSorter);
  111. assertEquals(FieldSelectorResult.LOAD, hitCollector.getFieldSelector().accept(DocumentConstants.ISSUE_TIME_ESTIMATE_CURR));
  112. assertEquals(FieldSelectorResult.LOAD, hitCollector.getFieldSelector().accept(DocumentConstants.ISSUE_TIME_ESTIMATE_ORIG));
  113. assertEquals(FieldSelectorResult.LOAD, hitCollector.getFieldSelector().accept(DocumentConstants.ISSUE_TIME_SPENT));
  114. assertEquals(FieldSelectorResult.NO_LOAD, hitCollector.getFieldSelector().accept(DocumentConstants.ISSUE_KEY));
  115. }
  116. @Test
  117. public void testSubTaskClause() throws ParseException {
  118. DocumentIssueAggregateTimeTrackingCalculator calculator = new DocumentIssueAggregateTimeTrackingCalculator(null, null, null, null) {
  119. LuceneFieldSorter getSorter(FieldManager fieldManager, String field) {
  120. return null;
  121. }
  122. };
  123. final TerminalClause expectedClause = new TerminalClauseImpl("parent", Operator.EQUALS, new SingleValueOperand(ONE_ESTIMATE));
  124. final JqlClauseBuilder taskClauseBuilder = calculator.getSubTaskClause(ONE_ESTIMATE);
  125. final Clause clause = taskClauseBuilder.buildClause();
  126. assertEquals(expectedClause, clause);
  127. }
  128. @Test
  129. public void testSearchingInvocation() {
  130. Mock mockJAC = new Mock(JiraAuthenticationContext.class);
  131. mockJAC.expectAndReturn("getUser", null);
  132. JiraAuthenticationContext ctx = (JiraAuthenticationContext) mockJAC.proxy();
  133. Mock mockSP = new Mock(SearchProvider.class);
  134. mockSP.expectVoid("search", P.args(P.IS_NOT_NULL, P.IS_NULL, P.IS_NOT_NULL));
  135. SearchProvider sp = (SearchProvider) mockSP.proxy();
  136. Mock mockSPF = new Mock(SearchProviderFactory.class);
  137. mockSPF.expectAndReturn("getSearcher", P.args(P.eq(SearchProviderFactory.ISSUE_INDEX)), null);
  138. SearchProviderFactory spf = (SearchProviderFactory) mockSPF.proxy();
  139. DocumentIssueAggregateTimeTrackingCalculator calculator = new DocumentIssueAggregateTimeTrackingCalculator(ctx, spf, sp, null) {
  140. LuceneFieldSorter getSorter(FieldManager fieldManager, String field) {
  141. return new LuceneFieldSorter() {
  142. public String getDocumentConstant() {
  143. return DocumentConstants.ISSUE_TIME_ESTIMATE_CURR;
  144. }
  145. public Object getValueFromLuceneField(String documentValue) {
  146. return 33L;
  147. }
  148. public Comparator getComparator() {
  149. return null;
  150. }
  151. };
  152. }
  153. };
  154. Issue issue = getIssue(null, null, null);
  155. calculator.getAggregates(issue);
  156. mockJAC.verify();
  157. mockSP.verify();
  158. mockSPF.verify();
  159. }
  160. private LuceneFieldSorter getLFS(final String documentConstant, final Object documentValue1) {
  161. return new LuceneFieldSorter() {
  162. public String getDocumentConstant() {
  163. return documentConstant;
  164. }
  165. public Object getValueFromLuceneField(String documentValue) {
  166. return documentValue1;
  167. }
  168. public Comparator getComparator() {
  169. return null;
  170. }
  171. };
  172. }
  173. private Issue getIssue(Long remainingEstimate, Long orginalEstimate, Long timeSpent) {
  174. return getIssue(remainingEstimate, orginalEstimate, timeSpent, false);
  175. }
  176. private Issue getIssue(Long remainingEstimate, Long orginalEstimate, Long timeSpent, final boolean isSubTask) {
  177. MockIssue mockIssue = new MockIssue(555L) {
  178. public boolean isSubTask() {
  179. return isSubTask;
  180. }
  181. };
  182. mockIssue.setEstimate(remainingEstimate);
  183. mockIssue.setOriginalEstimate(orginalEstimate);
  184. mockIssue.setTimeSpent(timeSpent);
  185. return mockIssue;
  186. }
  187. }