Coverage Report - com.jcabi.github.RtPullComments
 
Classes in this File Line Coverage Branch Coverage Complexity
RtPullComments
96%
57/59
0%
0/20
1
RtPullComments$1
100%
3/3
N/A
1
RtPullComments$2
100%
3/3
N/A
1
RtPullComments$AjcClosure1
0%
0/1
N/A
1
RtPullComments$AjcClosure11
100%
1/1
N/A
1
RtPullComments$AjcClosure13
100%
1/1
N/A
1
RtPullComments$AjcClosure3
100%
1/1
N/A
1
RtPullComments$AjcClosure5
100%
1/1
N/A
1
RtPullComments$AjcClosure7
100%
1/1
N/A
1
RtPullComments$AjcClosure9
100%
1/1
N/A
1
 
 1  0
 /**
 2  
  * Copyright (c) 2013-2017, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.github;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.response.JsonResponse;
 36  
 import com.jcabi.http.response.RestResponse;
 37  
 import java.io.IOException;
 38  
 import java.net.HttpURLConnection;
 39  
 import java.util.Map;
 40  
 import javax.json.Json;
 41  
 import javax.json.JsonObject;
 42  
 import javax.json.JsonStructure;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github pull comment.
 47  
  *
 48  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 49  
  * @version $Id: 973bb84b201937e882f5a160184ee514ca48175b $
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "request", "owner" })
 54  
 final class RtPullComments implements PullComments {
 55  
 
 56  
     /**
 57  
      * API entry point.
 58  
      */
 59  
     private final transient Request entry;
 60  
 
 61  
     /**
 62  
      * RESTful request.
 63  
      */
 64  
     private final transient Request request;
 65  
 
 66  
     /**
 67  
      * Owner of comments.
 68  
      */
 69  
     private final transient Pull owner;
 70  
 
 71  
     /**
 72  
      * Public ctor.
 73  
      * @param req Request
 74  
      * @param pull Pull
 75  
      */
 76  6
     RtPullComments(final Request req, final Pull pull) {
 77  6
         this.entry = req;
 78  6
         this.owner = pull;
 79  6
         this.request = this.entry.uri()
 80  
             // @checkstyle MultipleStringLiterals (8 lines)
 81  6
             .path("/repos")
 82  6
             .path(pull.repo().coordinates().user())
 83  6
             .path(pull.repo().coordinates().repo())
 84  6
             .path("/pulls")
 85  6
             .path(Integer.toString(pull.number()))
 86  6
             .path("/comments")
 87  6
             .back();
 88  6
     }
 89  
 
 90  
     @Override
 91  
     public Pull pull() {
 92  0
         return this.owner;
 93  
     }
 94  
 
 95  
     @Override
 96  
     public PullComment get(final int number) {
 97  14
         return new RtPullComment(this.entry, this.owner, number);
 98  
     }
 99  
 
 100  
     @Override
 101  
     public Iterable<PullComment> iterate(
 102  
         final Map<String, String> params
 103  
     ) {
 104  3
         return new RtPagination<PullComment>(
 105  1
             this.request.uri().queryParams(params).back(),
 106  3
             new RtValuePagination.Mapping<PullComment, JsonObject>() {
 107  
                 @Override
 108  
                 public PullComment map(final JsonObject value) {
 109  4
                     return RtPullComments.this.get(
 110  
                         // @checkstyle MultipleStringLiterals (3 lines)
 111  2
                         value.getInt("id")
 112  
                     );
 113  
                 }
 114  
             }
 115  
         );
 116  
     }
 117  
 
 118  
     @Override
 119  
     public Iterable<PullComment> iterate(
 120  
         final int number,
 121  
         final Map<String, String> params) {
 122  2
         final Request newreq = this.entry.uri()
 123  1
             .path("/repos")
 124  1
             .path(this.owner.repo().coordinates().user())
 125  1
             .path(this.owner.repo().coordinates().repo())
 126  1
             .path("/pulls")
 127  1
             .path(String.valueOf(number))
 128  1
             .path("/comments")
 129  1
             .back();
 130  2
         return new RtPagination<PullComment>(
 131  1
             newreq.uri().queryParams(params).back(),
 132  3
             new RtValuePagination.Mapping<PullComment, JsonObject>() {
 133  
                 @Override
 134  
                 public PullComment map(final JsonObject value) {
 135  4
                     return RtPullComments.this.get(
 136  2
                         value.getInt("id")
 137  
                     );
 138  
                 }
 139  
             }
 140  
         );
 141  
     }
 142  
 
 143  
     // @checkstyle ParameterNumberCheck (7 lines)
 144  
     @Override
 145  
     public PullComment post(
 146  
         final String body,
 147  
         final String commit,
 148  
         final String path,
 149  
         final int position
 150  
     ) throws IOException {
 151  2
         final JsonStructure json = Json.createObjectBuilder()
 152  
             // @checkstyle MultipleStringLiterals (4 line)
 153  1
             .add("body", body)
 154  1
             .add("commit_id", commit)
 155  1
             .add("path", path)
 156  1
             .add("position", position)
 157  1
             .build();
 158  2
         return this.get(
 159  1
             this.request.method(Request.POST)
 160  1
                 .body().set(json).back()
 161  1
                 .fetch()
 162  1
                 .as(RestResponse.class)
 163  1
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 164  1
                 .as(JsonResponse.class)
 165  1
                 .json().readObject().getInt("id")
 166  
         );
 167  
     }
 168  
 
 169  
     @Override
 170  
     public PullComment reply(
 171  
         final String body,
 172  
         final int comment
 173  
     ) throws IOException {
 174  2
         final JsonStructure json = Json.createObjectBuilder()
 175  1
             .add("body", body)
 176  1
             .add("in_reply_to", comment)
 177  1
             .build();
 178  2
         return this.get(
 179  1
             this.request.method(Request.POST)
 180  1
                 .body().set(json).back()
 181  1
                 .fetch()
 182  1
                 .as(RestResponse.class)
 183  1
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 184  1
                 .as(JsonResponse.class)
 185  1
                 .json().readObject().getInt("id")
 186  
         );
 187  
     }
 188  
 
 189  
     @Override
 190  
     public void remove(final int number) throws IOException {
 191  2
         this.request.uri().path(String.valueOf(number)).back()
 192  1
             .method(Request.DELETE)
 193  1
             .fetch().as(RestResponse.class)
 194  1
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 195  1
     }
 196  
 }