PageRenderTime 34ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit-db/src/test/java/com/atlassian/jira/upgrade/TestUpgradeVersionHistoryManagerImpl.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 263 lines | 210 code | 53 blank | 0 comment | 17 complexity | 29306818e7d6c9f70e004f394a790f40 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.upgrade;
  2. import com.atlassian.jira.database.QueryDslAccessor;
  3. import com.atlassian.jira.junit.rules.DatabaseContainer;
  4. import com.atlassian.jira.model.querydsl.UpgradeVersionHistoryDTO;
  5. import com.google.common.collect.Lists;
  6. import org.hamcrest.BaseMatcher;
  7. import org.hamcrest.Description;
  8. import org.hamcrest.Matcher;
  9. import org.junit.Before;
  10. import org.junit.Rule;
  11. import org.junit.Test;
  12. import org.mockito.Mock;
  13. import java.sql.Timestamp;
  14. import java.time.Instant;
  15. import java.time.temporal.ChronoUnit;
  16. import java.util.Date;
  17. import java.util.List;
  18. import static com.atlassian.jira.model.querydsl.QUpgradeHistory.UPGRADE_HISTORY;
  19. import static com.atlassian.jira.model.querydsl.QUpgradeVersionHistory.UPGRADE_VERSION_HISTORY;
  20. import static org.hamcrest.MatcherAssert.assertThat;
  21. import static org.hamcrest.Matchers.empty;
  22. import static org.hamcrest.Matchers.equalTo;
  23. import static org.hamcrest.Matchers.hasSize;
  24. import static org.mockito.Mockito.when;
  25. public class TestUpgradeVersionHistoryManagerImpl {
  26. private static final Date DO_NOT_TEST = Date.from(Instant.EPOCH);
  27. private static final Date NOW = Date.from(Instant.now());
  28. private static final Date A_MINUTE_AGO = MINUTES_AGO(1);
  29. private static final Date TWO_MINUTES_AGO = MINUTES_AGO(2);
  30. private static final Date THREE_MINUTES_AGO = MINUTES_AGO(3);
  31. private static final Date FOUR_MINUTES_AGO = MINUTES_AGO(4);
  32. @Rule
  33. public DatabaseContainer database = DatabaseContainer.rule(this);
  34. @Mock
  35. BuildVersionRegistry buildVersionRegistry;
  36. private UpgradeVersionHistoryManagerImpl upgradeVersionHistoryManager;
  37. private QueryDslAccessor queryDslAccessor;
  38. @Before
  39. public void setUp() throws Exception {
  40. queryDslAccessor = database.getAttachToDatabase().getQueryDslAccessor();
  41. upgradeVersionHistoryManager = new UpgradeVersionHistoryManagerImpl(buildVersionRegistry, queryDslAccessor);
  42. }
  43. @Test
  44. public void whenThereIsNoUpgradeHistoryOrUpgradeVersionHistoryAnEmptyListIsReturned() {
  45. assertThat(upgradeVersionHistoryManager.getAllUpgradeVersionHistory(), empty());
  46. }
  47. @Test
  48. public void whenThereIsAnUpgradeHistoryEntryWithAnInvalidBuildNumberNoHistoryIsReturned() {
  49. addUpgradeHistoryWithClassName("BAD_CLASS");
  50. assertThat(upgradeVersionHistoryManager.getAllUpgradeVersionHistory(), empty());
  51. }
  52. @Test
  53. public void whenAllUpgradeHistoryHasTargetBuildsNoUpgradeVersionHistoryIsReturned() {
  54. addUpgradeHistoryWithTargetBuild(100, "1.0.0");
  55. assertThat(upgradeVersionHistoryManager.getAllUpgradeVersionHistory(), empty());
  56. }
  57. @Test
  58. public void whenThereIsUpgradeHistoryWithNoTargetBuildsTheLatestIsIncludedInTheVersionHistoryWithInferredVersion() {
  59. addUpgradeHistoryWithNoTargetBuild(706, "7.0.6");
  60. addUpgradeHistoryWithNoTargetBuild(1001, "10.0.1");
  61. addUpgradeHistoryWithNoTargetBuild(2003, "20.0.3");
  62. addUpgradeHistoryWithNoTargetBuild(2005, "20.0.5");
  63. final UpgradeVersionHistoryItem expectedHistoryItem = new UpgradeVersionHistoryItemImpl(null, "2005", "20.0.5", "2005", null, true);
  64. assertThat(upgradeVersionHistoryManager.getAllUpgradeVersionHistory(), isUpgradeHistoryItemList(expectedHistoryItem));
  65. }
  66. @Test
  67. public void upgradeVersionHistoryIsIncludedInTheListAndPreviousIsLinkedCorrectly() {
  68. addUpgradeVersionHistory(100, "1.0-SNAPSHOT", A_MINUTE_AGO);
  69. addUpgradeVersionHistory(200, "2.0-SNAPSHOT", NOW);
  70. UpgradeVersionHistoryItem expectedFirstHistoryItem = new UpgradeVersionHistoryItemImpl(NOW, "200", "2.0-SNAPSHOT", "100", "1.0-SNAPSHOT");
  71. UpgradeVersionHistoryItem expectedSecondHistoryItem = new UpgradeVersionHistoryItemImpl(A_MINUTE_AGO, "100", "1.0-SNAPSHOT", null, null);
  72. assertThat(upgradeVersionHistoryManager.getAllUpgradeVersionHistory(), isUpgradeHistoryItemList(expectedFirstHistoryItem, expectedSecondHistoryItem));
  73. }
  74. @Test
  75. public void upgradeTaskInformationWithNoTargetBuildPopulatesPreviousOfVersionHistory() {
  76. addUpgradeHistoryWithNoTargetBuild(706, "7.0.6");
  77. addUpgradeVersionHistory(100, "1.0-SNAPSHOT", A_MINUTE_AGO);
  78. addUpgradeVersionHistory(200, "2.0-SNAPSHOT", NOW);
  79. UpgradeVersionHistoryItem expectedFirstHistoryItem = new UpgradeVersionHistoryItemImpl(NOW, "200", "2.0-SNAPSHOT", "100", "1.0-SNAPSHOT");
  80. UpgradeVersionHistoryItem expectedSecondHistoryItem = new UpgradeVersionHistoryItemImpl(A_MINUTE_AGO, "100", "1.0-SNAPSHOT", "706", "7.0.6");
  81. UpgradeVersionHistoryItem expectedThirdHistoryItem = new UpgradeVersionHistoryItemImpl(null, "706", "7.0.6", "706", null, true);
  82. assertThat(upgradeVersionHistoryManager.getAllUpgradeVersionHistory(), isUpgradeHistoryItemList(expectedFirstHistoryItem, expectedSecondHistoryItem, expectedThirdHistoryItem));
  83. }
  84. @Test
  85. public void worksWithLotsOfUpgradeHistoryAndUpgradeVersionHistoryItems() {
  86. addUpgradeHistoryWithNoTargetBuild(20, "0.2.0");
  87. addUpgradeHistoryWithNoTargetBuild(30, "0.3.0");
  88. addUpgradeHistoryWithNoTargetBuild(40, "0.4.0");
  89. addUpgradeHistoryWithNoTargetBuild(50, "0.5.0");
  90. addUpgradeVersionHistory(200, "2.0-SNAPSHOT", FOUR_MINUTES_AGO);
  91. addUpgradeVersionHistory(300, "3.0-SNAPSHOT", THREE_MINUTES_AGO);
  92. addUpgradeVersionHistory(400, "4.0-SNAPSHOT", TWO_MINUTES_AGO);
  93. addUpgradeVersionHistory(500, "5.0-SNAPSHOT", A_MINUTE_AGO);
  94. addUpgradeVersionHistory(600, "6.0-SNAPSHOT", NOW);
  95. List<UpgradeVersionHistoryItem> expectedVersionHistoryItems = Lists.newArrayList(
  96. new UpgradeVersionHistoryItemImpl(NOW, "600", "6.0-SNAPSHOT", "500", "5.0-SNAPSHOT"),
  97. new UpgradeVersionHistoryItemImpl(A_MINUTE_AGO, "500", "5.0-SNAPSHOT", "400", "4.0-SNAPSHOT"),
  98. new UpgradeVersionHistoryItemImpl(TWO_MINUTES_AGO, "400", "4.0-SNAPSHOT", "300", "3.0-SNAPSHOT"),
  99. new UpgradeVersionHistoryItemImpl(THREE_MINUTES_AGO, "300", "3.0-SNAPSHOT", "200", "2.0-SNAPSHOT"),
  100. new UpgradeVersionHistoryItemImpl(FOUR_MINUTES_AGO, "200", "2.0-SNAPSHOT", "50", "0.5.0"),
  101. new UpgradeVersionHistoryItemImpl(null, "50", "0.5.0", "50", null, true)
  102. );
  103. assertThat(upgradeVersionHistoryManager.getAllUpgradeVersionHistory(), isUpgradeHistoryItemList(expectedVersionHistoryItems));
  104. }
  105. @Test
  106. public void addingAHistoryItemPutsItInCorrectly() {
  107. upgradeVersionHistoryManager.addUpgradeVersionHistory(200, "2.0-SNAPSHOT");
  108. List<UpgradeVersionHistoryDTO> upgradeVersionHistory = getAllUpgradeVersionHistoryItems();
  109. assertThat(upgradeVersionHistory, hasSize(1));
  110. UpgradeVersionHistoryDTO historyItem = upgradeVersionHistory.get(0);
  111. assertThat(historyItem.getTargetbuild(), equalTo("200"));
  112. assertThat(historyItem.getTargetversion(), equalTo("2.0-SNAPSHOT"));
  113. }
  114. @Test
  115. public void addingATargetBuildToHistoryThatAlreadyExistsDoesNothing() {
  116. addUpgradeVersionHistory(100, "1.0-SNAPSHOT", NOW);
  117. upgradeVersionHistoryManager.addUpgradeVersionHistory(100, "2.0-SNAPSHOT");
  118. List<UpgradeVersionHistoryDTO> upgradeVersionHistory = getAllUpgradeVersionHistoryItems();
  119. assertThat(upgradeVersionHistory, hasSize(1));
  120. UpgradeVersionHistoryDTO historyItem = upgradeVersionHistory.get(0);
  121. assertThat(historyItem.getTargetbuild(), equalTo("100"));
  122. assertThat(historyItem.getTargetversion(), equalTo("1.0-SNAPSHOT"));
  123. }
  124. private Matcher<List<UpgradeVersionHistoryItem>> isUpgradeHistoryItemList(UpgradeVersionHistoryItem... expectedHistoryList) {
  125. return isUpgradeHistoryItemList(Lists.newArrayList(expectedHistoryList));
  126. }
  127. private Matcher<List<UpgradeVersionHistoryItem>> isUpgradeHistoryItemList(final List<UpgradeVersionHistoryItem> expectedHistoryList) {
  128. return new BaseMatcher<List<UpgradeVersionHistoryItem>>() {
  129. private String matchingError = null;
  130. @Override
  131. public void describeTo(Description description) {
  132. description.appendText("Expected all of the elements to match in the list of history items");
  133. }
  134. @Override
  135. public boolean matches(Object o) {
  136. List<UpgradeVersionHistoryItem> actualHistoryList = (List<UpgradeVersionHistoryItem>) o;
  137. if (expectedHistoryList.size() != actualHistoryList.size()) {
  138. matchingError = String.format("The length of history items do not match (actual: %s, expected: %s)",
  139. actualHistoryList.size(), expectedHistoryList.size());
  140. return false;
  141. }
  142. for (int index = 0; index < actualHistoryList.size(); ++index) {
  143. UpgradeVersionHistoryItem actual = actualHistoryList.get(index);
  144. UpgradeVersionHistoryItem expected = expectedHistoryList.get(index);
  145. if (!compare(index, "originalBuildNumber", expected.getOriginalBuildNumber(), actual.getOriginalBuildNumber()) ||
  146. !compare(index, "originalVersion", expected.getOriginalVersion(), actual.getOriginalVersion()) ||
  147. !compare(index, "targetBuildNumber", expected.getTargetBuildNumber(), actual.getTargetBuildNumber()) ||
  148. !compare(index, "targetVersion", expected.getTargetVersion(), actual.getTargetVersion()) ||
  149. !compare(index, "isInferred", expected.isInferred(), actual.isInferred())) {
  150. return false;
  151. } else if (!DO_NOT_TEST.equals(expected.getTimePerformed()) &&
  152. !compare(index, "timePerformed", expected.getTimePerformed(), actual.getTimePerformed())) {
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. @Override
  159. public void describeMismatch(Object item, Description description) {
  160. description.appendText(matchingError);
  161. }
  162. private boolean compare(int index, String itemName, Object expected, Object actual) {
  163. if (expected == null && actual == null) {
  164. return true;
  165. } else if (expected == null || actual == null || !expected.equals(actual)) {
  166. matchingError = String.format("Item Index: '%s', Field: '%s' do not match (expected: %s, actual: %s)",
  167. index, itemName, expected, actual);
  168. return false;
  169. } else {
  170. return true;
  171. }
  172. }
  173. };
  174. }
  175. private void addUpgradeHistoryWithClassName(String className) {
  176. addUpgradeHistory(className, "", 0, "0");
  177. }
  178. private void addUpgradeHistoryWithNoTargetBuild(int buildNumber, String buildVersion) {
  179. addUpgradeHistory("UpgradeTask_Build" + buildNumber, "", buildNumber, buildVersion);
  180. }
  181. private void addUpgradeHistoryWithTargetBuild(int buildNumber, String buildVersion) {
  182. addUpgradeHistory("UpgradeTask_Build" + buildNumber, String.valueOf(buildNumber), buildNumber, buildVersion);
  183. }
  184. private void addUpgradeHistory(String className, String targetBuild, int buildNumber, String buildVersion) {
  185. queryDslAccessor.execute(dbConnection -> dbConnection
  186. .insert(UPGRADE_HISTORY)
  187. .set(UPGRADE_HISTORY.upgradeclass, className)
  188. .set(UPGRADE_HISTORY.targetbuild, targetBuild)
  189. .set(UPGRADE_HISTORY.status, "complete")
  190. .executeWithId());
  191. when(buildVersionRegistry.getVersionForBuildNumber(buildNumber)).thenReturn(new BuildVersionImpl(buildNumber, buildVersion));
  192. }
  193. private void addUpgradeVersionHistory(int targetBuild, String targetVersion, Date timePerformed) {
  194. queryDslAccessor.execute(dbConnection -> dbConnection
  195. .insert(UPGRADE_VERSION_HISTORY)
  196. .set(UPGRADE_VERSION_HISTORY.timeperformed, new Timestamp(timePerformed.getTime()))
  197. .set(UPGRADE_VERSION_HISTORY.targetbuild, String.valueOf(targetBuild))
  198. .set(UPGRADE_VERSION_HISTORY.targetversion, targetVersion)
  199. .executeWithId());
  200. }
  201. private List<UpgradeVersionHistoryDTO> getAllUpgradeVersionHistoryItems() {
  202. return queryDslAccessor.executeQuery(dbConnection -> dbConnection.newSqlQuery()
  203. .select(UPGRADE_VERSION_HISTORY)
  204. .from(UPGRADE_VERSION_HISTORY)
  205. .fetch());
  206. }
  207. private static Date MINUTES_AGO(int minutesAgo) {
  208. return Date.from(Instant.now().minus(minutesAgo, ChronoUnit.MINUTES));
  209. }
  210. }