LCOV - code coverage report
Current view: top level - acceptance - AccountCreator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 57 58 98.3 %
Date: 2022-11-19 15:00:39 Functions: 16 17 94.1 %

          Line data    Source code
       1             : // Copyright (C) 2013 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;
      16             : 
      17             : import static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.common.collect.ImmutableList;
      20             : import com.google.common.collect.ImmutableSet;
      21             : import com.google.common.collect.Sets;
      22             : import com.google.gerrit.common.Nullable;
      23             : import com.google.gerrit.entities.Account;
      24             : import com.google.gerrit.entities.AccountGroup;
      25             : import com.google.gerrit.entities.InternalGroup;
      26             : import com.google.gerrit.exceptions.NoSuchGroupException;
      27             : import com.google.gerrit.server.ServerInitiated;
      28             : import com.google.gerrit.server.account.AccountsUpdate;
      29             : import com.google.gerrit.server.account.GroupCache;
      30             : import com.google.gerrit.server.account.ServiceUserClassifier;
      31             : import com.google.gerrit.server.account.externalids.ExternalId;
      32             : import com.google.gerrit.server.account.externalids.ExternalIdFactory;
      33             : import com.google.gerrit.server.group.db.GroupDelta;
      34             : import com.google.gerrit.server.group.db.GroupsUpdate;
      35             : import com.google.gerrit.server.notedb.Sequences;
      36             : import com.google.inject.Inject;
      37             : import com.google.inject.Provider;
      38             : import com.google.inject.Singleton;
      39             : import java.io.IOException;
      40             : import java.util.ArrayList;
      41             : import java.util.Collection;
      42             : import java.util.HashMap;
      43             : import java.util.List;
      44             : import java.util.Map;
      45             : import java.util.Optional;
      46             : import org.eclipse.jgit.errors.ConfigInvalidException;
      47             : 
      48             : @Singleton
      49             : public class AccountCreator {
      50             :   private final Map<String, TestAccount> accounts;
      51             : 
      52             :   private final Sequences sequences;
      53             :   private final Provider<AccountsUpdate> accountsUpdateProvider;
      54             :   private final GroupCache groupCache;
      55             :   private final Provider<GroupsUpdate> groupsUpdateProvider;
      56             :   private final ExternalIdFactory externalIdFactory;
      57             : 
      58             :   @Inject
      59             :   AccountCreator(
      60             :       Sequences sequences,
      61             :       @ServerInitiated Provider<AccountsUpdate> accountsUpdateProvider,
      62             :       GroupCache groupCache,
      63             :       @ServerInitiated Provider<GroupsUpdate> groupsUpdateProvider,
      64         138 :       ExternalIdFactory externalIdFactory) {
      65         138 :     accounts = new HashMap<>();
      66         138 :     this.sequences = sequences;
      67         138 :     this.accountsUpdateProvider = accountsUpdateProvider;
      68         138 :     this.groupCache = groupCache;
      69         138 :     this.groupsUpdateProvider = groupsUpdateProvider;
      70         138 :     this.externalIdFactory = externalIdFactory;
      71         138 :   }
      72             : 
      73             :   public synchronized TestAccount create(
      74             :       @Nullable String username,
      75             :       @Nullable String email,
      76             :       @Nullable String fullName,
      77             :       @Nullable String displayName,
      78             :       String... groupNames)
      79             :       throws Exception {
      80             : 
      81         138 :     TestAccount account = accounts.get(username);
      82         138 :     if (account != null) {
      83         120 :       return account;
      84             :     }
      85         138 :     Account.Id id = Account.id(sequences.nextAccountId());
      86             : 
      87         138 :     List<ExternalId> extIds = new ArrayList<>(2);
      88         138 :     String httpPass = null;
      89         138 :     if (username != null) {
      90         138 :       httpPass = "http-pass";
      91         138 :       extIds.add(externalIdFactory.createUsername(username, id, httpPass));
      92             :     }
      93             : 
      94         138 :     if (email != null) {
      95         138 :       extIds.add(externalIdFactory.createEmail(id, email));
      96             :     }
      97             : 
      98         138 :     accountsUpdateProvider
      99         138 :         .get()
     100         138 :         .insert(
     101             :             "Create Test Account",
     102             :             id,
     103             :             u ->
     104         138 :                 u.setFullName(fullName)
     105         138 :                     .setDisplayName(displayName)
     106         138 :                     .setPreferredEmail(email)
     107         138 :                     .addExternalIds(extIds));
     108             : 
     109         138 :     ImmutableList.Builder<String> tags = ImmutableList.builder();
     110         138 :     if (groupNames != null) {
     111         138 :       for (String n : groupNames) {
     112         138 :         AccountGroup.NameKey k = AccountGroup.nameKey(n);
     113         138 :         Optional<InternalGroup> group = groupCache.get(k);
     114         138 :         if (!group.isPresent()) {
     115           0 :           throw new NoSuchGroupException(n);
     116             :         }
     117         138 :         addGroupMember(group.get().getGroupUUID(), id);
     118         138 :         if (ServiceUserClassifier.SERVICE_USERS.equals(n)) {
     119           3 :           tags.add("SERVICE_USER");
     120             :         }
     121             :       }
     122             :     }
     123             : 
     124         138 :     account =
     125         138 :         TestAccount.create(id, username, email, fullName, displayName, httpPass, tags.build());
     126         138 :     if (username != null) {
     127         138 :       accounts.put(username, account);
     128             :     }
     129         138 :     return account;
     130             :   }
     131             : 
     132             :   public TestAccount create(@Nullable String username, String group) throws Exception {
     133           1 :     return create(username, null, username, null, group);
     134             :   }
     135             : 
     136             :   public TestAccount create() throws Exception {
     137           5 :     return create(null);
     138             :   }
     139             : 
     140             :   public TestAccount create(@Nullable String username) throws Exception {
     141           8 :     return create(username, null, username, null, (String[]) null);
     142             :   }
     143             : 
     144             :   public TestAccount admin() throws Exception {
     145         138 :     return create("admin", "admin@example.com", "Administrator", "Adminny", "Administrators");
     146             :   }
     147             : 
     148             :   public TestAccount admin2() throws Exception {
     149          11 :     return create("admin2", "admin2@example.com", "Administrator2", null, "Administrators");
     150             :   }
     151             : 
     152             :   public TestAccount user1() throws Exception {
     153         134 :     return create("user1", "user1@example.com", "User1", null);
     154             :   }
     155             : 
     156             :   public TestAccount user2() throws Exception {
     157          20 :     return create("user2", "user2@example.com", "User2", null);
     158             :   }
     159             : 
     160             :   public TestAccount get(String username) {
     161         129 :     return requireNonNull(
     162         129 :         accounts.get(username), () -> String.format("No TestAccount created for %s ", username));
     163             :   }
     164             : 
     165             :   public void evict(Collection<Account.Id> ids) {
     166         132 :     accounts.values().removeIf(a -> ids.contains(a.id()));
     167         132 :   }
     168             : 
     169             :   public ImmutableList<TestAccount> getAll() {
     170           1 :     return ImmutableList.copyOf(accounts.values());
     171             :   }
     172             : 
     173             :   private void addGroupMember(AccountGroup.UUID groupUuid, Account.Id accountId)
     174             :       throws IOException, NoSuchGroupException, ConfigInvalidException {
     175             :     GroupDelta groupDelta =
     176         138 :         GroupDelta.builder()
     177         138 :             .setMemberModification(memberIds -> Sets.union(memberIds, ImmutableSet.of(accountId)))
     178         138 :             .build();
     179         138 :     groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta);
     180         138 :   }
     181             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750