Skip to content

Commit

Permalink
SONAR-6123 Update generation of module UUID path on views and developers
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Lancelot authored and jblievremont committed Feb 13, 2015
1 parent d11e107 commit 13c31ee
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ public ComponentDto getNullableByUuid(String uuid) {
}

public void updateKey(String projectOrModuleKey, String newKey) {
UserSession.get().checkComponentPermission(UserRole.ADMIN, projectOrModuleKey);

DbSession session = dbClient.openSession(false);
try {
ComponentDto projectOrModule = getByKey(session, projectOrModuleKey);
UserSession.get().checkProjectUuidPermission(UserRole.ADMIN, projectOrModule.projectUuid());
resourceKeyUpdaterDao.updateKey(projectOrModule.getId(), newKey);
session.commit();

Expand All @@ -121,23 +120,22 @@ public void updateKey(String projectOrModuleKey, String newKey) {
}

public Map<String, String> checkModuleKeysBeforeRenaming(String projectKey, String stringToReplace, String replacementString) {
UserSession.get().checkProjectPermission(UserRole.ADMIN, projectKey);
DbSession session = dbClient.openSession(false);
try {
ComponentDto project = getByKey(projectKey);
UserSession.get().checkProjectUuidPermission(UserRole.ADMIN, project.projectUuid());
return resourceKeyUpdaterDao.checkModuleKeysBeforeRenaming(project.getId(), stringToReplace, replacementString);
} finally {
session.close();
}
}

public void bulkUpdateKey(String projectKey, String stringToReplace, String replacementString) {
UserSession.get().checkProjectPermission(UserRole.ADMIN, projectKey);

// Open a batch session
DbSession session = dbClient.openSession(true);
try {
ComponentDto project = getByKey(session, projectKey);
UserSession.get().checkProjectUuidPermission(UserRole.ADMIN, project.projectUuid());
resourceKeyUpdaterDao.bulkUpdateKey(session, project.getId(), stringToReplace, replacementString);
session.commit();
} finally {
Expand All @@ -163,6 +161,8 @@ public String create(NewComponent newComponent) {
ComponentDto component = dbClient.componentDao().insert(session,
new ComponentDto()
.setUuid(uuid)
.setModuleUuid(null)
.setModuleUuidPath(ComponentDto.MODULE_UUID_PATH_SEP + uuid + ComponentDto.MODULE_UUID_PATH_SEP)
.setProjectUuid(uuid)
.setKey(keyWithBranch)
.setDeprecatedKey(keyWithBranch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

/**
* It's not possible to replace usage of ServerTester by DbTester because of the usage of {@link InternalPermissionService}.
* Maybe we should create a mock of the permission service ?
*/
public class ComponentServiceMediumTest {

@ClassRule
Expand Down Expand Up @@ -104,7 +108,7 @@ public void update_project_key() throws Exception {

session.commit();

MockUserSession.set().setLogin("john").addComponentPermission(UserRole.ADMIN, project.key(), project.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
service.updateKey(project.key(), "sample2:root");
session.commit();

Expand All @@ -128,7 +132,7 @@ public void update_module_key() throws Exception {

session.commit();

MockUserSession.set().setLogin("john").addComponentPermission(UserRole.ADMIN, project.key(), module.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
service.updateKey(module.key(), "sample:root2:module");
session.commit();

Expand All @@ -151,7 +155,7 @@ public void update_provisioned_project_key() throws Exception {

session.commit();

MockUserSession.set().setLogin("john").addComponentPermission(UserRole.ADMIN, provisionedProject.key(), provisionedProject.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.ADMIN, provisionedProject.uuid());
service.updateKey(provisionedProject.key(), "provisionedProject2");
session.commit();

Expand All @@ -163,7 +167,7 @@ public void update_provisioned_project_key() throws Exception {
@Test(expected = ForbiddenException.class)
public void fail_to_update_project_key_without_admin_permission() throws Exception {
ComponentDto project = createProject("sample:root");
MockUserSession.set().setLogin("john").addComponentPermission(UserRole.USER, project.key(), project.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.USER, project.uuid());
service.updateKey(project.key(), "sample2:root");
}

Expand All @@ -178,7 +182,7 @@ public void check_module_keys_before_renaming() throws Exception {

session.commit();

MockUserSession.set().setLogin("john").addProjectPermissions(UserRole.ADMIN, project.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
Map<String, String> result = service.checkModuleKeysBeforeRenaming(project.key(), "sample", "sample2");

assertThat(result).hasSize(2);
Expand All @@ -197,7 +201,7 @@ public void check_module_keys_before_renaming_return_duplicate_key() throws Exce

session.commit();

MockUserSession.set().setLogin("john").addProjectPermissions(UserRole.ADMIN, project.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
Map<String, String> result = service.checkModuleKeysBeforeRenaming(project.key(), "sample:root", "foo");

assertThat(result).hasSize(2);
Expand All @@ -208,7 +212,7 @@ public void check_module_keys_before_renaming_return_duplicate_key() throws Exce
@Test(expected = ForbiddenException.class)
public void fail_to_check_module_keys_before_renaming_without_admin_permission() throws Exception {
ComponentDto project = createProject("sample:root");
MockUserSession.set().setLogin("john").addComponentPermission(UserRole.USER, project.key(), project.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.USER, project.uuid());
service.checkModuleKeysBeforeRenaming(project.key(), "sample", "sample2");
}

Expand All @@ -223,7 +227,7 @@ public void bulk_update_project_key() throws Exception {

session.commit();

MockUserSession.set().setLogin("john").addProjectPermissions(UserRole.ADMIN, project.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
service.bulkUpdateKey(project.key(), "sample", "sample2");
session.commit();

Expand All @@ -247,7 +251,7 @@ public void bulk_update_provisioned_project_key() throws Exception {

session.commit();

MockUserSession.set().setLogin("john").addComponentPermission(UserRole.ADMIN, provisionedProject.key(), provisionedProject.key());
MockUserSession.set().setLogin("john").addProjectUuidPermissions(UserRole.ADMIN, provisionedProject.uuid());
service.bulkUpdateKey(provisionedProject.key(), "provisionedProject", "provisionedProject2");
session.commit();

Expand Down Expand Up @@ -276,7 +280,7 @@ public void create_project() throws Exception {
assertThat(project.uuid()).isNotNull();
assertThat(project.projectUuid()).isEqualTo(project.uuid());
assertThat(project.moduleUuid()).isNull();
assertThat(project.moduleUuidPath()).isNull();
assertThat(project.moduleUuidPath()).isEqualTo("." + project.uuid() + ".");
assertThat(project.name()).isEqualTo("Struts project");
assertThat(project.longName()).isEqualTo("Struts project");
assertThat(project.scope()).isEqualTo("PRJ");
Expand Down Expand Up @@ -309,7 +313,7 @@ public void create_view() throws Exception {
assertThat(project.uuid()).isNotNull();
assertThat(project.projectUuid()).isEqualTo(project.uuid());
assertThat(project.moduleUuid()).isNull();
assertThat(project.moduleUuidPath()).isNull();
assertThat(project.moduleUuidPath()).isEqualTo("." + project.uuid() + ".");
assertThat(project.name()).isEqualTo("All Projects");
assertThat(project.longName()).isEqualTo("All Projects");
assertThat(project.scope()).isEqualTo("PRJ");
Expand Down
7 changes: 3 additions & 4 deletions sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
*/
package org.sonar.core.user;

import org.sonar.core.persistence.DaoComponent;

import com.google.common.base.Function;
import com.google.common.base.Strings;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.BatchComponent;
import org.sonar.api.ServerComponent;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.persistence.DaoComponent;
import org.sonar.core.persistence.DaoUtils;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.resource.ResourceDao;
Expand Down Expand Up @@ -84,9 +83,9 @@ public void insertAuthor(String login, long personId) {
public void insertAuthorAndDeveloper(String login, ResourceDto resourceDto) {
SqlSession session = mybatis.openSession(false);
try {
// Hack in order to set "." on DEV
// Hack in order to set the right module uuid path on DEVs
if (Strings.isNullOrEmpty(resourceDto.getModuleUuidPath())) {
resourceDto.setModuleUuidPath(ComponentDto.MODULE_UUID_PATH_SEP);
resourceDto.setModuleUuidPath(ComponentDto.MODULE_UUID_PATH_SEP + resourceDto.getUuid() + ComponentDto.MODULE_UUID_PATH_SEP);
}
resourceDao.insertUsingExistingSession(resourceDto, session);
insertAuthor(login, resourceDto.getId(), session);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<dataset>
<projects id="1" name="developer@company.net" qualifier="DEV" uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="."/>
<projects id="1" name="developer@company.net" qualifier="DEV" uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path=".ABCD."/>
<authors id="1" person_id="1" login="developer@company.net"/>

<projects id="2" name="developer2@company.net" qualifier="DEV" uuid="BCDE" project_uuid="BCDE" module_uuid="[null]" module_uuid_path="."/>
<projects id="2" name="developer2@company.net" qualifier="DEV" uuid="BCDE" project_uuid="BCDE" module_uuid="[null]" module_uuid_path=".BCDE."/>
<authors id="2" person_id="2" login="developer2@company.net"/>
</dataset>

0 comments on commit 13c31ee

Please sign in to comment.