Coverage Report - com.jcabi.github.mock.MkReleaseAsset
 
Classes in this File Line Coverage Branch Coverage Complexity
MkReleaseAsset
94%
18/19
40%
8/20
1
MkReleaseAsset$AjcClosure1
100%
1/1
N/A
1
MkReleaseAsset$AjcClosure11
100%
1/1
N/A
1
MkReleaseAsset$AjcClosure3
100%
1/1
N/A
1
MkReleaseAsset$AjcClosure5
100%
1/1
N/A
1
MkReleaseAsset$AjcClosure7
100%
1/1
N/A
1
MkReleaseAsset$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.mock;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.github.Coordinates;
 35  
 import com.jcabi.github.Release;
 36  
 import com.jcabi.github.ReleaseAsset;
 37  
 import java.io.ByteArrayInputStream;
 38  
 import java.io.IOException;
 39  
 import java.io.InputStream;
 40  
 import javax.json.JsonObject;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import javax.xml.bind.DatatypeConverter;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Mock Github release asset.
 49  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 50  
  * @version $Id$
 51  
  * @since 0.8
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @ToString
 56  1
 @EqualsAndHashCode(of = { "storage", "coords", "rel", "num" })
 57  
 final class MkReleaseAsset implements ReleaseAsset {
 58  
     /**
 59  
      * Storage.
 60  
      */
 61  
     private final transient MkStorage storage;
 62  
 
 63  
     /**
 64  
      * Login of the user logged in.
 65  
      */
 66  
     private final transient String self;
 67  
 
 68  
     /**
 69  
      * Repository coordinates.
 70  
      */
 71  
     private final transient Coordinates coords;
 72  
 
 73  
     /**
 74  
      * Release id.
 75  
      */
 76  
     private final transient int rel;
 77  
 
 78  
     /**
 79  
      * The asset id.
 80  
      */
 81  
     private final transient int num;
 82  
 
 83  
     /**
 84  
      * Public ctor.
 85  
      * @param stg Storage
 86  
      * @param login User to login
 87  
      * @param rep Repo
 88  
      * @param release Release ID
 89  
      * @param asset Asset ID
 90  
      * @checkstyle ParameterNumber (7 lines)
 91  
      */
 92  
     MkReleaseAsset(
 93  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 94  
         @NotNull(message = "login can't be NULL") final String login,
 95  
         @NotNull(message = "rep can't be NULL") final Coordinates rep,
 96  
         final int release,
 97  
         final int asset
 98  17
     ) {
 99  17
         this.storage = stg;
 100  17
         this.self = login;
 101  17
         this.coords = rep;
 102  17
         this.rel = release;
 103  17
         this.num = asset;
 104  17
     }
 105  
 
 106  
     @Override
 107  
     @NotNull(message = "JSON is never NULL")
 108  
     public JsonObject json() throws IOException {
 109  8
         return new JsonNode(
 110  
             this.storage.xml().nodes(this.xpath()).get(0)
 111  
         ).json();
 112  
     }
 113  
 
 114  
     @Override
 115  
     public void patch(
 116  
         @NotNull(message = "json can't be NULL") final JsonObject json
 117  
     ) throws IOException {
 118  2
         new JsonPatch(this.storage).patch(this.xpath(), json);
 119  1
     }
 120  
 
 121  
     @Override
 122  
     @NotNull(message = "Release is never NULL")
 123  
     public Release release() {
 124  2
         return new MkRelease(
 125  
             this.storage,
 126  
             this.self,
 127  
             this.coords,
 128  
             this.rel
 129  
         );
 130  
     }
 131  
 
 132  
     @Override
 133  
     public int number() {
 134  6
         return this.num;
 135  
     }
 136  
 
 137  
     /**
 138  
      * Remove asset.
 139  
      *
 140  
      * @throws IOException If there is any I/O problem
 141  
      */
 142  
     @Override
 143  
     public void remove() throws IOException {
 144  10
         this.storage.apply(
 145  
             new Directives().xpath(this.xpath()).strict(1).remove()
 146  
         );
 147  5
     }
 148  
 
 149  
     /**
 150  
      * Get raw release asset content.
 151  
      *
 152  
      * @see <a href="http://developer.github.com/v3/repos/releases/">Releases API</a>
 153  
      * @return Stream with content
 154  
      * @throws IOException If some problem inside.
 155  
      */
 156  
     @Override
 157  
     @NotNull(message = "Input stream is never NULL")
 158  
     public InputStream raw() throws IOException {
 159  2
         return new ByteArrayInputStream(
 160  
             DatatypeConverter.parseBase64Binary(
 161  
                 this.storage.xml().xpath(
 162  
                     String.format("%s/content/text()", this.xpath())
 163  
                 ).get(0)
 164  
             )
 165  
         );
 166  
     }
 167  
 
 168  
     /**
 169  
      * XPath of this element in XML tree.
 170  
      * @return XPath
 171  
      */
 172  
     @NotNull(message = "Xpath is never NULL")
 173  
     private String xpath() {
 174  11
         return String.format(
 175  
             // @checkstyle LineLength (1 line)
 176  
             "/github/repos/repo[@coords='%s']/releases/release[id='%d']/assets/asset[id='%d']",
 177  
             this.coords, this.rel, this.num
 178  
         );
 179  
     }
 180  
 }