You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MigrationStepsExecutorImplTest.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform.db.migration.step;
  21. import java.sql.SQLException;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.sonar.api.utils.log.LogTester;
  30. import org.sonar.api.utils.log.LoggerLevel;
  31. import org.sonar.server.platform.db.migration.engine.MigrationContainer;
  32. import org.sonar.server.platform.db.migration.engine.SimpleMigrationContainer;
  33. import org.sonar.server.platform.db.migration.history.MigrationHistory;
  34. import static com.google.common.base.Preconditions.checkState;
  35. import static java.util.Arrays.asList;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.assertj.core.api.Assertions.fail;
  38. import static org.mockito.Mockito.mock;
  39. public class MigrationStepsExecutorImplTest {
  40. @Rule
  41. public LogTester logTester = new LogTester();
  42. private MigrationContainer migrationContainer = new SimpleMigrationContainer();
  43. private MigrationHistory migrationHistor = mock(MigrationHistory.class);
  44. private MigrationStepsExecutorImpl underTest = new MigrationStepsExecutorImpl(migrationContainer, migrationHistor);
  45. @Test
  46. public void execute_does_not_fail_when_stream_is_empty_and_log_start_stop_INFO() {
  47. underTest.execute(Collections.emptyList());
  48. assertThat(logTester.logs()).hasSize(2);
  49. assertLogLevel(LoggerLevel.INFO, "Executing DB migrations...", "Executed DB migrations: success | time=");
  50. }
  51. @Test
  52. public void execute_fails_with_ISE_if_no_instance_of_computation_step_exist_in_container() {
  53. List<RegisteredMigrationStep> steps = asList(registeredStepOf(1, MigrationStep1.class));
  54. try {
  55. underTest.execute(steps);
  56. fail("execute should have thrown a IllegalStateException");
  57. } catch (IllegalStateException e) {
  58. assertThat(e).hasMessage("Can not find instance of " + MigrationStep1.class);
  59. } finally {
  60. assertThat(logTester.logs()).hasSize(2);
  61. assertLogLevel(LoggerLevel.INFO, "Executing DB migrations...");
  62. assertLogLevel(LoggerLevel.ERROR, "Executed DB migrations: failure | time=");
  63. }
  64. }
  65. private void assertLogLevel(LoggerLevel level, String... expected) {
  66. List<String> logs = logTester.logs(level);
  67. assertThat(logs).hasSize(expected.length);
  68. Iterator<String> iterator = logs.iterator();
  69. Arrays.stream(expected).forEachOrdered(log -> {
  70. if (log.endsWith(" | time=")) {
  71. assertThat(iterator.next()).startsWith(log);
  72. } else {
  73. assertThat(iterator.next()).isEqualTo(log);
  74. }
  75. });
  76. }
  77. @Test
  78. public void execute_execute_the_instance_of_type_specified_in_step_in_stream_order() {
  79. migrationContainer.add(MigrationStep1.class, MigrationStep2.class, MigrationStep3.class);
  80. underTest.execute(asList(
  81. registeredStepOf(1, MigrationStep2.class),
  82. registeredStepOf(2, MigrationStep1.class),
  83. registeredStepOf(3, MigrationStep3.class)));
  84. assertThat(SingleCallCheckerMigrationStep.calledSteps)
  85. .containsExactly(MigrationStep2.class, MigrationStep1.class, MigrationStep3.class);
  86. assertThat(logTester.logs()).hasSize(8);
  87. assertLogLevel(LoggerLevel.INFO,
  88. "Executing DB migrations...",
  89. "#1 '1-MigrationStep2'...",
  90. "#1 '1-MigrationStep2': success | time=",
  91. "#2 '2-MigrationStep1'...",
  92. "#2 '2-MigrationStep1': success | time=",
  93. "#3 '3-MigrationStep3'...",
  94. "#3 '3-MigrationStep3': success | time=",
  95. "Executed DB migrations: success | time=");
  96. assertThat(migrationContainer.getComponentByType(MigrationStep1.class).isCalled()).isTrue();
  97. assertThat(migrationContainer.getComponentByType(MigrationStep2.class).isCalled()).isTrue();
  98. assertThat(migrationContainer.getComponentByType(MigrationStep3.class).isCalled()).isTrue();
  99. }
  100. @Test
  101. public void execute_throws_MigrationStepExecutionException_on_first_failing_step_execution_throws_SQLException() {
  102. migrationContainer.add(MigrationStep2.class, SqlExceptionFailingMigrationStep.class, MigrationStep3.class);
  103. List<RegisteredMigrationStep> steps = asList(
  104. registeredStepOf(1, MigrationStep2.class),
  105. registeredStepOf(2, SqlExceptionFailingMigrationStep.class),
  106. registeredStepOf(3, MigrationStep3.class));
  107. try {
  108. underTest.execute(steps);
  109. fail("a MigrationStepExecutionException should have been thrown");
  110. } catch (MigrationStepExecutionException e) {
  111. assertThat(e).hasMessage("Execution of migration step #2 '2-SqlExceptionFailingMigrationStep' failed");
  112. assertThat(e).hasCause(SqlExceptionFailingMigrationStep.THROWN_EXCEPTION);
  113. } finally {
  114. assertThat(logTester.logs()).hasSize(6);
  115. assertLogLevel(LoggerLevel.INFO,
  116. "Executing DB migrations...",
  117. "#1 '1-MigrationStep2'...",
  118. "#1 '1-MigrationStep2': success | time=",
  119. "#2 '2-SqlExceptionFailingMigrationStep'...");
  120. assertLogLevel(LoggerLevel.ERROR,
  121. "#2 '2-SqlExceptionFailingMigrationStep': failure | time=",
  122. "Executed DB migrations: failure | time=");
  123. }
  124. }
  125. @Test
  126. public void execute_throws_MigrationStepExecutionException_on_first_failing_step_execution_throws_any_exception() {
  127. migrationContainer.add(MigrationStep2.class, RuntimeExceptionFailingMigrationStep.class, MigrationStep3.class);
  128. List<RegisteredMigrationStep> steps = asList(
  129. registeredStepOf(1, MigrationStep2.class),
  130. registeredStepOf(2, RuntimeExceptionFailingMigrationStep.class),
  131. registeredStepOf(3, MigrationStep3.class));
  132. try {
  133. underTest.execute(steps);
  134. fail("should throw MigrationStepExecutionException");
  135. } catch (MigrationStepExecutionException e) {
  136. assertThat(e).hasMessage("Execution of migration step #2 '2-RuntimeExceptionFailingMigrationStep' failed");
  137. assertThat(e.getCause()).isSameAs(RuntimeExceptionFailingMigrationStep.THROWN_EXCEPTION);
  138. }
  139. }
  140. private static RegisteredMigrationStep registeredStepOf(int migrationNumber, Class<? extends MigrationStep> migrationStep1Class) {
  141. return new RegisteredMigrationStep(migrationNumber, migrationNumber + "-" + migrationStep1Class.getSimpleName(), migrationStep1Class);
  142. }
  143. private static abstract class SingleCallCheckerMigrationStep implements MigrationStep {
  144. private static List<Class<? extends MigrationStep>> calledSteps = new ArrayList<>();
  145. private boolean called = false;
  146. @Override
  147. public void execute() {
  148. checkState(!called, "execute must not be called twice");
  149. this.called = true;
  150. calledSteps.add(getClass());
  151. }
  152. public boolean isCalled() {
  153. return called;
  154. }
  155. public static List<Class<? extends MigrationStep>> getCalledSteps() {
  156. return calledSteps;
  157. }
  158. }
  159. public static final class MigrationStep1 extends SingleCallCheckerMigrationStep {
  160. }
  161. public static final class MigrationStep2 extends SingleCallCheckerMigrationStep {
  162. }
  163. public static final class MigrationStep3 extends SingleCallCheckerMigrationStep {
  164. }
  165. public static class SqlExceptionFailingMigrationStep implements MigrationStep {
  166. private static final SQLException THROWN_EXCEPTION = new SQLException("Faking SQL exception in MigrationStep#execute()");
  167. @Override
  168. public void execute() throws SQLException {
  169. throw THROWN_EXCEPTION;
  170. }
  171. }
  172. public static class RuntimeExceptionFailingMigrationStep implements MigrationStep {
  173. private static final RuntimeException THROWN_EXCEPTION = new RuntimeException("Faking failing migration step");
  174. @Override
  175. public void execute() {
  176. throw THROWN_EXCEPTION;
  177. }
  178. }
  179. }