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;
}
}
- 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());
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) {
*/
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;
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;
@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")))
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;
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;
}
}
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();
}
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;
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;
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;
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 {
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");
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");
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");
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
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
dbClient.componentDao().insert(dbSession, project);
dbSession.commit();
- underTest.create(NewComponent.create("struts", "Struts project"));
+ underTest.create(dbSession, NewComponent.create("struts", "Struts project"));
}
@Test
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);
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);
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"));
}
}
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 {
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
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();
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");
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
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);
}
*/
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
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");
- }
}
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;
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() {