Coverage Report - com.jcabi.github.RtPullComments
 
Classes in this File Line Coverage Branch Coverage Complexity
RtPullComments
88%
16/18
0%
0/16
1
RtPullComments$1
100%
2/2
N/A
1
RtPullComments$2
100%
2/2
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-2015, 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 javax.validation.constraints.NotNull;
 44  
 import lombok.EqualsAndHashCode;
 45  
 
 46  
 /**
 47  
  * Github pull comment.
 48  
  *
 49  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 50  
  * @version $Id$
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = { "request", "owner" })
 55  
 final class RtPullComments implements PullComments {
 56  
 
 57  
     /**
 58  
      * API entry point.
 59  
      */
 60  
     private final transient Request entry;
 61  
 
 62  
     /**
 63  
      * RESTful request.
 64  
      */
 65  
     private final transient Request request;
 66  
 
 67  
     /**
 68  
      * Owner of comments.
 69  
      */
 70  
     private final transient Pull owner;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param req Request
 75  
      * @param pull Pull
 76  
      */
 77  6
     RtPullComments(final Request req, final Pull pull) {
 78  6
         this.entry = req;
 79  6
         this.owner = pull;
 80  6
         this.request = this.entry.uri()
 81  
             // @checkstyle MultipleStringLiterals (8 lines)
 82  
             .path("/repos")
 83  
             .path(pull.repo().coordinates().user())
 84  
             .path(pull.repo().coordinates().repo())
 85  
             .path("/pulls")
 86  
             .path(Integer.toString(pull.number()))
 87  
             .path("/comments")
 88  
             .back();
 89  6
     }
 90  
 
 91  
     @Override
 92  
     @NotNull(message = "Pull is never NUll")
 93  
     public Pull pull() {
 94  0
         return this.owner;
 95  
     }
 96  
 
 97  
     @Override
 98  
     @NotNull(message = "PullComment is never NULL")
 99  
     public PullComment get(final int number) {
 100  14
         return new RtPullComment(this.entry, this.owner, number);
 101  
     }
 102  
 
 103  
     @Override
 104  
     @NotNull(message = "Iterable of pull comments is never NULL")
 105  
     public Iterable<PullComment> iterate(
 106  
         @NotNull(message = "params can't be NULL")
 107  
         final Map<String, String> params
 108  
     ) {
 109  2
         return new RtPagination<PullComment>(
 110  
             this.request.uri().queryParams(params).back(),
 111  3
             new RtValuePagination.Mapping<PullComment, JsonObject>() {
 112  
                 @Override
 113  
                 public PullComment map(final JsonObject value) {
 114  2
                     return RtPullComments.this.get(
 115  
                         // @checkstyle MultipleStringLiterals (3 lines)
 116  
                         value.getInt("id")
 117  
                     );
 118  
                 }
 119  
             }
 120  
         );
 121  
     }
 122  
 
 123  
     @Override
 124  
     @NotNull(message = "Iterable of pull comments is never NULL")
 125  
     public Iterable<PullComment> iterate(
 126  
         @NotNull(message = "number can't be NULL") final int number,
 127  
         @NotNull(message = "params can't be NULL")
 128  
         final Map<String, String> params) {
 129  2
         final Request newreq = this.entry.uri()
 130  
             .path("/repos")
 131  
             .path(this.owner.repo().coordinates().user())
 132  
             .path(this.owner.repo().coordinates().repo())
 133  
             .path("/pulls")
 134  
             .path(String.valueOf(number))
 135  
             .path("/comments")
 136  
             .back();
 137  1
         return new RtPagination<PullComment>(
 138  
             newreq.uri().queryParams(params).back(),
 139  3
             new RtValuePagination.Mapping<PullComment, JsonObject>() {
 140  
                 @Override
 141  
                 public PullComment map(final JsonObject value) {
 142  2
                     return RtPullComments.this.get(
 143  
                         value.getInt("id")
 144  
                     );
 145  
                 }
 146  
             }
 147  
         );
 148  
     }
 149  
 
 150  
     // @checkstyle ParameterNumberCheck (7 lines)
 151  
     @Override
 152  
     @NotNull(message = "PullComment is never NULL")
 153  
     public PullComment post(
 154  
         @NotNull(message = "body can't be NULL") final String body,
 155  
         @NotNull(message = "commit can't be NULL") final String commit,
 156  
         @NotNull(message = "path can't be NULL") final String path,
 157  
         final int position
 158  
     ) throws IOException {
 159  2
         final JsonStructure json = Json.createObjectBuilder()
 160  
             // @checkstyle MultipleStringLiterals (4 line)
 161  
             .add("body", body)
 162  
             .add("commit_id", commit)
 163  
             .add("path", path)
 164  
             .add("position", position)
 165  
             .build();
 166  1
         return this.get(
 167  
             this.request.method(Request.POST)
 168  
                 .body().set(json).back()
 169  
                 .fetch()
 170  
                 .as(RestResponse.class)
 171  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 172  
                 .as(JsonResponse.class)
 173  
                 .json().readObject().getInt("id")
 174  
         );
 175  
     }
 176  
 
 177  
     @Override
 178  
     @NotNull(message = "pull comment is never NULL")
 179  
     public PullComment reply(
 180  
         @NotNull(message = "text can't be NULL") final String body,
 181  
         @NotNull(message = "comment can't be NULL") final int comment
 182  
     ) throws IOException {
 183  2
         final JsonStructure json = Json.createObjectBuilder()
 184  
             .add("body", body)
 185  
             .add("in_reply_to", comment)
 186  
             .build();
 187  1
         return this.get(
 188  
             this.request.method(Request.POST)
 189  
                 .body().set(json).back()
 190  
                 .fetch()
 191  
                 .as(RestResponse.class)
 192  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 193  
                 .as(JsonResponse.class)
 194  
                 .json().readObject().getInt("id")
 195  
         );
 196  
     }
 197  
 
 198  
     @Override
 199  
     public void remove(final int number) throws IOException {
 200  2
         this.request.uri().path(String.valueOf(number)).back()
 201  
             .method(Request.DELETE)
 202  
             .fetch().as(RestResponse.class)
 203  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 204  1
     }
 205  
 }