import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
+import org.slf4j.event.Level;
+import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.System2;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.db.MigrationDbTester;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.Mockito.mock;
@RegisterExtension
public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(MigrateBranchesLiveMeasuresToMeasures.class);
+ @RegisterExtension
+ private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
+
private final SequenceUuidFactory uuidFactory = new SequenceUuidFactory();
private final System2 system2 = mock();
private final DataChange underTest = new MigrateBranchesLiveMeasuresToMeasures(db.database(), system2);
assertThat(db.countRowsOfTable("measures")).isZero();
}
+ @Test
+ void log_the_item_uuid_when_the_migration_fails() {
+ String nclocMetricUuid = insertMetric("ncloc", "INT");
+ String branch1 = "branch_1";
+ insertNotMigratedBranch(branch1);
+ insertMeasure(branch1, nclocMetricUuid, Map.of("value", 120));
+
+ db.executeDdl("drop table measures");
+ db.assertTableDoesNotExist("measures");
+
+ assertThatExceptionOfType(SQLException.class)
+ .isThrownBy(underTest::execute);
+
+ assertThat(logTester.logs(Level.ERROR))
+ .contains("Migration of branch branch_1 failed");
+ }
+
@Test
void shall_not_migrate_when_branch_is_already_flagged() throws SQLException {
String nclocMetricUuid = insertMetric("ncloc", "INT");
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
+import org.slf4j.event.Level;
+import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.System2;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.db.MigrationDbTester;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.Mockito.mock;
@RegisterExtension
public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(MigratePortfoliosLiveMeasuresToMeasures.class);
+ @RegisterExtension
+ private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
+
private final SequenceUuidFactory uuidFactory = new SequenceUuidFactory();
private final System2 system2 = mock();
private final DataChange underTest = new MigratePortfoliosLiveMeasuresToMeasures(db.database(), system2);
assertThat(db.countRowsOfTable("measures")).isZero();
}
+ @Test
+ void log_the_item_uuid_when_the_migration_fails() {
+ String nclocMetricUuid = insertMetric("ncloc", "INT");
+ String portfolio = "portfolio_1";
+ insertNotMigratedPortfolio(portfolio);
+ insertMeasure(portfolio, nclocMetricUuid, Map.of("value", 120));
+
+ db.executeDdl("drop table measures");
+ db.assertTableDoesNotExist("measures");
+
+ assertThatExceptionOfType(SQLException.class)
+ .isThrownBy(underTest::execute);
+
+ assertThat(logTester.logs(Level.ERROR))
+ .contains("Migration of portfolio portfolio_1 failed");
+ }
+
@Test
void shall_not_migrate_when_portfolio_is_already_flagged() throws SQLException {
String nclocMetricUuid = insertMetric("ncloc", "INT");
int migrated = 0;
for (String uuid : uuids) {
- migrateItem(uuid, context);
+ try {
+ migrateItem(uuid, context);
+ } catch (Exception e) {
+ LOGGER.error(format("Migration of %s %s failed", item, uuid));
+ throw e;
+ }
migrated++;
if (migrated % 100 == 0) {