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