diff options
Diffstat (limited to 'java/com/google/gerrit/acceptance/testsuite/group/GroupOperationsImpl.java')
-rw-r--r-- | java/com/google/gerrit/acceptance/testsuite/group/GroupOperationsImpl.java | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/java/com/google/gerrit/acceptance/testsuite/group/GroupOperationsImpl.java b/java/com/google/gerrit/acceptance/testsuite/group/GroupOperationsImpl.java new file mode 100644 index 0000000000..f9769c5fd6 --- /dev/null +++ b/java/com/google/gerrit/acceptance/testsuite/group/GroupOperationsImpl.java @@ -0,0 +1,159 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.acceptance.testsuite.group; + +import static com.google.common.base.Preconditions.checkState; + +import com.google.gerrit.common.errors.NoSuchGroupException; +import com.google.gerrit.reviewdb.client.AccountGroup; +import com.google.gerrit.server.GerritPersonIdent; +import com.google.gerrit.server.Sequences; +import com.google.gerrit.server.ServerInitiated; +import com.google.gerrit.server.account.GroupUUID; +import com.google.gerrit.server.group.InternalGroup; +import com.google.gerrit.server.group.db.Groups; +import com.google.gerrit.server.group.db.GroupsUpdate; +import com.google.gerrit.server.group.db.InternalGroupCreation; +import com.google.gerrit.server.group.db.InternalGroupUpdate; +import com.google.gwtorm.server.OrmDuplicateKeyException; +import com.google.gwtorm.server.OrmException; +import com.google.inject.Inject; +import java.io.IOException; +import java.util.Optional; +import org.eclipse.jgit.errors.ConfigInvalidException; +import org.eclipse.jgit.lib.PersonIdent; + +/** + * The implementation of {@code GroupOperations}. + * + * <p>There is only one implementation of {@code GroupOperations}. Nevertheless, we keep the + * separation between interface and implementation to enhance clarity. + */ +public class GroupOperationsImpl implements GroupOperations { + private final Groups groups; + private final GroupsUpdate groupsUpdate; + private final Sequences seq; + private final PersonIdent serverIdent; + + @Inject + public GroupOperationsImpl( + Groups groups, + @ServerInitiated GroupsUpdate groupsUpdate, + Sequences seq, + @GerritPersonIdent PersonIdent serverIdent) { + this.groups = groups; + this.groupsUpdate = groupsUpdate; + this.seq = seq; + this.serverIdent = serverIdent; + } + + @Override + public MoreGroupOperations group(AccountGroup.UUID groupUuid) { + return new MoreGroupOperationsImpl(groupUuid); + } + + @Override + public TestGroupCreation.Builder newGroup() { + return TestGroupCreation.builder(this::createNewGroup); + } + + private AccountGroup.UUID createNewGroup(TestGroupCreation groupCreation) + throws ConfigInvalidException, IOException, OrmException { + InternalGroupCreation internalGroupCreation = toInternalGroupCreation(groupCreation); + InternalGroupUpdate internalGroupUpdate = toInternalGroupUpdate(groupCreation); + InternalGroup internalGroup = + groupsUpdate.createGroup(internalGroupCreation, internalGroupUpdate); + return internalGroup.getGroupUUID(); + } + + private InternalGroupCreation toInternalGroupCreation(TestGroupCreation groupCreation) + throws OrmException { + AccountGroup.Id groupId = new AccountGroup.Id(seq.nextGroupId()); + String groupName = groupCreation.name().orElse("group-with-id-" + groupId.get()); + AccountGroup.UUID groupUuid = GroupUUID.make(groupName, serverIdent); + AccountGroup.NameKey nameKey = new AccountGroup.NameKey(groupName); + return InternalGroupCreation.builder() + .setId(groupId) + .setGroupUUID(groupUuid) + .setNameKey(nameKey) + .build(); + } + + private static InternalGroupUpdate toInternalGroupUpdate(TestGroupCreation groupCreation) { + InternalGroupUpdate.Builder builder = InternalGroupUpdate.builder(); + groupCreation.description().ifPresent(builder::setDescription); + groupCreation.ownerGroupUuid().ifPresent(builder::setOwnerGroupUUID); + groupCreation.visibleToAll().ifPresent(builder::setVisibleToAll); + builder.setMemberModification(originalMembers -> groupCreation.members()); + builder.setSubgroupModification(originalSubgroups -> groupCreation.subgroups()); + return builder.build(); + } + + private class MoreGroupOperationsImpl implements MoreGroupOperations { + private final AccountGroup.UUID groupUuid; + + MoreGroupOperationsImpl(AccountGroup.UUID groupUuid) { + this.groupUuid = groupUuid; + } + + @Override + public boolean exists() throws Exception { + return groups.getGroup(groupUuid).isPresent(); + } + + @Override + public TestGroup get() throws Exception { + Optional<InternalGroup> group = groups.getGroup(groupUuid); + checkState(group.isPresent(), "Tried to get non-existing test group"); + return toTestGroup(group.get()); + } + + private TestGroup toTestGroup(InternalGroup internalGroup) { + return TestGroup.builder() + .groupUuid(internalGroup.getGroupUUID()) + .groupId(internalGroup.getId()) + .nameKey(internalGroup.getNameKey()) + .description(Optional.ofNullable(internalGroup.getDescription())) + .ownerGroupUuid(internalGroup.getOwnerGroupUUID()) + .visibleToAll(internalGroup.isVisibleToAll()) + .createdOn(internalGroup.getCreatedOn()) + .members(internalGroup.getMembers()) + .subgroups(internalGroup.getSubgroups()) + .build(); + } + + @Override + public TestGroupUpdate.Builder forUpdate() { + return TestGroupUpdate.builder(this::updateGroup); + } + + private void updateGroup(TestGroupUpdate groupUpdate) + throws OrmDuplicateKeyException, NoSuchGroupException, ConfigInvalidException, IOException { + InternalGroupUpdate internalGroupUpdate = toInternalGroupUpdate(groupUpdate); + groupsUpdate.updateGroup(groupUuid, internalGroupUpdate); + } + + private InternalGroupUpdate toInternalGroupUpdate(TestGroupUpdate groupUpdate) { + InternalGroupUpdate.Builder builder = InternalGroupUpdate.builder(); + groupUpdate.name().map(AccountGroup.NameKey::new).ifPresent(builder::setName); + groupUpdate.description().ifPresent(builder::setDescription); + groupUpdate.ownerGroupUuid().ifPresent(builder::setOwnerGroupUUID); + groupUpdate.visibleToAll().ifPresent(builder::setVisibleToAll); + builder.setMemberModification(groupUpdate.memberModification()::apply); + builder.setSubgroupModification(groupUpdate.subgroupModification()::apply); + return builder.build(); + } + } +} |