Coverage Report - com.jcabi.github.mock.MkComments
 
Classes in this File Line Coverage Branch Coverage Complexity
MkComments
90%
20/22
0%
0/24
1
MkComments$1
100%
2/2
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-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.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 javax.validation.constraints.NotNull;
 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$
 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  
         @NotNull(message = "stg cannot be NULL") final MkStorage stg,
 91  
         @NotNull(message = "login cannot be NULL") final String login,
 92  
         @NotNull(message = "reo cannot be NULL") 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  6
         this.storage.apply(
 100  
             new Directives().xpath(
 101  
                 String.format(
 102  
                     // @checkstyle LineLength (1 line)
 103  
                     "/github/repos/repo[@coords='%s']/issues/issue[number='%d']",
 104  
                     this.repo, this.ticket
 105  
                 )
 106  
             ).addIf("comments")
 107  
         );
 108  6
     }
 109  
 
 110  
     @Override
 111  
     @NotNull(message = "Issue is never NULL")
 112  
     public Issue issue() {
 113  12
         return new MkIssue(this.storage, this.self, this.repo, this.ticket);
 114  
     }
 115  
 
 116  
     @Override
 117  
     @NotNull(message = "Comment isn't ever NULL")
 118  
     public Comment get(final int number) {
 119  18
         return new MkComment(
 120  
             this.storage, this.self, this.repo, this.ticket, number
 121  
         );
 122  
     }
 123  
 
 124  
     @Override
 125  
     @NotNull(message = "Iterable of comments is never NULL")
 126  
     public Iterable<Comment> iterate() {
 127  4
         return new MkIterable<Comment>(
 128  
             this.storage,
 129  
             String.format("%s/comment", this.xpath()),
 130  5
             new MkIterable.Mapping<Comment>() {
 131  
                 @Override
 132  
                 public Comment map(final XML xml) {
 133  3
                     return MkComments.this.get(
 134  
                         Integer.parseInt(xml.xpath("number/text()").get(0))
 135  
                     );
 136  
                 }
 137  
             }
 138  
         );
 139  
     }
 140  
 
 141  
     @Override
 142  
     @NotNull(message = "comment is never NULL")
 143  
     public Comment post(
 144  
         @NotNull(message = "text should not be NULL") final String text
 145  
     ) throws IOException {
 146  12
         this.storage.lock();
 147  
         final int number;
 148  
         try {
 149  6
             final String timestamp = new Github.Time().toString();
 150  6
             number = 1 + this.storage.xml()
 151  
                 .nodes("//comment/number").size();
 152  6
             this.storage.apply(
 153  
                 new Directives().xpath(this.xpath()).add("comment")
 154  
                     .add("number").set(Integer.toString(number)).up()
 155  
                     .add("url")
 156  
                     .set(
 157  
                         String.format(
 158  
                             // @checkstyle LineLength (1 line)
 159  
                             "https://api.jcabi-github.invalid/repos/%s/%s/issues/comments/%d",
 160  
                             this.repo.user(),
 161  
                             this.repo.repo(),
 162  
                             number
 163  
                     )
 164  
                 )
 165  
                     .up()
 166  
                     .add("body").set(text).up()
 167  
                     .add("user")
 168  
                         .add("login").set(this.self).up()
 169  
                     .up()
 170  
                     .add("created_at").set(timestamp).up()
 171  
                     .add("updated_at").set(timestamp)
 172  
             );
 173  
         } finally {
 174  6
             this.storage.unlock();
 175  6
         }
 176  6
         Logger.info(
 177  
             this, "comment #%d posted to issue #%d by %s: %[text]s",
 178  
             number, this.issue().number(), this.self, text
 179  
         );
 180  6
         return this.get(number);
 181  
     }
 182  
 
 183  
     /**
 184  
      * XPath of this element in XML tree.
 185  
      * @return XPath
 186  
      */
 187  
     @NotNull(message = "Xpath is never NULL")
 188  
     private String xpath() {
 189  8
         return String.format(
 190  
             // @checkstyle LineLength (1 line)
 191  
             "/github/repos/repo[@coords='%s']/issues/issue[number='%d']/comments",
 192  
             this.repo, this.ticket
 193  
         );
 194  
     }
 195  
 
 196  
 }