LCOV - code coverage report
Current view: top level - server/group/testing - TestGroupBackend.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 31 35 88.6 %
Date: 2022-11-19 15:00:39 Functions: 13 15 86.7 %

          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.server.group.testing;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkState;
      18             : import static java.util.Objects.requireNonNull;
      19             : 
      20             : import com.google.common.collect.ImmutableList;
      21             : import com.google.gerrit.common.Nullable;
      22             : import com.google.gerrit.entities.Account;
      23             : import com.google.gerrit.entities.AccountGroup;
      24             : import com.google.gerrit.entities.GroupDescription;
      25             : import com.google.gerrit.entities.GroupReference;
      26             : import com.google.gerrit.server.CurrentUser;
      27             : import com.google.gerrit.server.account.GroupBackend;
      28             : import com.google.gerrit.server.account.GroupMembership;
      29             : import com.google.gerrit.server.project.ProjectState;
      30             : import java.util.Collection;
      31             : import java.util.HashMap;
      32             : import java.util.Map;
      33             : 
      34             : /** Implementation of GroupBackend for tests. */
      35          18 : public class TestGroupBackend implements GroupBackend {
      36             :   public static final String PREFIX = "testbackend:";
      37             : 
      38          18 :   private final Map<AccountGroup.UUID, GroupDescription.Basic> groups = new HashMap<>();
      39          18 :   private final Map<Account.Id, GroupMembership> memberships = new HashMap<>();
      40             : 
      41             :   /**
      42             :    * Create a group by name.
      43             :    *
      44             :    * @param name the group name, optionally prefixed by "testbackend:".
      45             :    * @return the created group
      46             :    */
      47             :   public GroupDescription.Basic create(String name) {
      48           3 :     requireNonNull(name);
      49           3 :     return create(AccountGroup.uuid(name.startsWith(PREFIX) ? name : PREFIX + name));
      50             :   }
      51             : 
      52             :   /**
      53             :    * Create a group by UUID.
      54             :    *
      55             :    * @param uuid the group UUID to add.
      56             :    * @return the created group
      57             :    */
      58             :   public GroupDescription.Basic create(AccountGroup.UUID uuid) {
      59           8 :     checkState(uuid.get().startsWith(PREFIX), "test group UUID must have prefix '" + PREFIX + "'");
      60           8 :     if (groups.containsKey(uuid)) {
      61           0 :       return groups.get(uuid);
      62             :     }
      63           8 :     GroupDescription.Basic group =
      64           8 :         new GroupDescription.Basic() {
      65             :           @Override
      66             :           public AccountGroup.UUID getGroupUUID() {
      67           7 :             return uuid;
      68             :           }
      69             : 
      70             :           @Override
      71             :           public String getName() {
      72           6 :             return uuid.get().substring(PREFIX.length());
      73             :           }
      74             : 
      75             :           @Override
      76             :           public String getEmailAddress() {
      77           0 :             return null;
      78             :           }
      79             : 
      80             :           @Override
      81             :           public String getUrl() {
      82           1 :             return null;
      83             :           }
      84             :         };
      85           8 :     groups.put(uuid, group);
      86           8 :     return group;
      87             :   }
      88             : 
      89             :   /**
      90             :    * Remove a group. No-op if the group does not exist.
      91             :    *
      92             :    * @param uuid the group.
      93             :    */
      94             :   public void remove(AccountGroup.UUID uuid) {
      95           1 :     groups.remove(uuid);
      96           1 :   }
      97             : 
      98             :   /**
      99             :    * Makes this backend return the specified {@link GroupMembership} when being asked for the
     100             :    * specified {@link com.google.gerrit.entities.Account.Id}.
     101             :    */
     102             :   public void setMembershipsOf(Account.Id user, GroupMembership membership) {
     103           7 :     memberships.put(user, membership);
     104           7 :   }
     105             : 
     106             :   @Override
     107             :   public boolean handles(AccountGroup.UUID uuid) {
     108          11 :     if (uuid != null) {
     109          11 :       String id = uuid.get();
     110          11 :       return id != null && id.startsWith(PREFIX);
     111             :     }
     112           1 :     return false;
     113             :   }
     114             : 
     115             :   @Nullable
     116             :   @Override
     117             :   public GroupDescription.Basic get(AccountGroup.UUID uuid) {
     118           8 :     return uuid == null ? null : groups.get(uuid);
     119             :   }
     120             : 
     121             :   @Override
     122             :   public Collection<GroupReference> suggest(String name, ProjectState project) {
     123           7 :     AccountGroup.UUID uuid = AccountGroup.uuid(name);
     124           7 :     if (handles(uuid)) {
     125           5 :       GroupDescription.Basic g = get(uuid);
     126           5 :       if (g != null) {
     127           4 :         return ImmutableList.of(GroupReference.forGroup(g));
     128             :       }
     129             :     }
     130           7 :     return ImmutableList.of();
     131             :   }
     132             : 
     133             :   @Override
     134             :   public GroupMembership membershipsOf(CurrentUser user) {
     135          17 :     if (!user.isIdentifiedUser()) {
     136           0 :       return GroupMembership.EMPTY;
     137             :     }
     138          17 :     return memberships.getOrDefault(user.getAccountId(), GroupMembership.EMPTY);
     139             :   }
     140             : 
     141             :   @Override
     142             :   public boolean isVisibleToAll(AccountGroup.UUID uuid) {
     143           0 :     return false;
     144             :   }
     145             : }

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