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