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