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