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