]> source.dussan.org Git - sonarqube.git/blob
b60f89715700631e1722bf9966bffbe2c52fe147
[sonarqube.git] /
1 package org.sonar.server.platform.db.migration.version.v63;
2
3 import java.sql.SQLException;
4 import java.sql.Types;
5 import javax.annotation.Nullable;
6 import org.junit.Rule;
7 import org.junit.Test;
8 import org.junit.rules.ExpectedException;
9 import org.sonar.api.utils.System2;
10 import org.sonar.db.DbTester;
11
12 public class MakeColumnGuardedOfOrganizationsNotNullableTest {
13   private static final String TABLE_ORGANIZATIONS = "organizations";
14
15   @Rule
16   public DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, MakeColumnGuardedOfOrganizationsNotNullableTest.class, "organizations_with_nullable_guarded.sql");
17   @Rule
18   public ExpectedException expectedException = ExpectedException.none();
19
20   private MakeColumnGuardedOfOrganizationsNotNullable underTest = new MakeColumnGuardedOfOrganizationsNotNullable(dbTester.database());
21
22   @Test
23   public void migration_sets_guarded_column_not_nullable_on_empty_table() throws SQLException {
24     underTest.execute();
25
26     verifyColumnDefinition();
27   }
28
29   @Test
30   public void migration_sets_guarded_column_not_nullable_on_populated_table() throws SQLException {
31     insertOrganization("org_A", true);
32     insertOrganization("org_B", false);
33
34     underTest.execute();
35
36     verifyColumnDefinition();
37   }
38
39   @Test
40   public void migration_fails_if_some_row_has_a_null_guarded() throws SQLException {
41     insertOrganization("org_c", null);
42
43     expectedException.expect(IllegalStateException.class);
44     expectedException.expectMessage("Fail to execute");
45
46     underTest.execute();
47   }
48
49   private void verifyColumnDefinition() {
50     dbTester.assertColumnDefinition(TABLE_ORGANIZATIONS, "guarded", Types.BOOLEAN, null, false);
51   }
52
53   private void insertOrganization(String uuid, @Nullable Boolean guarded) {
54     dbTester.executeInsert(
55       "ORGANIZATIONS",
56       "UUID", uuid,
57       "KEE", uuid,
58       "NAME", uuid,
59       "GUARDED", guarded == null ? null : String.valueOf(guarded),
60       "CREATED_AT", "1000",
61       "UPDATED_AT", "1000");
62   }
63 }