Coverage Report - com.jcabi.github.mock.MkPullComments
 
Classes in this File Line Coverage Branch Coverage Complexity
MkPullComments
95%
57/60
0%
0/36
1.091
MkPullComments$1
100%
3/3
N/A
1.091
MkPullComments$2
100%
3/3
N/A
1.091
 
 1  2
 /**
 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.github.Coordinates;
 34  
 import com.jcabi.github.Github;
 35  
 import com.jcabi.github.Pull;
 36  
 import com.jcabi.github.PullComment;
 37  
 import com.jcabi.github.PullComments;
 38  
 import com.jcabi.xml.XML;
 39  
 import java.io.IOException;
 40  
 import java.util.Map;
 41  
 import javax.json.Json;
 42  
 import javax.json.JsonObject;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Mock Github pull comments.
 49  
  *
 50  
  * @author Andres Candal (andres.candal@rollasolution.com)
 51  
  * @version $Id: b2ff2beaa9a63150e2c58030397112608208b58e $
 52  
  * @since 0.8
 53  
  * @see <a href="http://developer.github.com/v3/pulls/comments/">Review Comments API</a>
 54  
  */
 55  
 @Immutable
 56  0
 @ToString
 57  0
 @EqualsAndHashCode(of = { "storage", "self", "repo", "owner" })
 58  
 final class MkPullComments implements PullComments {
 59  
     /**
 60  
      * Storage.
 61  
      */
 62  
     private final transient MkStorage storage;
 63  
 
 64  
     /**
 65  
      * Login of the user logged in.
 66  
      */
 67  
     private final transient String self;
 68  
 
 69  
     /**
 70  
      * Repo name.
 71  
      */
 72  
     private final transient Coordinates repo;
 73  
 
 74  
     /**
 75  
      * Owner of comments.
 76  
      */
 77  
     private final transient Pull owner;
 78  
 
 79  
     /**
 80  
      * Public ctor.
 81  
      * @param stg Storage
 82  
      * @param login User to login
 83  
      * @param rep Repo
 84  
      * @param pull Pull
 85  
      * @throws IOException If there is any I/O problem
 86  
      * @checkstyle ParameterNumber (5 lines)
 87  
      */
 88  
     MkPullComments(
 89  
         final MkStorage stg,
 90  
         final String login,
 91  
         final Coordinates rep,
 92  
         final Pull pull
 93  12
     ) throws IOException {
 94  12
         this.storage = stg;
 95  12
         this.self = login;
 96  12
         this.repo = rep;
 97  12
         this.owner = pull;
 98  24
         this.storage.apply(
 99  12
             new Directives().xpath(
 100  12
                 String.format(
 101  
                     "/github/repos/repo[@coords='%s']/pulls/pull[number='%d']",
 102  12
                     this.repo, this.owner.number()
 103  
                 )
 104  12
             ).addIf("comments")
 105  
         );
 106  12
     }
 107  
     @Override
 108  
     public Pull pull() {
 109  3
         return this.owner;
 110  
     }
 111  
 
 112  
     @Override
 113  
     public PullComment get(final int number) {
 114  21
         return new MkPullComment(this.storage, this.repo, this.owner, number);
 115  
     }
 116  
 
 117  
     @Override
 118  
     public Iterable<PullComment> iterate(
 119  
         final Map<String, String> params
 120  
     ) {
 121  2
         return new MkIterable<PullComment>(
 122  
             this.storage,
 123  1
             String.format(
 124  
                 "/github/repos/repo[@coords='%s']/pulls/pull/comments",
 125  
                 this.repo
 126  
             ),
 127  3
             new MkIterable.Mapping<PullComment>() {
 128  
                 @Override
 129  
                 public PullComment map(final XML xml) {
 130  4
                     return MkPullComments.this.get(
 131  2
                         Integer.parseInt(xml.xpath("comment/id/text()").get(0))
 132  
                     );
 133  
                 }
 134  
             }
 135  
         );
 136  
     }
 137  
 
 138  
     @Override
 139  
     public Iterable<PullComment> iterate(
 140  
         final int number,
 141  
         final Map<String, String> params
 142  
     ) {
 143  8
         return new MkIterable<PullComment>(
 144  4
             this.storage, String.format("%s/comment", this.xpath()),
 145  8
             new MkIterable.Mapping<PullComment>() {
 146  
                 @Override
 147  
                 public PullComment map(final XML xml) {
 148  8
                     return MkPullComments.this.get(
 149  4
                         Integer.parseInt(xml.xpath("id/text()").get(0))
 150  
                     );
 151  
                 }
 152  
             }
 153  
         );
 154  
     }
 155  
 
 156  
     // @checkstyle ParameterNumberCheck (7 lines)
 157  
     @Override
 158  
     public PullComment post(
 159  
         final String body,
 160  
         final String commit,
 161  
         final String path,
 162  
         final int position
 163  
     ) throws IOException {
 164  13
         this.storage.lock();
 165  
         final int number;
 166  
         try {
 167  13
             number = 1 + this.storage.xml()
 168  13
                 .nodes(String.format("%s/comment/id/text()", this.xpath()))
 169  13
                 .size();
 170  26
             this.storage.apply(
 171  13
                 new Directives().xpath(this.xpath()).add("comment")
 172  13
                     .add("id").set(Integer.toString(number)).up()
 173  13
                     .add("url").set("http://localhost/1").up()
 174  13
                     .add("diff_hunk").set("@@ -16,33 +16,40 @@ public...").up()
 175  
                     // @checkstyle MultipleStringLiteralsCheck (4 lines)
 176  13
                     .add("path").set(path).up()
 177  13
                     .add("position").set(Integer.toString(position)).up()
 178  13
                     .add("original_position").set(Integer.toString(number)).up()
 179  13
                     .add("commit_id").set(commit).up()
 180  13
                     .add("original_commit_id").set(commit).up()
 181  13
                     .add("body").set(body).up()
 182  13
                     .add("created_at").set(new Github.Time().toString()).up()
 183  13
                     .add("published_at").set(new Github.Time().toString()).up()
 184  13
                     .add("user").add("login").set(this.self).up()
 185  13
                     .add("pull_request_url").set("http://localhost/2").up()
 186  
             );
 187  
         } finally {
 188  13
             this.storage.unlock();
 189  13
         }
 190  13
         return this.get(number);
 191  
     }
 192  
 
 193  
     @Override
 194  
     public PullComment reply(
 195  
         final String body,
 196  
         final int comment
 197  
     )
 198  
         throws IOException {
 199  1
         this.storage.lock();
 200  
         try {
 201  1
             final JsonObject orig = this.get(comment).json();
 202  2
             final PullComment reply = this.post(
 203  
                 body,
 204  1
                 orig.getString("commit_id"),
 205  1
                 orig.getString("path"),
 206  
                 comment
 207  
             );
 208  2
             reply.patch(
 209  1
                 Json.createObjectBuilder()
 210  1
                     .add("original_position", String.valueOf(comment)).build()
 211  
             );
 212  2
             return reply;
 213  
         } finally {
 214  1
             this.storage.unlock();
 215  0
         }
 216  
     }
 217  
 
 218  
     @Override
 219  
     public void remove(final int number) throws IOException {
 220  2
         this.storage.apply(
 221  1
             new Directives().xpath(
 222  1
                 String.format("%s/comment[id='%d']", this.xpath(), number)
 223  1
             ).remove()
 224  
         );
 225  1
     }
 226  
 
 227  
     /**
 228  
      * XPath of this element in XML tree.
 229  
      * @return XPath
 230  
      */
 231  
     private String xpath() {
 232  62
         return String.format(
 233  
             // @checkstyle LineLength (1 line)
 234  
             "/github/repos/repo[@coords='%s']/pulls/pull[number='%d']/comments",
 235  31
             this.repo, this.owner.number()
 236  
         );
 237  
     }
 238  
 }