Coverage Report - com.jcabi.github.mock.MkPull
 
Classes in this File Line Coverage Branch Coverage Complexity
MkPull
75%
21/28
7%
2/26
1.154
MkPull$AjcClosure1
100%
1/1
N/A
1.154
MkPull$AjcClosure11
0%
0/1
N/A
1.154
MkPull$AjcClosure13
100%
1/1
N/A
1.154
MkPull$AjcClosure15
100%
1/1
N/A
1.154
MkPull$AjcClosure17
100%
1/1
N/A
1.154
MkPull$AjcClosure19
100%
1/1
N/A
1.154
MkPull$AjcClosure3
100%
1/1
N/A
1.154
MkPull$AjcClosure5
0%
0/1
N/A
1.154
MkPull$AjcClosure7
0%
0/1
N/A
1.154
MkPull$AjcClosure9
0%
0/1
N/A
1.154
 
 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.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.Repo;
 40  
 import java.io.IOException;
 41  
 import java.util.Collections;
 42  
 import java.util.Map;
 43  
 import javax.json.Json;
 44  
 import javax.json.JsonObject;
 45  
 import javax.json.JsonObjectBuilder;
 46  
 import javax.json.JsonValue;
 47  
 import javax.validation.constraints.NotNull;
 48  
 import lombok.EqualsAndHashCode;
 49  
 import lombok.ToString;
 50  
 
 51  
 /**
 52  
  * Mock Github pull.
 53  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 54  
  * @version $Id$
 55  
  * @since 0.5
 56  
  */
 57  0
 @Immutable
 58  
 @Loggable(Loggable.DEBUG)
 59  0
 @ToString
 60  0
 @EqualsAndHashCode(of = { "storage", "self", "coords", "num" })
 61  
 @SuppressWarnings("PMD.TooManyMethods")
 62  
 final class MkPull implements Pull {
 63  
 
 64  
     /**
 65  
      * Storage.
 66  
      */
 67  
     private final transient MkStorage storage;
 68  
 
 69  
     /**
 70  
      * Login of the user logged in.
 71  
      */
 72  
     private final transient String self;
 73  
 
 74  
     /**
 75  
      * Repo name.
 76  
      */
 77  
     private final transient Coordinates coords;
 78  
 
 79  
     /**
 80  
      * Pull number.
 81  
      */
 82  
     private final transient int num;
 83  
 
 84  
     /**
 85  
      * Public ctor.
 86  
      * @param stg Storage
 87  
      * @param login User to login
 88  
      * @param rep Repo
 89  
      * @param number Pull request number
 90  
      * @checkstyle ParameterNumber (5 lines)
 91  
      */
 92  
     MkPull(
 93  
         @NotNull(message = "stg is never NULL") final MkStorage stg,
 94  
         @NotNull(message = "login is never NULL") final String login,
 95  
         @NotNull(message = "rep is never NULL") final Coordinates rep,
 96  17
         final int number) {
 97  17
         this.storage = stg;
 98  17
         this.self = login;
 99  17
         this.coords = rep;
 100  17
         this.num = number;
 101  17
     }
 102  
 
 103  
     @Override
 104  
     @NotNull(message = "repository is never NULL")
 105  
     public Repo repo() {
 106  2
         return new MkRepo(this.storage, this.self, this.coords);
 107  
     }
 108  
 
 109  
     @Override
 110  
     public int number() {
 111  112
         return this.num;
 112  
     }
 113  
 
 114  
     @Override
 115  
     @NotNull(message = "Iterable of commits is never NULL")
 116  
     public Iterable<Commit> commits() throws IOException {
 117  0
         return Collections.emptyList();
 118  
     }
 119  
 
 120  
     @Override
 121  
     @NotNull(message = "Iterable of files is never NULL")
 122  
     public Iterable<JsonObject> files() throws IOException {
 123  0
         return Collections.emptyList();
 124  
     }
 125  
 
 126  
     @Override
 127  
     public void merge(
 128  
         @NotNull(message = "msg can't be NULL") final String msg
 129  
     ) throws IOException {
 130  
         // nothing to do here
 131  0
     }
 132  
 
 133  
     @Override
 134  
     public MergeState merge(
 135  
         @NotNull(message = "message can't be NULL") final String msg,
 136  
         @NotNull(message = "sha can't be NULL") final String sha)
 137  
         throws IOException {
 138  0
         throw new UnsupportedOperationException("Merge not supported");
 139  
     }
 140  
 
 141  
     @Override
 142  
     @NotNull(message = "comments is never NULL")
 143  
     public PullComments comments() throws IOException {
 144  24
         return new MkPullComments(this.storage, this.self, this.coords, this);
 145  
     }
 146  
 
 147  
     @Override
 148  
     public int compareTo(
 149  
         @NotNull(message = "pull cannot be NULL") final Pull pull
 150  
     ) {
 151  4
         return this.number() - pull.number();
 152  
     }
 153  
 
 154  
     @Override
 155  
     public void patch(
 156  
         @NotNull(message = "json can't be NULL") final JsonObject json
 157  
     ) throws IOException {
 158  2
         new JsonPatch(this.storage).patch(this.xpath(), json);
 159  1
     }
 160  
 
 161  
     @Override
 162  
     @NotNull(message = "JSON is never NULL")
 163  
     public JsonObject json() throws IOException {
 164  8
         final JsonObject obj = new JsonNode(
 165  
             this.storage.xml().nodes(this.xpath()).get(0)
 166  
         ).json();
 167  4
         final JsonObjectBuilder json = Json.createObjectBuilder();
 168  4
         for (final Map.Entry<String, JsonValue> val : obj.entrySet()) {
 169  14
             json.add(val.getKey(), val.getValue());
 170  14
         }
 171  4
         return json
 172  
             .add(
 173  
                 "comments",
 174  
                 this.storage.xml().nodes(this.comment()).size()
 175  
         ).build();
 176  
     }
 177  
 
 178  
     /**
 179  
      * XPath of this element in XML tree.
 180  
      * @return XPath
 181  
      */
 182  
     @NotNull(message = "Xpath is never NULL")
 183  
     private String xpath() {
 184  5
         return String.format(
 185  
             "/github/repos/repo[@coords='%s']/pulls/pull[number='%d']",
 186  
             this.coords, this.num
 187  
         );
 188  
     }
 189  
 
 190  
     /**
 191  
      * XPath of issue element in XML tree.
 192  
      * @return XPath
 193  
      */
 194  
     @NotNull(message = "comment is never NULL")
 195  
     private String comment() {
 196  4
         return String.format(
 197  
             // @checkstyle LineLengthCheck (1 line)
 198  
             "/github/repos/repo[@coords='%s']/pulls/pull[number='%d']/comments/comment",
 199  
             this.coords, this.num
 200  
         );
 201  
     }
 202  
 
 203  
 }