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