Coverage Report - com.jcabi.github.mock.MkEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
MkEvent
85%
17/20
42%
11/26
1.333
MkEvent$AjcClosure1
100%
1/1
N/A
1.333
MkEvent$AjcClosure3
100%
1/1
N/A
1.333
MkEvent$AjcClosure5
0%
0/1
N/A
1.333
MkEvent$AjcClosure7
100%
1/1
N/A
1.333
 
 1  2
 /**
 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.Coordinates;
 35  
 import com.jcabi.github.Event;
 36  
 import com.jcabi.github.Repo;
 37  
 import java.io.IOException;
 38  
 import javax.json.Json;
 39  
 import javax.json.JsonObject;
 40  
 import javax.json.JsonObjectBuilder;
 41  
 import javax.json.JsonValue;
 42  
 import javax.validation.constraints.NotNull;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 
 46  
 /**
 47  
  * Mock Github event.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 50  
  * @version $Id$
 51  
  * @since 0.6.1
 52  
  */
 53  0
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @ToString
 56  2
 @EqualsAndHashCode(of = { "storage", "self", "coords", "num" })
 57  
 final class MkEvent implements Event {
 58  
     /**
 59  
      * Created at.
 60  
      */
 61  
     private static final String CREATED_AT = "created_at";
 62  
     /**
 63  
      * Event.
 64  
      */
 65  
     private static final String EVENT = "event";
 66  
     /**
 67  
      * Login.
 68  
      */
 69  
     private static final String LOGIN = "login";
 70  
 
 71  
     /**
 72  
      * Storage.
 73  
      */
 74  
     private final transient MkStorage storage;
 75  
 
 76  
     /**
 77  
      * Login of the user logged in.
 78  
      */
 79  
     private final transient String self;
 80  
 
 81  
     /**
 82  
      * Repo name.
 83  
      */
 84  
     private final transient Coordinates coords;
 85  
 
 86  
     /**
 87  
      * ID number of event.
 88  
      */
 89  
     private final transient int num;
 90  
 
 91  
     /**
 92  
      * Public ctor.
 93  
      * @param stg Storage
 94  
      * @param login User to login
 95  
      * @param rep Repo
 96  
      * @param nmbr Number
 97  
      * @checkstyle ParameterNumber (5 lines)
 98  
      */
 99  
     MkEvent(
 100  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 101  
         @NotNull(message = "login can't be NULL") final String login,
 102  
         @NotNull(message = "rep can't be NULL") final Coordinates rep,
 103  
         final int nmbr
 104  35
     ) {
 105  35
         this.storage = stg;
 106  35
         this.self = login;
 107  35
         this.coords = rep;
 108  35
         this.num = nmbr;
 109  35
     }
 110  
 
 111  
     @Override
 112  
     @NotNull(message = "repo is never NULL")
 113  
     public Repo repo() {
 114  12
         return new MkRepo(this.storage, this.self, this.coords);
 115  
     }
 116  
 
 117  
     @Override
 118  
     public int number() {
 119  6
         return this.num;
 120  
     }
 121  
 
 122  
     @Override
 123  
     public int compareTo(
 124  
         @NotNull(message = "event can't be NULL") final Event event
 125  
     ) {
 126  0
         throw new UnsupportedOperationException("#compareTo()");
 127  
     }
 128  
 
 129  
     /**
 130  
      * Describes the event in a JSON object.
 131  
      * @return JSON object
 132  
      * @throws IOException If there is any I/O problem
 133  
      * @todo #1063:30min When the event has a label, retrieve and include the
 134  
      *  label's color too. MkIssueEvents.create() will also need to be
 135  
      *  updated accordingly.
 136  
      */
 137  
     @Override
 138  
     @NotNull(message = "JSON is never NULL")
 139  
     public JsonObject json() throws IOException {
 140  36
         final JsonObject obj = new JsonNode(
 141  
             this.storage.xml().nodes(this.xpath()).get(0)
 142  
         ).json();
 143  18
         JsonObjectBuilder builder = Json.createObjectBuilder()
 144  
             .add("id", this.num)
 145  
             .add(
 146  
                 "url",
 147  
                 String.format(
 148  
                     // @checkstyle LineLength (1 line)
 149  
                     "https://api.jcabi-github.invalid/repos/%s/issues/events/%s",
 150  
                     this.coords,
 151  
                     this.num
 152  
                 )
 153  
             )
 154  
             .add("commit_id", JsonValue.NULL)
 155  
             // @checkstyle MultipleStringLiteralsCheck (1 line)
 156  
             .add(MkEvent.EVENT, obj.getString(MkEvent.EVENT))
 157  
             .add(
 158  
                 "actor",
 159  
                 Json.createObjectBuilder()
 160  
                     // @checkstyle MultipleStringLiteralsCheck (1 line)
 161  
                     .add(MkEvent.LOGIN, obj.getString(MkEvent.LOGIN))
 162  
                     .build()
 163  
             )
 164  
             // @checkstyle MultipleStringLiteralsCheck (1 line)
 165  
             .add(MkEvent.CREATED_AT, obj.getString(MkEvent.CREATED_AT));
 166  18
         final String label = "label";
 167  18
         if (obj.containsKey(label)) {
 168  8
             builder = builder.add(
 169  
                 label,
 170  
                 Json.createObjectBuilder()
 171  
                     .add("name", obj.getString(label))
 172  
                     .build()
 173  
             );
 174  
         }
 175  18
         return builder.build();
 176  
     }
 177  
 
 178  
     /**
 179  
      * XPath of this element in XML tree.
 180  
      * @return XPath
 181  
      */
 182  
     @NotNull(message = "Xpath is never NULL")
 183  
     private String xpath() {
 184  18
         return String.format(
 185  
             // @checkstyle LineLength (1 line)
 186  
             "/github/repos/repo[@coords='%s']/issue-events/issue-event[number='%d']",
 187  
             this.coords, this.num
 188  
         );
 189  
     }
 190  
 }