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