diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2018-10-24 09:48:59 +0200 |
---|---|---|
committer | Eric Hartmann <hartmann.eric@gmail.com> | 2018-10-25 10:26:00 +0200 |
commit | ba52896830cdcf9e265a1fdddb154b3144b558d8 (patch) | |
tree | 257e89cc3cadbef71f80f57feb214d3b3b18372d | |
parent | a7e9f336455aa801eec9f81d72cb2ab800261268 (diff) | |
download | sonarqube-ba52896830cdcf9e265a1fdddb154b3144b558d8.tar.gz sonarqube-ba52896830cdcf9e265a1fdddb154b3144b558d8.zip |
SONARCLOUD-150 Fix UT on Oracle
3 files changed, 101 insertions, 109 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java index 60344b58569..03361e5b586 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java @@ -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); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java index 8cd15f7f975..5a15a5a04ab 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java @@ -19,11 +19,8 @@ */ 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,13 +53,48 @@ 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 index 00000000000..c6d922ba3b0 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v75/DbVersion75Test.java @@ -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); + } +} |