Skip to content

Commit

Permalink
SONAR-9478 Missing Settings -> Configuration replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Jul 4, 2017
1 parent 9a7dcc5 commit ccde391
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.util.Date;
import javax.annotation.CheckForNull;
import org.sonar.api.CoreProperties;
import org.sonar.api.SonarRuntime;
import org.sonar.api.ce.ComputeEngineSide;
Expand All @@ -46,9 +47,13 @@ public ServerImpl(Configuration config, StartupMetadata state, ServerFileSystem
this.runtime = runtime;
}

/**
* Can be null when server is waiting for migration
*/
@Override
@CheckForNull
public String getId() {
return config.get(CoreProperties.SERVER_ID).get();
return config.get(CoreProperties.SERVER_ID).orElse(null);
}

@Override
Expand Down
Expand Up @@ -22,7 +22,7 @@

import com.google.common.collect.Multimap;
import java.util.Collection;
import org.sonar.api.config.Settings;
import org.sonar.api.config.Configuration;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.server.notification.NotificationManager;
Expand All @@ -39,16 +39,16 @@ public class BuiltInQualityProfilesUpdateListener {

private final NotificationManager notificationManager;
private final Languages languages;
private final Settings settings;
private final Configuration config;

public BuiltInQualityProfilesUpdateListener(NotificationManager notificationManager, Languages languages, Settings settings) {
public BuiltInQualityProfilesUpdateListener(NotificationManager notificationManager, Languages languages, Configuration config) {
this.notificationManager = notificationManager;
this.languages = languages;
this.settings = settings;
this.config = config;
}

void onChange(Multimap<QProfileName, ActiveRuleChange> changedProfiles, long startDate, long endDate) {
if (settings.getBoolean(DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES)) {
if (config.getBoolean(DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES).orElse(false)) {
return;
}
BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification();
Expand Down
Expand Up @@ -27,8 +27,7 @@
import org.assertj.core.groups.Tuple;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.Settings;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.notifications.Notification;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
Expand All @@ -53,7 +52,7 @@ public class BuiltInQualityProfilesUpdateListenerTest {

private static final Random RANDOM = new Random();
private NotificationManager notificationManager = mock(NotificationManager.class);
private Settings settings = new MapSettings();
private MapSettings settings = new MapSettings();

@Test
public void add_profile_to_notification_for_added_rules() throws Exception {
Expand All @@ -62,7 +61,7 @@ public void add_profile_to_notification_for_added_rules() throws Exception {
Languages languages = new Languages();
Tuple expectedTuple = addProfile(profiles, languages, ACTIVATED);

BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings);
BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings.asConfig());
underTest.onChange(profiles, 0, 1);

ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
Expand All @@ -80,7 +79,7 @@ public void add_profile_to_notification_for_updated_rules() throws Exception {
Languages languages = new Languages();
Tuple expectedTuple = addProfile(profiles, languages, UPDATED);

BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings);
BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings.asConfig());
underTest.onChange(profiles, 0, 1);

ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
Expand All @@ -98,7 +97,7 @@ public void add_profile_to_notification_for_removed_rules() throws Exception {
Languages languages = new Languages();
Tuple expectedTuple = addProfile(profiles, languages, DEACTIVATED);

BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings);
BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings.asConfig());
underTest.onChange(profiles, 0, 1);

ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
Expand All @@ -117,7 +116,7 @@ public void add_multiple_profiles_to_notification() throws Exception {
Tuple expectedTuple1 = addProfile(profiles, languages, ACTIVATED);
Tuple expectedTuple2 = addProfile(profiles, languages, ACTIVATED);

BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings);
BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings.asConfig());
underTest.onChange(profiles, 0, 1);

ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
Expand All @@ -137,7 +136,7 @@ public void add_start_and_end_dates_to_notification() throws Exception {
long startDate = RANDOM.nextInt(5000);
long endDate = startDate + RANDOM.nextInt(5000);

BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings);
BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings.asConfig());
underTest.onChange(profiles, startDate, endDate);

ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
Expand All @@ -155,7 +154,7 @@ public void avoid_notification_if_configured_in_settings() {
Languages languages = new Languages();
addProfile(profiles, languages, ACTIVATED);

BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings);
BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings.asConfig());
underTest.onChange(profiles, 0, 1);

verifyZeroInteractions(notificationManager);
Expand All @@ -176,7 +175,7 @@ private static String randomLowerCaseText() {
return randomAlphanumeric(20).toLowerCase();
}

private void enableNotificationInGlobalSettings(){
private void enableNotificationInGlobalSettings() {
settings.setProperty(DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES, false);
}
}
Expand Up @@ -23,7 +23,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.WebService;
Expand All @@ -46,14 +46,14 @@
import org.sonarqube.ws.QualityProfiles;
import org.sonarqube.ws.QualityProfiles.ShowResponse;
import org.sonarqube.ws.QualityProfiles.ShowResponse.CompareToSonarWay;
import org.sonarqube.ws.QualityProfiles.ShowResponse.QualityProfile;

import static java.util.stream.IntStream.range;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.rule.RuleStatus.DEPRECATED;
import static org.sonar.api.utils.DateUtils.parseDateTime;
import static org.sonar.server.language.LanguageTesting.newLanguage;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.QualityProfiles.ShowResponse.QualityProfile;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE;

Expand All @@ -64,7 +64,7 @@ public class ShowActionTest {
private static Languages LANGUAGES = new Languages(XOO1, XOO2);

@Rule
public EsTester es = new EsTester(new RuleIndexDefinition(new MapSettings()));
public EsTester es = new EsTester(new RuleIndexDefinition(new MapSettings().asConfig()));
@Rule
public DbTester db = DbTester.create();
@Rule
Expand Down Expand Up @@ -215,7 +215,7 @@ public void compare_to_sonar_way_profile_when_same_active_rules() {
CompareToSonarWay result = call(ws.newRequest()
.setParam(PARAM_PROFILE, profile.getKee())
.setParam(PARAM_COMPARE_TO_SONAR_WAY, "true"))
.getCompareToSonarWay();
.getCompareToSonarWay();

assertThat(result)
.extracting(CompareToSonarWay::getProfile, CompareToSonarWay::getProfileName, CompareToSonarWay::getMissingRuleCount)
Expand Down
Expand Up @@ -181,8 +181,8 @@ public String getBranch() {
String branch = properties.get(CoreProperties.PROJECT_BRANCH_PROPERTY);
if (StringUtils.isNotBlank(branch)) {
return branch;
} else if (getParent() != null) {
return getParent().getBranch();
} else if (parent != null) {
return parent.getBranch();
}
return null;
}
Expand Down
Expand Up @@ -20,7 +20,7 @@
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.config.Settings;
import org.sonar.api.security.UserDetails;

Expand Down
Expand Up @@ -89,7 +89,7 @@ public void analyzeProjectWithManyModulesAndBigProperties() throws IOException {
.setProjectDir(baseDir);

BuildResult result = orchestrator.executeBuild(scanner);
perfRule.assertDurationAround(MavenLogs.extractTotalTime(result.getLogs()), 6190L);
perfRule.assertDurationAround(MavenLogs.extractTotalTime(result.getLogs()), 6740L);

// Second execution with a property on server side
orchestrator.getServer().newHttpCall("/api/settings/set")
Expand All @@ -100,7 +100,7 @@ public void analyzeProjectWithManyModulesAndBigProperties() throws IOException {
.setParam("component", "big-module-tree")
.execute();
result = orchestrator.executeBuild(scanner);
perfRule.assertDurationAround(MavenLogs.extractTotalTime(result.getLogs()), 6120L);
perfRule.assertDurationAround(MavenLogs.extractTotalTime(result.getLogs()), 6720L);
}

private void prepareModule(File parentDir, String moduleName, int depth) throws IOException {
Expand Down

0 comments on commit ccde391

Please sign in to comment.