Coverage Report - com.jcabi.github.mock.MkRepos
 
Classes in this File Line Coverage Branch Coverage Complexity
MkRepos
77%
24/31
11%
2/18
1.75
MkRepos$1
100%
2/2
N/A
1.75
MkRepos$AjcClosure1
0%
0/1
N/A
1.75
MkRepos$AjcClosure3
100%
1/1
N/A
1.75
MkRepos$AjcClosure5
100%
1/1
N/A
1.75
MkRepos$AjcClosure7
100%
1/1
N/A
1.75
MkRepos$AjcClosure9
100%
1/1
N/A
1.75
 
 1  386
 /**
 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.Repo;
 37  
 import com.jcabi.github.Repos;
 38  
 import com.jcabi.log.Logger;
 39  
 import com.jcabi.xml.XML;
 40  
 import java.io.IOException;
 41  
 import javax.json.JsonObject;
 42  
 import javax.validation.constraints.NotNull;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Github repos.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 51  
  * @version $Id$
 52  
  * @since 0.5
 53  
  * @checkstyle MultipleStringLiterals (500 lines)
 54  
  */
 55  
 @Immutable
 56  
 @Loggable(Loggable.DEBUG)
 57  0
 @ToString
 58  0
 @EqualsAndHashCode(of = { "storage", "self" })
 59  4
 final class MkRepos implements Repos {
 60  
 
 61  
     /**
 62  
      * Storage.
 63  
      */
 64  
     private final transient MkStorage storage;
 65  
 
 66  
     /**
 67  
      * Login of the user logged in.
 68  
      */
 69  
     private final transient String self;
 70  
 
 71  
     /**
 72  
      * Public ctor.
 73  
      * @param stg Storage
 74  
      * @param login User to login
 75  
      * @throws IOException If there is any I/O problem
 76  
      */
 77  
     MkRepos(
 78  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 79  
         @NotNull(message = "login can't be NULL") final String login
 80  190
     ) throws IOException {
 81  190
         this.storage = stg;
 82  190
         this.self = login;
 83  190
         this.storage.apply(new Directives().xpath("/github").addIf("repos"));
 84  190
     }
 85  
 
 86  
     @Override
 87  
     @NotNull(message = "github can't be NULL")
 88  
     public Github github() {
 89  0
         return new MkGithub(this.storage, this.self);
 90  
     }
 91  
 
 92  
     @Override
 93  
     @NotNull(message = "repo is never NULL")
 94  
     public Repo create(
 95  
         @NotNull(message = "json can't be NULL") final JsonObject json
 96  
     ) throws IOException {
 97  374
         final String name = json.getString("name");
 98  187
         final Coordinates coords = new Coordinates.Simple(this.self, name);
 99  187
         this.storage.apply(
 100  
             new Directives().xpath(this.xpath()).add("repo")
 101  
                 .attr("coords", coords.toString())
 102  
                 .add("name").set(name).up()
 103  
                 .add("description").set("test repository").up()
 104  
                 .add("private").set("false").up()
 105  
         );
 106  187
         final Repo repo = this.get(coords);
 107  187
         repo.patch(json);
 108  187
         Logger.info(
 109  
             this, "repository %s created by %s",
 110  
             coords, this.self
 111  
         );
 112  187
         return repo;
 113  
     }
 114  
 
 115  
     @Override
 116  
     @NotNull(message = "Repo is never NULL")
 117  
     public Repo get(
 118  
         @NotNull(message = "coords can't be NULL") final Coordinates coords
 119  
     ) {
 120  
         try {
 121  386
             final String xpath = String.format(
 122  
                 "%s/repo[@coords='%s']", this.xpath(), coords
 123  
             );
 124  193
             if (this.storage.xml().nodes(xpath).isEmpty()) {
 125  1
                 throw new IllegalArgumentException(
 126  
                     String.format("repository %s doesn't exist", coords)
 127  
                 );
 128  
             }
 129  0
         } catch (final IOException ex) {
 130  0
             throw new IllegalStateException(ex);
 131  192
         }
 132  192
         return new MkRepo(this.storage, this.self, coords);
 133  
     }
 134  
 
 135  
     @Override
 136  
     public void remove(
 137  
         @NotNull(message = "coordinates can't be NULL")
 138  
         final Coordinates coords) {
 139  
         try {
 140  2
             this.storage.apply(
 141  
                 new Directives().xpath(
 142  
                     String.format("%s/repo[@coords='%s']", this.xpath(), coords)
 143  
                 ).remove()
 144  
             );
 145  0
         } catch (final IOException ex) {
 146  0
             throw new IllegalStateException(ex);
 147  1
         }
 148  1
     }
 149  
 
 150  
     /**
 151  
      * Iterate all public repos, starting with the one you've seen already.
 152  
      * @param identifier The integer ID of the last Repo that you’ve seen.
 153  
      * @return Iterator of repo
 154  
      */
 155  
     @Override
 156  
     public Iterable<Repo> iterate(
 157  
         @NotNull(message = "identifier can't be NULL")
 158  
         final String identifier) {
 159  2
         return new MkIterable<Repo>(
 160  
             this.storage,
 161  
             "/github/repos/repo",
 162  3
             new MkIterable.Mapping<Repo>() {
 163  
                 @Override
 164  
                 public Repo map(final XML xml) {
 165  2
                     return new MkRepo(
 166  
                         MkRepos.this.storage, MkRepos.this.self,
 167  
                         new Coordinates.Simple(xml.xpath("@coords").get(0))
 168  
                     );
 169  
                 }
 170  
             }
 171  
         );
 172  
     }
 173  
 
 174  
     /**
 175  
      * XPath of this element in XML tree.
 176  
      * @return XPath
 177  
      */
 178  
     @NotNull(message = "Xpath is never NULL")
 179  
     private String xpath() {
 180  381
         return "/github/repos";
 181  
     }
 182  
 
 183  
 }