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 com.google.auto.value.AutoValue; 18 : import com.google.common.collect.ImmutableSet; 19 : import com.google.common.collect.Sets; 20 : import com.google.gerrit.acceptance.testsuite.ThrowingFunction; 21 : import com.google.gerrit.entities.Account; 22 : import com.google.gerrit.entities.AccountGroup; 23 : import java.util.Optional; 24 : import java.util.Set; 25 : 26 : @AutoValue 27 14 : public abstract class TestGroupCreation { 28 : 29 : public abstract Optional<String> name(); 30 : 31 : public abstract Optional<String> description(); 32 : 33 : public abstract Optional<AccountGroup.UUID> ownerGroupUuid(); 34 : 35 : public abstract Optional<Boolean> visibleToAll(); 36 : 37 : public abstract ImmutableSet<Account.Id> members(); 38 : 39 : public abstract ImmutableSet<AccountGroup.UUID> subgroups(); 40 : 41 : abstract ThrowingFunction<TestGroupCreation, AccountGroup.UUID> groupCreator(); 42 : 43 : public static Builder builder( 44 : ThrowingFunction<TestGroupCreation, AccountGroup.UUID> groupCreator) { 45 14 : return new AutoValue_TestGroupCreation.Builder().groupCreator(groupCreator); 46 : } 47 : 48 : @AutoValue.Builder 49 14 : public abstract static class Builder { 50 : 51 : public abstract Builder name(String name); 52 : 53 : public abstract Builder description(String description); 54 : 55 : public Builder clearDescription() { 56 1 : return description(""); 57 : } 58 : 59 : public abstract Builder ownerGroupUuid(AccountGroup.UUID ownerGroupUuid); 60 : 61 : public abstract Builder visibleToAll(boolean visibleToAll); 62 : 63 : public Builder clearMembers() { 64 1 : return members(ImmutableSet.of()); 65 : } 66 : 67 : public Builder members(Account.Id member1, Account.Id... otherMembers) { 68 2 : return members(Sets.union(ImmutableSet.of(member1), ImmutableSet.copyOf(otherMembers))); 69 : } 70 : 71 : public abstract Builder members(Set<Account.Id> members); 72 : 73 : abstract ImmutableSet.Builder<Account.Id> membersBuilder(); 74 : 75 : public Builder addMember(Account.Id member) { 76 7 : membersBuilder().add(member); 77 7 : return this; 78 : } 79 : 80 : public Builder clearSubgroups() { 81 1 : return subgroups(ImmutableSet.of()); 82 : } 83 : 84 : public Builder subgroups(AccountGroup.UUID subgroup1, AccountGroup.UUID... otherSubgroups) { 85 2 : return subgroups(Sets.union(ImmutableSet.of(subgroup1), ImmutableSet.copyOf(otherSubgroups))); 86 : } 87 : 88 : public abstract Builder subgroups(Set<AccountGroup.UUID> subgroups); 89 : 90 : abstract ImmutableSet.Builder<AccountGroup.UUID> subgroupsBuilder(); 91 : 92 : public Builder addSubgroup(AccountGroup.UUID subgroup) { 93 2 : subgroupsBuilder().add(subgroup); 94 2 : return this; 95 : } 96 : 97 : abstract Builder groupCreator( 98 : ThrowingFunction<TestGroupCreation, AccountGroup.UUID> groupCreator); 99 : 100 : abstract TestGroupCreation autoBuild(); 101 : 102 : /** 103 : * Executes the group creation as specified. 104 : * 105 : * @return the UUID of the created group 106 : */ 107 : public AccountGroup.UUID create() { 108 14 : TestGroupCreation groupCreation = autoBuild(); 109 14 : return groupCreation.groupCreator().applyAndThrowSilently(groupCreation); 110 : } 111 : } 112 : }