Coverage Report - com.jcabi.github.mock.MkRepos
 
Classes in this File Line Coverage Branch Coverage Complexity
MkRepos
82%
34/41
9%
2/22
1.75
MkRepos$1
100%
4/4
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  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.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 lombok.EqualsAndHashCode;
 42  
 import lombok.ToString;
 43  
 import org.xembly.Directives;
 44  
 
 45  
 /**
 46  
  * Github repos.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id: cc0fe92f52365746b8b4bc26489e15e069e062a6 $
 50  
  * @since 0.5
 51  
  * @checkstyle MultipleStringLiterals (500 lines)
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @ToString
 56  0
 @EqualsAndHashCode(of = { "storage", "self" })
 57  4
 final class MkRepos implements Repos {
 58  
 
 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  
      * Public ctor.
 71  
      * @param stg Storage
 72  
      * @param login User to login
 73  
      * @throws IOException If there is any I/O problem
 74  
      */
 75  
     MkRepos(
 76  
         final MkStorage stg,
 77  
         final String login
 78  245
     ) throws IOException {
 79  248
         this.storage = stg;
 80  248
         this.self = login;
 81  248
         this.storage.apply(new Directives().xpath("/github").addIf("repos"));
 82  245
     }
 83  
 
 84  
     @Override
 85  
     public Github github() {
 86  0
         return new MkGithub(this.storage, this.self);
 87  
     }
 88  
 
 89  
     @Override
 90  
     public Repo create(
 91  
         final RepoCreate settings
 92  
     ) throws IOException {
 93  474
         final Coordinates coords = new Coordinates.Simple(
 94  
             this.self,
 95  237
             settings.name()
 96  
         );
 97  479
         this.storage.apply(
 98  235
             new Directives().xpath(this.xpath()).add("repo")
 99  236
                 .attr("coords", coords.toString())
 100  236
                 .add("name").set(settings.name()).up()
 101  236
                 .add("description").set("test repository").up()
 102  237
                 .add("private").set(settings.isPrivate()).up()
 103  
         );
 104  239
         final Repo repo = this.get(coords);
 105  240
         repo.patch(settings.json());
 106  241
         Logger.info(
 107  
             this, "repository %s created by %s",
 108  
             coords, this.self
 109  
         );
 110  241
         return repo;
 111  
     }
 112  
 
 113  
     @Override
 114  
     public Repo get(
 115  
         final Coordinates coords
 116  
     ) {
 117  
         try {
 118  747
             final String xpath = String.format(
 119  249
                 "%s/repo[@coords='%s']", this.xpath(), coords
 120  
             );
 121  251
             if (this.storage.xml().nodes(xpath).isEmpty()) {
 122  1
                 throw new IllegalArgumentException(
 123  1
                     String.format("repository %s doesn't exist", coords)
 124  
                 );
 125  
             }
 126  0
         } catch (final IOException ex) {
 127  0
             throw new IllegalStateException(ex);
 128  250
         }
 129  250
         return new MkRepo(this.storage, this.self, coords);
 130  
     }
 131  
 
 132  
     @Override
 133  
     public void remove(
 134  
         final Coordinates coords) {
 135  
         try {
 136  3
             this.storage.apply(
 137  1
                 new Directives().xpath(
 138  1
                     String.format("%s/repo[@coords='%s']", this.xpath(), coords)
 139  1
                 ).remove()
 140  
             );
 141  0
         } catch (final IOException ex) {
 142  0
             throw new IllegalStateException(ex);
 143  1
         }
 144  1
     }
 145  
 
 146  
     /**
 147  
      * Iterate all public repos, starting with the one you've seen already.
 148  
      * @param identifier The integer ID of the last Repo that you’ve seen.
 149  
      * @return Iterator of repo
 150  
      */
 151  
     @Override
 152  
     public Iterable<Repo> iterate(
 153  
         final String identifier) {
 154  2
         return new MkIterable<Repo>(
 155  
             this.storage,
 156  
             "/github/repos/repo",
 157  3
             new MkIterable.Mapping<Repo>() {
 158  
                 @Override
 159  
                 public Repo map(final XML xml) {
 160  4
                     return new MkRepo(
 161  2
                         MkRepos.this.storage, MkRepos.this.self,
 162  2
                         new Coordinates.Simple(xml.xpath("@coords").get(0))
 163  
                     );
 164  
                 }
 165  
             }
 166  
         );
 167  
     }
 168  
 
 169  
     /**
 170  
      * XPath of this element in XML tree.
 171  
      * @return XPath
 172  
      */
 173  
     private String xpath() {
 174  486
         return "/github/repos";
 175  
     }
 176  
 
 177  
 }