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