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;
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);
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));
}
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);
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);
.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");
.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");
.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");
.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");
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()
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() {
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();
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();
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");
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
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
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
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
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
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;
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);
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
}
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() {
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;
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;
@Rule
public DbTester dbTester = DbTester.create(system2);
- @Rule
- public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
-
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
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),