Coverage Report - com.jcabi.github.RtReleaseAssets
 
Classes in this File Line Coverage Branch Coverage Complexity
RtReleaseAssets
94%
37/39
0%
0/20
1
RtReleaseAssets$1
100%
3/3
N/A
1
RtReleaseAssets$AjcClosure1
0%
0/1
N/A
1
RtReleaseAssets$AjcClosure3
100%
1/1
N/A
1
RtReleaseAssets$AjcClosure5
100%
1/1
N/A
1
RtReleaseAssets$AjcClosure7
100%
1/1
N/A
1
 
 1  0
 /**
 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 java.net.URI;
 40  
 import javax.json.JsonObject;
 41  
 import javax.ws.rs.core.HttpHeaders;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Github release assets.
 46  
  *
 47  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 48  
  * @version $Id: 0d5dcc9a247ddad4166980c89381e328caa4deae $
 49  
  * @since 0.8
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "request", "owner" })
 54  
 final class RtReleaseAssets implements ReleaseAssets {
 55  
 
 56  
     /**
 57  
      * API entry point.
 58  
      */
 59  
     private final transient Request entry;
 60  
 
 61  
     /**
 62  
      * RESTful request.
 63  
      */
 64  
     private final transient Request request;
 65  
 
 66  
     /**
 67  
      * Owner of assets.
 68  
      */
 69  
     private final transient Release owner;
 70  
 
 71  
     /**
 72  
      * Public ctor.
 73  
      * @param req Request
 74  
      * @param release Issue
 75  
      */
 76  
     RtReleaseAssets(
 77  
         final Request req,
 78  
         final Release release
 79  3
     ) {
 80  3
         this.entry = req;
 81  3
         final Coordinates coords = release.repo().coordinates();
 82  
         // @checkstyle MultipleStringLiteralsCheck (7 lines)
 83  3
         this.request = this.entry.uri()
 84  3
             .path("/repos")
 85  3
             .path(coords.user())
 86  3
             .path(coords.repo())
 87  3
             .path("/releases")
 88  3
             .path(Integer.toString(release.number()))
 89  3
             .path("/assets")
 90  3
             .back();
 91  3
         this.owner = release;
 92  3
     }
 93  
 
 94  
     @Override
 95  
     public Release release() {
 96  0
         return this.owner;
 97  
     }
 98  
 
 99  
     @Override
 100  
     public Iterable<ReleaseAsset> iterate() {
 101  3
         return new RtPagination<ReleaseAsset>(
 102  1
             this.request.uri().back()
 103  1
                 .method(Request.GET),
 104  3
             new RtValuePagination.Mapping<ReleaseAsset, JsonObject>() {
 105  
                 @Override
 106  
                 public ReleaseAsset map(final JsonObject value) {
 107  4
                     return RtReleaseAssets.this.get(
 108  
                     //@checkstyle MultipleStringLiteralsCheck (1 line)
 109  2
                         value.getInt("id")
 110  
                     );
 111  
                 }
 112  
             }
 113  
         );
 114  
     }
 115  
 
 116  
     @Override
 117  
     public ReleaseAsset upload(
 118  
         final byte[] content,
 119  
         final String type,
 120  
         final String name
 121  
     ) throws IOException {
 122  3
         return this.get(
 123  1
             this.request.uri()
 124  1
                 .set(URI.create("https://uploads.github.com"))
 125  1
                 .path("/repos")
 126  1
                 .path(this.owner.repo().coordinates().user())
 127  1
                 .path(this.owner.repo().coordinates().repo())
 128  1
                 .path("/releases")
 129  1
                 .path(String.valueOf(this.owner.number()))
 130  1
                 .path("/assets")
 131  1
                 .queryParam("name", name)
 132  1
                 .back()
 133  1
                 .method(Request.POST)
 134  1
                 .reset(HttpHeaders.CONTENT_TYPE)
 135  1
                 .header(HttpHeaders.CONTENT_TYPE, type)
 136  1
                 .body().set(content).back()
 137  1
                 .fetch().as(RestResponse.class)
 138  1
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 139  1
                 .as(JsonResponse.class)
 140  1
                 .json().readObject().getInt("id")
 141  
         );
 142  
     }
 143  
 
 144  
     @Override
 145  
     public ReleaseAsset get(final int number) {
 146  8
         return new RtReleaseAsset(this.entry, this.owner, number);
 147  
     }
 148  
 
 149  
 }