Coverage Report - com.jcabi.github.RtIssue
 
Classes in this File Line Coverage Branch Coverage Complexity
RtIssue
77%
17/22
0%
0/20
1
RtIssue$1
50%
1/2
N/A
1
RtIssue$AjcClosure1
100%
1/1
N/A
1
RtIssue$AjcClosure11
0%
0/1
N/A
1
RtIssue$AjcClosure13
100%
1/1
N/A
1
RtIssue$AjcClosure15
100%
1/1
N/A
1
RtIssue$AjcClosure17
100%
1/1
N/A
1
RtIssue$AjcClosure3
100%
1/1
N/A
1
RtIssue$AjcClosure5
100%
1/1
N/A
1
RtIssue$AjcClosure7
100%
1/1
N/A
1
RtIssue$AjcClosure9
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.RestResponse;
 36  
 import java.io.IOException;
 37  
 import java.net.HttpURLConnection;
 38  
 import javax.json.JsonObject;
 39  
 import javax.validation.constraints.NotNull;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import org.hamcrest.Matchers;
 42  
 
 43  
 /**
 44  
  * Github issue.
 45  
  *
 46  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 47  
  * @version $Id$
 48  
  * @since 0.1
 49  
  * @checkstyle MultipleStringLiterals (500 lines)
 50  
  */
 51  0
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "request", "owner", "num" })
 54  
 @SuppressWarnings("PMD.TooManyMethods")
 55  0
 final class RtIssue implements Issue {
 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  
      * Repository we're in.
 69  
      */
 70  
     private final transient Repo owner;
 71  
 
 72  
     /**
 73  
      * Issue number.
 74  
      */
 75  
     private final transient int num;
 76  
 
 77  
     /**
 78  
      * Public ctor.
 79  
      * @param req Request
 80  
      * @param repo Repository
 81  
      * @param number Number of the get
 82  
      */
 83  15
     RtIssue(final Request req, final Repo repo, final int number) {
 84  15
         this.entry = req;
 85  15
         final Coordinates coords = repo.coordinates();
 86  15
         this.request = this.entry.uri()
 87  
             .path("/repos")
 88  
             .path(coords.user())
 89  
             .path(coords.repo())
 90  
             .path("/issues")
 91  
             .path(Integer.toString(number))
 92  
             .back();
 93  15
         this.owner = repo;
 94  15
         this.num = number;
 95  15
     }
 96  
 
 97  
     @Override
 98  
     @NotNull(message = "toString is never NULL")
 99  
     public String toString() {
 100  0
         return this.request.uri().get().toString();
 101  
     }
 102  
 
 103  
     @Override
 104  
     @NotNull(message = "repository is never NULL")
 105  
     public Repo repo() {
 106  6
         return this.owner;
 107  
     }
 108  
 
 109  
     @Override
 110  
     public int number() {
 111  14
         return this.num;
 112  
     }
 113  
 
 114  
     @Override
 115  
     @NotNull(message = "Comments can't be NULL")
 116  
     public Comments comments() {
 117  2
         return new RtComments(this.entry, this);
 118  
     }
 119  
 
 120  
     @Override
 121  
     @NotNull(message = "IssueLabels is never NULL")
 122  
     public IssueLabels labels() {
 123  2
         return new RtIssueLabels(this.entry, this);
 124  
     }
 125  
 
 126  
     @Override
 127  
     @NotNull(message = "Iterable of events is never NULL")
 128  
     public Iterable<Event> events() {
 129  2
         return new RtPagination<Event>(
 130  
             this.request.uri().path("/events").back(),
 131  1
             new RtValuePagination.Mapping<Event, JsonObject>() {
 132  
                 @Override
 133  
                 public Event map(final JsonObject object) {
 134  0
                     return new RtEvent(
 135  
                         RtIssue.this.entry,
 136  
                         RtIssue.this.owner,
 137  
                         object.getInt("id")
 138  
                     );
 139  
                 }
 140  
             }
 141  
         );
 142  
     }
 143  
 
 144  
     @Override
 145  
     public boolean exists() throws IOException {
 146  0
         return this.request.fetch().as(RestResponse.class)
 147  
             .assertStatus(
 148  
                 Matchers.isOneOf(
 149  
                     HttpURLConnection.HTTP_OK,
 150  
                     HttpURLConnection.HTTP_NOT_FOUND
 151  
             )
 152  
         ).status() == HttpURLConnection.HTTP_OK;
 153  
     }
 154  
 
 155  
     @Override
 156  
     @NotNull(message = "JSON is never NULL")
 157  
     public JsonObject json() throws IOException {
 158  8
         return new RtJson(this.request).fetch();
 159  
     }
 160  
 
 161  
     @Override
 162  
     public void patch(@NotNull(message = "JSON object can't be NULL")
 163  
         final JsonObject json) throws IOException {
 164  2
         new RtJson(this.request).patch(json);
 165  1
     }
 166  
 
 167  
     @Override
 168  
     public int compareTo(
 169  
         @NotNull(message = "issue can't be NULL") final Issue issue
 170  
     ) {
 171  4
         return this.number() - issue.number();
 172  
     }
 173  
 
 174  
 }