LCOV - code coverage report
Current view: top level - testing - TestCommentHelper.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 74 74 100.0 %
Date: 2022-11-19 15:00:39 Functions: 20 20 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2019 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.testing;
      16             : 
      17             : import static java.util.stream.Collectors.toList;
      18             : 
      19             : import com.google.common.collect.ImmutableList;
      20             : import com.google.gerrit.entities.Change;
      21             : import com.google.gerrit.extensions.api.GerritApi;
      22             : import com.google.gerrit.extensions.api.changes.DraftInput;
      23             : import com.google.gerrit.extensions.api.changes.ReviewInput;
      24             : import com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput;
      25             : import com.google.gerrit.extensions.client.Comment;
      26             : import com.google.gerrit.extensions.client.Comment.Range;
      27             : import com.google.gerrit.extensions.client.Side;
      28             : import com.google.gerrit.extensions.common.CommentInfo;
      29             : import com.google.gerrit.extensions.common.FixSuggestionInfo;
      30             : import com.google.gerrit.server.ChangeMessagesUtil;
      31             : import com.google.inject.Inject;
      32             : import java.util.Arrays;
      33             : import java.util.Collection;
      34             : import java.util.Collections;
      35             : import java.util.HashMap;
      36             : import java.util.List;
      37             : 
      38             : /** Test helper for dealing with comments/drafts. */
      39             : public class TestCommentHelper {
      40             :   private final GerritApi gApi;
      41             : 
      42             :   @Inject
      43           6 :   public TestCommentHelper(GerritApi gerritApi) {
      44           6 :     gApi = gerritApi;
      45           6 :   }
      46             : 
      47             :   public DraftInput newDraft(String message) {
      48           2 :     return populate(new DraftInput(), "file", message);
      49             :   }
      50             : 
      51             :   public DraftInput newDraft(String message, String inReplyTo) {
      52           2 :     return populate(new DraftInput(), "file", createLineRange(), message, inReplyTo);
      53             :   }
      54             : 
      55             :   public DraftInput newDraft(String path, Side side, int line, String message) {
      56           2 :     DraftInput d = new DraftInput();
      57           2 :     return populate(d, path, side, line, message);
      58             :   }
      59             : 
      60             :   public void addDraft(String changeId, String revId, DraftInput in) throws Exception {
      61           2 :     gApi.changes().id(changeId).revision(revId).createDraft(in);
      62           2 :   }
      63             : 
      64             :   public void addDraft(String changeId, DraftInput in) throws Exception {
      65           1 :     gApi.changes().id(changeId).current().createDraft(in);
      66           1 :   }
      67             : 
      68             :   public List<CommentInfo> getPublishedComments(String changeId) throws Exception {
      69           3 :     return gApi.changes().id(changeId).commentsRequest().get().values().stream()
      70           3 :         .flatMap(Collection::stream)
      71           3 :         .collect(toList());
      72             :   }
      73             : 
      74             :   public static <C extends Comment> C populate(C c, String path, String message) {
      75           2 :     return populate(c, path, createLineRange(), message, null);
      76             :   }
      77             : 
      78             :   private static <C extends Comment> C populate(
      79             :       C c, String path, Range range, String message, String inReplyTo) {
      80           3 :     int line = range.startLine;
      81           3 :     c.path = path;
      82           3 :     c.side = Side.REVISION;
      83           3 :     c.parent = null;
      84           3 :     c.line = line != 0 ? line : null;
      85           3 :     c.message = message;
      86           3 :     c.inReplyTo = inReplyTo;
      87           3 :     if (line != 0) c.range = range;
      88           3 :     return c;
      89             :   }
      90             : 
      91             :   private static <C extends Comment> C populate(
      92             :       C c, String path, Side side, Range range, String message) {
      93           2 :     int line = range.startLine;
      94           2 :     c.path = path;
      95           2 :     c.side = side;
      96           2 :     c.parent = null;
      97           2 :     c.line = line != 0 ? line : null;
      98           2 :     c.message = message;
      99           2 :     if (line != 0) c.range = range;
     100           2 :     return c;
     101             :   }
     102             : 
     103             :   private static <C extends Comment> C populate(
     104             :       C c, String path, Side side, int line, String message) {
     105           2 :     return populate(c, path, side, createLineRange(line), message);
     106             :   }
     107             : 
     108             :   private static Range createLineRange() {
     109           3 :     Range range = new Range();
     110           3 :     range.startLine = 0;
     111           3 :     range.startCharacter = 1;
     112           3 :     range.endLine = 0;
     113           3 :     range.endCharacter = 5;
     114           3 :     return range;
     115             :   }
     116             : 
     117             :   private static Range createLineRange(int line) {
     118           2 :     Range range = new Range();
     119           2 :     range.startLine = line;
     120           2 :     range.startCharacter = 1;
     121           2 :     range.endLine = line;
     122           2 :     range.endCharacter = 5;
     123           2 :     return range;
     124             :   }
     125             : 
     126             :   public static RobotCommentInput createRobotCommentInputWithMandatoryFields(String path) {
     127           6 :     RobotCommentInput in = new RobotCommentInput();
     128           6 :     in.robotId = "happyRobot";
     129           6 :     in.robotRunId = "1";
     130           6 :     in.message = "nit: trailing whitespace";
     131           6 :     in.path = path;
     132           6 :     return in;
     133             :   }
     134             : 
     135             :   public static RobotCommentInput createRobotCommentInput(
     136             :       String path, FixSuggestionInfo... fixSuggestionInfos) {
     137           3 :     RobotCommentInput in = TestCommentHelper.createRobotCommentInputWithMandatoryFields(path);
     138           3 :     in.url = "http://www.happy-robot.com";
     139           3 :     in.properties = new HashMap<>();
     140           3 :     in.properties.put("key1", "value1");
     141           3 :     in.properties.put("key2", "value2");
     142           3 :     in.fixSuggestions = Arrays.asList(fixSuggestionInfos);
     143           3 :     return in;
     144             :   }
     145             : 
     146             :   public void addRobotComment(String targetChangeId, RobotCommentInput robotCommentInput)
     147             :       throws Exception {
     148           3 :     addRobotComment(targetChangeId, robotCommentInput, "robot comment test");
     149           3 :   }
     150             : 
     151             :   public void addRobotComment(Change.Id targetChangeId, RobotCommentInput robotCommentInput)
     152             :       throws Exception {
     153           2 :     addRobotComment(targetChangeId, robotCommentInput, "robot comment test");
     154           2 :   }
     155             : 
     156             :   public void addRobotComment(
     157             :       String targetChangeId, RobotCommentInput robotCommentInput, String message) throws Exception {
     158           3 :     ReviewInput reviewInput = createReviewInput(robotCommentInput, message);
     159           3 :     gApi.changes().id(targetChangeId).current().review(reviewInput);
     160           3 :   }
     161             : 
     162             :   public void addRobotComment(
     163             :       Change.Id targetChangeId, RobotCommentInput robotCommentInput, String message)
     164             :       throws Exception {
     165           2 :     ReviewInput reviewInput = createReviewInput(robotCommentInput, message);
     166           2 :     gApi.changes().id(targetChangeId.get()).current().review(reviewInput);
     167           2 :   }
     168             : 
     169             :   private ReviewInput createReviewInput(RobotCommentInput robotCommentInput, String message) {
     170           4 :     ReviewInput reviewInput = new ReviewInput();
     171           4 :     reviewInput.robotComments =
     172           4 :         Collections.singletonMap(robotCommentInput.path, ImmutableList.of(robotCommentInput));
     173           4 :     reviewInput.message = message;
     174           4 :     reviewInput.tag = ChangeMessagesUtil.AUTOGENERATED_TAG_PREFIX;
     175           4 :     return reviewInput;
     176             :   }
     177             : }

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