Coverage Report - com.jcabi.github.mock.MkIssues
 
Classes in this File Line Coverage Branch Coverage Complexity
MkIssues
77%
35/45
0%
0/30
1.125
MkIssues$1
100%
3/3
N/A
1.125
MkIssues$AjcClosure1
100%
1/1
N/A
1.125
MkIssues$AjcClosure3
100%
1/1
N/A
1.125
MkIssues$AjcClosure5
100%
1/1
N/A
1.125
MkIssues$AjcClosure7
100%
1/1
N/A
1.125
MkIssues$AjcClosure9
0%
0/1
N/A
1.125
 
 1  322
 /**
 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.Github;
 36  
 import com.jcabi.github.Issue;
 37  
 import com.jcabi.github.Issues;
 38  
 import com.jcabi.github.Repo;
 39  
 import com.jcabi.github.Search;
 40  
 import com.jcabi.log.Logger;
 41  
 import com.jcabi.xml.XML;
 42  
 import java.io.IOException;
 43  
 import java.util.EnumMap;
 44  
 import java.util.HashMap;
 45  
 import java.util.Map;
 46  
 import lombok.EqualsAndHashCode;
 47  
 import lombok.ToString;
 48  
 import org.xembly.Directives;
 49  
 
 50  
 /**
 51  
  * Mock Github issues.
 52  
  *
 53  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 54  
  * @version $Id: f622adca47557c17fc1097bc97423dec683df85f $
 55  
  * @since 0.5
 56  
  */
 57  
 @Immutable
 58  
 @Loggable(Loggable.DEBUG)
 59  0
 @ToString
 60  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 61  
 final class MkIssues implements Issues {
 62  
 
 63  
     /**
 64  
      * Storage.
 65  
      */
 66  
     private final transient MkStorage storage;
 67  
 
 68  
     /**
 69  
      * Login of the user logged in.
 70  
      */
 71  
     private final transient String self;
 72  
 
 73  
     /**
 74  
      * Repo name.
 75  
      */
 76  
     private final transient Coordinates coords;
 77  
 
 78  
     /**
 79  
      * Public ctor.
 80  
      * @param stg Storage
 81  
      * @param login User to login
 82  
      * @param rep Repo
 83  
      * @throws IOException If there is any I/O problem
 84  
      */
 85  
     MkIssues(
 86  
         final MkStorage stg,
 87  
         final String login,
 88  
         final Coordinates rep
 89  167
     ) throws IOException {
 90  168
         this.storage = stg;
 91  168
         this.self = login;
 92  168
         this.coords = rep;
 93  335
         this.storage.apply(
 94  167
             new Directives().xpath(
 95  167
                 String.format(
 96  
                     "/github/repos/repo[@coords='%s']",
 97  
                     this.coords
 98  
                 )
 99  167
             ).addIf("issues")
 100  
         );
 101  168
     }
 102  
 
 103  
     @Override
 104  
     public Repo repo() {
 105  322
         return new MkRepo(this.storage, this.self, this.coords);
 106  
     }
 107  
 
 108  
     @Override
 109  
     public Issue get(final int number) {
 110  538
         return new MkIssue(this.storage, this.self, this.coords, number);
 111  
     }
 112  
 
 113  
     @Override
 114  
     public Issue create(final String title,
 115  
         final String body
 116  
     )
 117  
         throws IOException {
 118  322
         this.storage.lock();
 119  
         final int number;
 120  
         try {
 121  322
             number = 1 + this.storage.xml().xpath(
 122  161
                 String.format("%s/issue/number/text()", this.xpath())
 123  161
             ).size();
 124  322
             this.storage.apply(
 125  161
                 new Directives().xpath(this.xpath()).add("issue")
 126  161
                     .add("number").set(Integer.toString(number)).up()
 127  161
                     .add("state").set(Issue.OPEN_STATE).up()
 128  161
                     .add("title").set(title).up()
 129  161
                     .add("body").set(body).up()
 130  161
                     .add("created_at").set(new Github.Time().toString()).up()
 131  161
                     .add("updated_at").set(new Github.Time().toString()).up()
 132  161
                     .add("url").set("http://localhost/1").up()
 133  161
                     .add("html_url").set("http://localhost/2").up()
 134  161
                     .add("user").add("login").set(this.self).up().up()
 135  
             );
 136  
         } finally {
 137  161
             this.storage.unlock();
 138  161
         }
 139  322
         Logger.info(
 140  
             this, "issue #%d created in %s by %s: %[text]s",
 141  161
             number, this.repo().coordinates(), this.self, title
 142  
         );
 143  161
         return this.get(number);
 144  
     }
 145  
 
 146  
     @Override
 147  
     public Iterable<Issue> iterate(final Map<String, String> params
 148  
     ) {
 149  6
         return new MkIterable<Issue>(
 150  
             this.storage,
 151  2
             String.format("%s/issue", this.xpath()),
 152  105
             new MkIterable.Mapping<Issue>() {
 153  
                 @Override
 154  
                 public Issue map(final XML xml) {
 155  206
                     return MkIssues.this.get(
 156  103
                         Integer.parseInt(xml.xpath("number/text()").get(0))
 157  
                     );
 158  
                 }
 159  
             }
 160  
         );
 161  
     }
 162  
 
 163  
     @Override
 164  
     @SuppressWarnings("PMD.UseConcurrentHashMap")
 165  
     public Iterable<Issue> search(
 166  
         final Sort sort,
 167  
         final Search.Order direction,
 168  
         final EnumMap<Qualifier, String> qualifiers)
 169  
         throws IOException {
 170  0
         final Map<String, String> params = new HashMap<String, String>();
 171  0
         for (final EnumMap.Entry<Qualifier, String> entry : qualifiers
 172  0
             .entrySet()) {
 173  0
             params.put(entry.getKey().identifier(), entry.getValue());
 174  0
         }
 175  0
         params.put("sort", sort.identifier());
 176  0
         params.put("direction", direction.identifier());
 177  0
         return this.iterate(params);
 178  
     }
 179  
 
 180  
     /**
 181  
      * XPath of this element in XML tree.
 182  
      * @return XPath
 183  
      */
 184  
     private String xpath() {
 185  324
         return String.format(
 186  
             "/github/repos/repo[@coords='%s']/issues",
 187  
             this.coords
 188  
         );
 189  
     }
 190  
 
 191  
 }