]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5417 Do not fail when project does not exists
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 31 Jul 2014 07:43:45 +0000 (09:43 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 31 Jul 2014 07:43:45 +0000 (09:43 +0200)
server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java
server/sonar-server/src/test/java/org/sonar/server/batch/ProjectReferentialsActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/batch/ProjectReferentialsActionTest/return_quality_profiles_even_when_project_does_not_exists.json [new file with mode: 0644]

index 2dbe12f40ce53f9d876593d3a99bc39b7e7b2b42..be4f94f5381d5c0ab8cd8f4c05c920ea08b1a701 100644 (file)
@@ -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.<String, String>newHashMap(), hasScanPerm, session);
       }
-      addSettingsToChildrenModules(ref, projectOrModuleKey, Maps.<String, String>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;
index 4643ea4f6418b0259e08a87e14572f8faf77b19a..65c0f46025f83c6f347005625cd67fa7786db298 100644 (file)
@@ -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);
   }
index d5a6c989dd6b27ed244a3873b693f01db5fbc35b..65e1995054fa1e355eeb1f8ff749e2a383c31bc0 100644 (file)
@@ -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 (file)
index 0000000..a1bb33b
--- /dev/null
@@ -0,0 +1,13 @@
+{
+  "timestamp": 0,
+  "qprofilesByLanguage": {
+    "java": {
+      "key": "abcd",
+      "name": "Default",
+      "language": "java",
+      "rulesUpdatedAt": "Jan 14, 2014 1:00:00 PM"
+    }
+  },
+  "activeRules": [],
+  "settingsByModule": {}
+}