Coverage Report - com.jcabi.github.mock.MkIssueEvents
 
Classes in this File Line Coverage Branch Coverage Complexity
MkIssueEvents
94%
36/38
6%
2/30
1.143
MkIssueEvents$1
100%
3/3
N/A
1.143
MkIssueEvents$AjcClosure1
100%
1/1
N/A
1.143
MkIssueEvents$AjcClosure3
100%
1/1
N/A
1.143
MkIssueEvents$AjcClosure5
100%
1/1
N/A
1.143
MkIssueEvents$AjcClosure7
100%
1/1
N/A
1.143
 
 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.google.common.base.Optional;
 33  
 import com.jcabi.aspects.Immutable;
 34  
 import com.jcabi.aspects.Loggable;
 35  
 import com.jcabi.github.Coordinates;
 36  
 import com.jcabi.github.Event;
 37  
 import com.jcabi.github.Github;
 38  
 import com.jcabi.github.IssueEvents;
 39  
 import com.jcabi.github.Repo;
 40  
 import com.jcabi.log.Logger;
 41  
 import com.jcabi.xml.XML;
 42  
 import java.io.IOException;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Mock GitHub issue events.
 49  
  *
 50  
  * @author Chris Rebert (github@chrisrebert.com)
 51  
  * @version $Id: 3f1e5509e740feeec607f2ebdd2dc5c26882eab7 $
 52  
  * @since 0.23
 53  
  */
 54  
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @ToString
 57  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 58  
 final class MkIssueEvents implements IssueEvents {
 59  
     /**
 60  
      * Storage.
 61  
      */
 62  
     private final transient MkStorage storage;
 63  
 
 64  
     /**
 65  
      * Login of the user logged in.
 66  
      */
 67  
     private final transient String self;
 68  
 
 69  
     /**
 70  
      * Repo name.
 71  
      */
 72  
     private final transient Coordinates coords;
 73  
 
 74  
     /**
 75  
      * Public constructor.
 76  
      * @param stg Storage
 77  
      * @param login User to login
 78  
      * @param rep Repo
 79  
      * @throws IOException If there is any I/O problem
 80  
      */
 81  
     MkIssueEvents(
 82  
         final MkStorage stg,
 83  
         final String login,
 84  
         final Coordinates rep
 85  26
     ) throws IOException {
 86  26
         this.storage = stg;
 87  26
         this.self = login;
 88  26
         this.coords = rep;
 89  52
         this.storage.apply(
 90  26
             new Directives().xpath(
 91  26
                 String.format(
 92  
                     "/github/repos/repo[@coords='%s']",
 93  
                     this.coords
 94  
                 )
 95  26
             ).addIf("issue-events")
 96  
         );
 97  26
     }
 98  
 
 99  
     @Override
 100  
     public Repo repo() {
 101  2
         return new MkRepo(this.storage, this.self, this.coords);
 102  
     }
 103  
 
 104  
     @Override
 105  
     public Event get(final int number) {
 106  72
         return new MkEvent(this.storage, this.self, this.coords, number);
 107  
     }
 108  
 
 109  
     @Override
 110  
     public Iterable<Event> iterate() {
 111  6
         return new MkIterable<Event>(
 112  
             this.storage,
 113  2
             String.format("%s/issue-event", this.xpath()),
 114  6
             new MkIterable.Mapping<Event>() {
 115  
                 @Override
 116  
                 public Event map(final XML xml) {
 117  8
                     return MkIssueEvents.this.get(
 118  4
                         Integer.parseInt(xml.xpath("number/text()").get(0))
 119  
                     );
 120  
                 }
 121  
             }
 122  
         );
 123  
     }
 124  
 
 125  
     /**
 126  
      * Creates a new issue event.
 127  
      * This has no equivalent in GitHub's public API, since GitHub generates
 128  
      * events automatically in response to some other API calls.
 129  
      * @param type Type of event
 130  
      * @param issue ID number of issue the event is regarding
 131  
      * @param login Username of actor who caused the event
 132  
      * @param label Label added or removed
 133  
      * @return The newly created issue event
 134  
      * @throws IOException If there is any I/O problem
 135  
      * @todo #1063:30min Make it possible to set the "assignee" field for
 136  
      *  "assigned"/"unassigned" events. Make it possible to set the
 137  
      *  "milestone" field for "milestoned"/"demilestoned" events. Make it
 138  
      *  possible to set the "rename" field for "renamed" events. Make it
 139  
      *  possible to set the "commit_id" field for events related to commits.
 140  
      *  See https://developer.github.com/v3/issues/events/ for details.
 141  
      * @checkstyle ParameterNumberCheck (4 lines)
 142  
      */
 143  
     public Event create(
 144  
         final String type,
 145  
         final int issue, final String login, final Optional<String> label
 146  
     ) throws IOException {
 147  38
         final String created = new Github.Time().toString();
 148  19
         this.storage.lock();
 149  
         final int number;
 150  
         try {
 151  38
             number = 1 + this.storage.xml().xpath(
 152  19
                 String.format("%s/issue-event/number/text()", this.xpath())
 153  19
             ).size();
 154  19
             Directives directives = new Directives()
 155  19
                 .xpath(this.xpath())
 156  19
                 .add("issue-event")
 157  19
                 .add("issue").set(Integer.toString(issue)).up()
 158  19
                 .add("number").set(Integer.toString(number)).up()
 159  19
                 .add("event").set(type).up()
 160  19
                 .add("created_at").set(created).up()
 161  19
                 .add("login").set(login).up();
 162  19
             if (label.isPresent()) {
 163  10
                 directives = directives.add("label").set(label.get()).up();
 164  
             }
 165  19
             this.storage.apply(directives);
 166  
         } finally {
 167  19
             this.storage.unlock();
 168  19
         }
 169  38
         Logger.info(
 170  
             MkEvent.class,
 171  
             "issue event #%d of type %s created in %s for issue #%d by %s",
 172  19
             number, type, this.self, issue, login
 173  
         );
 174  19
         return this.get(number);
 175  
     }
 176  
 
 177  
     /**
 178  
      * XPath of this element in XML tree.
 179  
      * @return XPath
 180  
      */
 181  
     private String xpath() {
 182  40
         return String.format(
 183  
             "/github/repos/repo[@coords='%s']/issue-events",
 184  
             this.coords
 185  
         );
 186  
     }
 187  
 }