Coverage Report - com.jcabi.github.mock.MkReleases
 
Classes in this File Line Coverage Branch Coverage Complexity
MkReleases
93%
44/47
0%
0/28
1
MkReleases$1
100%
3/3
N/A
1
MkReleases$AjcClosure1
0%
0/1
N/A
1
MkReleases$AjcClosure3
100%
1/1
N/A
1
MkReleases$AjcClosure5
100%
1/1
N/A
1
MkReleases$AjcClosure7
100%
1/1
N/A
1
MkReleases$AjcClosure9
100%
1/1
N/A
1
 
 1  86
 /**
 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.Releases;
 38  
 import com.jcabi.github.Repo;
 39  
 import com.jcabi.xml.XML;
 40  
 import java.io.IOException;
 41  
 import lombok.EqualsAndHashCode;
 42  
 import lombok.ToString;
 43  
 import org.xembly.Directives;
 44  
 
 45  
 /**
 46  
  * Mock Github releases.
 47  
  * @author Paul Polishchuk (ppol@ua.fm)
 48  
  * @version $Id: 094df462c277b1f600cab1a42023895e1af1b2fc $
 49  
  * @since 0.8
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 55  
 final class MkReleases implements Releases {
 56  
 
 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  
      * Repo name.
 69  
      */
 70  
     private final transient Coordinates coords;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param stg Storage
 75  
      * @param login User to login
 76  
      * @param rep Repo
 77  
      * @throws IOException If there is any I/O problem
 78  
      */
 79  
     public MkReleases(
 80  
         final MkStorage stg,
 81  
         final String login,
 82  
         final Coordinates rep
 83  32
     ) throws IOException {
 84  32
         this.storage = stg;
 85  32
         this.self = login;
 86  32
         this.coords = rep;
 87  63
         this.storage.apply(
 88  31
             new Directives().xpath(
 89  32
                 String.format(
 90  
                     "/github/repos/repo[@coords='%s']",
 91  
                     this.coords
 92  
                 )
 93  31
             ).addIf("releases")
 94  
         );
 95  32
     }
 96  
 
 97  
     @Override
 98  
     public Repo repo() {
 99  0
         return new MkRepo(this.storage, this.self, this.coords);
 100  
     }
 101  
 
 102  
     @Override
 103  
     public Iterable<Release> iterate() {
 104  30
         return new MkIterable<Release>(
 105  
             this.storage,
 106  10
             String.format("%s/release", this.xpath()),
 107  20
             new MkIterable.Mapping<Release>() {
 108  
                 @Override
 109  
                 public Release map(final XML xml) {
 110  20
                     return MkReleases.this.get(
 111  10
                         Integer.parseInt(xml.xpath("id/text()").get(0))
 112  
                     );
 113  
                 }
 114  
             }
 115  
         );
 116  
     }
 117  
 
 118  
     @Override
 119  
     public Release get(final int number) {
 120  86
         return new MkRelease(this.storage, this.self, this.coords, number);
 121  
     }
 122  
 
 123  
     @Override
 124  
     public Release create(
 125  
         final String tag
 126  
     ) throws IOException {
 127  64
         this.storage.lock();
 128  
         final int number;
 129  
         try {
 130  64
             number = 1 + this.storage.xml().xpath(
 131  32
                 String.format("%s/release/id/text()", this.xpath())
 132  32
             ).size();
 133  64
             this.storage.apply(
 134  32
                 new Directives().xpath(this.xpath()).add("release")
 135  32
                     .add("id").set(Integer.toString(number)).up()
 136  32
                     .add("tag_name").set(tag).up()
 137  32
                     .add("target_commitish").set("master").up()
 138  32
                     .add("name").set("").up()
 139  32
                     .add("body").set("").up()
 140  32
                     .add("draft").set("true").up()
 141  32
                     .add("prerelease").set("false").up()
 142  32
                     .add("created_at").set(new Github.Time().toString()).up()
 143  32
                     .add("published_at").set(new Github.Time().toString()).up()
 144  32
                     .add("url").set("http://localhost/1").up()
 145  32
                     .add("html_url").set("http://localhost/2").up()
 146  32
                     .add("assets_url").set("http://localhost/3").up()
 147  32
                     .add("upload_url").set("http://localhost/4").up()
 148  
             );
 149  
         } finally {
 150  32
             this.storage.unlock();
 151  32
         }
 152  32
         return this.get(number);
 153  
     }
 154  
 
 155  
     @Override
 156  
     public void remove(final int number) throws IOException {
 157  2
         this.storage.lock();
 158  
         try {
 159  2
             this.storage.apply(
 160  1
                 new Directives().xpath(
 161  1
                     String.format("%s/release[id='%d']", this.xpath(), number)
 162  1
                 ).remove()
 163  
             );
 164  
         } finally {
 165  1
             this.storage.unlock();
 166  1
         }
 167  1
     }
 168  
 
 169  
     /**
 170  
      * XPath of this element in XML tree.
 171  
      * @return XPath
 172  
      */
 173  
     private String xpath() {
 174  75
         return String.format(
 175  
             "/github/repos/repo[@coords='%s']/releases",
 176  
             this.coords
 177  
         );
 178  
     }
 179  
 }