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