]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7299 Remove ComponentFinder from ComponentService
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 19 Jan 2017 12:57:41 +0000 (13:57 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 24 Jan 2017 17:36:49 +0000 (18:36 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/UpdateKeyAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java
server/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/BulkUpdateKeyActionTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/UpdateKeyActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/AddProjectActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java

index 2e241e5fee5bb03e98fcb60e67a2fc5d8c3a6fda..2fb921e652530b7660b1066d6c8e0c6d1c4c7223 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.component;
 
 import com.google.common.base.Joiner;
-import com.google.common.base.Optional;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Sets;
 import java.util.Collection;
@@ -60,29 +59,20 @@ public class ComponentService {
   private final I18n i18n;
   private final UserSession userSession;
   private final System2 system2;
-  private final ComponentFinder componentFinder;
   private final ProjectMeasuresIndexer projectMeasuresIndexer;
   private final ComponentIndexer componentIndexer;
 
-  public ComponentService(DbClient dbClient, I18n i18n, UserSession userSession, System2 system2, ComponentFinder componentFinder, ProjectMeasuresIndexer projectMeasuresIndexer,
+  public ComponentService(DbClient dbClient, I18n i18n, UserSession userSession, System2 system2, ProjectMeasuresIndexer projectMeasuresIndexer,
     ComponentIndexer componentIndexer) {
     this.dbClient = dbClient;
     this.i18n = i18n;
     this.userSession = userSession;
     this.system2 = system2;
-    this.componentFinder = componentFinder;
     this.projectMeasuresIndexer = projectMeasuresIndexer;
     this.componentIndexer = componentIndexer;
   }
 
-  public ComponentDto getByKey(String key) {
-    try (DbSession session = dbClient.openSession(false)) {
-      return componentFinder.getByKey(session, key);
-    }
-  }
-
-  public void updateKey(DbSession dbSession, String projectOrModuleKey, String newKey) {
-    ComponentDto component = componentFinder.getByKey(dbSession, projectOrModuleKey);
+  public void updateKey(DbSession dbSession, ComponentDto component, String newKey) {
     userSession.checkComponentUuidPermission(UserRole.ADMIN, component.projectUuid());
     checkIsProjectOrModule(component);
     checkProjectOrModuleKeyFormat(newKey);
@@ -124,9 +114,7 @@ public class ComponentService {
   private ComponentDto createRootComponent(DbSession session, NewComponent newComponent) {
     checkBranchFormat(newComponent.qualifier(), newComponent.branch());
     String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.branch());
-
-    Optional<ComponentDto> existingComponent = dbClient.componentDao().selectByKey(session, keyWithBranch);
-    if (existingComponent.isPresent()) {
+    if (dbClient.componentDao().selectByKey(session, keyWithBranch).isPresent()) {
       throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch));
     }
 
index 36475f774ae142db3e6cfc7e2c0b2df2af6418d0..45d33feb3e6617be79817de469373300a6131dff 100644 (file)
@@ -86,7 +86,7 @@ public class UpdateKeyAction implements ComponentsWsAction {
     DbSession dbSession = dbClient.openSession(false);
     try {
       ComponentDto projectOrModule = componentFinder.getByUuidOrKey(dbSession, request.getId(), request.getKey(), ParamNames.ID_AND_KEY);
-      componentService.updateKey(dbSession, projectOrModule.key(), request.getNewKey());
+      componentService.updateKey(dbSession, projectOrModule, request.getNewKey());
       dbSession.commit();
     } finally {
       dbClient.closeSession(dbSession);
index 4b0397455a977e0094513884432e3056d04ac357..6722c0350a72d48b6faae345392811a604c26555 100644 (file)
@@ -91,16 +91,10 @@ public class ComponentServiceTest {
   public void setUp() {
     i18n.put("qualifier.TRK", "Project");
 
-    underTest = new ComponentService(dbClient, i18n, userSession, system2, new ComponentFinder(dbClient), projectMeasuresIndexer, componentIndexer);
+    underTest = new ComponentService(dbClient, i18n, userSession, system2, projectMeasuresIndexer, componentIndexer);
     organization = dbTester.organizations().insert();
   }
 
-  @Test
-  public void get_by_key() {
-    ComponentDto project = insertSampleProject();
-    assertThat(underTest.getByKey(project.getKey())).isNotNull();
-  }
-
   @Test
   public void create_project() {
     userSession.login("john").setGlobalPermissions(PROVISIONING);
@@ -114,7 +108,7 @@ public class ComponentServiceTest {
         .build())
       .getKey();
 
-    ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, key);
+    ComponentDto project = dbTester.getDbClient().componentDao().selectOrFailByKey(dbSession, key);
     assertThat(project.getOrganizationUuid()).isEqualTo(organization.getUuid());
     assertThat(project.key()).isEqualTo("struts");
     assertThat(project.deprecatedKey()).isEqualTo("struts");
@@ -145,7 +139,7 @@ public class ComponentServiceTest {
         .build())
       .getKey();
 
-    ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, key);
+    ComponentDto project = dbTester.getDbClient().componentDao().selectOrFailByKey(dbSession, key);
     assertThat(project.getOrganizationUuid()).isEqualTo(organization.getUuid());
     assertThat(project.key()).isEqualTo("struts:origin/branch");
     assertThat(project.deprecatedKey()).isEqualTo("struts:origin/branch");
@@ -165,7 +159,7 @@ public class ComponentServiceTest {
         .build())
       .getKey();
 
-    ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, key);
+    ComponentDto project = dbTester.getDbClient().componentDao().selectOrFailByKey(dbSession, key);
     assertThat(project.getOrganizationUuid()).isEqualTo(organization.getUuid());
     assertThat(project.key()).isEqualTo("all-project");
     assertThat(project.deprecatedKey()).isEqualTo("all-project");
@@ -198,7 +192,7 @@ public class ComponentServiceTest {
       .getKey();
     dbTester.getSession().commit();
 
-    ComponentDto dev = dbClient.componentDao().selectOrFailByKey(dbSession, key);
+    ComponentDto dev = dbTester.getDbClient().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");
@@ -293,7 +287,7 @@ public class ComponentServiceTest {
       ComponentTesting.newProjectDto(organizationDto).setId(2L).setKey(projectKey),
       ComponentTesting.newProjectDto(organizationDto).setId(3L).setKey(projectKey)));
 
-    underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient), projectMeasuresIndexer, componentIndexer);
+    underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, projectMeasuresIndexer, componentIndexer);
     underTest.create(
       session,
       newComponentBuilder()
index c871eb3baab0ebe868f15dcd5e53cbc0146868d6..d7bbb9c0b07f3fc0422d30df9f32a4cd9fc6ef61 100644 (file)
@@ -80,7 +80,7 @@ public class ComponentServiceUpdateKeyTest {
 
   private ProjectMeasuresIndexer projectMeasuresIndexer = new ProjectMeasuresIndexer(system2, dbClient, es.client());
   private ComponentIndexer componentIndexer = new ComponentIndexer(dbClient, es.client());
-  private ComponentService underTest = new ComponentService(dbClient, i18n, userSession, system2, new ComponentFinder(dbClient), projectMeasuresIndexer, componentIndexer);
+  private ComponentService underTest = new ComponentService(dbClient, i18n, userSession, system2, projectMeasuresIndexer, componentIndexer);
 
   @Before
   public void setUp() {
@@ -96,16 +96,16 @@ public class ComponentServiceUpdateKeyTest {
     dbSession.commit();
 
     userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
-    underTest.updateKey(dbSession, project.key(), "sample2:root");
+    underTest.updateKey(dbSession, project, "sample2:root");
     dbSession.commit();
 
     // Check project key has been updated
-    assertThat(dbClient.componentDao().selectByKey(dbSession, project.key())).isAbsent();
-    assertThat(dbClient.componentDao().selectByKey(dbSession, "sample2:root")).isPresent();
+    assertThat(db.getDbClient().componentDao().selectByKey(dbSession, project.key())).isAbsent();
+    assertThat(db.getDbClient().componentDao().selectByKey(dbSession, "sample2:root")).isNotNull();
 
     // Check file key has been updated
-    assertThat(dbClient.componentDao().selectByKey(dbSession, file.key())).isAbsent();
-    assertThat(dbClient.componentDao().selectByKey(dbSession, "sample2:root:src/File.xoo")).isPresent();
+    assertThat(db.getDbClient().componentDao().selectByKey(dbSession, file.key())).isAbsent();
+    assertThat(db.getDbClient().componentDao().selectByKey(dbSession, "sample2:root:src/File.xoo")).isNotNull();
 
     assertThat(dbClient.componentDao().selectByKey(dbSession, inactiveFile.getKey())).isPresent();
 
@@ -122,7 +122,7 @@ public class ComponentServiceUpdateKeyTest {
     dbSession.commit();
     userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
 
-    underTest.updateKey(dbSession, module.key(), "sample:root2:module");
+    underTest.updateKey(dbSession, module, "sample:root2:module");
     dbSession.commit();
 
     assertThat(dbClient.componentDao().selectByKey(dbSession, project.key())).isPresent();
@@ -139,7 +139,7 @@ public class ComponentServiceUpdateKeyTest {
     dbSession.commit();
 
     userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, provisionedProject.uuid());
-    underTest.updateKey(dbSession, provisionedProject.key(), "provisionedProject2");
+    underTest.updateKey(dbSession, provisionedProject, "provisionedProject2");
     dbSession.commit();
 
     assertComponentKeyHasBeenUpdated(provisionedProject.key(), "provisionedProject2");
@@ -153,7 +153,7 @@ public class ComponentServiceUpdateKeyTest {
     ComponentDto project = insertSampleRootProject();
     userSession.login("john").addProjectUuidPermissions(UserRole.USER, project.uuid());
 
-    underTest.updateKey(dbSession, project.key(), "sample2:root");
+    underTest.updateKey(dbSession, project, "sample2:root");
   }
 
   @Test
@@ -165,7 +165,7 @@ public class ComponentServiceUpdateKeyTest {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Impossible to update key: a component with key \"" + anotherProject.key() + "\" already exists.");
 
-    underTest.updateKey(dbSession, project.key(), anotherProject.key());
+    underTest.updateKey(dbSession, project, anotherProject.key());
   }
 
   @Test
@@ -176,7 +176,7 @@ public class ComponentServiceUpdateKeyTest {
     expectedException.expect(BadRequestException.class);
     expectedException.expectMessage("Malformed key for ''. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
 
-    underTest.updateKey(dbSession, project.key(), "");
+    underTest.updateKey(dbSession, project, "");
   }
 
   @Test
@@ -187,7 +187,7 @@ public class ComponentServiceUpdateKeyTest {
     expectedException.expect(BadRequestException.class);
     expectedException.expectMessage("Malformed key for 'sample?root'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
 
-    underTest.updateKey(dbSession, project.key(), "sample?root");
+    underTest.updateKey(dbSession, project, "sample?root");
   }
 
   @Test
@@ -199,7 +199,7 @@ public class ComponentServiceUpdateKeyTest {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Component updated must be a module or a key");
 
-    underTest.updateKey(dbSession, file.key(), "file:key");
+    underTest.updateKey(dbSession, file, "file:key");
   }
 
   @Test
index 1158499318ae5d68cbb20a551097f49520b4fe98..c85c582c25f55efc503d5c81a91a19e8020d9c07 100644 (file)
@@ -29,7 +29,6 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.ResourceDao;
 import org.sonar.server.component.index.ComponentIndexDefinition;
 import org.sonar.server.component.index.ComponentIndexer;
 import org.sonar.server.es.EsTester;
@@ -68,7 +67,7 @@ public class DefaultRubyComponentServiceTest {
   private DbClient dbClient = db.getDbClient();
   private DbSession dbSession = db.getSession();
 
-  private ComponentService componentService = new ComponentService(dbClient, i18n, userSession, system2, new ComponentFinder(dbClient),
+  private ComponentService componentService = new ComponentService(dbClient, i18n, userSession, system2,
     new ProjectMeasuresIndexer(system2, dbClient, es.client()), new ComponentIndexer(dbClient, es.client()));
   private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
   private FavoriteUpdater favoriteUpdater = mock(FavoriteUpdater.class);
index 3362b65d36dff5ffd735a858bbf621842705c71b..0c468c869485c17c9331cc6583d3f624b682b690 100644 (file)
@@ -93,7 +93,7 @@ public class BulkUpdateKeyActionTest {
 
   private WsActionTester ws = new WsActionTester(
     new BulkUpdateKeyAction(dbClient, componentFinder,
-      new ComponentService(dbClient, null, null, null, null, new ProjectMeasuresIndexer(system2, dbClient, es.client()), new ComponentIndexer(dbClient, es.client())),
+      new ComponentService(dbClient, null, null, null, new ProjectMeasuresIndexer(system2, dbClient, es.client()), new ComponentIndexer(dbClient, es.client())),
       userSession));
 
   @Before
index 15db560356c26878cf413e963e767a480f1789ed..8ee5a3d7049dafd59cf1db8e09c3466e8aa02d04 100644 (file)
@@ -125,7 +125,7 @@ public class UpdateKeyActionTest {
   }
 
   private void assertCallComponentService(@Nullable String oldKey, @Nullable String newKey) {
-    verify(componentService).updateKey(any(DbSession.class), eq(oldKey), eq(newKey));
+    verify(componentService).updateKey(any(DbSession.class), any(ComponentDto.class), eq(newKey));
   }
 
   private ComponentDto insertProject() {
diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java
new file mode 100644 (file)
index 0000000..e69de29
index 9e854d0f2b80ebeba86168ef9ed028539219ff90..ff74681ba94a5a0fff24deb2650701672d957b93 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.server.qualityprofile.ws;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
-import org.sonar.api.config.MapSettings;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbClient;
@@ -34,9 +33,7 @@ import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.qualityprofile.QualityProfileDbTester;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.es.EsTester;
 import org.sonar.server.language.LanguageTesting;
-import org.sonar.server.measure.index.ProjectMeasuresIndexDefinition;
 import org.sonar.server.qualityprofile.QProfileLookup;
 import org.sonar.server.qualityprofile.QProfileName;
 import org.sonar.server.qualityprofile.QProfileProjectOperations;
@@ -58,9 +55,6 @@ public class AddProjectActionTest {
   @Rule
   public DbTester dbTester = DbTester.create(system2);
 
-  @Rule
-  public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
-
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
 
index 28d13b49e269b8b704e8bd42e86bd6171898b3c2..bcaa5df67f787d2313a4f9a51b9e56a0d3916d92 100644 (file)
@@ -64,8 +64,8 @@ public class QProfilesWsTest {
     controller = new WsTester(new QProfilesWs(
       new RuleActivationActions(profileService),
       new BulkRuleActivationActions(profileService, null, i18n, userSessionRule),
-      new AddProjectAction(projectAssociationParameters, null, null, null),
-      new RemoveProjectAction(projectAssociationParameters, null, null, null),
+      new AddProjectAction(projectAssociationParameters, null, null, dbClient),
+      new RemoveProjectAction(projectAssociationParameters, null, null, dbClient),
       new CreateAction(null, null, null, languages, importers, userSessionRule, null),
       new ImportersAction(importers),
       new RestoreBuiltInAction(null, languages, userSessionRule),