From: Simon Brandhof Date: Mon, 23 Jan 2017 12:52:20 +0000 (+0100) Subject: Drop methods from ComponentService X-Git-Tag: 6.3-RC1~469 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5c3fb12cfa7a9d1b5ed6c029f5f7d036fa1f1ef5;p=sonarqube.git Drop methods from ComponentService which duplicate ComponentDao --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java index 0be403673bb..2e241e5fee5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java @@ -28,7 +28,6 @@ import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Set; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.i18n.I18n; @@ -77,40 +76,8 @@ public class ComponentService { } public ComponentDto getByKey(String key) { - DbSession session = dbClient.openSession(false); - try { - return getByKey(session, key); - } finally { - session.close(); - } - } - - @CheckForNull - public ComponentDto getNullableByKey(String key) { - DbSession session = dbClient.openSession(false); - try { - Optional component = dbClient.componentDao().selectByKey(session, key); - return component.orNull(); - } finally { - session.close(); - } - } - - public ComponentDto getNonNullByUuid(String uuid) { - DbSession session = dbClient.openSession(false); - try { - return dbClient.componentDao().selectOrFailByUuid(session, uuid); - } finally { - session.close(); - } - } - - public Optional getByUuid(String uuid) { - DbSession session = dbClient.openSession(false); - try { - return dbClient.componentDao().selectByUuid(session, uuid); - } finally { - session.close(); + try (DbSession session = dbClient.openSession(false)) { + return componentFinder.getByKey(session, key); } } @@ -158,8 +125,8 @@ public class ComponentService { checkBranchFormat(newComponent.qualifier(), newComponent.branch()); String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.branch()); - ComponentDto existingComponent = getNullableByKey(keyWithBranch); - if (existingComponent != null) { + Optional existingComponent = dbClient.componentDao().selectByKey(session, keyWithBranch); + if (existingComponent.isPresent()) { throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch)); } @@ -199,15 +166,6 @@ public class ComponentService { session.commit(); } - public Collection componentUuids(@Nullable Collection componentKeys) { - DbSession session = dbClient.openSession(false); - try { - return componentUuids(session, componentKeys, false); - } finally { - dbClient.closeSession(session); - } - } - public Collection componentUuids(DbSession session, @Nullable Collection componentKeys, boolean ignoreMissingComponents) { Collection componentUuids = newArrayList(); if (componentKeys != null && !componentKeys.isEmpty()) { @@ -272,7 +230,4 @@ public class ComponentService { return String.format(message, i18n.message(Locale.getDefault(), "qualifier." + qualifier, "Project"), key); } - private ComponentDto getByKey(DbSession session, String key) { - return componentFinder.getByKey(session, key); - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java index c0bc592dcc0..6a5cf4f2549 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java @@ -250,7 +250,7 @@ public class IssueQueryService { @CheckForNull private Date findCreatedAfterFromComponentUuid(DbSession dbSession, String uuid) { - ComponentDto component = checkFoundWithOptional(componentService.getByUuid(uuid), "Component with id '%s' not found", uuid); + ComponentDto component = checkFoundWithOptional(dbClient.componentDao().selectByUuid(dbSession, uuid), "Component with id '%s' not found", uuid); Optional snapshot = dbClient.snapshotDao().selectLastAnalysisByComponentUuid(dbSession, component.uuid()); if (snapshot.isPresent()) { return longToDate(snapshot.get().getPeriodDate(1)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java index f61ea88e27f..4b0397455a9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java @@ -21,7 +21,6 @@ package org.sonar.server.component; import com.google.common.base.Optional; import java.util.Arrays; -import org.assertj.core.api.Fail; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -42,7 +41,6 @@ import org.sonar.server.component.index.ComponentIndexDefinition; import org.sonar.server.component.index.ComponentIndexer; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.i18n.I18nRule; import org.sonar.server.measure.index.ProjectMeasuresIndexDefinition; import org.sonar.server.measure.index.ProjectMeasuresIndexer; @@ -103,26 +101,6 @@ public class ComponentServiceTest { assertThat(underTest.getByKey(project.getKey())).isNotNull(); } - @Test - public void get_nullable_by_key() { - ComponentDto project = insertSampleProject(); - assertThat(underTest.getNullableByKey(project.getKey())).isNotNull(); - assertThat(underTest.getNullableByKey("unknown")).isNull(); - } - - @Test - public void get_by_uuid() { - ComponentDto project = insertSampleProject(); - assertThat(underTest.getNonNullByUuid(project.uuid())).isNotNull(); - } - - @Test - public void get_nullable_by_uuid() { - ComponentDto project = insertSampleProject(); - assertThat(underTest.getByUuid(project.uuid())).isPresent(); - assertThat(underTest.getByUuid("unknown")).isAbsent(); - } - @Test public void create_project() { userSession.login("john").setGlobalPermissions(PROVISIONING); @@ -136,7 +114,7 @@ public class ComponentServiceTest { .build()) .getKey(); - ComponentDto project = underTest.getNullableByKey(key); + ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, key); assertThat(project.getOrganizationUuid()).isEqualTo(organization.getUuid()); assertThat(project.key()).isEqualTo("struts"); assertThat(project.deprecatedKey()).isEqualTo("struts"); @@ -167,7 +145,7 @@ public class ComponentServiceTest { .build()) .getKey(); - ComponentDto project = underTest.getNullableByKey(key); + ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, key); assertThat(project.getOrganizationUuid()).isEqualTo(organization.getUuid()); assertThat(project.key()).isEqualTo("struts:origin/branch"); assertThat(project.deprecatedKey()).isEqualTo("struts:origin/branch"); @@ -187,7 +165,7 @@ public class ComponentServiceTest { .build()) .getKey(); - ComponentDto project = underTest.getNullableByKey(key); + ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, key); assertThat(project.getOrganizationUuid()).isEqualTo(organization.getUuid()); assertThat(project.key()).isEqualTo("all-project"); assertThat(project.deprecatedKey()).isEqualTo("all-project"); @@ -220,7 +198,7 @@ public class ComponentServiceTest { .getKey(); dbTester.getSession().commit(); - ComponentDto dev = underTest.getNullableByKey(key); + ComponentDto dev = dbClient.componentDao().selectOrFailByKey(dbSession, key); assertThat(dev.getOrganizationUuid()).isEqualTo(organization.getUuid()); assertThat(dev.key()).isEqualTo("DEV:jon.name@mail.com"); assertThat(dev.deprecatedKey()).isEqualTo("DEV:jon.name@mail.com"); @@ -328,35 +306,6 @@ public class ComponentServiceTest { verify(componentDao).delete(session, 3L); } - @Test - public void should_return_project_uuids() { - ComponentDto project = insertSampleProject(); - String moduleKey = "sample:root:module"; - ComponentDto module = ComponentTesting.newModuleDto(project).setKey(moduleKey); - dbClient.componentDao().insert(dbSession, module); - String fileKey = "sample:root:module:Foo.xoo"; - ComponentDto file = newFileDto(module, null).setKey(fileKey); - dbClient.componentDao().insert(dbSession, file); - dbSession.commit(); - - assertThat(underTest.componentUuids(Arrays.asList(moduleKey, fileKey))).hasSize(2); - assertThat(underTest.componentUuids(null)).isEmpty(); - assertThat(underTest.componentUuids(Arrays.asList())).isEmpty(); - } - - @Test - public void should_fail_on_components_not_found() { - String moduleKey = "sample:root:module"; - String fileKey = "sample:root:module:Foo.xoo"; - - try { - underTest.componentUuids(Arrays.asList(moduleKey, fileKey)); - Fail.fail("Should throw NotFoundException"); - } catch (NotFoundException notFound) { - assertThat(notFound.getMessage()).contains(moduleKey).contains(fileKey); - } - } - @Test public void should_fail_silently_on_components_not_found_if_told_so() { String moduleKey = "sample:root:module"; diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java index 997f684da43..c871eb3baab 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java @@ -100,12 +100,12 @@ public class ComponentServiceUpdateKeyTest { dbSession.commit(); // Check project key has been updated - assertThat(underTest.getNullableByKey(project.key())).isNull(); - assertThat(underTest.getNullableByKey("sample2:root")).isNotNull(); + assertThat(dbClient.componentDao().selectByKey(dbSession, project.key())).isAbsent(); + assertThat(dbClient.componentDao().selectByKey(dbSession, "sample2:root")).isPresent(); // Check file key has been updated - assertThat(underTest.getNullableByKey(file.key())).isNull(); - assertThat(underTest.getNullableByKey("sample2:root:src/File.xoo")).isNotNull(); + assertThat(dbClient.componentDao().selectByKey(dbSession, file.key())).isAbsent(); + assertThat(dbClient.componentDao().selectByKey(dbSession, "sample2:root:src/File.xoo")).isPresent(); assertThat(dbClient.componentDao().selectByKey(dbSession, inactiveFile.getKey())).isPresent();