<tr>
<td>assertText</td>
<td>css=.js-profile-changelog-event</td>
- <td>*Administrator*Activated*Has Tag*Major*tag*xoo*</td>
+ <td>*System*Activated*Has Tag*Major*tag*xoo*</td>
</tr>
</tbody>
</table>
*/
package org.sonar.server.organization;
-import java.util.ArrayList;
import java.util.Date;
-import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.db.user.UserGroupDto;
-import org.sonar.server.qualityprofile.ActiveRuleChange;
import org.sonar.server.qualityprofile.DefinedQProfile;
-import org.sonar.server.qualityprofile.DefinedQProfileCreation;
+import org.sonar.server.qualityprofile.DefinedQProfileInsert;
import org.sonar.server.qualityprofile.DefinedQProfileRepository;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.user.index.UserIndexer;
import org.sonar.server.usergroups.DefaultGroupCreator;
private final OrganizationValidation organizationValidation;
private final Settings settings;
private final DefinedQProfileRepository definedQProfileRepository;
- private final DefinedQProfileCreation definedQProfileCreation;
+ private final DefinedQProfileInsert definedQProfileInsert;
private final DefaultGroupCreator defaultGroupCreator;
- private final ActiveRuleIndexer activeRuleIndexer;
private final UserIndexer userIndexer;
public OrganizationCreationImpl(DbClient dbClient, System2 system2, UuidFactory uuidFactory,
OrganizationValidation organizationValidation, Settings settings, UserIndexer userIndexer,
- DefinedQProfileRepository definedQProfileRepository, DefinedQProfileCreation definedQProfileCreation, DefaultGroupCreator defaultGroupCreator,
- ActiveRuleIndexer activeRuleIndexer) {
+ DefinedQProfileRepository definedQProfileRepository, DefinedQProfileInsert definedQProfileInsert,
+ DefaultGroupCreator defaultGroupCreator) {
this.dbClient = dbClient;
this.system2 = system2;
this.uuidFactory = uuidFactory;
this.settings = settings;
this.userIndexer = userIndexer;
this.definedQProfileRepository = definedQProfileRepository;
- this.definedQProfileCreation = definedQProfileCreation;
+ this.definedQProfileInsert = definedQProfileInsert;
this.defaultGroupCreator = defaultGroupCreator;
- this.activeRuleIndexer = activeRuleIndexer;
}
@Override
GroupDto ownerGroup = insertOwnersGroup(dbSession, organization);
GroupDto defaultGroup = defaultGroupCreator.create(dbSession, organization.getUuid());
insertDefaultTemplateOnGroups(dbSession, organization, ownerGroup, defaultGroup);
- List<ActiveRuleChange> activeRuleChanges = insertQualityProfiles(dbSession, organization);
- addCurrentUserToGroup(dbSession, ownerGroup, userCreator.getId());
- addCurrentUserToGroup(dbSession, defaultGroup, userCreator.getId());
+ try (DbSession batchDbSession = dbClient.openSession(true)) {
+ insertQualityProfiles(dbSession, batchDbSession, organization);
+ addCurrentUserToGroup(dbSession, ownerGroup, userCreator.getId());
+ addCurrentUserToGroup(dbSession, defaultGroup, userCreator.getId());
- dbSession.commit();
+ dbSession.commit();
+ batchDbSession.commit();
- // Elasticsearch is updated when DB session is committed
- userIndexer.index(userCreator.getLogin());
- activeRuleIndexer.index(activeRuleChanges);
+ // Elasticsearch is updated when DB session is committed
+ userIndexer.index(userCreator.getLogin());
- return organization;
+ return organization;
+ }
}
@Override
OrganizationPermission.all()
.forEach(p -> insertUserPermissions(dbSession, newUser, organization, p));
insertPersonalOrgDefaultTemplate(dbSession, organization, defaultGroup);
- List<ActiveRuleChange> activeRuleChanges = insertQualityProfiles(dbSession, organization);
- addCurrentUserToGroup(dbSession, defaultGroup, newUser.getId());
+ try (DbSession batchDbSession = dbClient.openSession(true)) {
+ insertQualityProfiles(dbSession, batchDbSession, organization);
+ addCurrentUserToGroup(dbSession, defaultGroup, newUser.getId());
- dbSession.commit();
+ dbSession.commit();
+ batchDbSession.commit();
- // Elasticsearch is updated when DB session is committed
- activeRuleIndexer.index(activeRuleChanges);
- userIndexer.index(newUser.getLogin());
+ // Elasticsearch is updated when DB session is committed
+ userIndexer.index(newUser.getLogin());
- return Optional.of(organization);
+ return Optional.of(organization);
+ }
}
private static String nameOrLogin(UserDto newUser) {
dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getId(), group == null ? null : group.getId(), permission);
}
- private List<ActiveRuleChange> insertQualityProfiles(DbSession dbSession, OrganizationDto organization) {
- List<ActiveRuleChange> changes = new ArrayList<>();
+ private void insertQualityProfiles(DbSession dbSession, DbSession batchDbSession, OrganizationDto organization) {
definedQProfileRepository.getQProfilesByLanguage().entrySet()
.stream()
.flatMap(entry -> entry.getValue().stream())
- .forEach(profile -> insertQualityProfile(dbSession, profile, organization, changes));
- return changes;
+ .forEach(profile -> insertQualityProfile(dbSession, batchDbSession, profile, organization));
}
- private void insertQualityProfile(DbSession dbSession, DefinedQProfile profile, OrganizationDto organization, List<ActiveRuleChange> changes) {
+ private void insertQualityProfile(DbSession regularSession, DbSession batchDbSession, DefinedQProfile profile, OrganizationDto organization) {
LOGGER.debug("Creating quality profile {} for language {} for organization {}", profile.getName(), profile.getLanguage(), organization.getKey());
- definedQProfileCreation.create(dbSession, profile, organization, changes);
+ definedQProfileInsert.create(regularSession, batchDbSession, profile, organization);
}
/**
import org.sonar.server.property.ws.PropertiesWs;
import org.sonar.server.qualitygate.QualityGateModule;
import org.sonar.server.qualityprofile.DefinedQProfileCreationImpl;
+import org.sonar.server.qualityprofile.DefinedQProfileInsertImpl;
import org.sonar.server.qualityprofile.DefinedQProfileRepositoryImpl;
import org.sonar.server.qualityprofile.QProfileBackuperImpl;
import org.sonar.server.qualityprofile.QProfileComparison;
// quality profile
DefinedQProfileRepositoryImpl.class,
+ DefinedQProfileInsertImpl.class,
ActiveRuleIndexer.class,
XMLProfileParser.class,
XMLProfileSerializer.class,
import org.sonar.server.qualityprofile.CachingDefinedQProfileCreationImpl;
import org.sonar.server.qualityprofile.CachingRuleActivator;
import org.sonar.server.qualityprofile.CachingRuleActivatorContextFactory;
-import org.sonar.server.qualityprofile.DefinedQProfileInsertImpl;
import org.sonar.server.qualityprofile.DefinedQProfileLoader;
import org.sonar.server.qualityprofile.MassRegisterQualityProfiles;
import org.sonar.server.qualityprofile.RegisterQualityProfiles;
RegisterRules.class);
add(DefinedQProfileLoader.class);
addIfStartupLeader(
- DefinedQProfileInsertImpl.class,
MassRegisterQualityProfiles.class,
CachingRuleActivatorContextFactory.class,
CachingRuleActivator.class,
*/
package org.sonar.server.organization;
-import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.sonar.server.language.LanguageTesting;
import org.sonar.server.qualityprofile.ActiveRuleChange;
import org.sonar.server.qualityprofile.DefinedQProfile;
-import org.sonar.server.qualityprofile.DefinedQProfileCreationRule;
+import org.sonar.server.qualityprofile.DefinedQProfileInsertRule;
import org.sonar.server.qualityprofile.DefinedQProfileRepositoryRule;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.user.index.UserIndex;
import org.sonar.server.user.index.UserIndexDefinition;
import org.sonar.server.user.index.UserIndexer;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.sonar.server.organization.OrganizationCreation.NewOrganization.newOrganizationBuilder;
@Rule
public DefinedQProfileRepositoryRule definedQProfileRepositoryRule = new DefinedQProfileRepositoryRule();
@Rule
- public DefinedQProfileCreationRule definedQProfileCreationRule = new DefinedQProfileCreationRule();
+ public DefinedQProfileInsertRule definedQProfileCreationRule = new DefinedQProfileInsertRule();
private DbSession dbSession = dbTester.getSession();
private MapSettings settings = new MapSettings();
private UserIndexer userIndexer = new UserIndexer(dbClient, es.client());
private UserIndex userIndex = new UserIndex(es.client());
- private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
private DefaultGroupCreator defaultGroupCreator = new DefaultGroupCreatorImpl(dbClient);
private OrganizationCreationImpl underTest = new OrganizationCreationImpl(dbClient, system2, uuidFactory, organizationValidation, settings, userIndexer,
- definedQProfileRepositoryRule, definedQProfileCreationRule, defaultGroupCreator, activeRuleIndexer);
+ definedQProfileRepositoryRule, definedQProfileCreationRule, defaultGroupCreator);
private UserDto someUser;
DefinedQProfile definedQProfile3 = definedQProfileRepositoryRule.add(LanguageTesting.newLanguage("foo"), "qp3");
DefinedQProfile definedQProfile4 = definedQProfileRepositoryRule.add(LanguageTesting.newLanguage("foo"), "qp4");
definedQProfileRepositoryRule.initialize();
- ActiveRuleChange[] changes = {newActiveRuleChange("0"), newActiveRuleChange("1"), newActiveRuleChange("2"), newActiveRuleChange("3"), newActiveRuleChange("4")};
- definedQProfileCreationRule.addChanges();
- definedQProfileCreationRule.addChanges(changes[2], changes[1], changes[4]);
- definedQProfileCreationRule.addChanges(changes[3]);
- definedQProfileCreationRule.addChanges(changes[0]);
mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION);
OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, FULL_POPULATED_NEW_ORGANIZATION.getKey()).get();
assertThat(definedQProfileCreationRule.getCallLogs())
.hasSize(4)
- .extracting(DefinedQProfileCreationRule.CallLog::getOrganizationDto)
+ .extracting(DefinedQProfileInsertRule.CallLog::getOrganizationDto)
.extracting(OrganizationDto::getUuid)
.containsOnly(organization.getUuid());
assertThat(definedQProfileCreationRule.getCallLogs())
- .extracting(DefinedQProfileCreationRule.CallLog::getDefinedQProfile)
+ .extracting(DefinedQProfileInsertRule.CallLog::getDefinedQProfile)
.extracting(DefinedQProfile::getName)
.containsExactly(definedQProfile1.getName(), definedQProfile2.getName(), definedQProfile3.getName(), definedQProfile4.getName());
- verify(activeRuleIndexer).index(Arrays.asList(changes[2], changes[1], changes[4], changes[3], changes[0]));
- verifyNoMoreInteractions(activeRuleIndexer);
}
@Test
DefinedQProfile definedQProfile3 = definedQProfileRepositoryRule.add(LanguageTesting.newLanguage("foo"), "qp3");
DefinedQProfile definedQProfile4 = definedQProfileRepositoryRule.add(LanguageTesting.newLanguage("foo"), "qp4");
definedQProfileRepositoryRule.initialize();
- ActiveRuleChange[] changes = {newActiveRuleChange("0"), newActiveRuleChange("1"), newActiveRuleChange("2"), newActiveRuleChange("3"), newActiveRuleChange("4")};
- definedQProfileCreationRule.addChanges();
- definedQProfileCreationRule.addChanges(changes[2], changes[1], changes[4]);
- definedQProfileCreationRule.addChanges(changes[3]);
- definedQProfileCreationRule.addChanges(changes[0]);
mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
enableCreatePersonalOrg(true);
OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, SLUG_OF_A_LOGIN).get();
assertThat(definedQProfileCreationRule.getCallLogs())
.hasSize(4)
- .extracting(DefinedQProfileCreationRule.CallLog::getOrganizationDto)
+ .extracting(DefinedQProfileInsertRule.CallLog::getOrganizationDto)
.extracting(OrganizationDto::getUuid)
.containsOnly(organization.getUuid());
assertThat(definedQProfileCreationRule.getCallLogs())
- .extracting(DefinedQProfileCreationRule.CallLog::getDefinedQProfile)
+ .extracting(DefinedQProfileInsertRule.CallLog::getDefinedQProfile)
.extracting(DefinedQProfile::getName)
.containsExactly(definedQProfile1.getName(), definedQProfile2.getName(), definedQProfile3.getName(), definedQProfile4.getName());
- verify(activeRuleIndexer).index(Arrays.asList(changes[2], changes[1], changes[4], changes[3], changes[0]));
- verifyNoMoreInteractions(activeRuleIndexer);
}
private static ActiveRuleChange newActiveRuleChange(String id) {
import org.sonar.server.organization.OrganizationValidation;
import org.sonar.server.organization.OrganizationValidationImpl;
import org.sonar.server.organization.TestOrganizationFlags;
-import org.sonar.server.qualityprofile.DefinedQProfileCreation;
+import org.sonar.server.qualityprofile.DefinedQProfileInsert;
import org.sonar.server.qualityprofile.DefinedQProfileRepository;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.index.UserIndex;
import org.sonar.server.user.index.UserIndexDefinition;
private UserIndexer userIndexer = new UserIndexer(dbClient, es.client());
private UserIndex userIndex = new UserIndex(es.client());
private OrganizationCreation organizationCreation = new OrganizationCreationImpl(dbClient, system2, uuidFactory, organizationValidation, settings, userIndexer,
- mock(DefinedQProfileRepository.class), mock(DefinedQProfileCreation.class), new DefaultGroupCreatorImpl(dbClient), mock(ActiveRuleIndexer.class));
+ mock(DefinedQProfileRepository.class), mock(DefinedQProfileInsert.class), new DefaultGroupCreatorImpl(dbClient));
private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone().setEnabled(true);
private UserDto user;
--- /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 java.util.ArrayList;
+import java.util.List;
+import org.junit.rules.ExternalResource;
+import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
+
+public class DefinedQProfileInsertRule extends ExternalResource implements DefinedQProfileInsert {
+ private final List<DefinedQProfileInsertRule.CallLog> callLogs = new ArrayList<>();
+
+ @Override
+ protected void before() throws Throwable {
+ callLogs.clear();
+ }
+
+ @Override
+ public void create(DbSession session, DbSession batchSession, DefinedQProfile definedQProfile, OrganizationDto organization) {
+ callLogs.add(new DefinedQProfileInsertRule.CallLog(definedQProfile, organization));
+ }
+
+ public List<DefinedQProfileInsertRule.CallLog> getCallLogs() {
+ return callLogs;
+ }
+
+ public static final class CallLog {
+ private final DefinedQProfile definedQProfile;
+ private final OrganizationDto organizationDto;
+
+ private CallLog(DefinedQProfile definedQProfile, OrganizationDto organizationDto) {
+ this.definedQProfile = definedQProfile;
+ this.organizationDto = organizationDto;
+ }
+
+ public DefinedQProfile getDefinedQProfile() {
+ return definedQProfile;
+ }
+
+ public OrganizationDto getOrganizationDto() {
+ return organizationDto;
+ }
+ }
+}