Coverage Report - com.jcabi.github.mock.MkForks
 
Classes in this File Line Coverage Branch Coverage Complexity
MkForks
92%
26/28
0%
0/28
1
MkForks$1
100%
3/3
N/A
1
 
 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.github.Coordinates;
 34  
 import com.jcabi.github.Fork;
 35  
 import com.jcabi.github.Forks;
 36  
 import com.jcabi.github.Repo;
 37  
 import com.jcabi.log.Logger;
 38  
 import com.jcabi.xml.XML;
 39  
 import java.io.IOException;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import org.xembly.Directives;
 42  
 
 43  
 /**
 44  
  * Mock Github forks.
 45  
  *
 46  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 47  
  * @version $Id: 52ae497fe04935f69285aea62801156ae2f88d6c $
 48  
  */
 49  
 @Immutable
 50  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 51  
 final class MkForks implements Forks {
 52  
 
 53  
     /**
 54  
      * Storage.
 55  
      */
 56  
     private final transient MkStorage storage;
 57  
 
 58  
     /**
 59  
      * Login of the user logged in.
 60  
      */
 61  
     private final transient String self;
 62  
 
 63  
     /**
 64  
      * Repo name.
 65  
      */
 66  
     private final transient Coordinates coords;
 67  
 
 68  
     /**
 69  
      * Public ctor.
 70  
      * @param stg Storage
 71  
      * @param login User to login
 72  
      * @param rep Repo
 73  
      * @throws IOException If there is any I/O problem
 74  
      */
 75  
     public MkForks(
 76  
         final MkStorage stg,
 77  
         final String login,
 78  
         final Coordinates rep
 79  3
     ) throws IOException {
 80  3
         this.storage = stg;
 81  3
         this.self = login;
 82  3
         this.coords = rep;
 83  6
         this.storage.apply(
 84  3
             new Directives().xpath(
 85  3
                 String.format(
 86  
                     "/github/repos/repo[@coords='%s']",
 87  
                     this.coords
 88  
                 )
 89  3
             ).addIf("forks")
 90  
         );
 91  3
     }
 92  
     @Override
 93  
     public Repo repo() {
 94  0
         return new MkRepo(this.storage, this.self, this.coords);
 95  
     }
 96  
     /**
 97  
      * Gets a mocked Fork.
 98  
      * @param forkid Fork id
 99  
      * @return Mocked Fork
 100  
      */
 101  
     public Fork get(final int forkid) {
 102  4
         return new MkFork(this.storage, forkid, this.coords);
 103  
     }
 104  
     @Override
 105  
     public Iterable<Fork> iterate(
 106  
         final String sort
 107  
     ) {
 108  2
         return new MkIterable<Fork>(
 109  
             this.storage,
 110  1
             String.format("%s/fork", this.xpath()),
 111  3
             new MkIterable.Mapping<Fork>() {
 112  
                 @Override
 113  
                 public Fork map(final XML xml) {
 114  4
                     return MkForks.this.get(
 115  2
                         Integer.parseInt(xml.xpath("id/text()").get(0))
 116  
                     );
 117  
                 }
 118  
             }
 119  
         );
 120  
     }
 121  
     @Override
 122  
     public Fork create(
 123  
         final String org
 124  
     ) throws IOException {
 125  2
         this.storage.lock();
 126  
         final int number;
 127  
         try {
 128  4
             number = 1 + this.storage.xml().xpath(
 129  2
                 String.format("%s/fork/id/text()", this.xpath())
 130  2
             ).size();
 131  4
             this.storage.apply(
 132  2
                 new Directives().xpath(this.xpath()).add("fork")
 133  2
                     .add("id").set(Integer.toString(number)).up()
 134  2
                     .attr("organization", org)
 135  
             );
 136  
         } finally {
 137  2
             this.storage.unlock();
 138  2
         }
 139  2
         Logger.info(
 140  
             this, "fork %s created inside %s by %s",
 141  
             this.coords, org, this.self
 142  
         );
 143  2
         return this.get(number);
 144  
     }
 145  
 
 146  
     /**
 147  
      * XPath of this element in XML tree.
 148  
      * @return XPath
 149  
      */
 150  
     private String xpath() {
 151  5
         return String.format(
 152  
             "/github/repos/repo[@coords='%s']/forks",
 153  
             this.coords
 154  
         );
 155  
     }
 156  
 }