Coverage Report - com.jcabi.github.mock.MkComments
 
Classes in this File Line Coverage Branch Coverage Complexity
MkComments
95%
43/45
0%
0/30
1
MkComments$1
100%
3/3
N/A
1
MkComments$AjcClosure1
100%
1/1
N/A
1
MkComments$AjcClosure3
100%
1/1
N/A
1
MkComments$AjcClosure5
100%
1/1
N/A
1
MkComments$AjcClosure7
100%
1/1
N/A
1
 
 1  12
 /**
 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.Comment;
 35  
 import com.jcabi.github.Comments;
 36  
 import com.jcabi.github.Coordinates;
 37  
 import com.jcabi.github.Github;
 38  
 import com.jcabi.github.Issue;
 39  
 import com.jcabi.log.Logger;
 40  
 import com.jcabi.xml.XML;
 41  
 import java.io.IOException;
 42  
 import java.util.Date;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Mock Github comments.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 51  
  * @version $Id: 23c19f0875755f542102fc010b98d3741a0161e6 $
 52  
  * @since 0.5
 53  
  */
 54  
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @ToString
 57  0
 @EqualsAndHashCode(of = { "storage", "self", "repo", "ticket" })
 58  
 final class MkComments implements Comments {
 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 repo;
 74  
 
 75  
     /**
 76  
      * Issue number.
 77  
      */
 78  
     private final transient int ticket;
 79  
 
 80  
     /**
 81  
      * Public ctor.
 82  
      * @param stg Storage
 83  
      * @param login User to login
 84  
      * @param rep Repo
 85  
      * @param issue Issue number
 86  
      * @throws IOException If there is any I/O problem
 87  
      * @checkstyle ParameterNumber (5 lines)
 88  
      */
 89  
     MkComments(
 90  
         final MkStorage stg,
 91  
         final String login,
 92  
         final Coordinates rep,
 93  
         final int issue
 94  6
     ) throws IOException {
 95  6
         this.storage = stg;
 96  6
         this.self = login;
 97  6
         this.repo = rep;
 98  6
         this.ticket = issue;
 99  12
         this.storage.apply(
 100  6
             new Directives().xpath(
 101  6
                 String.format(
 102  
                     // @checkstyle LineLength (1 line)
 103  
                     "/github/repos/repo[@coords='%s']/issues/issue[number='%d']",
 104  6
                     this.repo, this.ticket
 105  
                 )
 106  6
             ).addIf("comments")
 107  
         );
 108  6
     }
 109  
 
 110  
     @Override
 111  
     public Issue issue() {
 112  12
         return new MkIssue(this.storage, this.self, this.repo, this.ticket);
 113  
     }
 114  
 
 115  
     @Override
 116  
     public Comment get(final int number) {
 117  18
         return new MkComment(
 118  
             this.storage, this.self, this.repo, this.ticket, number
 119  
         );
 120  
     }
 121  
 
 122  
     @Override
 123  
     public Iterable<Comment> iterate(final Date since) {
 124  6
         return new MkIterable<Comment>(
 125  
             this.storage,
 126  2
             String.format("%s/comment", this.xpath()),
 127  5
             new MkIterable.Mapping<Comment>() {
 128  
                 @Override
 129  
                 public Comment map(final XML xml) {
 130  6
                     return MkComments.this.get(
 131  3
                         Integer.parseInt(xml.xpath("number/text()").get(0))
 132  
                     );
 133  
                 }
 134  
             }
 135  
         );
 136  
     }
 137  
 
 138  
     @Override
 139  
     public Comment post(
 140  
         final String text
 141  
     ) throws IOException {
 142  12
         this.storage.lock();
 143  
         final int number;
 144  
         try {
 145  6
             final String timestamp = new Github.Time().toString();
 146  6
             number = 1 + this.storage.xml()
 147  6
                 .nodes("//comment/number").size();
 148  12
             this.storage.apply(
 149  6
                 new Directives().xpath(this.xpath()).add("comment")
 150  6
                     .add("number").set(Integer.toString(number)).up()
 151  6
                     .add("url")
 152  6
                     .set(
 153  6
                         String.format(
 154  
                             // @checkstyle LineLength (1 line)
 155  
                             "https://api.jcabi-github.invalid/repos/%s/%s/issues/comments/%d",
 156  6
                             this.repo.user(),
 157  6
                             this.repo.repo(),
 158  6
                             number
 159  
                     )
 160  
                 )
 161  6
                     .up()
 162  6
                     .add("body").set(text).up()
 163  6
                     .add("user")
 164  6
                         .add("login").set(this.self).up()
 165  6
                     .up()
 166  6
                     .add("created_at").set(timestamp).up()
 167  6
                     .add("updated_at").set(timestamp)
 168  
             );
 169  
         } finally {
 170  6
             this.storage.unlock();
 171  6
         }
 172  12
         Logger.info(
 173  
             this, "comment #%d posted to issue #%d by %s: %[text]s",
 174  6
             number, this.issue().number(), this.self, text
 175  
         );
 176  6
         return this.get(number);
 177  
     }
 178  
 
 179  
     /**
 180  
      * XPath of this element in XML tree.
 181  
      * @return XPath
 182  
      */
 183  
     private String xpath() {
 184  16
         return String.format(
 185  
             // @checkstyle LineLength (1 line)
 186  
             "/github/repos/repo[@coords='%s']/issues/issue[number='%d']/comments",
 187  8
             this.repo, this.ticket
 188  
         );
 189  
     }
 190  
 
 191  
 }