Coverage Report - com.jcabi.github.mock.MkPull
 
Classes in this File Line Coverage Branch Coverage Complexity
MkPull
88%
63/71
8%
3/34
1.267
MkPull$AjcClosure1
100%
1/1
N/A
1.267
MkPull$AjcClosure11
0%
0/1
N/A
1.267
MkPull$AjcClosure13
0%
0/1
N/A
1.267
MkPull$AjcClosure15
0%
0/1
N/A
1.267
MkPull$AjcClosure17
100%
1/1
N/A
1.267
MkPull$AjcClosure19
100%
1/1
N/A
1.267
MkPull$AjcClosure21
100%
1/1
N/A
1.267
MkPull$AjcClosure23
100%
1/1
N/A
1.267
MkPull$AjcClosure3
100%
1/1
N/A
1.267
MkPull$AjcClosure5
100%
1/1
N/A
1.267
MkPull$AjcClosure7
100%
1/1
N/A
1.267
MkPull$AjcClosure9
0%
0/1
N/A
1.267
 
 1  4
 /**
 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.Commit;
 35  
 import com.jcabi.github.Coordinates;
 36  
 import com.jcabi.github.MergeState;
 37  
 import com.jcabi.github.Pull;
 38  
 import com.jcabi.github.PullComments;
 39  
 import com.jcabi.github.PullRef;
 40  
 import com.jcabi.github.Repo;
 41  
 import com.jcabi.xml.XML;
 42  
 import java.io.IOException;
 43  
 import java.util.Collections;
 44  
 import java.util.List;
 45  
 import javax.json.Json;
 46  
 import javax.json.JsonObject;
 47  
 import lombok.EqualsAndHashCode;
 48  
 import lombok.ToString;
 49  
 
 50  
 /**
 51  
  * Mock Github pull.
 52  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 53  
  * @version $Id: cdafe4454dafcf0bdcb429d53ac87c0d41a2ef68 $
 54  
  * @since 0.5
 55  
  */
 56  0
 @Immutable
 57  
 @Loggable(Loggable.DEBUG)
 58  0
 @ToString
 59  0
 @EqualsAndHashCode(of = { "storage", "self", "coords", "num" })
 60  
 @SuppressWarnings("PMD.TooManyMethods")
 61  
 final class MkPull implements Pull {
 62  
     /**
 63  
      * The separator between the username and
 64  
      * the branch name in the labels of pull request base/head objects.
 65  
      */
 66  
     private static final String USER_BRANCH_SEP = ":";
 67  
     /**
 68  
      * Property name for ref in pull request ref JSON object.
 69  
      */
 70  
     private static final String REF_PROP = "ref";
 71  
     /**
 72  
      * Property name for label in pull request ref JSON object.
 73  
      */
 74  
     private static final String LABEL_PROP = "label";
 75  
 
 76  
     /**
 77  
      * Storage.
 78  
      */
 79  
     private final transient MkStorage storage;
 80  
 
 81  
     /**
 82  
      * Login of the user logged in.
 83  
      */
 84  
     private final transient String self;
 85  
 
 86  
     /**
 87  
      * Repo name.
 88  
      */
 89  
     private final transient Coordinates coords;
 90  
 
 91  
     /**
 92  
      * Pull number.
 93  
      */
 94  
     private final transient int num;
 95  
 
 96  
     /**
 97  
      * Public ctor.
 98  
      * @param stg Storage
 99  
      * @param login User to login
 100  
      * @param rep Repo
 101  
      * @param number Pull request number
 102  
      * @checkstyle ParameterNumber (5 lines)
 103  
      */
 104  
     MkPull(
 105  
         final MkStorage stg,
 106  
         final String login,
 107  
         final Coordinates rep,
 108  20
         final int number) {
 109  20
         this.storage = stg;
 110  20
         this.self = login;
 111  20
         this.coords = rep;
 112  20
         this.num = number;
 113  20
     }
 114  
 
 115  
     @Override
 116  
     public Repo repo() {
 117  4
         return new MkRepo(this.storage, this.self, this.coords);
 118  
     }
 119  
 
 120  
     @Override
 121  
     public int number() {
 122  114
         return this.num;
 123  
     }
 124  
 
 125  
     @Override
 126  
     public PullRef base() throws IOException {
 127  3
         return new MkPullRef(
 128  
             this.storage,
 129  
             new MkBranches(
 130  
                 this.storage,
 131  
                 this.self,
 132  
                 this.coords
 133  1
             ).get(
 134  2
                 this.storage.xml().xpath(
 135  1
                     String.format("%s/base/text()", this.xpath())
 136  1
                 ).get(0)
 137  
             )
 138  
         );
 139  
     }
 140  
 
 141  
     @Override
 142  
     public PullRef head() throws IOException {
 143  2
         final String userbranch = this.storage.xml()
 144  1
             .xpath(String.format("%s/head/text()", this.xpath()))
 145  1
             .get(0);
 146  1
         final String[] parts = userbranch.split(MkPull.USER_BRANCH_SEP, 2);
 147  1
         if (parts.length != 2) {
 148  0
             throw new IllegalStateException("Invalid MkPull head");
 149  
         }
 150  1
         final String user = parts[0];
 151  1
         final String branch = parts[1];
 152  2
         return new MkPullRef(
 153  
             this.storage,
 154  
             new MkBranches(
 155  
                 this.storage,
 156  
                 this.self,
 157  
                 new Coordinates.Simple(
 158  
                     user,
 159  1
                     this.coords.repo()
 160  
                 )
 161  1
             ).get(branch)
 162  
         );
 163  
     }
 164  
 
 165  
     @Override
 166  
     public Iterable<Commit> commits() throws IOException {
 167  0
         return Collections.emptyList();
 168  
     }
 169  
 
 170  
     @Override
 171  
     public Iterable<JsonObject> files() throws IOException {
 172  0
         return Collections.emptyList();
 173  
     }
 174  
 
 175  
     @Override
 176  
     public void merge(
 177  
         final String msg
 178  
     ) throws IOException {
 179  
         // nothing to do here
 180  0
     }
 181  
 
 182  
     @Override
 183  
     public MergeState merge(
 184  
         final String msg,
 185  
         final String sha)
 186  
         throws IOException {
 187  0
         throw new UnsupportedOperationException("Merge not supported");
 188  
     }
 189  
 
 190  
     @Override
 191  
     public PullComments comments() throws IOException {
 192  24
         return new MkPullComments(this.storage, this.self, this.coords, this);
 193  
     }
 194  
 
 195  
     @Override
 196  
     public int compareTo(
 197  
         final Pull pull
 198  
     ) {
 199  4
         return this.number() - pull.number();
 200  
     }
 201  
 
 202  
     @Override
 203  
     public void patch(
 204  
         final JsonObject json
 205  
     ) throws IOException {
 206  2
         new JsonPatch(this.storage).patch(this.xpath(), json);
 207  1
     }
 208  
 
 209  
     @Override
 210  
     public JsonObject json() throws IOException {
 211  8
         final XML xml = this.storage.xml().nodes(this.xpath()).get(0);
 212  4
         final String branch = xml.xpath("base/text()").get(0);
 213  4
         final String head = xml.xpath("head/text()").get(0);
 214  4
         final String[] parts = head.split(MkPull.USER_BRANCH_SEP, 2);
 215  4
         final List<String> patches = xml.xpath("patch/text()");
 216  
         final String patch;
 217  4
         if (patches.isEmpty()) {
 218  3
             patch = "";
 219  
         } else {
 220  1
             patch = patches.get(0);
 221  
         }
 222  8
         return Json.createObjectBuilder()
 223  4
             .add(
 224  
                 "number",
 225  4
                 Integer.parseInt(xml.xpath("number/text()").get(0))
 226  
             )
 227  4
             .add(
 228  
                 "user",
 229  4
                 Json.createObjectBuilder()
 230  4
                     .add("login", xml.xpath("user/login/text()").get(0))
 231  4
                     .build()
 232  
             )
 233  4
             .add("patch", patch)
 234  4
             .add(
 235  
                 "head",
 236  4
                 Json.createObjectBuilder()
 237  4
                     .add(MkPull.REF_PROP, parts[1])
 238  4
                     .add(MkPull.LABEL_PROP, head)
 239  4
                     .build()
 240  4
             ).add(
 241  
                 "base",
 242  4
                 Json.createObjectBuilder()
 243  4
                     .add(MkPull.REF_PROP, branch)
 244  4
                     .add(
 245  
                         MkPull.LABEL_PROP,
 246  4
                         String.format(
 247  
                             "%s:%s",
 248  4
                             this.coords.user(),
 249  
                             branch
 250  
                         )
 251  4
                     ).build()
 252  4
             ).add(
 253  
                 "comments",
 254  4
                 this.storage.xml().nodes(this.comment()).size()
 255  4
             ).build();
 256  
     }
 257  
 
 258  
     /**
 259  
      * XPath of this element in XML tree.
 260  
      * @return XPath
 261  
      */
 262  
     private String xpath() {
 263  14
         return String.format(
 264  
             "/github/repos/repo[@coords='%s']/pulls/pull[number='%d']",
 265  7
             this.coords, this.num
 266  
         );
 267  
     }
 268  
 
 269  
     /**
 270  
      * XPath of issue element in XML tree.
 271  
      * @return XPath
 272  
      */
 273  
     private String comment() {
 274  8
         return String.format(
 275  
             // @checkstyle LineLengthCheck (1 line)
 276  
             "/github/repos/repo[@coords='%s']/pulls/pull[number='%d']/comments/comment",
 277  4
             this.coords, this.num
 278  
         );
 279  
     }
 280  
 
 281  
 }