From a162217822f8a361f4374b3563e1cbda50f6f2f6 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 31 Jul 2014 09:43:45 +0200 Subject: [PATCH] SONAR-5417 Do not fail when project does not exists --- .../batch/ProjectReferentialsAction.java | 30 ++++++++++++------- .../qualityprofile/QProfileFactory.java | 2 ++ .../batch/ProjectReferentialsActionTest.java | 20 +++++++++++-- ...les_even_when_project_does_not_exists.json | 13 ++++++++ 4 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profiles_even_when_project_does_not_exists.json 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 2dbe12f40ce..be4f94f5381 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 @@ -108,14 +108,18 @@ public class ProjectReferentialsAction implements RequestHandler { String profileName = request.param(PARAM_PROFILE); ProjectReferentials ref = new ProjectReferentials(); - ComponentDto module = dbClient.componentDao().getByKey(session, projectOrModuleKey); - 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)); + String projectKey = null; + ComponentDto module = dbClient.componentDao().getNullableByKey(session, projectOrModuleKey); + 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)); + } + projectKey = project.key(); + addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.newHashMap(), hasScanPerm, session); } - addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.newHashMap(), hasScanPerm, session); - addProfiles(ref, project.key(), profileName, session); + addProfiles(ref, projectKey, profileName, session); addActiveRules(ref); response.stream().setMediaType(MimeTypes.JSON); @@ -177,7 +181,7 @@ public class ProjectReferentialsAction implements RequestHandler { return !key.contains(".secured") || hasScanPerm; } - private void addProfiles(ProjectReferentials ref, String projectKey, @Nullable String profileName, DbSession session) { + private void addProfiles(ProjectReferentials ref, @Nullable String projectKey, @Nullable String profileName, DbSession session) { for (Language language : languages.all()) { String languageKey = language.getKey(); QualityProfileDto qualityProfileDto = getProfile(languageKey, projectKey, profileName, session); @@ -191,12 +195,16 @@ public class ProjectReferentialsAction implements RequestHandler { /** * First try to find a quality profile matching the given name (if provided) and current language - * If null, try to find the quality profile set on the project - * If null, try to find the default profile of the language + * If no profile found, try to find the quality profile set on the project (if provided) + * If still no profile found, try to find the default profile of the language + * + * Never return null because a default profile should always be set on ech language */ - private QualityProfileDto getProfile(String languageKey, String projectKey, @Nullable String profileName, DbSession session) { + private QualityProfileDto getProfile(String languageKey, @Nullable String projectKey, @Nullable String profileName, DbSession session) { QualityProfileDto qualityProfileDto = profileName != null ? qProfileFactory.getByNameAndLanguage(session, profileName, languageKey) : null; - qualityProfileDto = qualityProfileDto != null ? qualityProfileDto : qProfileFactory.getByProjectAndLanguage(session, projectKey, languageKey); + if (qualityProfileDto == null && projectKey != null) { + qualityProfileDto = qProfileFactory.getByProjectAndLanguage(session, projectKey, languageKey); + } qualityProfileDto = qualityProfileDto != null ? qualityProfileDto : qProfileFactory.getDefault(session, languageKey); if (qualityProfileDto != null) { return qualityProfileDto; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java index 4643ea4f641..65c0f46025f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java @@ -177,6 +177,7 @@ public class QProfileFactory implements ServerComponent { } } + @CheckForNull public QualityProfileDto getByProjectAndLanguage(DbSession session, String projectKey, String language) { return db.qualityProfileDao().getByProjectAndLanguage(projectKey, language, PROFILE_PROPERTY_PREFIX + language, session); } @@ -190,6 +191,7 @@ public class QProfileFactory implements ServerComponent { } } + @CheckForNull public QualityProfileDto getByNameAndLanguage(DbSession session, String name, String language) { return db.qualityProfileDao().getByNameAndLanguage(name, language, session); } 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 d5a6c989dd6..65e1995054f 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.getByKey(session, project.key())).thenReturn(project); - when(componentDao.getByKey(session, module.key())).thenReturn(module); - when(componentDao.getByKey(session, subModule.key())).thenReturn(subModule); + 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(language.getKey()).thenReturn("java"); when(languages.all()).thenReturn(new Language[] {language}); @@ -326,6 +326,20 @@ public class ProjectReferentialsActionTest { request.execute().assertJson(getClass(), "return_quality_profile_from_given_profile_name.json"); } + @Test + public void return_quality_profiles_even_when_project_does_not_exists() throws Exception { + MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); + String projectKey = "org.codehaus.sonar:sonar"; + when(componentDao.getNullableByKey(session, projectKey)).thenReturn(null); + + when(qProfileFactory.getDefault(session, "java")).thenReturn( + QualityProfileDto.createFor("abcd").setName("Default").setLanguage("java").setRulesUpdatedAt("2014-01-14T14:00:00+0200") + ); + + WsTester.TestRequest request = tester.newGetRequest("batch", "project").setParam("key", projectKey); + request.execute().assertJson(getClass(), "return_quality_profiles_even_when_project_does_not_exists.json"); + } + @Test public void return_active_rules() throws Exception { MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profiles_even_when_project_does_not_exists.json b/server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profiles_even_when_project_does_not_exists.json new file mode 100644 index 00000000000..a1bb33bb01f --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profiles_even_when_project_does_not_exists.json @@ -0,0 +1,13 @@ +{ + "timestamp": 0, + "qprofilesByLanguage": { + "java": { + "key": "abcd", + "name": "Default", + "language": "java", + "rulesUpdatedAt": "Jan 14, 2014 1:00:00 PM" + } + }, + "activeRules": [], + "settingsByModule": {} +} -- 2.39.5