Coverage Report - com.jcabi.github.RtComments
 
Classes in this File Line Coverage Branch Coverage Complexity
RtComments
50%
7/14
0%
0/16
1
RtComments$1
0%
0/2
N/A
1
RtComments$AjcClosure1
0%
0/1
N/A
1
RtComments$AjcClosure3
0%
0/1
N/A
1
RtComments$AjcClosure5
0%
0/1
N/A
1
RtComments$AjcClosure7
0%
0/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 javax.json.Json;
 40  
 import javax.json.JsonObject;
 41  
 import javax.json.JsonStructure;
 42  
 import javax.validation.constraints.NotNull;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github comments.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id$
 50  
  * @since 0.1
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = { "request", "owner" })
 55  
 final class RtComments implements Comments {
 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 Issue owner;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param req Request
 75  
      * @param issue Issue
 76  
      */
 77  1
     RtComments(final Request req, final Issue issue) {
 78  1
         this.entry = req;
 79  1
         final Coordinates coords = issue.repo().coordinates();
 80  1
         this.request = this.entry.uri()
 81  
             .path("/repos")
 82  
             .path(coords.user())
 83  
             .path(coords.repo())
 84  
             .path("/issues")
 85  
             .path(Integer.toString(issue.number()))
 86  
             .path("/comments")
 87  
             .back();
 88  1
         this.owner = issue;
 89  1
     }
 90  
 
 91  
     @Override
 92  
     @NotNull(message = "toString is never NULL")
 93  
     public String toString() {
 94  0
         return this.request.uri().get().toString();
 95  
     }
 96  
 
 97  
     @Override
 98  
     @NotNull(message = "Issue is never NULL")
 99  
     public Issue issue() {
 100  0
         return this.owner;
 101  
     }
 102  
 
 103  
     @Override
 104  
     @NotNull(message = "Comment is never NULL")
 105  
     public Comment get(final int number) {
 106  0
         return new RtComment(this.entry, this.owner, number);
 107  
     }
 108  
 
 109  
     @Override
 110  
     @NotNull(message = "Comment is never NULL")
 111  
     public Comment post(@NotNull(message = "post text can't be NULL")
 112  
         final String text) throws IOException {
 113  0
         final JsonStructure json = Json.createObjectBuilder()
 114  
             .add("body", text)
 115  
             .build();
 116  0
         return this.get(
 117  
             this.request.method(Request.POST)
 118  
                 .body().set(json).back()
 119  
                 .fetch()
 120  
                 .as(RestResponse.class)
 121  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 122  
                 .as(JsonResponse.class)
 123  
                 // @checkstyle MultipleStringLiterals (1 line)
 124  
                 .json().readObject().getInt("id")
 125  
         );
 126  
     }
 127  
 
 128  
     @Override
 129  
     @NotNull(message = "Iterable of comments is never NULL")
 130  
     public Iterable<Comment> iterate() {
 131  0
         return new RtPagination<Comment>(
 132  
             this.request,
 133  0
             new RtValuePagination.Mapping<Comment, JsonObject>() {
 134  
                 @Override
 135  
                 public Comment map(final JsonObject object) {
 136  0
                     return RtComments.this.get(object.getInt("id"));
 137  
                 }
 138  
             }
 139  
         );
 140  
     }
 141  
 
 142  
 }