Coverage Report - com.jcabi.github.RtRepoCommits
 
Classes in this File Line Coverage Branch Coverage Complexity
RtRepoCommits
81%
13/16
0%
0/22
1
RtRepoCommits$1
100%
2/2
N/A
1
RtRepoCommits$AjcClosure1
100%
1/1
N/A
1
RtRepoCommits$AjcClosure11
0%
0/1
N/A
1
RtRepoCommits$AjcClosure3
100%
1/1
N/A
1
RtRepoCommits$AjcClosure5
100%
1/1
N/A
1
RtRepoCommits$AjcClosure7
100%
1/1
N/A
1
RtRepoCommits$AjcClosure9
100%
1/1
N/A
1
 
 1  0
 /**
 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.RequestURI;
 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.JsonObject;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import javax.ws.rs.core.HttpHeaders;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Commits of a Github repository.
 47  
  * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
 48  
  * @version $Id$
 49  
  * @checkstyle MultipleStringLiteralsCheck (200 lines)
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "request", "owner", "entry" })
 54  
 final class RtRepoCommits implements RepoCommits {
 55  
 
 56  
     /**
 57  
      * RESTful API entry point.
 58  
      */
 59  
     private final transient Request entry;
 60  
 
 61  
     /**
 62  
      * RESTful request for the commits.
 63  
      */
 64  
     private final transient Request request;
 65  
 
 66  
     /**
 67  
      * RESTful request for the comparison.
 68  
      */
 69  
     private final transient Request comp;
 70  
 
 71  
     /**
 72  
      * Parent repository.
 73  
      */
 74  
     private final transient Repo owner;
 75  
 
 76  
     /**
 77  
      * Public ctor.
 78  
      * @param req Entry point of API
 79  
      * @param repo Repository
 80  
      */
 81  8
     RtRepoCommits(final Request req, final Repo repo) {
 82  8
         this.entry = req;
 83  8
         this.owner = repo;
 84  8
         final RequestURI rep = req.uri()
 85  
             .path("/repos")
 86  
             .path(repo.coordinates().user())
 87  
             .path(repo.coordinates().repo());
 88  8
         this.request = rep
 89  
             .path("/commits")
 90  
             .back();
 91  8
         this.comp = rep
 92  
             .path("/compare")
 93  
             .back();
 94  8
     }
 95  
 
 96  
     @Override
 97  
     @NotNull(message = "Iterable of commits is never NULL")
 98  
     public  Iterable<RepoCommit> iterate(
 99  
         @NotNull(message = "params can't be NULL")
 100  
         final Map<String, String> params
 101  
     ) {
 102  2
         return new RtPagination<RepoCommit>(
 103  
             this.request.uri().queryParams(params).back(),
 104  2
             new RtValuePagination.Mapping<RepoCommit, JsonObject>() {
 105  
                 @Override
 106  
                 public RepoCommit map(final JsonObject value) {
 107  1
                     return RtRepoCommits.this.get(value.getString("sha"));
 108  
                 }
 109  
             }
 110  
         );
 111  
     }
 112  
 
 113  
     @Override
 114  
     @NotNull(message = "commit is never NULL")
 115  
     public RepoCommit get(
 116  
         @NotNull(message = "sha can't be NULL") final String sha
 117  
     ) {
 118  6
         return new RtRepoCommit(this.entry, this.owner, sha);
 119  
     }
 120  
 
 121  
     @Override
 122  
     @NotNull(message = "commits comparison is never NULL")
 123  
     public CommitsComparison compare(
 124  
         @NotNull(message = "base is never NULL") final String base,
 125  
         @NotNull(message = "head is never NULL") final String head) {
 126  4
         return new RtCommitsComparison(this.entry, this.owner, base, head);
 127  
     }
 128  
 
 129  
     @Override
 130  
     @NotNull(message = "comparison result is never NULL")
 131  
     public String diff(
 132  
         @NotNull(message = "base is never NULL") final String base,
 133  
         @NotNull(message = "head is never NULL") final String head)
 134  
         throws IOException {
 135  2
         return this.comp.reset(HttpHeaders.ACCEPT)
 136  
             .header(HttpHeaders.ACCEPT, "application/vnd.github.v3.diff")
 137  
             .uri()
 138  
             .path(String.format("%s...%s", base, head))
 139  
             .back()
 140  
             .fetch().as(RestResponse.class)
 141  
             .assertStatus(HttpURLConnection.HTTP_OK)
 142  
             .body();
 143  
     }
 144  
 
 145  
     @Override
 146  
     @NotNull(message = "comparison result is never NULL")
 147  
     public String patch(
 148  
         @NotNull(message = "base is never NULL") final String base,
 149  
         @NotNull(message = "head is never NULL") final String head)
 150  
         throws IOException {
 151  2
         return this.comp.reset(HttpHeaders.ACCEPT)
 152  
             .header(HttpHeaders.ACCEPT, "application/vnd.github.v3.patch")
 153  
             .uri()
 154  
             .path(String.format("%s...%s", base, head))
 155  
             .back()
 156  
             .fetch().as(RestResponse.class)
 157  
             .assertStatus(HttpURLConnection.HTTP_OK)
 158  
             .body();
 159  
     }
 160  
 
 161  
     @Override
 162  
     @NotNull(message = "toString is never NULL")
 163  
     public String toString() {
 164  0
         return this.request.uri().get().toString();
 165  
     }
 166  
 
 167  
     @Override
 168  
     @NotNull(message = "JSON is never NULL")
 169  
     public JsonObject json() throws IOException {
 170  0
         return new RtJson(this.request).fetch();
 171  
     }
 172  
 }