]> source.dussan.org Git - sonarqube.git/blob
24023f2fed6f63c8dcd183e92e29daae1c95c189
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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;
21
22 import java.util.Date;
23 import org.junit.Test;
24 import org.mockito.InOrder;
25 import org.sonar.server.platform.Platform;
26 import org.sonar.server.platform.db.migration.engine.MigrationEngine;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.inOrder;
32 import static org.mockito.Mockito.mock;
33
34 /**
35  * Unit test for DatabaseMigrationImpl which does not test any of its concurrency management and asynchronous execution code.
36  */
37 public class DatabaseMigrationImplTest {
38   private static final Throwable AN_ERROR = new RuntimeException("runtime exception created on purpose");
39
40   /**
41    * Implementation of execute runs Runnable synchronously.
42    */
43   private DatabaseMigrationExecutorService executorService = new DatabaseMigrationExecutorServiceAdaptor() {
44     @Override
45     public void execute(Runnable command) {
46       command.run();
47     }
48   };
49   private MutableDatabaseMigrationState migrationState = new DatabaseMigrationStateImpl();
50   private Platform platform = mock(Platform.class);
51   private MigrationEngine migrationEngine = mock(MigrationEngine.class);
52   private InOrder inOrder = inOrder(platform, migrationEngine);
53
54   private DatabaseMigrationImpl underTest = new DatabaseMigrationImpl(executorService, migrationState, migrationEngine, platform);
55
56   @Test
57   public void startit_calls_MigrationEngine_execute() {
58     underTest.startIt();
59
60     inOrder.verify(migrationEngine).execute();
61     inOrder.verify(platform).doStart();
62     inOrder.verifyNoMoreInteractions();
63   }
64
65   @Test
66   public void status_is_SUCCEEDED_and_failure_is_null_when_trigger_runs_without_an_exception() {
67     underTest.startIt();
68
69     assertThat(migrationState.getStatus()).isEqualTo(DatabaseMigrationState.Status.SUCCEEDED);
70     assertThat(migrationState.getError()).isNull();
71     assertThat(migrationState.getStartedAt()).isNotNull();
72   }
73
74   @Test
75   public void status_is_FAILED_and_failure_stores_the_exception_when_trigger_throws_an_exception() {
76     mockMigrationThrowsError();
77
78     underTest.startIt();
79
80     assertThat(migrationState.getStatus()).isEqualTo(DatabaseMigrationState.Status.FAILED);
81     assertThat(migrationState.getError()).isSameAs(AN_ERROR);
82     assertThat(migrationState.getStartedAt()).isNotNull();
83   }
84
85   @Test
86   public void successive_calls_to_startIt_reset_status_startedAt_and_failureError() {
87     mockMigrationThrowsError();
88
89     underTest.startIt();
90
91     assertThat(migrationState.getStatus()).isEqualTo(DatabaseMigrationState.Status.FAILED);
92     assertThat(migrationState.getError()).isSameAs(AN_ERROR);
93     Date firstStartDate = migrationState.getStartedAt();
94     assertThat(firstStartDate).isNotNull();
95
96     mockMigrationDoesNothing();
97
98     underTest.startIt();
99
100     assertThat(migrationState.getStatus()).isEqualTo(DatabaseMigrationState.Status.SUCCEEDED);
101     assertThat(migrationState.getError()).isNull();
102     assertThat(migrationState.getStartedAt()).isNotSameAs(firstStartDate);
103   }
104
105   private void mockMigrationThrowsError() {
106     doThrow(AN_ERROR).when(migrationEngine).execute();
107   }
108
109   private void mockMigrationDoesNothing() {
110     doNothing().when(migrationEngine).execute();
111   }
112 }