3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.usergroups.ws;
22 import java.util.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.core.util.UuidFactoryFast;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.user.ExternalGroupDto;
31 import org.sonar.db.user.GroupDto;
33 import static org.assertj.core.api.Assertions.assertThat;
35 public class ExternalGroupServiceIT {
37 private static final String GROUP_NAME = "GROUP_NAME";
38 private static final String EXTERNAL_ID = "EXTERNAL_ID";
39 private static final String EXTERNAL_IDENTITY_PROVIDER = "EXTERNAL_IDENTITY_PROVIDER";
42 public DbTester dbTester = DbTester.create(System2.INSTANCE);
43 private final DbClient dbClient = dbTester.getDbClient();
44 private final DbSession dbSession = dbTester.getSession();
45 private final GroupService groupService = new GroupService(dbClient, UuidFactoryFast.getInstance());
47 private final ExternalGroupService externalGroupService = new ExternalGroupService(dbClient, groupService);
50 public void createOrUpdateExternalGroup_whenNewGroup_shouldCreateIt() {
51 externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, GROUP_NAME));
53 assertGroupAndExternalGroup();
57 public void createOrUpdateExternalGroup_whenExistingLocalGroup_shouldMatchAndMakeItExternal() {
58 dbTester.users().insertGroup(GROUP_NAME);
60 externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, GROUP_NAME));
62 assertThat(dbTester.users().countAllGroups()).isEqualTo(1);
63 assertGroupAndExternalGroup();
67 public void createOrUpdateExternalGroup_whenExistingExternalGroup_shouldUpdate() {
68 dbTester.users().insertDefaultGroup();
69 GroupDto existingGroupDto = dbTester.users().insertGroup(GROUP_NAME);
70 dbTester.users().insertExternalGroup(new ExternalGroupDto(existingGroupDto.getUuid(), EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER));
72 String updatedGroupName = "updated_" + GROUP_NAME;
73 externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, updatedGroupName));
75 Optional<GroupDto> groupDto = dbTester.users().selectGroup(updatedGroupName);
78 .extracting(GroupDto::getName)
79 .isEqualTo(updatedGroupName);
82 private void assertGroupAndExternalGroup() {
83 Optional<GroupDto> groupDto = dbTester.users().selectGroup(GROUP_NAME);
86 .extracting(GroupDto::getName).isEqualTo(GROUP_NAME);
88 assertThat((dbTester.users().selectExternalGroupByGroupUuid(groupDto.get().getUuid())))
90 .extracting(ExternalGroupDto::externalId, ExternalGroupDto::externalIdentityProvider)
91 .containsExactly(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER);