]> source.dussan.org Git - sonarqube.git/commitdiff
SONARCLOUD-150 Fix UT on Oracle
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 24 Oct 2018 07:48:59 +0000 (09:48 +0200)
committerEric Hartmann <hartmann.eric@gmail.com>
Thu, 25 Oct 2018 08:26:00 +0000 (10:26 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java
server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v75/DbVersion75Test.java [new file with mode: 0644]

index 60344b58569b734c09a35311426e958256a1033d..03361e5b586bba901ed71dc00b8c4c83b68a28f2 100644 (file)
@@ -44,6 +44,18 @@ public class AlmAppInstallDao implements Dao {
     this.uuidFactory = uuidFactory;
   }
 
+  public Optional<AlmAppInstallDto> selectByOwner(DbSession dbSession, ALM alm, String ownerId) {
+    checkAlm(alm);
+    checkOwnerId(ownerId);
+
+    AlmAppInstallMapper mapper = getMapper(dbSession);
+    return Optional.ofNullable(mapper.selectByOwner(alm.getId(), ownerId));
+  }
+
+  public List<AlmAppInstallDto> findAllWithNoOwnerType(DbSession dbSession) {
+    return getMapper(dbSession).selectAllWithNoOwnerType();
+  }
+
   /**
    * @param alm Unique identifier of the ALM, like 'bitbucketcloud' or 'github', can't be null
    * @param ownerId ALM specific identifier of the owner of the app, like team or user uuid for Bitbucket Cloud or organization id for Github, can't be null
@@ -62,18 +74,6 @@ public class AlmAppInstallDao implements Dao {
     }
   }
 
-  public List<AlmAppInstallDto> findAllWithNoOwnerType(DbSession dbSession) {
-    return getMapper(dbSession).selectAllWithNoOwnerType();
-  }
-
-  public Optional<AlmAppInstallDto> selectByOwner(DbSession dbSession, ALM alm, String ownerId) {
-    checkAlm(alm);
-    checkOwnerId(ownerId);
-
-    AlmAppInstallMapper mapper = getMapper(dbSession);
-    return Optional.ofNullable(mapper.selectByOwner(alm.getId(), ownerId));
-  }
-
   public void delete(DbSession dbSession, ALM alm, String ownerId) {
     checkAlm(alm);
     checkOwnerId(ownerId);
index 8cd15f7f975e9d2ae11ca0ca397cad7cb47f420b..5a15a5a04abc3f28324d7c1c0b59f1944c9697db 100644 (file)
  */
 package org.sonar.db.alm;
 
-import java.util.List;
-import java.util.Map;
 import java.util.Objects;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
+import java.util.Optional;
 import org.assertj.core.api.AbstractAssert;
 import org.junit.Rule;
 import org.junit.Test;
@@ -36,6 +33,7 @@ import org.sonar.db.DbTester;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.sonar.db.alm.ALM.BITBUCKETCLOUD;
 import static org.sonar.db.alm.ALM.GITHUB;
 
 public class AlmAppInstallDaoTest {
@@ -55,12 +53,47 @@ public class AlmAppInstallDaoTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
-  public DbTester dbTester = DbTester.create(system2);
+  public DbTester db = DbTester.create(system2);
 
-  private DbSession dbSession = dbTester.getSession();
+  private DbSession dbSession = db.getSession();
   private UuidFactory uuidFactory = mock(UuidFactory.class);
   private AlmAppInstallDao underTest = new AlmAppInstallDao(system2, uuidFactory);
 
+  @Test
+  public void selectByOwner() {
+    when(uuidFactory.create()).thenReturn(A_UUID);
+    when(system2.now()).thenReturn(DATE);
+    underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, AN_INSTALL);
+
+    assertThat(underTest.selectByOwner(dbSession, GITHUB, A_OWNER).get())
+      .extracting(AlmAppInstallDto::getUuid, AlmAppInstallDto::getInstallId, AlmAppInstallDto::getOwnerId, AlmAppInstallDto::getAlm, AlmAppInstallDto::getCreatedAt, AlmAppInstallDto::getUpdatedAt)
+      .containsExactlyInAnyOrder(A_UUID, AN_INSTALL, A_OWNER, ALM.GITHUB, DATE, DATE);
+    assertThat(underTest.selectByOwner(dbSession, GITHUB, "unknown")).isEmpty();
+    assertThat(underTest.selectByOwner(dbSession, BITBUCKETCLOUD, A_OWNER)).isEmpty();
+  }
+
+
+  @Test
+  public void selectByOwner_throws_NPE_when_alm_is_null() {
+    expectAlmNPE();
+
+    underTest.selectByOwner(dbSession, null, A_OWNER);
+  }
+
+  @Test
+  public void selectByOwner_throws_IAE_when_owner_id_is_null() {
+    expectOwnerIdNullOrEmptyIAE();
+
+    underTest.selectByOwner(dbSession, GITHUB, null);
+  }
+
+  @Test
+  public void selectByOwner_throws_IAE_when_owner_id_is_empty() {
+    expectOwnerIdNullOrEmptyIAE();
+
+    underTest.selectByOwner(dbSession, GITHUB, EMPTY_STRING);
+  }
+
   @Test
   public void insert_throws_NPE_if_alm_is_null() {
     expectAlmNPE();
@@ -137,7 +170,7 @@ public class AlmAppInstallDaoTest {
     underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, AN_INSTALL);
 
     when(system2.now()).thenReturn(DATE_LATER);
-    underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER,true,  OTHER_INSTALL);
+    underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, OTHER_INSTALL);
 
     assertThatAlmAppInstall(GITHUB, A_OWNER)
       .hasInstallId(OTHER_INSTALL)
@@ -167,40 +200,6 @@ public class AlmAppInstallDaoTest {
       .hasUpdatedAt(DATE);
   }
 
-  @Test
-  public void getInstallId_throws_NPE_when_alm_is_null() {
-    expectAlmNPE();
-
-    underTest.selectByOwner(dbSession, null, A_OWNER);
-  }
-
-  @Test
-  public void getInstallId_throws_IAE_when_owner_id_is_null() {
-    expectOwnerIdNullOrEmptyIAE();
-
-    underTest.selectByOwner(dbSession, GITHUB, null);
-  }
-
-  @Test
-  public void getInstallId_throws_IAE_when_owner_id_is_empty() {
-    expectOwnerIdNullOrEmptyIAE();
-
-    underTest.selectByOwner(dbSession, GITHUB, EMPTY_STRING);
-  }
-
-  @Test
-  public void getInstallId_returns_empty_optional_when_entry_does_not_exist_in_DB() {
-    assertThat(underTest.selectByOwner(dbSession, GITHUB, A_OWNER)).isEmpty();
-  }
-
-  @Test
-  public void getInstallId_returns_install_id_when_entry_exists() {
-    when(uuidFactory.create()).thenReturn(A_UUID);
-    underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, AN_INSTALL);
-
-    assertThat(underTest.selectByOwner(dbSession, GITHUB, A_OWNER).map(AlmAppInstallDto::getInstallId)).contains(AN_INSTALL);
-  }
-
   private void expectAlmNPE() {
     expectedException.expect(NullPointerException.class);
     expectedException.expectMessage("alm can't be null");
@@ -217,40 +216,25 @@ public class AlmAppInstallDaoTest {
   }
 
   private AlmAppInstallAssert assertThatAlmAppInstall(ALM alm, String ownerId) {
-    return new AlmAppInstallAssert(dbTester, dbSession, alm, ownerId);
+    return new AlmAppInstallAssert(db, dbSession, alm, ownerId);
   }
 
-  private static class AlmAppInstallAssert extends AbstractAssert<AlmAppInstallAssert, AlmAppInstall> {
+  private static class AlmAppInstallAssert extends AbstractAssert<AlmAppInstallAssert, AlmAppInstallDto> {
 
     private AlmAppInstallAssert(DbTester dbTester, DbSession dbSession, ALM alm, String ownerId) {
       super(asAlmAppInstall(dbTester, dbSession, alm, ownerId), AlmAppInstallAssert.class);
     }
 
-    private static AlmAppInstall asAlmAppInstall(DbTester dbTester, DbSession dbSession, ALM alm, String ownerId) {
-      List<Map<String, Object>> rows = dbTester.select(
-        dbSession,
-        "select" +
-          " install_id as \"installId\", is_owner_user as \"isOwnerUser\", created_at as \"createdAt\", updated_at as \"updatedAt\"" +
-          " from alm_app_installs" +
-          " where alm_id='" + alm.getId() + "' and owner_id='" + ownerId + "'");
-      if (rows.isEmpty()) {
-        return null;
-      }
-      if (rows.size() > 1) {
-        throw new IllegalStateException("Unique index violation");
-      }
-      return new AlmAppInstall(
-        (String) rows.get(0).get("installId"),
-        (Boolean) rows.get(0).get("isOwnerUser"),
-        (Long) rows.get(0).get("createdAt"),
-        (Long) rows.get(0).get("updatedAt"));
+    private static AlmAppInstallDto asAlmAppInstall(DbTester db, DbSession dbSession, ALM alm, String ownerId) {
+      Optional<AlmAppInstallDto> almAppInstall = db.getDbClient().almAppInstallDao().selectByOwner(dbSession, alm, ownerId);
+      return almAppInstall.orElse(null);
     }
 
     public void doesNotExist() {
       isNull();
     }
 
-    public AlmAppInstallAssert hasInstallId(String expected) {
+    AlmAppInstallAssert hasInstallId(String expected) {
       isNotNull();
 
       if (!Objects.equals(actual.getInstallId(), expected)) {
@@ -259,7 +243,7 @@ public class AlmAppInstallDaoTest {
       return this;
     }
 
-    public AlmAppInstallAssert hasOwnerUser(boolean expected) {
+    AlmAppInstallAssert hasOwnerUser(boolean expected) {
       isNotNull();
 
       if (!Objects.equals(actual.isOwnerUser(), expected)) {
@@ -268,7 +252,7 @@ public class AlmAppInstallDaoTest {
       return this;
     }
 
-    public AlmAppInstallAssert hasCreatedAt(long expected) {
+    AlmAppInstallAssert hasCreatedAt(long expected) {
       isNotNull();
 
       if (!Objects.equals(actual.getCreatedAt(), expected)) {
@@ -278,7 +262,7 @@ public class AlmAppInstallDaoTest {
       return this;
     }
 
-    public AlmAppInstallAssert hasUpdatedAt(long expected) {
+    AlmAppInstallAssert hasUpdatedAt(long expected) {
       isNotNull();
 
       if (!Objects.equals(actual.getUpdatedAt(), expected)) {
@@ -290,36 +274,4 @@ public class AlmAppInstallDaoTest {
 
   }
 
-  private static final class AlmAppInstall {
-    private final String installId;
-    private final Boolean isOwnerUser;
-    private final Long createdAt;
-    private final Long updatedAt;
-
-    public AlmAppInstall(@Nullable String installId, @Nullable Boolean isOwnerUser, @Nullable Long createdAt, @Nullable Long updatedAt) {
-      this.installId = installId;
-      this.isOwnerUser = isOwnerUser;
-      this.createdAt = createdAt;
-      this.updatedAt = updatedAt;
-    }
-
-    @CheckForNull
-    public String getInstallId() {
-      return installId;
-    }
-
-    @CheckForNull
-    public Long getCreatedAt() {
-      return createdAt;
-    }
-
-    @CheckForNull
-    public Long getUpdatedAt() {
-      return updatedAt;
-    }
-
-    public Boolean isOwnerUser() {
-      return isOwnerUser;
-    }
-  }
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v75/DbVersion75Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v75/DbVersion75Test.java
new file mode 100644 (file)
index 0000000..c6d922b
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v75;
+
+import org.junit.Test;
+
+import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount;
+import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber;
+
+public class DbVersion75Test {
+
+  private DbVersion75 underTest = new DbVersion75();
+
+  @Test
+  public void migrationNumber_starts_at_2400() {
+    verifyMinimumMigrationNumber(underTest, 2400);
+  }
+
+  @Test
+  public void verify_migration_count() {
+    verifyMigrationCount(underTest, 1);
+  }
+}