Coverage Report - com.jcabi.github.mock.MkReleaseAssets
 
Classes in this File Line Coverage Branch Coverage Complexity
MkReleaseAssets
95%
39/41
0%
0/22
1
MkReleaseAssets$1
100%
3/3
N/A
1
MkReleaseAssets$AjcClosure1
100%
1/1
N/A
1
MkReleaseAssets$AjcClosure3
100%
1/1
N/A
1
MkReleaseAssets$AjcClosure5
100%
1/1
N/A
1
MkReleaseAssets$AjcClosure7
100%
1/1
N/A
1
 
 1  10
 /**
 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.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.github.Coordinates;
 35  
 import com.jcabi.github.Github;
 36  
 import com.jcabi.github.Release;
 37  
 import com.jcabi.github.ReleaseAsset;
 38  
 import com.jcabi.github.ReleaseAssets;
 39  
 import com.jcabi.xml.XML;
 40  
 import java.io.IOException;
 41  
 import javax.xml.bind.DatatypeConverter;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 import org.xembly.Directives;
 45  
 
 46  
 /**
 47  
  * Mock Github Release Assets.
 48  
  *
 49  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 50  
  * @version $Id: 96d69ac801906447f860d034d9e8fe62290d4e1d $
 51  
  * @since 0.8
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @ToString
 56  0
 @EqualsAndHashCode(of = { "storage", "coords", "rel" })
 57  
 final class MkReleaseAssets implements ReleaseAssets {
 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  
      * Public ctor.
 80  
      * @param stg Storage
 81  
      * @param login User to login
 82  
      * @param rep Repo
 83  
      * @param number Release ID
 84  
      * @throws IOException If an IO Exception occurs
 85  
      * @checkstyle ParameterNumber (7 lines)
 86  
      */
 87  
     MkReleaseAssets(
 88  
         final MkStorage stg,
 89  
         final String login,
 90  
         final Coordinates rep,
 91  
         final int number
 92  12
     ) throws IOException {
 93  12
         this.storage = stg;
 94  12
         this.self = login;
 95  12
         this.coords = rep;
 96  12
         this.rel = number;
 97  24
         this.storage.apply(
 98  12
             new Directives().xpath(
 99  12
                 String.format(
 100  
                     // @checkstyle LineLengthCheck (1 line)
 101  
                     "/github/repos/repo[@coords='%s']/releases/release[id='%d']",
 102  12
                     this.coords, this.rel
 103  
                 )
 104  12
             ).addIf("assets")
 105  
         );
 106  12
     }
 107  
 
 108  
     @Override
 109  
     public Release release() {
 110  2
         return new MkRelease(
 111  
             this.storage,
 112  
             this.self,
 113  
             this.coords,
 114  
             this.rel
 115  
         );
 116  
     }
 117  
 
 118  
     @Override
 119  
     public Iterable<ReleaseAsset> iterate() {
 120  15
         return new MkIterable<ReleaseAsset>(
 121  
             this.storage,
 122  5
             String.format("%s/asset", this.xpath()),
 123  9
             new MkIterable.Mapping<ReleaseAsset>() {
 124  
                 @Override
 125  
                 public ReleaseAsset map(final XML xml) {
 126  8
                     return MkReleaseAssets.this.get(
 127  4
                         Integer.parseInt(xml.xpath("id/text()").get(0))
 128  
                     );
 129  
                 }
 130  
             }
 131  
         );
 132  
     }
 133  
 
 134  
     @Override
 135  
     public ReleaseAsset upload(
 136  
         final byte[] content,
 137  
         final String type,
 138  
         final String name
 139  
     ) throws IOException {
 140  22
         this.storage.lock();
 141  
         final int number;
 142  
         try {
 143  22
             number = 1 + this.storage.xml().xpath(
 144  11
                 String.format("%s/asset/id/text()", this.xpath())
 145  11
             ).size();
 146  21
             this.storage.apply(
 147  11
                 new Directives().xpath(this.xpath()).add("asset")
 148  11
                     .add("id").set(Integer.toString(number)).up()
 149  11
                     .add("name").set(name).up()
 150  22
                     .add("content").set(
 151  11
                         DatatypeConverter.printBase64Binary(content)
 152  11
                     ).up()
 153  11
                     .add("content_type").set(type).up()
 154  11
                     .add("size").set(Integer.toString(content.length)).up()
 155  11
                     .add("download_count").set("42").up()
 156  11
                     .add("created_at").set(new Github.Time().toString()).up()
 157  10
                     .add("updated_at").set(new Github.Time().toString()).up()
 158  10
                     .add("url").set("http://localhost/1").up()
 159  11
                     .add("html_url").set("http://localhost/2").up()
 160  
             );
 161  
         } finally {
 162  11
             this.storage.unlock();
 163  11
         }
 164  11
         return this.get(number);
 165  
     }
 166  
 
 167  
     @Override
 168  
     public ReleaseAsset get(final int number) {
 169  36
         return new MkReleaseAsset(
 170  
             this.storage,
 171  
             this.self,
 172  
             this.coords,
 173  
             this.rel,
 174  
             number
 175  
         );
 176  
     }
 177  
 
 178  
     /**
 179  
      * XPath of this element in XML tree.
 180  
      * @return XPath
 181  
      */
 182  
     private String xpath() {
 183  54
         return String.format(
 184  
             "/github/repos/repo[@coords='%s']/releases/release[id='%d']/assets",
 185  27
             this.coords, this.rel
 186  
         );
 187  
     }
 188  
 }