]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 20 Jan 2014 08:36:15 +0000 (09:36 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 20 Jan 2014 08:36:26 +0000 (09:36 +0100)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java

index cc6a07f79b8e06006468cc94620252caa24e578e..ccdb1a29608dc77bbff37435d0cbf8ff95bd0045 100644 (file)
@@ -92,11 +92,14 @@ public class QProfileBackup implements ServerComponent {
         hibernateSession.saveWithoutFlush(importedProfile);
         hibernateSession.commit();
 
-        QProfile profile = qProfileLookup.profile(importedProfile.getId(), session);
-        ruleRegistry.bulkIndexProfile(profile.id(), session);
+        QProfile newProfile = qProfileLookup.profile(importedProfile.getId(), session);
+        if (newProfile == null) {
+          throw new BadRequestException("Restore of the profile has failed.");
+        }
+        ruleRegistry.bulkIndexProfile(newProfile.id(), session);
         dryRunCache.reportGlobalModification(session);
         session.commit();
-        result.setProfile(profile);
+        result.setProfile(newProfile);
       }
     } finally {
       MyBatis.closeQuietly(session);
index a137920d6647e9a97680458e27199dfe981ff53c..6f8a7573d81a5e94c69727e2e11e3e64b608949d 100644 (file)
@@ -254,4 +254,26 @@ public class QProfileBackupTest {
     verifyZeroInteractions(ruleRegistry);
     verifyZeroInteractions(dryRunCache);
   }
+
+  @Test
+  public void do_not_restore_if_new_profile_is_null_after_import() throws Exception {
+    RulesProfile profile = mock(RulesProfile.class);
+    when(profile.getName()).thenReturn("Default");
+    when(profile.getLanguage()).thenReturn("java");
+    when(profile.getId()).thenReturn(1);
+    when(xmlProfileParser.parse(any(Reader.class), any(ValidationMessages.class))).thenReturn(profile);
+
+    when(hibernateSession.getSingleResult(any(Class.class), eq("name"), eq("Default"), eq("language"), eq("java"))).thenReturn(null);
+    when(qProfileLookup.profile(anyInt(), eq(session))).thenReturn(null);
+
+    try {
+      backup.restore("<xml/>", false, userSession);
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("Restore of the profile has failed.");
+    }
+
+    verifyZeroInteractions(ruleRegistry);
+    verifyZeroInteractions(dryRunCache);
+  }
 }