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