From: Julien Lancelot Date: Tue, 12 Aug 2014 08:34:49 +0000 (+0200) Subject: SONAR-5417 Get batch project referentials using WS X-Git-Tag: 4.5-RC1~177 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cf786503239c86eadd59dd518833a4b8bb456e66;p=sonarqube.git SONAR-5417 Get batch project referentials using WS --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java index be4f94f5381..76b04cf40d6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java @@ -24,13 +24,13 @@ import com.google.common.collect.Maps; import org.apache.commons.io.IOUtils; import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; -import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.batch.protocol.input.ProjectReferentials; import org.sonar.core.UtcDateUtils; +import org.sonar.core.component.AuthorizedComponentDto; import org.sonar.core.component.ComponentDto; import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.persistence.DbSession; @@ -109,14 +109,21 @@ public class ProjectReferentialsAction implements RequestHandler { ProjectReferentials ref = new ProjectReferentials(); String projectKey = null; - ComponentDto module = dbClient.componentDao().getNullableByKey(session, projectOrModuleKey); + AuthorizedComponentDto module = dbClient.componentDao().getNullableAuthorizedComponentByKey(projectOrModuleKey, session); + // Current project can be null when analysing a new project if (module != null) { - ComponentDto project = !module.qualifier().equals(Qualifiers.PROJECT) ? dbClient.componentDao().getRootProjectByKey(projectOrModuleKey, session) : module; - if (!project.key().equals(module.key())) { - addSettings(ref, module.getKey(), getSettingsFromParentModules(module.key(), hasScanPerm, session)); + ComponentDto project = dbClient.componentDao().getNullableRootProjectByKey(projectOrModuleKey, session); + // Can be null if the given project is a provisioned one + if (project != null) { + if (!project.key().equals(module.key())) { + addSettings(ref, module.getKey(), getSettingsFromParents(module.key(), hasScanPerm, session)); + } + projectKey = project.key(); + addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.newHashMap(), hasScanPerm, session); + } else { + // Add settings of the provisioned project + addSettings(ref, projectOrModuleKey, getPropertiesMap(propertiesDao.selectProjectProperties(projectOrModuleKey, session), hasScanPerm)); } - projectKey = project.key(); - addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.newHashMap(), hasScanPerm, session); } addProfiles(ref, projectKey, profileName, session); @@ -129,7 +136,7 @@ public class ProjectReferentialsAction implements RequestHandler { } } - private Map getSettingsFromParentModules(String moduleKey, boolean hasScanPerm, DbSession session) { + private Map getSettingsFromParents(String moduleKey, boolean hasScanPerm, DbSession session) { List parents = newArrayList(); aggregateParentModules(moduleKey, parents, session); Collections.reverse(parents); diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java b/server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java index 5b6ff6d7ec8..376e78ec3bf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java @@ -60,8 +60,17 @@ public class ComponentDao extends BaseDao return mapper(session).countById(id) > 0; } + /** + * Return null only if the component does not exists. + * If the component if a root project, it will return itself. + */ + @CheckForNull + public ComponentDto getNullableRootProjectByKey(String componentKey, DbSession session) { + return mapper(session).selectRootProjectByKey(componentKey); + } + public ComponentDto getRootProjectByKey(String componentKey, DbSession session) { - ComponentDto componentDto = mapper(session).selectRootProjectByKey(componentKey); + ComponentDto componentDto = getNullableRootProjectByKey(componentKey, session); if (componentDto == null) { throw new NotFoundException(String.format("Root project for project '%s' not found", componentKey)); } @@ -90,6 +99,19 @@ public class ComponentDao extends BaseDao return componentDto; } + @CheckForNull + public AuthorizedComponentDto getNullableAuthorizedComponentByKey(String key, DbSession session) { + return mapper(session).selectAuthorizedComponentByKey(key); + } + + public AuthorizedComponentDto getAuthorizedComponentByKey(String key, DbSession session) { + AuthorizedComponentDto componentDto = getNullableAuthorizedComponentByKey(key, session); + if (componentDto == null) { + throw new NotFoundException(String.format("Project with key '%s' not found", key)); + } + return componentDto; + } + @Override @CheckForNull protected ComponentDto doGetNullableByKey(DbSession session, String key) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectReferentialsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectReferentialsActionTest.java index 65e1995054f..75ea4a1fb6f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectReferentialsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectReferentialsActionTest.java @@ -98,9 +98,9 @@ public class ProjectReferentialsActionTest { module = new ComponentDto().setKey("org.codehaus.sonar:sonar-server").setQualifier(Qualifiers.MODULE); subModule = new ComponentDto().setKey("org.codehaus.sonar:sonar-server-dao").setQualifier(Qualifiers.MODULE); - when(componentDao.getNullableByKey(session, project.key())).thenReturn(project); - when(componentDao.getNullableByKey(session, module.key())).thenReturn(module); - when(componentDao.getNullableByKey(session, subModule.key())).thenReturn(subModule); + when(componentDao.getNullableAuthorizedComponentByKey(project.key(), session)).thenReturn(project); + when(componentDao.getNullableAuthorizedComponentByKey(module.key(), session)).thenReturn(module); + when(componentDao.getNullableAuthorizedComponentByKey(subModule.key(), session)).thenReturn(subModule); when(language.getKey()).thenReturn("java"); when(languages.all()).thenReturn(new Language[] {language}); @@ -118,6 +118,7 @@ public class ProjectReferentialsActionTest { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); // Project without modules + when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(project); when(componentDao.findModulesByProject(project.key(), session)).thenReturn(Collections.emptyList()); when(propertiesDao.selectProjectProperties(project.key(), session)).thenReturn(newArrayList( @@ -133,6 +134,7 @@ public class ProjectReferentialsActionTest { public void return_project_with_module_settings() throws Exception { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); + when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(project); when(componentDao.findModulesByProject(project.key(), session)).thenReturn(newArrayList(module)); when(propertiesDao.selectProjectProperties(project.key(), session)).thenReturn(newArrayList( @@ -153,6 +155,7 @@ public class ProjectReferentialsActionTest { public void return_project_with_module_settings_inherited_from_project() throws Exception { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); + when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(project); when(componentDao.findModulesByProject(project.key(), session)).thenReturn(newArrayList(module)); when(propertiesDao.selectProjectProperties(project.key(), session)).thenReturn(newArrayList( @@ -169,6 +172,7 @@ public class ProjectReferentialsActionTest { public void return_project_with_module_with_sub_module() throws Exception { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); + when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(project); when(componentDao.findModulesByProject(project.key(), session)).thenReturn(newArrayList(module)); when(componentDao.findModulesByProject(module.key(), session)).thenReturn(newArrayList(subModule)); @@ -190,11 +194,27 @@ public class ProjectReferentialsActionTest { request.execute().assertJson(getClass(), "return_project_with_module_with_sub_module.json"); } + @Test + public void return_provisioned_project_settings() throws Exception { + MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); + + // No root project will be found on provisioned project + when(componentDao.getNullableRootProjectByKey(project.key(), session)).thenReturn(null); + + when(propertiesDao.selectProjectProperties(project.key(), session)).thenReturn(newArrayList( + new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR"), + new PropertyDto().setKey("sonar.jira.login.secured").setValue("john") + )); + + WsTester.TestRequest request = tester.newGetRequest("batch", "project").setParam("key", project.key()); + request.execute().assertJson(getClass(), "return_project_settings.json"); + } + @Test public void return_sub_module_settings() throws Exception { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); - when(componentDao.getRootProjectByKey(subModule.key(), session)).thenReturn(project); + when(componentDao.getNullableRootProjectByKey(subModule.key(), session)).thenReturn(project); when(componentDao.getParentModuleByKey(module.key(), session)).thenReturn(project); when(componentDao.getParentModuleByKey(subModule.key(), session)).thenReturn(module); @@ -212,7 +232,7 @@ public class ProjectReferentialsActionTest { public void return_sub_module_settings_including_settings_from_parent_modules() throws Exception { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); - when(componentDao.getRootProjectByKey(subModule.key(), session)).thenReturn(project); + when(componentDao.getNullableRootProjectByKey(subModule.key(), session)).thenReturn(project); when(componentDao.getParentModuleByKey(module.key(), session)).thenReturn(project); when(componentDao.getParentModuleByKey(subModule.key(), session)).thenReturn(module); @@ -236,7 +256,7 @@ public class ProjectReferentialsActionTest { public void return_sub_module_settings_only_inherited_from_project() throws Exception { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); - when(componentDao.getRootProjectByKey(subModule.key(), session)).thenReturn(project); + when(componentDao.getNullableRootProjectByKey(subModule.key(), session)).thenReturn(project); when(componentDao.getParentModuleByKey(module.key(), session)).thenReturn(project); when(componentDao.getParentModuleByKey(subModule.key(), session)).thenReturn(module); @@ -255,7 +275,7 @@ public class ProjectReferentialsActionTest { public void return_sub_module_settings_inherited_from_project_and_module() throws Exception { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); - when(componentDao.getRootProjectByKey(subModule.key(), session)).thenReturn(project); + when(componentDao.getNullableRootProjectByKey(subModule.key(), session)).thenReturn(project); when(componentDao.getParentModuleByKey(module.key(), session)).thenReturn(project); when(componentDao.getParentModuleByKey(subModule.key(), session)).thenReturn(module); diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/persistence/ComponentDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/persistence/ComponentDaoTest.java index 4ff1d97a556..5296d905c94 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/persistence/ComponentDaoTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/persistence/ComponentDaoTest.java @@ -140,6 +140,19 @@ public class ComponentDaoTest extends AbstractDaoTestCase { assertThat(dao.findModulesByProject("unknown", session)).isEmpty(); } + @Test + public void get_nullable_root_project_by_key() throws Exception { + setupData("multi-modules"); + + assertThat(dao.getNullableRootProjectByKey("org.struts:struts-data", session).getKey()).isEqualTo("org.struts:struts"); + assertThat(dao.getNullableRootProjectByKey("org.struts:struts-core", session).getKey()).isEqualTo("org.struts:struts"); + + // Root project of a project is itself + assertThat(dao.getNullableRootProjectByKey("org.struts:struts", session).getKey()).isEqualTo("org.struts:struts"); + + assertThat(dao.getNullableRootProjectByKey("unknown", session)).isNull(); + } + @Test public void get_root_project_by_key() throws Exception { setupData("multi-modules"); @@ -192,6 +205,31 @@ public class ComponentDaoTest extends AbstractDaoTestCase { dao.getAuthorizedComponentById(111L, session); } + @Test + public void get_nullable_authorized_component_by_key() { + setupData("shared"); + + AuthorizedComponentDto result = dao.getNullableAuthorizedComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java", session); + assertThat(result).isNotNull(); + assertThat(result.key()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java"); + + assertThat(dao.getNullableAuthorizedComponentByKey("unknown", session)).isNull(); + } + + @Test + public void get_authorized_component_by_key() { + setupData("shared"); + + assertThat(dao.getAuthorizedComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java", session)).isNotNull(); + } + + @Test(expected = NotFoundException.class) + public void fail_to_get_authorized_component_by_key_when_project_not_found() { + setupData("shared"); + + dao.getAuthorizedComponentByKey("unknown", session); + } + @Test public void insert() { when(system2.now()).thenReturn(DateUtils.parseDate("2014-06-18").getTime()); diff --git a/sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java b/sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java index 569e8646269..6691124af8a 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java +++ b/sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java @@ -77,7 +77,6 @@ public class ComponentDto extends AuthorizedComponentDto implements Component { return this; } - @Override public String qualifier() { return qualifier; } diff --git a/sonar-core/src/main/java/org/sonar/core/component/db/ComponentMapper.java b/sonar-core/src/main/java/org/sonar/core/component/db/ComponentMapper.java index 5561948dc8c..58cfdba2b5d 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/db/ComponentMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/component/db/ComponentMapper.java @@ -54,5 +54,7 @@ public interface ComponentMapper { @CheckForNull AuthorizedComponentDto selectAuthorizedComponentById(long id); + AuthorizedComponentDto selectAuthorizedComponentByKey(String key); + void insert(ComponentDto rule); } diff --git a/sonar-core/src/main/resources/org/sonar/core/component/db/ComponentMapper.xml b/sonar-core/src/main/resources/org/sonar/core/component/db/ComponentMapper.xml index 77a9ad0a35d..452bebb9fcf 100644 --- a/sonar-core/src/main/resources/org/sonar/core/component/db/ComponentMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/component/db/ComponentMapper.xml @@ -92,6 +92,15 @@ + + (kee, name, long_name, qualifier, scope, language, root_id, path, created_at)