import org.sonar.server.qualitygate.RegisterQualityGates;
import org.sonar.server.qualityprofile.CachingRuleActivator;
import org.sonar.server.qualityprofile.CachingRuleActivatorContextFactory;
+import org.sonar.server.qualityprofile.DefinedQProfileLoader;
import org.sonar.server.qualityprofile.RegisterQualityProfiles;
import org.sonar.server.rule.RegisterRules;
import org.sonar.server.startup.ClearRulesOverloadedDebt;
IndexerStartupTask.class,
RegisterMetrics.class,
RegisterQualityGates.class,
- RegisterRules.class,
+ RegisterRules.class);
+ add(DefinedQProfileLoader.class);
+ addIfStartupLeader(
CachingRuleActivatorContextFactory.class,
CachingRuleActivator.class,
RegisterQualityProfiles.class,
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.qualityprofile;
+
+import org.picocontainer.Startable;
+
+/**
+ * Startable added to {@link org.sonar.server.platform.platformlevel.PlatformLevelStartup} responsible for initializing
+ * {@link DefinedQProfileRepository}.
+ */
+public class DefinedQProfileLoader implements Startable {
+ private final DefinedQProfileRepository definedQProfileRepository;
+
+ public DefinedQProfileLoader(DefinedQProfileRepository definedQProfileRepository) {
+ this.definedQProfileRepository = definedQProfileRepository;
+ }
+
+ @Override
+ public void start() {
+ definedQProfileRepository.initialize();
+ }
+
+ @Override
+ public void stop() {
+ // nothing to do
+ }
+}
public void start() {
Profiler profiler = Profiler.create(Loggers.get(getClass())).startInfo("Register quality profiles");
- definedQProfileRepository.initialize();
try (DbSession session = dbClient.openSession(false)) {
List<ActiveRuleChange> changes = new ArrayList<>();
definedQProfileRepository.getQProfilesByLanguage().entrySet()
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.qualityprofile;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DefinedQProfileLoaderTest {
+ @Rule
+ public DefinedQProfileRepositoryRule definedQProfileRepositoryRule = new DefinedQProfileRepositoryRule();
+
+ private DefinedQProfileLoader underTest = new DefinedQProfileLoader(definedQProfileRepositoryRule);
+
+ @Test
+ public void start_initializes_DefinedQProfileRepository() {
+ underTest.start();
+
+ assertThat(definedQProfileRepositoryRule.isInitialized()).isTrue();
+ }
+}
new CachingRuleActivator(mockedSystem2, dbClient, null, new CachingRuleActivatorContextFactory(dbClient), null, null, userSessionRule),
mockedActiveRuleIndexer);
+ @Test
+ public void start_fails_if_DefinedQProfileRepository_has_not_been_initialized() {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("initialize must be called first");
+
+ underTest.start();
+ }
+
@Test
public void no_action_in_DB_nothing_to_index_when_there_is_no_DefinedQProfile() {
RegisterQualityProfiles underTest = new RegisterQualityProfiles(definedQProfileRepositoryRule, mockedDbClient, null, null, mockedActiveRuleIndexer);
+ definedQProfileRepositoryRule.initialize();
underTest.start();
String[] formattedDates = {formatDateTime(new Date(dates[0])), formatDateTime(new Date(dates[1]))};
DefinedQProfile definedQProfile = definedQProfileRepositoryRule.add(FOO_LANGUAGE, "foo1");
mockForQPInserts(uuids, dates);
+ definedQProfileRepositoryRule.initialize();
underTest.start();
definedQProfileRepositoryRule.add(BAR_LANGUAGE, "bar1", true);
definedQProfileRepositoryRule.add(BAR_LANGUAGE, "bar2", false);
mockForQPInserts(new String[]{"uuid1", "uuid2", "uuid3", "uuid4"}, new Long[]{ 1L, 2L, 3L, 4L});
+ definedQProfileRepositoryRule.initialize();
underTest.start();
- assertThat(definedQProfileRepositoryRule.isInitialized()).isTrue();
assertThat(getPersistedQP(dbTester.getDefaultOrganization(), FOO_LANGUAGE, "foo1").isDefault()).isFalse();
assertThat(getPersistedQP(dbTester.getDefaultOrganization(), FOO_LANGUAGE, "foo2").isDefault()).isTrue();
assertThat(getPersistedQP(dbTester.getDefaultOrganization(), BAR_LANGUAGE, "bar1").isDefault()).isTrue();
dbClient.loadedTemplateDao().insert(new LoadedTemplateDto(org1.getUuid(), definedQProfile.getLoadedTemplateType()), dbTester.getSession());
dbTester.commit();
mockForSingleQPInsert();
+ definedQProfileRepositoryRule.initialize();
underTest.start();
- assertThat(definedQProfileRepositoryRule.isInitialized()).isTrue();
assertThat(dbClient.loadedTemplateDao().countByTypeAndKey(definedQProfile.getLoadedTemplateType(), org2.getUuid(), dbTester.getSession()))
.isEqualTo(1);
assertThat(dbTester.countRowsOfTable(dbTester.getSession(), TABLE_LOADED_TEMPLATES)).isEqualTo(3);
DefinedQProfile definedQProfile1 = definedQProfileRepositoryRule.add(FOO_LANGUAGE, name, true);
DefinedQProfile definedQProfile2 = definedQProfileRepositoryRule.add(BAR_LANGUAGE, name, true);
mockForQPInserts(new String[]{uuid1, uuid2}, new Long[]{date1, date2});
+ definedQProfileRepositoryRule.initialize();
underTest.start();
- assertThat(definedQProfileRepositoryRule.isInitialized()).isTrue();
OrganizationDto organization = dbTester.getDefaultOrganization();
QualityProfileDto dto = getPersistedQP(organization, FOO_LANGUAGE, name);
String uuidQP1 = dto.getKee();