Coverage Report - com.jcabi.github.RtForks
 
Classes in this File Line Coverage Branch Coverage Complexity
RtForks
75%
9/12
0%
0/16
1
RtForks$1
50%
1/2
N/A
1
RtForks$AjcClosure1
0%
0/1
N/A
1
RtForks$AjcClosure3
100%
1/1
N/A
1
RtForks$AjcClosure5
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 forks.
 47  
  *
 48  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 49  
  * @version $Id$
 50  
  * @since 0.8
 51  
  * @see <a href="http://developer.github.com/v3/repos/forks/">Forks API</a>
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @EqualsAndHashCode(of = {"request", "owner" })
 56  0
 final class RtForks implements Forks {
 57  
 
 58  
     /**
 59  
      * Fork's id name in JSON object.
 60  
      */
 61  
     public static final String ID = "id";
 62  
 
 63  
     /**
 64  
      * Restful Request.
 65  
      */
 66  
     private final transient Request request;
 67  
     /**
 68  
      * Repository.
 69  
      */
 70  
     private final transient Repo owner;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param req Request
 75  
      * @param repo Repository
 76  
      */
 77  2
     public RtForks(final Request req, final Repo repo) {
 78  2
         this.request = req.uri()
 79  
             .path("/repos")
 80  
             .path(repo.coordinates().user())
 81  
             .path(repo.coordinates().repo())
 82  
             .path("forks")
 83  
             .back();
 84  2
         this.owner = repo;
 85  2
     }
 86  
 
 87  
     @Override
 88  
     @NotNull(message = "Repository can't be null")
 89  
     public Repo repo() {
 90  0
         return this.owner;
 91  
     }
 92  
 
 93  
     @Override
 94  
     @NotNull(message = "iterable of Fork can't be NULL")
 95  
     public Iterable<Fork> iterate(
 96  
         @NotNull(message = "sort can't be NULL") final String sort) {
 97  2
         return new RtPagination<Fork>(
 98  
             this.request.uri().queryParam("sort", sort).back(),
 99  1
             new RtValuePagination.Mapping<Fork, JsonObject>() {
 100  
                 @Override
 101  
                 public Fork map(final JsonObject object) {
 102  0
                     return RtForks.this.get(object.getInt(ID));
 103  
                 }
 104  
             }
 105  
         );
 106  
     }
 107  
 
 108  
     @Override
 109  
     @NotNull(message = "Fork can't be NULL")
 110  
     public Fork create(
 111  
         @NotNull(message = "organization can't be NULL")
 112  
         final String organization) throws IOException {
 113  2
         final JsonStructure json = Json.createObjectBuilder()
 114  
             .add("organization", organization)
 115  
             .build();
 116  1
         return this.get(
 117  
             this.request.method(Request.POST)
 118  
                 .body().set(json).back()
 119  
                 .fetch().as(RestResponse.class)
 120  
                 .assertStatus(HttpURLConnection.HTTP_ACCEPTED)
 121  
                 .as(JsonResponse.class)
 122  
                 .json().readObject().getInt(ID)
 123  
         );
 124  
     }
 125  
 
 126  
     /**
 127  
      * Get fork by number.
 128  
      * @param number Fork number
 129  
      * @return Fork
 130  
      */
 131  
     private RtFork get(final Integer number) {
 132  1
         return new RtFork(this.request, this.owner, number);
 133  
     }
 134  
 }