Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance.testsuite.group; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : 19 : import com.google.gerrit.entities.AccountGroup; 20 : import com.google.gerrit.entities.InternalGroup; 21 : import com.google.gerrit.exceptions.DuplicateKeyException; 22 : import com.google.gerrit.exceptions.NoSuchGroupException; 23 : import com.google.gerrit.server.GerritPersonIdent; 24 : import com.google.gerrit.server.ServerInitiated; 25 : import com.google.gerrit.server.account.GroupUuid; 26 : import com.google.gerrit.server.group.db.GroupDelta; 27 : import com.google.gerrit.server.group.db.Groups; 28 : import com.google.gerrit.server.group.db.GroupsUpdate; 29 : import com.google.gerrit.server.group.db.InternalGroupCreation; 30 : import com.google.gerrit.server.notedb.Sequences; 31 : import com.google.inject.Inject; 32 : import java.io.IOException; 33 : import java.util.Optional; 34 : import org.eclipse.jgit.errors.ConfigInvalidException; 35 : import org.eclipse.jgit.lib.PersonIdent; 36 : 37 : /** 38 : * The implementation of {@code GroupOperations}. 39 : * 40 : * <p>There is only one implementation of {@code GroupOperations}. Nevertheless, we keep the 41 : * separation between interface and implementation to enhance clarity. 42 : */ 43 : public class GroupOperationsImpl implements GroupOperations { 44 : private final Groups groups; 45 : private final GroupsUpdate groupsUpdate; 46 : private final Sequences seq; 47 : private final PersonIdent serverIdent; 48 : 49 : @Inject 50 : public GroupOperationsImpl( 51 : Groups groups, 52 : @ServerInitiated GroupsUpdate groupsUpdate, 53 : Sequences seq, 54 16 : @GerritPersonIdent PersonIdent serverIdent) { 55 16 : this.groups = groups; 56 16 : this.groupsUpdate = groupsUpdate; 57 16 : this.seq = seq; 58 16 : this.serverIdent = serverIdent; 59 16 : } 60 : 61 : @Override 62 : public PerGroupOperations group(AccountGroup.UUID groupUuid) { 63 11 : return new PerGroupOperationsImpl(groupUuid); 64 : } 65 : 66 : @Override 67 : public TestGroupCreation.Builder newGroup() { 68 14 : return TestGroupCreation.builder(this::createNewGroup); 69 : } 70 : 71 : private AccountGroup.UUID createNewGroup(TestGroupCreation groupCreation) 72 : throws ConfigInvalidException, IOException { 73 14 : InternalGroupCreation internalGroupCreation = toInternalGroupCreation(groupCreation); 74 14 : GroupDelta groupDelta = toGroupDelta(groupCreation); 75 14 : InternalGroup internalGroup = groupsUpdate.createGroup(internalGroupCreation, groupDelta); 76 14 : return internalGroup.getGroupUUID(); 77 : } 78 : 79 : private InternalGroupCreation toInternalGroupCreation(TestGroupCreation groupCreation) { 80 14 : AccountGroup.Id groupId = AccountGroup.id(seq.nextGroupId()); 81 14 : String groupName = groupCreation.name().orElse("group-with-id-" + groupId.get()); 82 14 : AccountGroup.UUID groupUuid = GroupUuid.make(groupName, serverIdent); 83 14 : AccountGroup.NameKey nameKey = AccountGroup.nameKey(groupName); 84 14 : return InternalGroupCreation.builder() 85 14 : .setId(groupId) 86 14 : .setGroupUUID(groupUuid) 87 14 : .setNameKey(nameKey) 88 14 : .build(); 89 : } 90 : 91 : private static GroupDelta toGroupDelta(TestGroupCreation groupCreation) { 92 14 : GroupDelta.Builder builder = GroupDelta.builder(); 93 14 : groupCreation.description().ifPresent(builder::setDescription); 94 14 : groupCreation.ownerGroupUuid().ifPresent(builder::setOwnerGroupUUID); 95 14 : groupCreation.visibleToAll().ifPresent(builder::setVisibleToAll); 96 14 : builder.setMemberModification(originalMembers -> groupCreation.members()); 97 14 : builder.setSubgroupModification(originalSubgroups -> groupCreation.subgroups()); 98 14 : return builder.build(); 99 : } 100 : 101 : private class PerGroupOperationsImpl implements PerGroupOperations { 102 : private final AccountGroup.UUID groupUuid; 103 : 104 11 : PerGroupOperationsImpl(AccountGroup.UUID groupUuid) { 105 11 : this.groupUuid = groupUuid; 106 11 : } 107 : 108 : @Override 109 : public boolean exists() { 110 1 : return getGroup(groupUuid).isPresent(); 111 : } 112 : 113 : @Override 114 : public TestGroup get() { 115 4 : Optional<InternalGroup> group = getGroup(groupUuid); 116 4 : checkState(group.isPresent(), "Tried to get non-existing test group"); 117 4 : return toTestGroup(group.get()); 118 : } 119 : 120 : private Optional<InternalGroup> getGroup(AccountGroup.UUID groupUuid) { 121 : try { 122 4 : return groups.getGroup(groupUuid); 123 0 : } catch (IOException | ConfigInvalidException e) { 124 0 : throw new IllegalStateException(e); 125 : } 126 : } 127 : 128 : private TestGroup toTestGroup(InternalGroup internalGroup) { 129 4 : return TestGroup.builder() 130 4 : .groupUuid(internalGroup.getGroupUUID()) 131 4 : .groupId(internalGroup.getId()) 132 4 : .nameKey(internalGroup.getNameKey()) 133 4 : .description(Optional.ofNullable(internalGroup.getDescription())) 134 4 : .ownerGroupUuid(internalGroup.getOwnerGroupUUID()) 135 4 : .visibleToAll(internalGroup.isVisibleToAll()) 136 4 : .createdOn(internalGroup.getCreatedOn()) 137 4 : .members(internalGroup.getMembers()) 138 4 : .subgroups(internalGroup.getSubgroups()) 139 4 : .build(); 140 : } 141 : 142 : @Override 143 : public TestGroupUpdate.Builder forUpdate() { 144 10 : return TestGroupUpdate.builder(this::updateGroup); 145 : } 146 : 147 : private void updateGroup(TestGroupUpdate groupUpdate) 148 : throws DuplicateKeyException, NoSuchGroupException, ConfigInvalidException, IOException { 149 9 : GroupDelta groupDelta = toGroupDelta(groupUpdate); 150 9 : groupsUpdate.updateGroup(groupUuid, groupDelta); 151 9 : } 152 : 153 : private GroupDelta toGroupDelta(TestGroupUpdate groupUpdate) { 154 9 : GroupDelta.Builder builder = GroupDelta.builder(); 155 9 : groupUpdate.name().map(AccountGroup::nameKey).ifPresent(builder::setName); 156 9 : groupUpdate.description().ifPresent(builder::setDescription); 157 9 : groupUpdate.ownerGroupUuid().ifPresent(builder::setOwnerGroupUUID); 158 9 : groupUpdate.visibleToAll().ifPresent(builder::setVisibleToAll); 159 9 : builder.setMemberModification(groupUpdate.memberModification()::apply); 160 9 : builder.setSubgroupModification(groupUpdate.subgroupModification()::apply); 161 9 : return builder.build(); 162 : } 163 : } 164 : }