From f2cbd58e6a46c1c835c39f1dfc1c6316741a00eb Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 20 Jan 2014 09:36:15 +0100 Subject: [PATCH] Fix quality flaws --- .../server/qualityprofile/QProfileBackup.java | 9 +++++--- .../qualityprofile/QProfileBackupTest.java | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java index cc6a07f79b8..ccdb1a29608 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java @@ -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); diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java index a137920d664..6f8a7573d81 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java @@ -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("", false, userSession); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("Restore of the profile has failed."); + } + + verifyZeroInteractions(ruleRegistry); + verifyZeroInteractions(dryRunCache); + } } -- 2.39.5