Coverage Report - com.jcabi.github.RtPulls
 
Classes in this File Line Coverage Branch Coverage Complexity
RtPulls
78%
11/14
0%
0/22
1
RtPulls$1
100%
2/2
N/A
1
RtPulls$AjcClosure1
0%
0/1
N/A
1
RtPulls$AjcClosure3
100%
1/1
N/A
1
RtPulls$AjcClosure5
100%
1/1
N/A
1
RtPulls$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 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 requests.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 50  
  * @version $Id$
 51  
  * @since 0.3
 52  
  * @checkstyle MultipleStringLiterals (500 lines)
 53  
  */
 54  
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @EqualsAndHashCode(of = { "entry", "request", "owner" })
 57  
 final class RtPulls implements Pulls {
 58  
 
 59  
     /**
 60  
      * API entry point.
 61  
      */
 62  
     private final transient Request entry;
 63  
 
 64  
     /**
 65  
      * RESTful request.
 66  
      */
 67  
     private final transient Request request;
 68  
 
 69  
     /**
 70  
      * Repository.
 71  
      */
 72  
     private final transient Repo owner;
 73  
 
 74  
     /**
 75  
      * Public ctor.
 76  
      * @param req Request
 77  
      * @param repo Repository
 78  
      */
 79  4
     RtPulls(final Request req, final Repo repo) {
 80  4
         this.entry = req;
 81  4
         final Coordinates coords = repo.coordinates();
 82  4
         this.request = this.entry.uri()
 83  
             .path("/repos")
 84  
             .path(coords.user())
 85  
             .path(coords.repo())
 86  
             .path("/pulls")
 87  
             .back();
 88  4
         this.owner = repo;
 89  4
     }
 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 = "repository is never NULL")
 99  
     public Repo repo() {
 100  0
         return this.owner;
 101  
     }
 102  
 
 103  
     @Override
 104  
     @NotNull(message = "pull is never NULL")
 105  
     public Pull get(final int number) {
 106  8
         return new RtPull(this.entry, this.owner, number);
 107  
     }
 108  
 
 109  
     @Override
 110  
     @NotNull(message = "pull is never NULL")
 111  
     public Pull create(
 112  
         @NotNull(message = "pull title is never NULL") final String title,
 113  
         @NotNull(message = "head is never NULL") final String head,
 114  
         @NotNull(message = "base is never NULL") final String base)
 115  
         throws IOException {
 116  2
         final JsonStructure json = Json.createObjectBuilder()
 117  
             .add("title", title)
 118  
             .add("head", head)
 119  
             .add("base", base)
 120  
             .build();
 121  1
         return this.get(
 122  
             this.request.method(Request.POST)
 123  
                 .body().set(json).back()
 124  
                 .fetch().as(RestResponse.class)
 125  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 126  
                 .as(JsonResponse.class)
 127  
                 .json().readObject().getInt("number")
 128  
         );
 129  
     }
 130  
 
 131  
     @Override
 132  
     @NotNull(message = "Iterable of pulls is never NULL")
 133  
     public Iterable<Pull> iterate(@NotNull(message = "params can't be NULL")
 134  
         final Map<String, String> params) {
 135  2
         return new RtPagination<Pull>(
 136  
             this.request.uri().queryParams(params).back(),
 137  3
             new RtValuePagination.Mapping<Pull, JsonObject>() {
 138  
                 @Override
 139  
                 public Pull map(final JsonObject object) {
 140  2
                     return RtPulls.this.get(object.getInt("number"));
 141  
                 }
 142  
             }
 143  
         );
 144  
     }
 145  
 
 146  
 }