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