Coverage Report - com.jcabi.github.mock.MkReleaseAssets
 
Classes in this File Line Coverage Branch Coverage Complexity
MkReleaseAssets
90%
18/20
0%
0/18
1
MkReleaseAssets$1
100%
2/2
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  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.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.validation.constraints.NotNull;
 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$
 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  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 89  
         @NotNull(message = "login can't be NULL") final String login,
 90  
         @NotNull(message = "rep can't be NULL") final Coordinates rep,
 91  
         final int number
 92  11
     ) throws IOException {
 93  11
         this.storage = stg;
 94  11
         this.self = login;
 95  11
         this.coords = rep;
 96  11
         this.rel = number;
 97  11
         this.storage.apply(
 98  
             new Directives().xpath(
 99  
                 String.format(
 100  
                     // @checkstyle LineLengthCheck (1 line)
 101  
                     "/github/repos/repo[@coords='%s']/releases/release[id='%d']",
 102  
                     this.coords, this.rel
 103  
                 )
 104  
             ).addIf("assets")
 105  
         );
 106  11
     }
 107  
 
 108  
     @Override
 109  
     @NotNull(message = "release is never NULL")
 110  
     public Release release() {
 111  2
         return new MkRelease(
 112  
             this.storage,
 113  
             this.self,
 114  
             this.coords,
 115  
             this.rel
 116  
         );
 117  
     }
 118  
 
 119  
     @Override
 120  
     @NotNull(message = "Iterable is never NULL")
 121  
     public Iterable<ReleaseAsset> iterate() {
 122  10
         return new MkIterable<ReleaseAsset>(
 123  
             this.storage,
 124  
             String.format("%s/asset", this.xpath()),
 125  9
             new MkIterable.Mapping<ReleaseAsset>() {
 126  
                 @Override
 127  
                 public ReleaseAsset map(final XML xml) {
 128  4
                     return MkReleaseAssets.this.get(
 129  
                         Integer.parseInt(xml.xpath("id/text()").get(0))
 130  
                     );
 131  
                 }
 132  
             }
 133  
         );
 134  
     }
 135  
 
 136  
     @Override
 137  
     @NotNull(message = "Asset is never NULL")
 138  
     public ReleaseAsset upload(
 139  
         @NotNull(message = "content can't be NULL") final byte[] content,
 140  
         @NotNull(message = "type can't be NULL") final String type,
 141  
         @NotNull(message = "name can't be NULL") final String name
 142  
     ) throws IOException {
 143  20
         this.storage.lock();
 144  
         final int number;
 145  
         try {
 146  10
             number = 1 + this.storage.xml().xpath(
 147  
                 String.format("%s/asset/id/text()", this.xpath())
 148  
             ).size();
 149  10
             this.storage.apply(
 150  
                 new Directives().xpath(this.xpath()).add("asset")
 151  
                     .add("id").set(Integer.toString(number)).up()
 152  
                     .add("name").set(name).up()
 153  
                     .add("content").set(new String(content, "UTF-8")).up()
 154  
                     .add("content_type").set(type).up()
 155  
                     .add("size").set(Integer.toString(content.length)).up()
 156  
                     .add("download_count").set("42").up()
 157  
                     .add("created_at").set(new Github.Time().toString()).up()
 158  
                     .add("updated_at").set(new Github.Time().toString()).up()
 159  
                     .add("url").set("http://localhost/1").up()
 160  
                     .add("html_url").set("http://localhost/2").up()
 161  
             );
 162  
         } finally {
 163  10
             this.storage.unlock();
 164  10
         }
 165  10
         return this.get(number);
 166  
     }
 167  
 
 168  
     @Override
 169  
     @NotNull(message = "Asset can't be NULL")
 170  
     public ReleaseAsset get(final int number) {
 171  34
         return new MkReleaseAsset(
 172  
             this.storage,
 173  
             this.self,
 174  
             this.coords,
 175  
             this.rel,
 176  
             number
 177  
         );
 178  
     }
 179  
 
 180  
     /**
 181  
      * XPath of this element in XML tree.
 182  
      * @return XPath
 183  
      */
 184  
     @NotNull(message = "Xpath si never NULL")
 185  
     private String xpath() {
 186  25
         return String.format(
 187  
             "/github/repos/repo[@coords='%s']/releases/release[id='%d']/assets",
 188  
             this.coords, this.rel
 189  
         );
 190  
     }
 191  
 }