Coverage Report - com.jcabi.github.RtReferences
 
Classes in this File Line Coverage Branch Coverage Complexity
RtReferences
88%
15/17
0%
0/22
1
RtReferences$1
50%
1/2
N/A
1
RtReferences$2
100%
2/2
N/A
1
RtReferences$AjcClosure1
0%
0/1
N/A
1
RtReferences$AjcClosure11
100%
1/1
N/A
1
RtReferences$AjcClosure13
100%
1/1
N/A
1
RtReferences$AjcClosure15
100%
1/1
N/A
1
RtReferences$AjcClosure3
100%
1/1
N/A
1
RtReferences$AjcClosure5
100%
1/1
N/A
1
RtReferences$AjcClosure7
100%
1/1
N/A
1
RtReferences$AjcClosure9
100%
1/1
N/A
1
 
 1  8
 /**
 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.validation.constraints.NotNull;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Github references.
 46  
  *
 47  
  * @author Mihai Andronache (amihaiemil@gmail.com)
 48  
  * @version $Id$
 49  
  * @checkstyle MultipleStringLiterals (500 lines)
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = {"entry", "request", "owner" })
 54  
 final class RtReferences implements References {
 55  
 
 56  
     /**
 57  
      * Reference field name in JSON.
 58  
      */
 59  
     private static final String REF = "ref";
 60  
 
 61  
     /**
 62  
      * RESTful API entry point.
 63  
      */
 64  
     private final transient Request entry;
 65  
 
 66  
     /**
 67  
      * RESTful request for the commits.
 68  
      */
 69  
     private final transient Request request;
 70  
 
 71  
     /**
 72  
      * Repository.
 73  
      */
 74  
     private final transient Repo owner;
 75  
 
 76  
     /**
 77  
      * Public constructor.
 78  
      * @param req RESTful request.
 79  
      * @param repo The owner repo.
 80  
      */
 81  6
     RtReferences(final Request req, final Repo repo) {
 82  6
         this.entry = req;
 83  6
         this.owner = repo;
 84  6
         this.request = req.uri().path("/repos").path(repo.coordinates().user())
 85  
             .path(repo.coordinates().repo()).path("/git").path("/refs").back();
 86  6
     }
 87  
 
 88  
     @Override
 89  
     @NotNull(message = "repository is never NULL")
 90  
     public Repo repo() {
 91  0
         return this.owner;
 92  
     }
 93  
 
 94  
     @Override
 95  
     @NotNull(message = "reference is never NULL")
 96  
     public Reference create(
 97  
         @NotNull(message = "ref can't be NULL") final String ref,
 98  
         @NotNull(message = "sha can't be NULL") final String sha
 99  
     ) throws IOException {
 100  4
         final JsonObject json = Json.createObjectBuilder()
 101  
             .add("sha", sha).add(RtReferences.REF, ref).build();
 102  2
         return this.get(
 103  
             this.request.method(Request.POST)
 104  
                 .body().set(json).back()
 105  
                 .fetch().as(RestResponse.class)
 106  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 107  
                 .as(JsonResponse.class)
 108  
                 .json().readObject().getString(RtReferences.REF)
 109  
         );
 110  
     }
 111  
 
 112  
     @Override
 113  
     @NotNull(message = "reference is never NULL")
 114  
     public Reference get(
 115  
         @NotNull(message = "identifier can't be NULL") final String identifier
 116  
     ) {
 117  8
         return new RtReference(this.entry, this.owner, identifier);
 118  
     }
 119  
 
 120  
     @Override
 121  
     @NotNull(message = "Iterable of references is never NULL")
 122  
     public Iterable<Reference> iterate() {
 123  2
         return new RtPagination<Reference>(
 124  
             this.request,
 125  1
             new RtValuePagination.Mapping<Reference, JsonObject>() {
 126  
                 @Override
 127  
                 public Reference map(final JsonObject object) {
 128  0
                     return RtReferences.this.get(
 129  
                         object.getString(RtReferences.REF)
 130  
                     );
 131  
                 }
 132  
             }
 133  
         );
 134  
     }
 135  
 
 136  
     @Override
 137  
     @NotNull(message = "Iterable of references is never NULL")
 138  
     public Iterable<Reference> iterate(
 139  
         @NotNull(message = "subnamespace can't be NULL")
 140  
         final String subnamespace
 141  
     ) {
 142  4
         return new RtPagination<Reference>(
 143  
             this.request.uri().path(subnamespace).back(),
 144  4
             new RtValuePagination.Mapping<Reference, JsonObject>() {
 145  
                 @Override
 146  
                 public Reference map(final JsonObject object) {
 147  2
                     return RtReferences.this.get(
 148  
                         object.getString(RtReferences.REF)
 149  
                     );
 150  
                 }
 151  
             }
 152  
         );
 153  
     }
 154  
 
 155  
     @Override
 156  
     @NotNull(message = "Iterable of tag references is never NULL")
 157  
     public Iterable<Reference> tags() {
 158  2
         return this.iterate("tags");
 159  
     }
 160  
 
 161  
     @Override
 162  
     @NotNull(message = "Iterable of head references is never NULL")
 163  
     public Iterable<Reference> heads() {
 164  2
         return this.iterate("heads");
 165  
     }
 166  
 
 167  
     @Override
 168  
     public void remove(
 169  
         @NotNull(message = "identifier can't be NULL") final String identifier
 170  
     ) throws IOException {
 171  2
         this.request.method(Request.DELETE)
 172  
             .uri().path(identifier).back().fetch()
 173  
             .as(RestResponse.class)
 174  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 175  1
     }
 176  
 
 177  
 }