Coverage Report - com.jcabi.github.RtRepos
 
Classes in this File Line Coverage Branch Coverage Complexity
RtRepos
76%
10/13
0%
0/16
1
RtRepos$1
100%
2/2
N/A
1
RtRepos$AjcClosure1
0%
0/1
N/A
1
RtRepos$AjcClosure3
100%
1/1
N/A
1
RtRepos$AjcClosure5
100%
1/1
N/A
1
RtRepos$AjcClosure7
100%
1/1
N/A
1
RtRepos$AjcClosure9
100%
1/1
N/A
1
 
 1  34
 /**
 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.JsonObject;
 40  
 import javax.validation.constraints.NotNull;
 41  
 import lombok.EqualsAndHashCode;
 42  
 
 43  
 /**
 44  
  * Github repositories.
 45  
  *
 46  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 47  
  * @version $Id$
 48  
  * @since 0.8
 49  
  * @checkstyle ClassDataAbstractionCoupling (500 lines)
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "ghub", "entry" })
 54  
 final class RtRepos implements Repos {
 55  
 
 56  
     /**
 57  
      * Github.
 58  
      */
 59  
     private final transient Github ghub;
 60  
 
 61  
     /**
 62  
      * RESTful entry.
 63  
      */
 64  
     private final transient Request entry;
 65  
 
 66  
     /**
 67  
      * Public ctor.
 68  
      * @param github Github
 69  
      * @param req Request
 70  
      */
 71  18
     RtRepos(final Github github, final Request req) {
 72  18
         this.ghub = github;
 73  18
         this.entry = req;
 74  18
     }
 75  
 
 76  
     @Override
 77  
     @NotNull(message = "toString is never NULL")
 78  
     public String toString() {
 79  0
         return this.entry.uri().get().toString();
 80  
     }
 81  
 
 82  
     @Override
 83  
     @NotNull(message = "Github is never NULL")
 84  
     public Github github() {
 85  0
         return this.ghub;
 86  
     }
 87  
 
 88  
     @Override
 89  
     @NotNull(message = "repo is never NULL")
 90  
     public Repo create(@NotNull(message = "JSON can't be NULL")
 91  
         final JsonObject json) throws IOException {
 92  2
         return this.get(
 93  
             new Coordinates.Simple(
 94  
                 this.entry.uri().path("user/repos")
 95  
                     .back().method(Request.POST)
 96  
                     .body().set(json).back()
 97  
                     .fetch().as(RestResponse.class)
 98  
                     .assertStatus(HttpURLConnection.HTTP_CREATED)
 99  
                     .as(JsonResponse.class)
 100  
                     // @checkstyle MultipleStringLiterals (1 line)
 101  
                     .json().readObject().getString("full_name")
 102  
             )
 103  
         );
 104  
     }
 105  
 
 106  
     @Override
 107  
     @NotNull(message = "repo is never NULL")
 108  
     public Repo get(@NotNull(message = "coordinates can't be NULL")
 109  
         final Coordinates name) {
 110  34
         return new RtRepo(this.ghub, this.entry, name);
 111  
     }
 112  
 
 113  
     @Override
 114  
     public void remove(
 115  
         @NotNull(message = "coordinates can't be NULL")
 116  
         final Coordinates coords) throws IOException {
 117  2
         this.entry.uri().path("/repos")
 118  
             .back().method(Request.DELETE)
 119  
             .uri().path(coords.toString()).back()
 120  
             .fetch()
 121  
             .as(RestResponse.class)
 122  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 123  1
     }
 124  
 
 125  
     @Override
 126  
     public Iterable<Repo> iterate(
 127  
         @NotNull(message = "identifier can't be NULL")
 128  
         final String identifier) {
 129  2
         return new RtPagination<Repo>(
 130  
             this.entry.uri().queryParam("since", identifier).back(),
 131  3
             new RtValuePagination.Mapping<Repo, JsonObject>() {
 132  
                 @Override
 133  
                 public Repo map(final JsonObject object) {
 134  2
                     return RtRepos.this.get(
 135  
                         new Coordinates.Simple(object.getString("full_name"))
 136  
                     );
 137  
                 }
 138  
             }
 139  
         );
 140  
     }
 141  
 }