Coverage Report - com.jcabi.github.RtReleaseAsset
 
Classes in this File Line Coverage Branch Coverage Complexity
RtReleaseAsset
93%
30/32
0%
0/22
1
RtReleaseAsset$AjcClosure1
100%
1/1
N/A
1
RtReleaseAsset$AjcClosure11
100%
1/1
N/A
1
RtReleaseAsset$AjcClosure3
100%
1/1
N/A
1
RtReleaseAsset$AjcClosure5
100%
1/1
N/A
1
RtReleaseAsset$AjcClosure7
100%
1/1
N/A
1
RtReleaseAsset$AjcClosure9
100%
1/1
N/A
1
 
 1  4
 /**
 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.RestResponse;
 36  
 import java.io.ByteArrayInputStream;
 37  
 import java.io.IOException;
 38  
 import java.io.InputStream;
 39  
 import java.net.HttpURLConnection;
 40  
 import javax.json.JsonObject;
 41  
 import javax.ws.rs.core.HttpHeaders;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Github release asset.
 46  
  *
 47  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 48  
  * @version $Id: 632de61706513f4882cbef2edddc439a54e8b2b0 $
 49  
  */
 50  
 @Immutable
 51  
 @Loggable(Loggable.DEBUG)
 52  0
 @EqualsAndHashCode(of = { "request", "owner", "num" })
 53  
 final class RtReleaseAsset implements ReleaseAsset {
 54  
 
 55  
     /**
 56  
      * RESTful request.
 57  
      */
 58  
     private final transient Request request;
 59  
 
 60  
     /**
 61  
      * Release we're in.
 62  
      */
 63  
     private final transient Release owner;
 64  
 
 65  
     /**
 66  
      * Release Asset number.
 67  
      */
 68  
     private final transient int num;
 69  
 
 70  
     /**
 71  
      * Public ctor.
 72  
      * @param req RESTful Request
 73  
      * @param release Release
 74  
      * @param number Number of the release asset.
 75  
      */
 76  
     RtReleaseAsset(
 77  
         final Request req,
 78  
         final Release release,
 79  
         final int number
 80  9
     ) {
 81  9
         final Coordinates coords = release.repo().coordinates();
 82  9
         this.request = req.uri()
 83  9
             .path("/repos")
 84  9
             .path(coords.user())
 85  9
             .path(coords.repo())
 86  9
             .path("/releases")
 87  9
             .path("/assets")
 88  9
             .path(Integer.toString(number))
 89  9
             .back();
 90  9
         this.owner = release;
 91  9
         this.num = number;
 92  9
     }
 93  
 
 94  
     @Override
 95  
     public String toString() {
 96  0
         return this.request.uri().get().toString();
 97  
     }
 98  
 
 99  
     @Override
 100  
     public Release release() {
 101  2
         return this.owner;
 102  
     }
 103  
 
 104  
     @Override
 105  
     public int number() {
 106  4
         return this.num;
 107  
     }
 108  
 
 109  
     @Override
 110  
     public JsonObject json() throws IOException {
 111  2
         return new RtJson(this.request).fetch();
 112  
     }
 113  
 
 114  
     @Override
 115  
     public void patch(
 116  
         final JsonObject json
 117  
     ) throws IOException {
 118  2
         new RtJson(this.request).patch(json);
 119  1
     }
 120  
 
 121  
     @Override
 122  
     public void remove() throws IOException {
 123  2
         this.request.method(Request.DELETE).fetch()
 124  1
             .as(RestResponse.class)
 125  1
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 126  1
     }
 127  
 
 128  
     /**
 129  
      * Get raw release asset content.
 130  
      *
 131  
      * @see <a href="http://developer.github.com/v3/repos/releases/">Releases API</a>
 132  
      * @return Stream with content
 133  
      * @throws IOException If some problem inside.
 134  
      */
 135  
     @Override
 136  
     public InputStream raw() throws IOException {
 137  3
         return new ByteArrayInputStream(
 138  1
             this.request.method(Request.GET)
 139  1
                 .reset(HttpHeaders.ACCEPT).header(
 140  
                     HttpHeaders.ACCEPT,
 141  
                     "application/vnd.github.v3.raw"
 142  
                 )
 143  1
                 .fetch()
 144  1
                 .as(RestResponse.class)
 145  1
                 .assertStatus(HttpURLConnection.HTTP_OK)
 146  1
                 .binary()
 147  
         );
 148  
     }
 149  
 
 150  
 }