GsonGroup gsonGroup = mock(GsonGroup.class);
when(gsonGroup.getId()).thenReturn(number);
when(gsonGroup.getFullPath()).thenReturn(fullPath);
- when(gsonGroup.getDescription()).thenReturn(description);
return gsonGroup;
}
private String id;
@SerializedName("full_path")
private String fullPath;
- @SerializedName("description")
- private String description;
public GsonGroup() {
// http://stackoverflow.com/a/18645370/229031
- this("", "", "");
+ this("", "");
}
- private GsonGroup(String id, String fullPath, String description) {
+ private GsonGroup(String id, String fullPath) {
this.id = id;
this.fullPath = fullPath;
- this.description = description;
}
public String getId() {
return fullPath;
}
- public String getDescription() {
- return description;
- }
-
static List<GsonGroup> parse(String json) {
- Type collectionType = new TypeToken<Collection<GsonGroup>>() {
- }.getType();
+ Type collectionType = new TypeToken<Collection<GsonGroup>>() {}.getType();
Gson gson = new Gson();
return gson.fromJson(json, collectionType);
}
assertThat(groups.size()).isOne();
assertThat(groups.get(0).getId()).isEqualTo("123456789");
assertThat(groups.get(0).getFullPath()).isEqualTo("my-awesome-group/my-project");
- assertThat(groups.get(0).getDescription()).isEqualTo("toto");
}
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2023 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.usergroups.ws;
-
-import java.util.Optional;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.utils.System2;
-import org.sonar.core.util.UuidFactoryFast;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.user.ExternalGroupDto;
-import org.sonar.db.user.GroupDto;
-import org.sonar.server.common.group.service.GroupService;
-import org.sonar.server.management.ManagedInstanceService;
-import org.sonar.server.usergroups.DefaultGroupFinder;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-
-public class ExternalGroupServiceIT {
-
- private static final String GROUP_NAME = "GROUP_NAME";
- private static final String EXTERNAL_ID = "EXTERNAL_ID";
- private static final String EXTERNAL_IDENTITY_PROVIDER = "EXTERNAL_IDENTITY_PROVIDER";
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
- private final DbClient dbClient = dbTester.getDbClient();
- private final DbSession dbSession = dbTester.getSession();
-
- private final DefaultGroupFinder defaultGroupFinder = new DefaultGroupFinder(dbClient);
-
- private final ManagedInstanceService managedInstanceService = mock();
- private final GroupService groupService = new GroupService(dbClient, UuidFactoryFast.getInstance(), defaultGroupFinder, managedInstanceService);
-
- private final ExternalGroupService externalGroupService = new ExternalGroupService(dbClient, groupService);
-
- @Before
- public void setUp() {
- dbTester.users().insertDefaultGroup();
- }
-
- @Test
- public void createOrUpdateExternalGroup_whenNewGroup_shouldCreateIt() {
- externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, GROUP_NAME));
-
- assertGroupAndExternalGroup();
- }
-
- @Test
- public void createOrUpdateExternalGroup_whenExistingLocalGroup_shouldMatchAndMakeItExternal() {
- dbTester.users().insertGroup(GROUP_NAME);
-
- externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, GROUP_NAME));
-
- assertThat(dbTester.users().countAllGroups()).isEqualTo(2);
- assertGroupAndExternalGroup();
- }
-
- @Test
- public void createOrUpdateExternalGroup_whenExistingExternalGroup_shouldUpdate() {
- GroupDto existingGroupDto = dbTester.users().insertGroup(GROUP_NAME);
- dbTester.users().insertExternalGroup(new ExternalGroupDto(existingGroupDto.getUuid(), EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER));
-
- String updatedGroupName = "updated_" + GROUP_NAME;
- externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, updatedGroupName));
-
- Optional<GroupDto> groupDto = dbTester.users().selectGroup(updatedGroupName);
- assertThat(groupDto)
- .isPresent().get()
- .extracting(GroupDto::getName)
- .isEqualTo(updatedGroupName);
- }
-
- private void assertGroupAndExternalGroup() {
- Optional<GroupDto> groupDto = dbTester.users().selectGroup(GROUP_NAME);
- assertThat(groupDto)
- .isPresent().get()
- .extracting(GroupDto::getName).isEqualTo(GROUP_NAME);
-
- assertThat((dbTester.users().selectExternalGroupByGroupUuid(groupDto.get().getUuid())))
- .isPresent().get()
- .extracting(ExternalGroupDto::externalId, ExternalGroupDto::externalIdentityProvider)
- .containsExactly(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER);
- }
-
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2023 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.usergroups.ws;
-
-import java.util.Optional;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.user.ExternalGroupDto;
-import org.sonar.db.user.GroupDto;
-import org.sonar.server.common.group.service.GroupService;
-
-public class ExternalGroupService {
-
- private static final Logger LOG = LoggerFactory.getLogger(ExternalGroupService.class);
-
- private final DbClient dbClient;
- private final GroupService groupService;
-
- public ExternalGroupService(DbClient dbClient, GroupService groupService) {
- this.dbClient = dbClient;
- this.groupService = groupService;
- }
-
- public void createOrUpdateExternalGroup(DbSession dbSession, GroupRegistration groupRegistration) {
- Optional<GroupDto> groupDto = retrieveGroupByExternalInformation(dbSession, groupRegistration);
- if (groupDto.isPresent()) {
- updateExternalGroup(dbSession, groupDto.get(), groupRegistration.name());
- } else {
- createOrMatchExistingLocalGroup(dbSession, groupRegistration);
- }
- }
-
- private Optional<GroupDto> retrieveGroupByExternalInformation(DbSession dbSession, GroupRegistration groupRegistration) {
- Optional<ExternalGroupDto> externalGroupDto = dbClient.externalGroupDao().selectByExternalIdAndIdentityProvider(dbSession, groupRegistration.externalId(),
- groupRegistration.externalIdentityProvider());
- return externalGroupDto.flatMap(existingExternalGroupDto -> Optional.ofNullable(dbClient.groupDao().selectByUuid(dbSession, existingExternalGroupDto.groupUuid())));
- }
-
- private void updateExternalGroup(DbSession dbSession, GroupDto groupDto, String newName) {
- LOG.debug("Updating external group: {} with new name {}", groupDto.getName(), newName);
- groupService.updateGroup(dbSession, groupDto, newName);
- }
-
- private void createOrMatchExistingLocalGroup(DbSession dbSession, GroupRegistration groupRegistration) {
- GroupDto groupDto = findOrCreateLocalGroup(dbSession, groupRegistration);
- createExternalGroup(dbSession, groupDto.getUuid(), groupRegistration);
- }
-
- private GroupDto findOrCreateLocalGroup(DbSession dbSession, GroupRegistration groupRegistration) {
- Optional<GroupDto> groupDto = groupService.findGroup(dbSession, groupRegistration.name());
- if (groupDto.isPresent()) {
- LOG.debug("Marking existing local group {} as managed group.", groupDto.get().getName());
- return groupDto.get();
- } else {
- LOG.debug("Creating new group {}", groupRegistration.name());
- return groupService.createGroup(dbSession, groupRegistration.name(), null).groupDto();
- }
- }
-
- private void createExternalGroup(DbSession dbSession, String groupUuid, GroupRegistration groupRegistration) {
- dbClient.externalGroupDao().insert(dbSession, new ExternalGroupDto(groupUuid, groupRegistration.externalId(), groupRegistration.externalIdentityProvider()));
- }
-
-}