Coverage Report - com.jcabi.github.RtGistComments
 
Classes in this File Line Coverage Branch Coverage Complexity
RtGistComments
76%
10/13
0%
0/16
1
RtGistComments$1
100%
2/2
N/A
1
RtGistComments$AjcClosure1
0%
0/1
N/A
1
RtGistComments$AjcClosure3
100%
1/1
N/A
1
RtGistComments$AjcClosure5
100%
1/1
N/A
1
RtGistComments$AjcClosure7
100%
1/1
N/A
1
 
 1  2
 /**
 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 Giang Le (giang@vn-smartsolutions.com)
 49  
  * @version $Id$
 50  
  * @since 0.8
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = { "request", "owner" })
 55  
 final class RtGistComments implements GistComments {
 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 Gist owner;
 70  
 
 71  
     /**
 72  
      * Public ctor.
 73  
      * @param req Request
 74  
      * @param gist Gist
 75  
      */
 76  3
     RtGistComments(final Request req, final Gist gist) {
 77  3
         this.entry = req;
 78  3
         this.request = this.entry.uri()
 79  
             .path("/gists")
 80  
             .path(gist.identifier())
 81  
             .path("/comments")
 82  
             .back();
 83  3
         this.owner = gist;
 84  3
     }
 85  
 
 86  
     @Override
 87  
     @NotNull(message = "toString is never NULL")
 88  
     public String toString() {
 89  0
         return this.request.uri().get().toString();
 90  
     }
 91  
 
 92  
     @Override
 93  
     @NotNull(message = "gist can't be NULL")
 94  
     public Gist gist() {
 95  0
         return this.owner;
 96  
     }
 97  
 
 98  
     @Override
 99  
     @NotNull(message = "comment can't be NULL")
 100  
     public GistComment get(final int number) {
 101  8
         return new RtGistComment(this.entry, this.owner, number);
 102  
     }
 103  
 
 104  
     @Override
 105  
     @NotNull(message = "GistComment is never NULL")
 106  
     public GistComment post(
 107  
         @NotNull(message = "text can't be NULL") final String text
 108  
     ) throws IOException {
 109  2
         final JsonStructure json = Json.createObjectBuilder()
 110  
             .add("body", text)
 111  
             .build();
 112  1
         return this.get(
 113  
             this.request.method(Request.POST)
 114  
                 .body().set(json).back()
 115  
                 .fetch()
 116  
                 .as(RestResponse.class)
 117  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 118  
                 .as(JsonResponse.class)
 119  
                 // @checkstyle MultipleStringLiterals (1 line)
 120  
                 .json().readObject().getInt("id")
 121  
         );
 122  
     }
 123  
 
 124  
     @Override
 125  
     @NotNull(message = "iterable of GistComment can't be NULL")
 126  
     public Iterable<GistComment> iterate() {
 127  2
         return new RtPagination<GistComment>(
 128  
             this.request,
 129  3
             new RtValuePagination.Mapping<GistComment, JsonObject>() {
 130  
                 @Override
 131  
                 public GistComment map(final JsonObject object) {
 132  2
                     return RtGistComments.this.get(object.getInt("id"));
 133  
                 }
 134  
             }
 135  
         );
 136  
     }
 137  
 }