LCOV - code coverage report
Current view: top level - server/api/groups - GroupApiImpl.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 75 101 74.3 %
Date: 2022-11-19 15:00:39 Functions: 20 20 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2015 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.api.groups;
      16             : 
      17             : import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
      18             : 
      19             : import com.google.gerrit.extensions.api.groups.GroupApi;
      20             : import com.google.gerrit.extensions.api.groups.OwnerInput;
      21             : import com.google.gerrit.extensions.common.AccountInfo;
      22             : import com.google.gerrit.extensions.common.DescriptionInput;
      23             : import com.google.gerrit.extensions.common.GroupAuditEventInfo;
      24             : import com.google.gerrit.extensions.common.GroupInfo;
      25             : import com.google.gerrit.extensions.common.GroupOptionsInfo;
      26             : import com.google.gerrit.extensions.common.Input;
      27             : import com.google.gerrit.extensions.common.NameInput;
      28             : import com.google.gerrit.extensions.restapi.RestApiException;
      29             : import com.google.gerrit.server.group.GroupResource;
      30             : import com.google.gerrit.server.restapi.group.AddMembers;
      31             : import com.google.gerrit.server.restapi.group.AddSubgroups;
      32             : import com.google.gerrit.server.restapi.group.DeleteMembers;
      33             : import com.google.gerrit.server.restapi.group.DeleteSubgroups;
      34             : import com.google.gerrit.server.restapi.group.GetAuditLog;
      35             : import com.google.gerrit.server.restapi.group.GetDescription;
      36             : import com.google.gerrit.server.restapi.group.GetDetail;
      37             : import com.google.gerrit.server.restapi.group.GetGroup;
      38             : import com.google.gerrit.server.restapi.group.GetName;
      39             : import com.google.gerrit.server.restapi.group.GetOptions;
      40             : import com.google.gerrit.server.restapi.group.GetOwner;
      41             : import com.google.gerrit.server.restapi.group.Index;
      42             : import com.google.gerrit.server.restapi.group.ListMembers;
      43             : import com.google.gerrit.server.restapi.group.ListSubgroups;
      44             : import com.google.gerrit.server.restapi.group.PutDescription;
      45             : import com.google.gerrit.server.restapi.group.PutName;
      46             : import com.google.gerrit.server.restapi.group.PutOptions;
      47             : import com.google.gerrit.server.restapi.group.PutOwner;
      48             : import com.google.inject.Inject;
      49             : import com.google.inject.assistedinject.Assisted;
      50             : import java.util.List;
      51             : 
      52             : class GroupApiImpl implements GroupApi {
      53             :   interface Factory {
      54             :     GroupApiImpl create(GroupResource rsrc);
      55             :   }
      56             : 
      57             :   private final GetGroup getGroup;
      58             :   private final GetDetail getDetail;
      59             :   private final GetName getName;
      60             :   private final PutName putName;
      61             :   private final GetOwner getOwner;
      62             :   private final PutOwner putOwner;
      63             :   private final GetDescription getDescription;
      64             :   private final PutDescription putDescription;
      65             :   private final GetOptions getOptions;
      66             :   private final PutOptions putOptions;
      67             :   private final ListMembers listMembers;
      68             :   private final AddMembers addMembers;
      69             :   private final DeleteMembers deleteMembers;
      70             :   private final ListSubgroups listSubgroups;
      71             :   private final AddSubgroups addSubgroups;
      72             :   private final DeleteSubgroups deleteSubgroups;
      73             :   private final GetAuditLog getAuditLog;
      74             :   private final GroupResource rsrc;
      75             :   private final Index index;
      76             : 
      77             :   @Inject
      78             :   GroupApiImpl(
      79             :       GetGroup getGroup,
      80             :       GetDetail getDetail,
      81             :       GetName getName,
      82             :       PutName putName,
      83             :       GetOwner getOwner,
      84             :       PutOwner putOwner,
      85             :       GetDescription getDescription,
      86             :       PutDescription putDescription,
      87             :       GetOptions getOptions,
      88             :       PutOptions putOptions,
      89             :       ListMembers listMembers,
      90             :       AddMembers addMembers,
      91             :       DeleteMembers deleteMembers,
      92             :       ListSubgroups listSubgroups,
      93             :       AddSubgroups addSubgroups,
      94             :       DeleteSubgroups deleteSubgroups,
      95             :       GetAuditLog getAuditLog,
      96             :       Index index,
      97          29 :       @Assisted GroupResource rsrc) {
      98          29 :     this.getGroup = getGroup;
      99          29 :     this.getDetail = getDetail;
     100          29 :     this.getName = getName;
     101          29 :     this.putName = putName;
     102          29 :     this.getOwner = getOwner;
     103          29 :     this.putOwner = putOwner;
     104          29 :     this.getDescription = getDescription;
     105          29 :     this.putDescription = putDescription;
     106          29 :     this.getOptions = getOptions;
     107          29 :     this.putOptions = putOptions;
     108          29 :     this.listMembers = listMembers;
     109          29 :     this.addMembers = addMembers;
     110          29 :     this.deleteMembers = deleteMembers;
     111          29 :     this.listSubgroups = listSubgroups;
     112          29 :     this.addSubgroups = addSubgroups;
     113          29 :     this.deleteSubgroups = deleteSubgroups;
     114          29 :     this.getAuditLog = getAuditLog;
     115          29 :     this.index = index;
     116          29 :     this.rsrc = rsrc;
     117          29 :   }
     118             : 
     119             :   @Override
     120             :   public GroupInfo get() throws RestApiException {
     121             :     try {
     122          15 :       return getGroup.apply(rsrc).value();
     123           0 :     } catch (Exception e) {
     124           0 :       throw asRestApiException("Cannot retrieve group", e);
     125             :     }
     126             :   }
     127             : 
     128             :   @Override
     129             :   public GroupInfo detail() throws RestApiException {
     130             :     try {
     131           3 :       return getDetail.apply(rsrc).value();
     132           0 :     } catch (Exception e) {
     133           0 :       throw asRestApiException("Cannot retrieve group", e);
     134             :     }
     135             :   }
     136             : 
     137             :   @Override
     138             :   public String name() throws RestApiException {
     139             :     try {
     140           2 :       return getName.apply(rsrc).value();
     141           0 :     } catch (Exception e) {
     142           0 :       throw asRestApiException("Cannot get group name", e);
     143             :     }
     144             :   }
     145             : 
     146             :   @Override
     147             :   public void name(String name) throws RestApiException {
     148           1 :     NameInput in = new NameInput();
     149           1 :     in.name = name;
     150             :     try {
     151           1 :       putName.apply(rsrc, in);
     152           1 :     } catch (Exception e) {
     153           1 :       throw asRestApiException("Cannot put group name", e);
     154           1 :     }
     155           1 :   }
     156             : 
     157             :   @Override
     158             :   public GroupInfo owner() throws RestApiException {
     159             :     try {
     160           1 :       return getOwner.apply(rsrc).value();
     161           0 :     } catch (Exception e) {
     162           0 :       throw asRestApiException("Cannot get group owner", e);
     163             :     }
     164             :   }
     165             : 
     166             :   @Override
     167             :   public void owner(String owner) throws RestApiException {
     168           1 :     OwnerInput in = new OwnerInput();
     169           1 :     in.owner = owner;
     170             :     try {
     171           1 :       putOwner.apply(rsrc, in);
     172           1 :     } catch (Exception e) {
     173           1 :       throw asRestApiException("Cannot put group owner", e);
     174           1 :     }
     175           1 :   }
     176             : 
     177             :   @Override
     178             :   public String description() throws RestApiException {
     179             :     try {
     180           1 :       return getDescription.apply(rsrc).value();
     181           0 :     } catch (Exception e) {
     182           0 :       throw asRestApiException("Cannot get group description", e);
     183             :     }
     184             :   }
     185             : 
     186             :   @Override
     187             :   public void description(String description) throws RestApiException {
     188           3 :     DescriptionInput in = new DescriptionInput();
     189           3 :     in.description = description;
     190             :     try {
     191           3 :       putDescription.apply(rsrc, in);
     192           0 :     } catch (Exception e) {
     193           0 :       throw asRestApiException("Cannot put group description", e);
     194           3 :     }
     195           3 :   }
     196             : 
     197             :   @Override
     198             :   public GroupOptionsInfo options() throws RestApiException {
     199             :     try {
     200           1 :       return getOptions.apply(rsrc).value();
     201           0 :     } catch (Exception e) {
     202           0 :       throw asRestApiException("Cannot get group options", e);
     203             :     }
     204             :   }
     205             : 
     206             :   @Override
     207             :   public void options(GroupOptionsInfo options) throws RestApiException {
     208             :     try {
     209           1 :       putOptions.apply(rsrc, options);
     210           0 :     } catch (Exception e) {
     211           0 :       throw asRestApiException("Cannot put group options", e);
     212           1 :     }
     213           1 :   }
     214             : 
     215             :   @Override
     216             :   public List<AccountInfo> members() throws RestApiException {
     217           5 :     return members(false);
     218             :   }
     219             : 
     220             :   @Override
     221             :   public List<AccountInfo> members(boolean recursive) throws RestApiException {
     222           5 :     listMembers.setRecursive(recursive);
     223             :     try {
     224           5 :       return listMembers.apply(rsrc).value();
     225           0 :     } catch (Exception e) {
     226           0 :       throw asRestApiException("Cannot list group members", e);
     227             :     }
     228             :   }
     229             : 
     230             :   @Override
     231             :   public void addMembers(List<String> members) throws RestApiException {
     232             :     try {
     233          12 :       addMembers.apply(rsrc, AddMembers.Input.fromMembers(members));
     234           1 :     } catch (Exception e) {
     235           1 :       throw asRestApiException("Cannot add group members", e);
     236          12 :     }
     237          12 :   }
     238             : 
     239             :   @Override
     240             :   public void removeMembers(List<String> members) throws RestApiException {
     241             :     try {
     242           5 :       deleteMembers.apply(rsrc, AddMembers.Input.fromMembers(members));
     243           0 :     } catch (Exception e) {
     244           0 :       throw asRestApiException("Cannot remove group members", e);
     245           5 :     }
     246           5 :   }
     247             : 
     248             :   @Override
     249             :   public List<GroupInfo> includedGroups() throws RestApiException {
     250             :     try {
     251           1 :       return listSubgroups.apply(rsrc).value();
     252           0 :     } catch (Exception e) {
     253           0 :       throw asRestApiException("Cannot list subgroups", e);
     254             :     }
     255             :   }
     256             : 
     257             :   @Override
     258             :   public void addGroups(List<String> groups) throws RestApiException {
     259             :     try {
     260           4 :       addSubgroups.apply(rsrc, AddSubgroups.Input.fromGroups(groups));
     261           1 :     } catch (Exception e) {
     262           1 :       throw asRestApiException("Cannot add subgroups", e);
     263           4 :     }
     264           4 :   }
     265             : 
     266             :   @Override
     267             :   public void removeGroups(List<String> groups) throws RestApiException {
     268             :     try {
     269           3 :       deleteSubgroups.apply(rsrc, AddSubgroups.Input.fromGroups(groups));
     270           0 :     } catch (Exception e) {
     271           0 :       throw asRestApiException("Cannot remove subgroups", e);
     272           3 :     }
     273           3 :   }
     274             : 
     275             :   @Override
     276             :   public List<? extends GroupAuditEventInfo> auditLog() throws RestApiException {
     277             :     try {
     278           1 :       return getAuditLog.apply(rsrc).value();
     279           0 :     } catch (Exception e) {
     280           0 :       throw asRestApiException("Cannot get audit log", e);
     281             :     }
     282             :   }
     283             : 
     284             :   @Override
     285             :   public void index() throws RestApiException {
     286             :     try {
     287           3 :       index.apply(rsrc, new Input());
     288           1 :     } catch (Exception e) {
     289           1 :       throw asRestApiException("Cannot index group", e);
     290           3 :     }
     291           3 :   }
     292             : }

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