Coverage Report - com.jcabi.github.mock.MkPulls
 
Classes in this File Line Coverage Branch Coverage Complexity
MkPulls
85%
17/20
0%
0/22
1
MkPulls$1
0%
0/2
N/A
1
MkPulls$AjcClosure1
100%
1/1
N/A
1
MkPulls$AjcClosure3
100%
1/1
N/A
1
MkPulls$AjcClosure5
100%
1/1
N/A
1
MkPulls$AjcClosure7
0%
0/1
N/A
1
 
 1  30
 /**
 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.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 javax.validation.constraints.NotNull;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Mock Github pull requests.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 51  
  * @version $Id$
 52  
  * @since 0.5
 53  
  */
 54  
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @ToString
 57  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 58  
 final class MkPulls implements Pulls {
 59  
 
 60  
     /**
 61  
      * Storage.
 62  
      */
 63  
     private final transient MkStorage storage;
 64  
 
 65  
     /**
 66  
      * Login of the user logged in.
 67  
      */
 68  
     private final transient String self;
 69  
 
 70  
     /**
 71  
      * Repo name.
 72  
      */
 73  
     private final transient Coordinates coords;
 74  
 
 75  
     /**
 76  
      * Public ctor.
 77  
      * @param stg Storage
 78  
      * @param login User to login
 79  
      * @param rep Repo
 80  
      * @throws IOException If there is any I/O problem
 81  
      */
 82  
     MkPulls(
 83  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 84  
         @NotNull(message = "login can't be ") final String login,
 85  
         @NotNull(message = "rep can't be") final Coordinates rep
 86  15
     ) throws IOException {
 87  15
         this.storage = stg;
 88  15
         this.self = login;
 89  15
         this.coords = rep;
 90  15
         this.storage.apply(
 91  
             new Directives().xpath(
 92  
                 String.format(
 93  
                     "/github/repos/repo[@coords='%s']",
 94  
                     this.coords
 95  
                 )
 96  
             ).addIf("pulls")
 97  
         );
 98  15
     }
 99  
 
 100  
     @Override
 101  
     @NotNull(message = "Repo is never NULL")
 102  
     public Repo repo() {
 103  30
         return new MkRepo(this.storage, this.self, this.coords);
 104  
     }
 105  
 
 106  
     @Override
 107  
     @NotNull(message = "Pull is never NULL")
 108  
     public Pull get(final int number) {
 109  30
         return new MkPull(this.storage, this.self, this.coords, number);
 110  
     }
 111  
 
 112  
     @Override
 113  
     @NotNull(message = "created pull is never NULL")
 114  
     public Pull create(
 115  
         @NotNull(message = "title can't be NULL") final String title,
 116  
         @NotNull(message = "head can't be NULL") final String head,
 117  
         @NotNull(message = "base can't be NULL") final String base
 118  
     ) throws IOException {
 119  30
         this.storage.lock();
 120  
         final int number;
 121  
         try {
 122  15
             final Issue issue = this.repo().issues().create(title, "some body");
 123  15
             number = issue.number();
 124  15
             this.storage.apply(
 125  
                 new Directives().xpath(this.xpath()).add("pull")
 126  
                     .add("number").set(Integer.toString(number)).up()
 127  
                     .add("head").set(head).up()
 128  
                     .add("base").set(base).up()
 129  
             );
 130  
         } finally {
 131  15
             this.storage.unlock();
 132  15
         }
 133  15
         return this.get(number);
 134  
     }
 135  
 
 136  
     @Override
 137  
     @NotNull(message = "Iterable of pulls is never NULL")
 138  
     public Iterable<Pull> iterate(@NotNull(message = "params can't be NULL")
 139  
         final Map<String, String> params) {
 140  0
         return new MkIterable<Pull>(
 141  
             this.storage,
 142  
             String.format("%s/pull", this.xpath()),
 143  0
             new MkIterable.Mapping<Pull>() {
 144  
                 @Override
 145  
                 public Pull map(final XML xml) {
 146  0
                     return MkPulls.this.get(
 147  
                         Integer.parseInt(xml.xpath("number/text()").get(0))
 148  
                     );
 149  
                 }
 150  
             }
 151  
         );
 152  
     }
 153  
 
 154  
     /**
 155  
      * XPath of this element in XML tree.
 156  
      * @return XPath
 157  
      */
 158  
     @NotNull(message = "Xpath is never NULL")
 159  
     private String xpath() {
 160  15
         return String.format(
 161  
             "/github/repos/repo[@coords='%s']/pulls",
 162  
             this.coords
 163  
         );
 164  
     }
 165  
 }