Coverage Report - com.jcabi.github.mock.MkIssue
 
Classes in this File Line Coverage Branch Coverage Complexity
MkIssue
86%
37/43
32%
11/34
1.714
MkIssue$AjcClosure1
100%
1/1
N/A
1.714
MkIssue$AjcClosure11
100%
1/1
N/A
1.714
MkIssue$AjcClosure13
100%
1/1
N/A
1.714
MkIssue$AjcClosure15
100%
1/1
N/A
1.714
MkIssue$AjcClosure17
100%
1/1
N/A
1.714
MkIssue$AjcClosure3
100%
1/1
N/A
1.714
MkIssue$AjcClosure5
100%
1/1
N/A
1.714
MkIssue$AjcClosure7
100%
1/1
N/A
1.714
MkIssue$AjcClosure9
100%
1/1
N/A
1.714
MkIssue$MkIssueEventMapping
100%
6/6
N/A
1.714
 
 1  60
 /**
 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.google.common.base.Optional;
 33  
 import com.jcabi.aspects.Immutable;
 34  
 import com.jcabi.aspects.Loggable;
 35  
 import com.jcabi.github.Comments;
 36  
 import com.jcabi.github.Coordinates;
 37  
 import com.jcabi.github.Event;
 38  
 import com.jcabi.github.Issue;
 39  
 import com.jcabi.github.IssueLabels;
 40  
 import com.jcabi.github.Label;
 41  
 import com.jcabi.github.Repo;
 42  
 import com.jcabi.xml.XML;
 43  
 import java.io.IOException;
 44  
 import java.util.Map;
 45  
 import javax.json.Json;
 46  
 import javax.json.JsonArrayBuilder;
 47  
 import javax.json.JsonObject;
 48  
 import javax.json.JsonObjectBuilder;
 49  
 import javax.json.JsonValue;
 50  
 import javax.validation.constraints.NotNull;
 51  
 import lombok.EqualsAndHashCode;
 52  
 
 53  
 /**
 54  
  * Mock Github issue.
 55  
  *
 56  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 57  
  * @version $Id$
 58  
  * @since 0.5
 59  
  * @checkstyle ClassDataAbstractionCoupling (500 lines)
 60  
  */
 61  0
 @Immutable
 62  
 @Loggable(Loggable.DEBUG)
 63  1
 @EqualsAndHashCode(of = { "storage", "self", "coords", "num" })
 64  
 @SuppressWarnings("PMD.TooManyMethods")
 65  24
 final class MkIssue implements Issue {
 66  
 
 67  
     /**
 68  
      * Storage.
 69  
      */
 70  
     private final transient MkStorage storage;
 71  
 
 72  
     /**
 73  
      * Login of the user logged in.
 74  
      */
 75  
     private final transient String self;
 76  
 
 77  
     /**
 78  
      * Repo name.
 79  
      */
 80  
     private final transient Coordinates coords;
 81  
 
 82  
     /**
 83  
      * Issue number.
 84  
      */
 85  
     private final transient int num;
 86  
 
 87  
     /**
 88  
      * Public ctor.
 89  
      * @param stg Storage
 90  
      * @param login User to login
 91  
      * @param rep Repo
 92  
      * @param number Issue number
 93  
      * @checkstyle ParameterNumber (5 lines)
 94  
      */
 95  
     MkIssue(
 96  
         @NotNull(message = "stg is never NULL") final MkStorage stg,
 97  
         @NotNull(message = "login is never NULL") final String login,
 98  
         @NotNull(message = "rep is never NULL") final Coordinates rep,
 99  
         final int number
 100  280
     ) {
 101  280
         this.storage = stg;
 102  280
         this.self = login;
 103  280
         this.coords = rep;
 104  280
         this.num = number;
 105  280
     }
 106  
 
 107  
     @Override
 108  
     public String toString() {
 109  0
         return Integer.toString(this.num);
 110  
     }
 111  
 
 112  
     @Override
 113  
     @NotNull(message = "Repository is never NULL")
 114  
     public Repo repo() {
 115  38
         return new MkRepo(this.storage, this.self, this.coords);
 116  
     }
 117  
 
 118  
     @Override
 119  
     public int number() {
 120  58
         return this.num;
 121  
     }
 122  
 
 123  
     @Override
 124  
     @NotNull(message = "comments is never NULL")
 125  
     public Comments comments() {
 126  
         try {
 127  12
             return new MkComments(
 128  
                 this.storage, this.self, this.coords, this.num
 129  
             );
 130  0
         } catch (final IOException ex) {
 131  0
             throw new IllegalStateException(ex);
 132  
         }
 133  
     }
 134  
 
 135  
     @Override
 136  
     @NotNull(message = "labels is never NULL")
 137  
     public IssueLabels labels() {
 138  
         try {
 139  80
             return new MkIssueLabels(
 140  
                 this.storage, this.self, this.coords, this.num
 141  
             );
 142  0
         } catch (final IOException ex) {
 143  0
             throw new IllegalStateException(ex);
 144  
         }
 145  
     }
 146  
 
 147  
     @Override
 148  
     @NotNull(message = "Iterable of events is never NULL")
 149  
     public Iterable<Event> events() throws IOException {
 150  16
         return new MkIterable<Event>(
 151  
             this.storage,
 152  
             String.format(
 153  
                 // @checkstyle LineLength (1 line)
 154  
                 "/github/repos/repo[@coords='%s']/issue-events/issue-event[issue='%s']",
 155  
                 this.coords,
 156  
                 this.num
 157  
             ),
 158  
             new MkIssueEventMapping(this)
 159  
         );
 160  
     }
 161  
 
 162  
     @Override
 163  
     public boolean exists() throws IOException {
 164  4
         return this.storage.xml().xpath(
 165  
             String.format("%s/number/text()", this.xpath())
 166  
         ).size() == 1;
 167  
     }
 168  
 
 169  
     @Override
 170  
     public int compareTo(
 171  
         @NotNull(message = "issue should not be NULL") final Issue issue
 172  
     ) {
 173  4
         return this.number() - issue.number();
 174  
     }
 175  
 
 176  
     @Override
 177  
     public void patch(
 178  
         @NotNull(message = "json can't be NULL") final JsonObject json
 179  
     ) throws IOException {
 180  14
         final Issue.Smart smart = new Issue.Smart(this);
 181  7
         final boolean was = smart.isOpen();
 182  7
         new JsonPatch(this.storage).patch(this.xpath(), json);
 183  7
         final boolean now = smart.isOpen();
 184  7
         if (now != was) {
 185  
             final String type;
 186  4
             if (now) {
 187  1
                 type = Event.REOPENED;
 188  
             } else {
 189  3
                 type = Event.CLOSED;
 190  
             }
 191  4
             new MkIssueEvents(this.storage, this.self, this.coords)
 192  
                 .create(type, this.num, this.self, Optional.<String>absent());
 193  
         }
 194  7
     }
 195  
 
 196  
     @Override
 197  
     @NotNull(message = "JSON is never NULL")
 198  
     public JsonObject json() throws IOException {
 199  60
         final JsonObject obj = new JsonNode(
 200  
             this.storage.xml().nodes(this.xpath()).get(0)
 201  
         ).json();
 202  30
         final JsonObjectBuilder json = Json.createObjectBuilder();
 203  30
         for (final Map.Entry<String, JsonValue> val: obj.entrySet()) {
 204  291
             json.add(val.getKey(), val.getValue());
 205  291
         }
 206  30
         final JsonArrayBuilder array = Json.createArrayBuilder();
 207  30
         for (final Label label : this.labels().iterate()) {
 208  1
             array.add(
 209  
                 Json.createObjectBuilder().add("name", label.name()).build()
 210  
             );
 211  1
         }
 212  30
         return json
 213  
             .add("labels", array)
 214  
             .add(
 215  
                 // @checkstyle MultipleStringLiteralsCheck (1 line)
 216  
                 "assignee",
 217  
                 Json.createObjectBuilder().add(
 218  
                     "login", obj.getString("assignee", "")
 219  
                 ).build()
 220  
             )
 221  
             .add(
 222  
                 "pull_request",
 223  
                 Json.createObjectBuilder().addNull("html_url").build()
 224  
             )
 225  
             .build();
 226  
     }
 227  
 
 228  
     /**
 229  
      * XPath of this element in XML tree.
 230  
      * @return XPath
 231  
      */
 232  
     @NotNull(message = "Xpath is never NULL")
 233  
     private String xpath() {
 234  39
         return String.format(
 235  
             "/github/repos/repo[@coords='%s']/issues/issue[number='%d']",
 236  
             this.coords, this.num
 237  
         );
 238  
     }
 239  
 
 240  12
     private static class MkIssueEventMapping
 241  
         implements MkIterable.Mapping<Event> {
 242  
         /**
 243  
          * Issue events.
 244  
          */
 245  
         private final transient MkIssueEvents evts;
 246  
 
 247  
         /**
 248  
          * Constructor.
 249  
          * @param issue Mock issue to get events from
 250  
          * @throws IOException If there is any I/O problem
 251  
          */
 252  
         public MkIssueEventMapping(
 253  
             @NotNull(message = "issue is never NULL") final MkIssue issue
 254  8
         ) throws IOException {
 255  8
             this.evts = new MkIssueEvents(
 256  
                 issue.storage,
 257  
                 issue.self,
 258  
                 issue.coords
 259  
             );
 260  8
         }
 261  
 
 262  
         @Override
 263  
         public Event map(
 264  
             @NotNull(message = "xml is never NULL") final XML xml
 265  
         ) {
 266  12
             return this.evts.get(
 267  
                 Integer.parseInt(xml.xpath("number/text()").get(0))
 268  
             );
 269  
         }
 270  
     }
 271  
 }