]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8223 Clean up ComponentService
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 13 Oct 2016 12:44:39 +0000 (14:44 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 17 Oct 2016 13:35:54 +0000 (15:35 +0200)
Remove no more used code
BulkUpdateKeyAction is now using ComponentService for the bulk update of key

server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java
server/sonar-server/src/main/java/org/sonar/server/component/DefaultRubyComponentService.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/BulkUpdateKeyAction.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

index 59aa3d0b31fb96d8dc4f9b446ec5bec494583765..e9d7ec06212d6b76876f0c33811426eaf4fec13e 100644 (file)
@@ -27,7 +27,6 @@ import java.util.Collection;
 import java.util.Date;
 import java.util.List;
 import java.util.Locale;
-import java.util.Map;
 import java.util.Set;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
@@ -108,15 +107,6 @@ public class ComponentService {
     }
   }
 
-  public void updateKey(String projectOrModuleKey, String newKey) {
-    DbSession dbSession = dbClient.openSession(false);
-    try {
-      updateKey(dbSession, projectOrModuleKey, newKey);
-    } finally {
-      dbSession.close();
-    }
-  }
-
   public void updateKey(DbSession dbSession, String projectOrModuleKey, String newKey) {
     ComponentDto component = componentFinder.getByKey(dbSession, projectOrModuleKey);
     userSession.checkComponentUuidPermission(UserRole.ADMIN, component.projectUuid());
@@ -126,44 +116,8 @@ public class ComponentService {
     dbClient.componentKeyUpdaterDao().updateKey(component.uuid(), newKey);
   }
 
-  public Map<String, String> checkModuleKeysBeforeRenaming(String projectKey, String stringToReplace, String replacementString) {
-    DbSession session = dbClient.openSession(false);
-    try {
-      ComponentDto project = getByKey(projectKey);
-      userSession.checkComponentUuidPermission(UserRole.ADMIN, project.projectUuid());
-      return dbClient.componentKeyUpdaterDao().checkModuleKeysBeforeRenaming(project.uuid(), stringToReplace, replacementString);
-    } finally {
-      session.close();
-    }
-  }
-
-  public void bulkUpdateKey(DbSession dbSession, String projectKey, String stringToReplace, String replacementString) {
-    ComponentDto project = getByKey(dbSession, projectKey);
-    userSession.checkComponentUuidPermission(UserRole.ADMIN, project.projectUuid());
-    checkIsProjectOrModule(project);
-    dbClient.componentKeyUpdaterDao().bulkUpdateKey(dbSession, project.uuid(), stringToReplace, replacementString);
-  }
-
-  public void bulkUpdateKey(String projectKey, String stringToReplace, String replacementString) {
-    // Open a batch session
-    DbSession dbSession = dbClient.openSession(true);
-    try {
-      bulkUpdateKey(dbSession, projectKey, stringToReplace, replacementString);
-      dbSession.commit();
-    } finally {
-      dbSession.close();
-    }
-  }
-
-  public ComponentDto create(NewComponent newComponent) {
-    userSession.checkPermission(GlobalPermissions.PROVISIONING);
-
-    DbSession session = dbClient.openSession(false);
-    try {
-      return create(session, newComponent);
-    } finally {
-      dbClient.closeSession(session);
-    }
+  public void bulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) {
+    dbClient.componentKeyUpdaterDao().bulkUpdateKey(dbSession, projectUuid, stringToReplace, replacementString);
   }
 
   public ComponentDto create(DbSession session, NewComponent newComponent) {
index 5d0611a6338a518cb917d4850d6747be784b8788..5b633541ee93bb0cead440c6914bd31cb12b538e 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.component;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Strings;
 import java.util.List;
 import java.util.Map;
@@ -27,21 +26,23 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.api.component.Component;
 import org.sonar.api.component.RubyComponentService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ResourceDao;
 import org.sonar.db.component.ResourceDto;
 import org.sonar.server.permission.PermissionService;
 import org.sonar.server.util.RubyUtils;
 
-import static org.sonar.server.ws.WsUtils.checkRequest;
-
 public class DefaultRubyComponentService implements RubyComponentService {
 
+  private final DbClient dbClient;
   private final ResourceDao resourceDao;
   private final ComponentService componentService;
   private final PermissionService permissionService;
 
-  public DefaultRubyComponentService(ResourceDao resourceDao, ComponentService componentService, PermissionService permissionService) {
+  public DefaultRubyComponentService(DbClient dbClient, ResourceDao resourceDao, ComponentService componentService, PermissionService permissionService) {
+    this.dbClient = dbClient;
     this.resourceDao = resourceDao;
     this.componentService = componentService;
     this.permissionService = permissionService;
@@ -50,51 +51,42 @@ public class DefaultRubyComponentService implements RubyComponentService {
   @Override
   @CheckForNull
   public Component findByKey(String key) {
-    return resourceDao.selectByKey(key);
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      return dbClient.componentDao().selectByKey(dbSession, key).orNull();
+    }
   }
 
+  // Used in rails
   @CheckForNull
   public Component findByUuid(String uuid) {
-    Optional<ComponentDto> componentOptional = componentService.getByUuid(uuid);
-    return componentOptional.isPresent() ? componentOptional.get() : null;
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      return dbClient.componentDao().selectByUuid(dbSession, uuid).orNull();
+    }
   }
 
-  /**
-   * Be careful when updating this method, it's used by the Views plugin
-   */
+  // Used in GOV
   @CheckForNull
   public Long createComponent(String key, String name, String qualifier) {
     return createComponent(key, null, name, qualifier);
   }
 
+  // Used in rails
   @CheckForNull
   public Long createComponent(String key, @Nullable String branch, String name, @Nullable String qualifier) {
-    ComponentDto provisionedComponent = componentService.create(NewComponent.create(key, name).setQualifier(qualifier).setBranch(branch));
-    checkRequest(provisionedComponent != null, "Component not created: %s", key);
-    ComponentDto componentInDb = (ComponentDto) resourceDao.selectByKey(provisionedComponent.getKey());
-    checkRequest(componentInDb != null, "Component not created: %s", key);
-
-    permissionService.applyDefaultPermissionTemplate(componentInDb.getKey());
-    return componentInDb.getId();
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      ComponentDto provisionedComponent = componentService.create(dbSession, NewComponent.create(key, name).setQualifier(qualifier).setBranch(branch));
+      permissionService.applyDefaultPermissionTemplate(dbSession, provisionedComponent.getKey());
+      dbSession.commit();
+      return provisionedComponent.getId();
+    }
   }
 
+  // Used in GOV
   public List<ResourceDto> findProvisionedProjects(Map<String, Object> params) {
     ComponentQuery query = toQuery(params);
     return resourceDao.selectProvisionedProjects(query.qualifiers());
   }
 
-  public void updateKey(String projectOrModuleKey, String newKey) {
-    componentService.updateKey(projectOrModuleKey, newKey);
-  }
-
-  public Map<String, String> checkModuleKeysBeforeRenaming(String projectKey, String stringToReplace, String replacementString) {
-    return componentService.checkModuleKeysBeforeRenaming(projectKey, stringToReplace, replacementString);
-  }
-
-  public void bulkUpdateKey(String projectKey, String stringToReplace, String replacementString) {
-    componentService.bulkUpdateKey(projectKey, stringToReplace, replacementString);
-  }
-
   static ComponentQuery toQuery(Map<String, Object> props) {
     ComponentQuery.Builder builder = ComponentQuery.builder()
       .keys(RubyUtils.toStrings(props.get("keys")))
index b5eeb685e469b489b7f7da22343dd9037f241ffd..c735e7afe99ad8191a0c3240f2f2bdb101e0c7d9 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentKeyUpdaterDao;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.component.ComponentFinder.ParamNames;
+import org.sonar.server.component.ComponentService;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.WsComponents;
 import org.sonarqube.ws.WsComponents.BulkUpdateKeyWsResponse;
@@ -51,12 +52,14 @@ public class BulkUpdateKeyAction implements ComponentsWsAction {
   private final DbClient dbClient;
   private final ComponentFinder componentFinder;
   private final ComponentKeyUpdaterDao componentKeyUpdater;
+  private final ComponentService componentService;
   private final UserSession userSession;
 
-  public BulkUpdateKeyAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession) {
+  public BulkUpdateKeyAction(DbClient dbClient, ComponentFinder componentFinder, ComponentService componentService, UserSession userSession) {
     this.dbClient = dbClient;
     this.componentKeyUpdater = dbClient.componentKeyUpdaterDao();
     this.componentFinder = componentFinder;
+    this.componentService = componentService;
     this.userSession = userSession;
   }
 
@@ -141,7 +144,7 @@ public class BulkUpdateKeyAction implements ComponentsWsAction {
   }
 
   private void bulkUpdateKey(DbSession dbSession, BulkUpdateWsRequest request, ComponentDto projectOrModule) {
-    componentKeyUpdater.bulkUpdateKey(dbSession, projectOrModule.uuid(), request.getFrom(), request.getTo());
+    componentService.bulkUpdateKey(dbSession, projectOrModule.uuid(), request.getFrom(), request.getTo());
     dbSession.commit();
   }
 
index 6e56f4981ce18f596789f958900bf8dea3b0735a..9e074d6dda24fd200a5b82a0d0f3f3a428a383f2 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.component;
 
 import com.google.common.base.Optional;
 import java.util.Arrays;
-import java.util.Map;
 import org.assertj.core.api.Fail;
 import org.junit.Before;
 import org.junit.Rule;
@@ -29,8 +28,6 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
@@ -40,7 +37,6 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.component.ResourceIndexDao;
 import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.i18n.I18nRule;
 import org.sonar.server.tester.UserSessionRule;
@@ -56,6 +52,7 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
+import static org.sonar.db.component.ComponentTesting.newModuleDto;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
 
 public class ComponentServiceTest {
@@ -108,59 +105,11 @@ public class ComponentServiceTest {
     assertThat(underTest.getByUuid("unknown")).isAbsent();
   }
 
-  @Test
-  public void check_module_keys_before_renaming() {
-    ComponentDto project = insertSampleProject();
-    ComponentDto module = ComponentTesting.newModuleDto(project).setKey("sample:root:module");
-    dbClient.componentDao().insert(dbSession, module);
-
-    ComponentDto file = newFileDto(module, null).setKey("sample:root:module:src/File.xoo");
-    dbClient.componentDao().insert(dbSession, file);
-
-    dbSession.commit();
-
-    userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
-    Map<String, String> result = underTest.checkModuleKeysBeforeRenaming(project.key(), "sample", "sample2");
-
-    assertThat(result).hasSize(2);
-    assertThat(result.get("sample:root")).isEqualTo("sample2:root");
-    assertThat(result.get("sample:root:module")).isEqualTo("sample2:root:module");
-  }
-
-  @Test
-  public void check_module_keys_before_renaming_return_duplicate_key() {
-    ComponentDto project = insertSampleProject();
-    ComponentDto module = ComponentTesting.newModuleDto(project).setKey("sample:root:module");
-    dbClient.componentDao().insert(dbSession, module);
-
-    ComponentDto module2 = ComponentTesting.newModuleDto(project).setKey("foo:module");
-    dbClient.componentDao().insert(dbSession, module2);
-
-    dbSession.commit();
-
-    userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
-    Map<String, String> result = underTest.checkModuleKeysBeforeRenaming(project.key(), "sample:root", "foo");
-
-    assertThat(result).hasSize(2);
-    assertThat(result.get("sample:root")).isEqualTo("foo");
-    assertThat(result.get("sample:root:module")).isEqualTo("#duplicate_key#");
-  }
-
-  @Test
-  public void fail_to_check_module_keys_before_renaming_without_admin_permission() {
-    expectedException.expect(ForbiddenException.class);
-
-    ComponentDto project = insertSampleProject();
-    userSession.login("john").addProjectUuidPermissions(UserRole.USER, project.uuid());
-
-    underTest.checkModuleKeysBeforeRenaming(project.key(), "sample", "sample2");
-  }
-
   @Test
   public void create_project() {
     userSession.login("john").setGlobalPermissions(PROVISIONING);
 
-    String key = underTest.create(NewComponent.create("struts", "Struts project")).getKey();
+    String key = underTest.create(dbSession, NewComponent.create("struts", "Struts project")).getKey();
 
     ComponentDto project = underTest.getNullableByKey(key);
     assertThat(project.key()).isEqualTo("struts");
@@ -180,7 +129,7 @@ public class ComponentServiceTest {
   public void create_new_project_with_branch() {
     userSession.login("john").setGlobalPermissions(PROVISIONING);
 
-    String key = underTest.create(NewComponent.create("struts", "Struts project").setBranch("origin/branch")).getKey();
+    String key = underTest.create(dbSession, NewComponent.create("struts", "Struts project").setBranch("origin/branch")).getKey();
 
     ComponentDto project = underTest.getNullableByKey(key);
     assertThat(project.key()).isEqualTo("struts:origin/branch");
@@ -191,7 +140,7 @@ public class ComponentServiceTest {
   public void create_view() {
     userSession.login("john").setGlobalPermissions(PROVISIONING);
 
-    String key = underTest.create(NewComponent.create("all-project", "All Projects").setQualifier(Qualifiers.VIEW)).getKey();
+    String key = underTest.create(dbSession, NewComponent.create("all-project", "All Projects").setQualifier(Qualifiers.VIEW)).getKey();
 
     ComponentDto project = underTest.getNullableByKey(key);
     assertThat(project.key()).isEqualTo("all-project");
@@ -235,7 +184,7 @@ public class ComponentServiceTest {
     expectedException.expect(BadRequestException.class);
     expectedException.expectMessage("Malformed key for Project: struts?parent. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
 
-    underTest.create(NewComponent.create("struts?parent", "Struts project"));
+    underTest.create(dbSession, NewComponent.create("struts?parent", "Struts project"));
   }
 
   @Test
@@ -245,7 +194,7 @@ public class ComponentServiceTest {
 
     userSession.login("john").setGlobalPermissions(PROVISIONING);
 
-    underTest.create(NewComponent.create("struts", "Struts project").setBranch("origin?branch"));
+    underTest.create(dbSession, NewComponent.create("struts", "Struts project").setBranch("origin?branch"));
   }
 
   @Test
@@ -258,7 +207,7 @@ public class ComponentServiceTest {
     dbClient.componentDao().insert(dbSession, project);
     dbSession.commit();
 
-    underTest.create(NewComponent.create("struts", "Struts project"));
+    underTest.create(dbSession, NewComponent.create("struts", "Struts project"));
   }
 
   @Test
@@ -270,7 +219,7 @@ public class ComponentServiceTest {
     DbSession session = mock(DbSession.class);
 
     ComponentDao componentDao = mock(ComponentDao.class);
-    when(componentDao.selectByKey(session, projectKey)).thenReturn(Optional.<ComponentDto>absent());
+    when(componentDao.selectByKey(session, projectKey)).thenReturn(Optional.absent());
 
     DbClient dbClient = mock(DbClient.class);
     when(dbClient.openSession(false)).thenReturn(session);
@@ -288,7 +237,7 @@ public class ComponentServiceTest {
       ComponentTesting.newProjectDto().setId(3L).setKey(projectKey)));
 
     underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient));
-    underTest.create(NewComponent.create(projectKey, projectKey));
+    underTest.create(session, NewComponent.create(projectKey, projectKey));
 
     verify(componentDao).delete(session, 2L);
     verify(componentDao).delete(session, 3L);
@@ -331,12 +280,34 @@ public class ComponentServiceTest {
     assertThat(underTest.componentUuids(dbSession, Arrays.asList(moduleKey, fileKey), true)).isEmpty();
   }
 
-  private ComponentDto insertSampleProject() {
-    return componentDb.insertComponent(newProjectDto().setKey("sample:root"));
+  @Test
+  public void bulk_update() {
+    ComponentDto project = componentDb.insertComponent(newProjectDto().setKey("my_project"));
+    ComponentDto module = componentDb.insertComponent(newModuleDto(project).setKey("my_project:root:module"));
+    ComponentDto inactiveModule = componentDb.insertComponent(newModuleDto(project).setKey("my_project:root:inactive_module").setEnabled(false));
+    ComponentDto file = componentDb.insertComponent(newFileDto(module, null).setKey("my_project:root:module:src/File.xoo"));
+    ComponentDto inactiveFile = componentDb.insertComponent(newFileDto(module, null).setKey("my_project:root:module:src/InactiveFile.xoo").setEnabled(false));
+
+    underTest.bulkUpdateKey(dbSession, project.uuid(), "my_", "your_");
+
+    assertComponentKeyUpdated(project.key(), "your_project");
+    assertComponentKeyUpdated(module.key(), "your_project:root:module");
+    assertComponentKeyUpdated(file.key(), "your_project:root:module:src/File.xoo");
+    assertComponentKeyNotUpdated(inactiveModule.key());
+    assertComponentKeyNotUpdated(inactiveFile.key());
+  }
+
+  private void assertComponentKeyUpdated(String oldKey, String newKey) {
+    assertThat(dbClient.componentDao().selectByKey(dbSession, oldKey)).isAbsent();
+    assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent();
   }
 
-  private void setGlobalAdminPermission() {
-    userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+  private void assertComponentKeyNotUpdated(String key) {
+    assertThat(dbClient.componentDao().selectByKey(dbSession, key)).isPresent();
+  }
+
+  private ComponentDto insertSampleProject() {
+    return componentDb.insertComponent(newProjectDto().setKey("sample:root"));
   }
 
 }
index 808ab9f8eae9d400d396437df79d07ecdf6559b8..327e07be42c86a2de4cd2c958079766df22b42b5 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.server.tester.UserSessionRule;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.guava.api.Assertions.assertThat;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
+import static org.sonar.db.component.ComponentTesting.newModuleDto;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
 
 public class ComponentServiceUpdateKeyTest {
@@ -76,7 +77,7 @@ public class ComponentServiceUpdateKeyTest {
     dbSession.commit();
 
     userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
-    underTest.updateKey(project.key(), "sample2:root");
+    underTest.updateKey(dbSession, project.key(), "sample2:root");
     dbSession.commit();
 
     // Check project key has been updated
@@ -102,7 +103,7 @@ public class ComponentServiceUpdateKeyTest {
     dbSession.commit();
 
     userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
-    underTest.updateKey(module.key(), "sample:root2:module");
+    underTest.updateKey(dbSession, module.key(), "sample:root2:module");
     dbSession.commit();
 
     assertThat(dbClient.componentDao().selectByKey(dbSession, project.key())).isPresent();
@@ -119,7 +120,7 @@ public class ComponentServiceUpdateKeyTest {
     dbSession.commit();
 
     userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, provisionedProject.uuid());
-    underTest.updateKey(provisionedProject.key(), "provisionedProject2");
+    underTest.updateKey(dbSession, provisionedProject.key(), "provisionedProject2");
     dbSession.commit();
 
     assertComponentKeyHasBeenUpdated(provisionedProject.key(), "provisionedProject2");
@@ -132,7 +133,7 @@ public class ComponentServiceUpdateKeyTest {
     ComponentDto project = insertSampleRootProject();
     userSession.login("john").addProjectUuidPermissions(UserRole.USER, project.uuid());
 
-    underTest.updateKey(project.key(), "sample2:root");
+    underTest.updateKey(dbSession, project.key(), "sample2:root");
   }
 
   @Test
@@ -181,6 +182,32 @@ public class ComponentServiceUpdateKeyTest {
     underTest.updateKey(dbSession, file.key(), "file:key");
   }
 
+  @Test
+  public void bulk_update_key() {
+    ComponentDto project = componentDb.insertComponent(newProjectDto().setKey("my_project"));
+    ComponentDto module = componentDb.insertComponent(newModuleDto(project).setKey("my_project:root:module"));
+    ComponentDto inactiveModule = componentDb.insertComponent(newModuleDto(project).setKey("my_project:root:inactive_module").setEnabled(false));
+    ComponentDto file = componentDb.insertComponent(newFileDto(module, null).setKey("my_project:root:module:src/File.xoo"));
+    ComponentDto inactiveFile = componentDb.insertComponent(newFileDto(module, null).setKey("my_project:root:module:src/InactiveFile.xoo").setEnabled(false));
+
+    underTest.bulkUpdateKey(dbSession, project.uuid(), "my_", "your_");
+
+    assertComponentKeyUpdated(project.key(), "your_project");
+    assertComponentKeyUpdated(module.key(), "your_project:root:module");
+    assertComponentKeyUpdated(file.key(), "your_project:root:module:src/File.xoo");
+    assertComponentKeyNotUpdated(inactiveModule.key());
+    assertComponentKeyNotUpdated(inactiveFile.key());
+  }
+
+  private void assertComponentKeyUpdated(String oldKey, String newKey) {
+    assertThat(dbClient.componentDao().selectByKey(dbSession, oldKey)).isAbsent();
+    assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent();
+  }
+
+  private void assertComponentKeyNotUpdated(String key) {
+    assertThat(dbClient.componentDao().selectByKey(dbSession, key)).isPresent();
+  }
+
   private void setGlobalAdminPermission() {
     userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
   }
index d19252b2a41e958200056092cfc55a0fb78a5153..1179868fc10abac4788fa13e4c170c1ef7693781 100644 (file)
  */
 package org.sonar.server.component;
 
-import com.google.common.base.Optional;
 import java.util.List;
 import java.util.Map;
-import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
-import org.mockito.ArgumentCaptor;
-import org.sonar.api.component.Component;
 import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.component.ResourceDao;
+import org.sonar.db.component.ResourceDto;
 import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.i18n.I18nRule;
 import org.sonar.server.permission.PermissionService;
+import org.sonar.server.tester.UserSessionRule;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Maps.newHashMap;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.any;
-import static org.mockito.Mockito.anyListOf;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
 
 public class DefaultRubyComponentServiceTest {
 
-  ResourceDao resourceDao = mock(ResourceDao.class);
-  ComponentService componentService = mock(ComponentService.class);
+  @Rule
+  public DbTester db = DbTester.create(System2.INSTANCE);
+
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+
+  I18nRule i18n = new I18nRule();
+
+  DbClient dbClient = db.getDbClient();
+  DbSession dbSession = db.getSession();
+
+  ResourceDao resourceDao = dbClient.resourceDao();
+  ComponentService componentService = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient));
   PermissionService permissionService = mock(PermissionService.class);
 
-  DefaultRubyComponentService service;
+  ComponentDbTester componentDb = new ComponentDbTester(db);
 
-  @Before
-  public void before() {
-    service = new DefaultRubyComponentService(resourceDao, componentService, permissionService);
-  }
+  DefaultRubyComponentService service = new DefaultRubyComponentService(dbClient, resourceDao, componentService, permissionService);
 
   @Test
   public void find_by_key() {
-    Component component = mock(Component.class);
-    when(resourceDao.selectByKey("struts")).thenReturn(component);
+    ComponentDto componentDto = componentDb.insertProject();
 
-    assertThat(service.findByKey("struts")).isEqualTo(component);
+    assertThat(service.findByKey(componentDto.getKey())).isNotNull();
   }
 
   @Test
   public void find_by_uuid() {
-    ComponentDto component = new ComponentDto();
-    when(componentService.getByUuid("ABCD")).thenReturn(Optional.of(component));
+    ComponentDto componentDto = componentDb.insertProject();
 
-    assertThat(service.findByUuid("ABCD")).isEqualTo(component);
+    assertThat(service.findByUuid(componentDto.uuid())).isNotNull();
   }
 
   @Test
   public void not_find_by_uuid() {
-    when(componentService.getByUuid("ABCD")).thenReturn(Optional.<ComponentDto>absent());
+    componentDb.insertProject();
 
-    assertThat(service.findByUuid("ABCD")).isNull();
+    assertThat(service.findByUuid("UNKNOWN")).isNull();
   }
 
   @Test
   public void create_component() {
+    userSession.login("john").setGlobalPermissions(PROVISIONING);
     String componentKey = "new-project";
     String componentName = "New Project";
     String qualifier = Qualifiers.PROJECT;
-    ComponentDto projectDto = ComponentTesting.newProjectDto().setKey(componentKey);
-    when(resourceDao.selectByKey(componentKey)).thenReturn(projectDto);
-    when(componentService.create(any(NewComponent.class))).thenReturn(projectDto);
-
-    service.createComponent(componentKey, componentName, qualifier);
 
-    ArgumentCaptor<NewComponent> newComponentArgumentCaptor = ArgumentCaptor.forClass(NewComponent.class);
+    Long result = service.createComponent(componentKey, componentName, qualifier);
 
-    verify(componentService).create(newComponentArgumentCaptor.capture());
-    NewComponent newComponent = newComponentArgumentCaptor.getValue();
-    assertThat(newComponent.key()).isEqualTo(componentKey);
-    assertThat(newComponent.name()).isEqualTo(componentName);
-    assertThat(newComponent.branch()).isNull();
-    assertThat(newComponent.qualifier()).isEqualTo(Qualifiers.PROJECT);
-
-    verify(permissionService).applyDefaultPermissionTemplate(componentKey);
-  }
-
-  @Test(expected = BadRequestException.class)
-  public void should_throw_exception_if_create_fails() {
-    String componentKey = "new-project";
-    String componentName = "New Project";
-    String qualifier = Qualifiers.PROJECT;
-    when(resourceDao.selectByKey(componentKey)).thenReturn(null);
-
-    service.createComponent(componentKey, componentName, qualifier);
+    ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, componentKey);
+    assertThat(project.key()).isEqualTo(componentKey);
+    assertThat(project.name()).isEqualTo(componentName);
+    assertThat(project.qualifier()).isEqualTo(qualifier);
+    assertThat(project.getId()).isEqualTo(result);
+    verify(permissionService).applyDefaultPermissionTemplate(any(DbSession.class), eq(componentKey));
   }
 
   @Test(expected = BadRequestException.class)
   public void should_throw_if_malformed_key1() {
+    userSession.login("john").setGlobalPermissions(PROVISIONING);
     service.createComponent("1234", "New Project", Qualifiers.PROJECT);
   }
 
   @Test
   public void should_find_provisioned_projects() {
+    componentDb.insertProject();
     List<String> qualifiers = newArrayList("TRK");
-
     Map<String, Object> map = newHashMap();
     map.put("qualifiers", qualifiers);
 
-    service.findProvisionedProjects(map);
-    verify(resourceDao).selectProvisionedProjects(anyListOf(String.class));
+    List<ResourceDto> resourceDtos = service.findProvisionedProjects(map);
+    assertThat(resourceDtos).hasSize(1);
   }
 
   @Test
@@ -165,21 +160,4 @@ public class DefaultRubyComponentServiceTest {
     assertThat(query.asc()).isTrue();
   }
 
-  @Test
-  public void update_key() {
-    service.updateKey("oldKey", "newKey");
-    verify(componentService).updateKey("oldKey", "newKey");
-  }
-
-  @Test
-  public void check_module_keys_before_renaming() {
-    service.checkModuleKeysBeforeRenaming("oldKey", "old", "new");
-    verify(componentService).checkModuleKeysBeforeRenaming("oldKey", "old", "new");
-  }
-
-  @Test
-  public void bulk_update_key() {
-    service.bulkUpdateKey("oldKey", "old", "new");
-    verify(componentService).bulkUpdateKey("oldKey", "old", "new");
-  }
 }
index 1851c88fb95be2284bb6f6ddc2015c5a8fe96445..ed8cc60a78eb99fe1fdede36774082d9fb1e87a9 100644 (file)
@@ -36,6 +36,7 @@ import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.component.ComponentService;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -77,7 +78,7 @@ public class BulkUpdateKeyActionTest {
 
   ComponentFinder componentFinder = new ComponentFinder(dbClient);
 
-  WsActionTester ws = new WsActionTester(new BulkUpdateKeyAction(dbClient, componentFinder, userSession));
+  WsActionTester ws = new WsActionTester(new BulkUpdateKeyAction(dbClient, componentFinder, new ComponentService(dbClient, null, null, null, null), userSession));
 
   @Before
   public void setUp() {