Coverage Report - com.jcabi.github.mock.MkPulls
 
Classes in this File Line Coverage Branch Coverage Complexity
MkPulls
82%
32/39
8%
3/34
1.714
MkPulls$1
0%
0/3
N/A
1.714
MkPulls$AjcClosure1
100%
1/1
N/A
1.714
MkPulls$AjcClosure3
100%
1/1
N/A
1.714
MkPulls$AjcClosure5
100%
1/1
N/A
1.714
MkPulls$AjcClosure7
0%
0/1
N/A
1.714
 
 1  36
 /**
 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.Issue;
 36  
 import com.jcabi.github.Pull;
 37  
 import com.jcabi.github.Pulls;
 38  
 import com.jcabi.github.Repo;
 39  
 import com.jcabi.xml.XML;
 40  
 import java.io.IOException;
 41  
 import java.util.Map;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 import org.xembly.Directives;
 45  
 
 46  
 /**
 47  
  * Mock Github pull requests.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 50  
  * @version $Id: 50811900c13592db2ca99f830932f3894f78e2a3 $
 51  
  * @since 0.5
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @ToString
 56  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 57  
 final class MkPulls implements Pulls {
 58  
     /**
 59  
      * The separator between the username and
 60  
      * the branch name in the base/head parameters
 61  
      * when creating a pull request.
 62  
      */
 63  
     private static final String USER_BRANCH_SEP = ":";
 64  
 
 65  
     /**
 66  
      * Storage.
 67  
      */
 68  
     private final transient MkStorage storage;
 69  
 
 70  
     /**
 71  
      * Login of the user logged in.
 72  
      */
 73  
     private final transient String self;
 74  
 
 75  
     /**
 76  
      * Repo name.
 77  
      */
 78  
     private final transient Coordinates coords;
 79  
 
 80  
     /**
 81  
      * Public ctor.
 82  
      * @param stg Storage
 83  
      * @param login User to login
 84  
      * @param rep Repo
 85  
      * @throws IOException If there is any I/O problem
 86  
      */
 87  
     MkPulls(
 88  
         final MkStorage stg,
 89  
         final String login,
 90  
         final Coordinates rep
 91  18
     ) throws IOException {
 92  18
         this.storage = stg;
 93  18
         this.self = login;
 94  18
         this.coords = rep;
 95  36
         this.storage.apply(
 96  18
             new Directives().xpath(
 97  18
                 String.format(
 98  
                     "/github/repos/repo[@coords='%s']",
 99  
                     this.coords
 100  
                 )
 101  18
             ).addIf("pulls")
 102  
         );
 103  18
     }
 104  
 
 105  
     @Override
 106  
     public Repo repo() {
 107  36
         return new MkRepo(this.storage, this.self, this.coords);
 108  
     }
 109  
 
 110  
     @Override
 111  
     public Pull get(final int number) {
 112  36
         return new MkPull(this.storage, this.self, this.coords, number);
 113  
     }
 114  
 
 115  
     @Override
 116  
     public Pull create(
 117  
         final String title,
 118  
         final String head,
 119  
         final String base
 120  
     ) throws IOException {
 121  36
         if (head.isEmpty()) {
 122  0
             throw new IllegalArgumentException("head cannot be empty!");
 123  
         }
 124  18
         if (base.isEmpty()) {
 125  0
             throw new IllegalArgumentException("base cannot be empty!");
 126  
         }
 127  
         final String canonical;
 128  18
         if (head.contains(MkPulls.USER_BRANCH_SEP)) {
 129  0
             canonical = head;
 130  
         } else {
 131  36
             canonical = String.format(
 132  
                 "%s%s%s",
 133  18
                 this.coords.user(),
 134  
                 MkPulls.USER_BRANCH_SEP,
 135  
                 head
 136  
             );
 137  
         }
 138  18
         this.storage.lock();
 139  
         final int number;
 140  
         try {
 141  18
             final Issue issue = this.repo().issues().create(title, "some body");
 142  18
             number = issue.number();
 143  36
             this.storage.apply(
 144  18
                 new Directives().xpath(this.xpath()).add("pull")
 145  18
                     .add("number").set(Integer.toString(number)).up()
 146  18
                     .add("head").set(canonical).up()
 147  18
                     .add("base").set(base).up()
 148  18
                     .add("user")
 149  18
                     .add("login").set(this.self)
 150  18
                     .up()
 151  
             );
 152  
         } finally {
 153  18
             this.storage.unlock();
 154  18
         }
 155  18
         return this.get(number);
 156  
     }
 157  
 
 158  
     @Override
 159  
     public Iterable<Pull> iterate(final Map<String, String> params) {
 160  0
         return new MkIterable<Pull>(
 161  
             this.storage,
 162  0
             String.format("%s/pull", this.xpath()),
 163  0
             new MkIterable.Mapping<Pull>() {
 164  
                 @Override
 165  
                 public Pull map(final XML xml) {
 166  0
                     return MkPulls.this.get(
 167  0
                         Integer.parseInt(xml.xpath("number/text()").get(0))
 168  
                     );
 169  
                 }
 170  
             }
 171  
         );
 172  
     }
 173  
 
 174  
     /**
 175  
      * XPath of this element in XML tree.
 176  
      * @return XPath
 177  
      */
 178  
     private String xpath() {
 179  18
         return String.format(
 180  
             "/github/repos/repo[@coords='%s']/pulls",
 181  
             this.coords
 182  
         );
 183  
     }
 184  
 }