LCOV - code coverage report
Current view: top level - server/restapi/change - Votes.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 22 24 91.7 %
Date: 2022-11-19 15:00:39 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2014 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.restapi.change;
      16             : 
      17             : import com.google.gerrit.entities.PatchSetApproval;
      18             : import com.google.gerrit.extensions.registration.DynamicMap;
      19             : import com.google.gerrit.extensions.restapi.AuthException;
      20             : import com.google.gerrit.extensions.restapi.ChildCollection;
      21             : import com.google.gerrit.extensions.restapi.IdString;
      22             : import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
      23             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      24             : import com.google.gerrit.extensions.restapi.Response;
      25             : import com.google.gerrit.extensions.restapi.RestReadView;
      26             : import com.google.gerrit.extensions.restapi.RestView;
      27             : import com.google.gerrit.server.approval.ApprovalsUtil;
      28             : import com.google.gerrit.server.change.ReviewerResource;
      29             : import com.google.gerrit.server.change.VoteResource;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.Singleton;
      32             : import java.util.Map;
      33             : import java.util.TreeMap;
      34             : 
      35             : @Singleton
      36             : public class Votes implements ChildCollection<ReviewerResource, VoteResource> {
      37             :   private final DynamicMap<RestView<VoteResource>> views;
      38             :   private final List list;
      39             : 
      40             :   @Inject
      41         138 :   Votes(DynamicMap<RestView<VoteResource>> views, List list) {
      42         138 :     this.views = views;
      43         138 :     this.list = list;
      44         138 :   }
      45             : 
      46             :   @Override
      47             :   public DynamicMap<RestView<VoteResource>> views() {
      48           2 :     return views;
      49             :   }
      50             : 
      51             :   @Override
      52             :   public RestView<ReviewerResource> list() throws AuthException {
      53           2 :     return list;
      54             :   }
      55             : 
      56             :   @Override
      57             :   public VoteResource parse(ReviewerResource reviewer, IdString id)
      58             :       throws ResourceNotFoundException, AuthException, MethodNotAllowedException {
      59           2 :     if (reviewer.getRevisionResource() != null && !reviewer.getRevisionResource().isCurrent()) {
      60           0 :       throw new MethodNotAllowedException("Cannot access on non-current patch set");
      61             :     }
      62           2 :     return new VoteResource(reviewer, id.get());
      63             :   }
      64             : 
      65             :   @Singleton
      66             :   public static class List implements RestReadView<ReviewerResource> {
      67             :     private final ApprovalsUtil approvalsUtil;
      68             : 
      69             :     @Inject
      70         142 :     List(ApprovalsUtil approvalsUtil) {
      71         142 :       this.approvalsUtil = approvalsUtil;
      72         142 :     }
      73             : 
      74             :     @Override
      75             :     public Response<Map<String, Short>> apply(ReviewerResource rsrc)
      76             :         throws MethodNotAllowedException {
      77           8 :       if (rsrc.getRevisionResource() != null && !rsrc.getRevisionResource().isCurrent()) {
      78           0 :         throw new MethodNotAllowedException("Cannot list votes on non-current patch set");
      79             :       }
      80             : 
      81           8 :       Map<String, Short> votes = new TreeMap<>();
      82           8 :       Iterable<PatchSetApproval> byPatchSetUser =
      83           8 :           approvalsUtil.byPatchSetUser(
      84           8 :               rsrc.getChangeResource().getNotes(),
      85           8 :               rsrc.getChange().currentPatchSetId(),
      86           8 :               rsrc.getReviewerUser().getAccountId());
      87           8 :       for (PatchSetApproval psa : byPatchSetUser) {
      88           7 :         votes.put(psa.label(), psa.value());
      89           7 :       }
      90           8 :       return Response.ok(votes);
      91             :     }
      92             :   }
      93             : }

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