Coverage Report - com.jcabi.github.mock.MkComment
 
Classes in this File Line Coverage Branch Coverage Complexity
MkComment
75%
15/20
0%
0/26
1
MkComment$AjcClosure1
100%
1/1
N/A
1
MkComment$AjcClosure11
100%
1/1
N/A
1
MkComment$AjcClosure3
100%
1/1
N/A
1
MkComment$AjcClosure5
0%
0/1
N/A
1
MkComment$AjcClosure7
100%
1/1
N/A
1
MkComment$AjcClosure9
100%
1/1
N/A
1
 
 1  4
 /**
 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.Coordinates;
 36  
 import com.jcabi.github.Issue;
 37  
 import java.io.IOException;
 38  
 import javax.json.JsonObject;
 39  
 import javax.validation.constraints.NotNull;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 import org.xembly.Directives;
 43  
 
 44  
 /**
 45  
  * Mock Github comment.
 46  
  *
 47  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 48  
  * @version $Id$
 49  
  * @since 0.5
 50  
  */
 51  0
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = { "storage", "self", "repo", "ticket", "num" })
 55  
 final class MkComment implements Comment {
 56  
 
 57  
     /**
 58  
      * Storage.
 59  
      */
 60  
     private final transient MkStorage storage;
 61  
 
 62  
     /**
 63  
      * Login of the user logged in.
 64  
      */
 65  
     private final transient String self;
 66  
 
 67  
     /**
 68  
      * Repo name.
 69  
      */
 70  
     private final transient Coordinates repo;
 71  
 
 72  
     /**
 73  
      * Issue number.
 74  
      */
 75  
     private final transient int ticket;
 76  
 
 77  
     /**
 78  
      * Comment number.
 79  
      */
 80  
     private final transient int num;
 81  
 
 82  
     /**
 83  
      * Public ctor.
 84  
      * @param stg Storage
 85  
      * @param login User to login
 86  
      * @param rep Repo
 87  
      * @param issue Issue number
 88  
      * @param number Comment number
 89  
      * @checkstyle ParameterNumber (5 lines)
 90  
      */
 91  
     MkComment(
 92  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 93  
         @NotNull(message = "login can't be NULL") final String login,
 94  
         @NotNull(message = "rep can't be NULL") final Coordinates rep,
 95  
         final int issue,
 96  
         final int number
 97  11
     ) {
 98  11
         this.storage = stg;
 99  11
         this.self = login;
 100  11
         this.repo = rep;
 101  11
         this.ticket = issue;
 102  11
         this.num = number;
 103  11
     }
 104  
 
 105  
     @Override
 106  
     @NotNull(message = "Issue is never NULL")
 107  
     public Issue issue() {
 108  10
         return new MkIssue(this.storage, this.self, this.repo, this.ticket);
 109  
     }
 110  
 
 111  
     @Override
 112  
     public int number() {
 113  10
         return this.num;
 114  
     }
 115  
 
 116  
     @Override
 117  
     public void remove() throws IOException {
 118  0
         this.storage.apply(
 119  
             new Directives().xpath(this.xpath()).strict(1).remove()
 120  
         );
 121  0
     }
 122  
 
 123  
     @Override
 124  
     public int compareTo(
 125  
         @NotNull(message = "comment cannot be NULL") final Comment comment
 126  
     ) {
 127  4
         return this.number() - comment.number();
 128  
     }
 129  
 
 130  
     @Override
 131  
     public void patch(
 132  
         @NotNull(message = "json can't be NULL") final JsonObject json
 133  
     ) throws IOException {
 134  2
         new JsonPatch(this.storage).patch(this.xpath(), json);
 135  1
     }
 136  
 
 137  
     @Override
 138  
     @NotNull(message = "JSON is never NULL")
 139  
     public JsonObject json() throws IOException {
 140  24
         return new JsonNode(
 141  
             this.storage.xml().nodes(this.xpath()).get(0)
 142  
         ).json();
 143  
     }
 144  
 
 145  
     /**
 146  
      * XPath of this element in XML tree.
 147  
      * @return XPath
 148  
      */
 149  
     @NotNull(message = "Xpath is never NULL")
 150  
     private String xpath() {
 151  13
         return String.format(
 152  
             // @checkstyle LineLength (1 line)
 153  
             "/github/repos/repo[@coords='%s']/issues/issue[number='%d']/comments/comment[number='%d']",
 154  
             this.repo, this.ticket, this.num
 155  
         );
 156  
     }
 157  
 
 158  
 }