Coverage Report - com.jcabi.github.mock.MkReferences
 
Classes in this File Line Coverage Branch Coverage Complexity
MkReferences
94%
18/19
0%
0/22
1
MkReferences$1
100%
2/2
N/A
1
MkReferences$2
100%
2/2
N/A
1
MkReferences$AjcClosure1
100%
1/1
N/A
1
MkReferences$AjcClosure11
100%
1/1
N/A
1
MkReferences$AjcClosure13
100%
1/1
N/A
1
MkReferences$AjcClosure15
100%
1/1
N/A
1
MkReferences$AjcClosure3
100%
1/1
N/A
1
MkReferences$AjcClosure5
100%
1/1
N/A
1
MkReferences$AjcClosure7
100%
1/1
N/A
1
MkReferences$AjcClosure9
100%
1/1
N/A
1
 
 1  36
 /**
 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  
 
 31  
 package com.jcabi.github.mock;
 32  
 
 33  
 import com.jcabi.aspects.Immutable;
 34  
 import com.jcabi.aspects.Loggable;
 35  
 import com.jcabi.github.Coordinates;
 36  
 import com.jcabi.github.Reference;
 37  
 import com.jcabi.github.References;
 38  
 import com.jcabi.github.Repo;
 39  
 import com.jcabi.xml.XML;
 40  
 import java.io.IOException;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import org.xembly.Directives;
 44  
 
 45  
 /**
 46  
  * Mock of Github References.
 47  
  * @author Mihai Andronache (amihaiemil@gmail.com)
 48  
  * @version $Id$
 49  
  * @checkstyle MultipleStringLiterals (500 lines)
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 54  
 final class MkReferences implements References {
 55  
 
 56  
     /**
 57  
      * Storage.
 58  
      */
 59  
     private final transient MkStorage storage;
 60  
 
 61  
     /**
 62  
      * Login of the user logged in.
 63  
      */
 64  
     private final transient String self;
 65  
 
 66  
     /**
 67  
      * Repo name.
 68  
      */
 69  
     private final transient Coordinates coords;
 70  
 
 71  
     /**
 72  
      * Public constructor.
 73  
      * @param stg Storage.
 74  
      * @param login Login name.
 75  
      * @param rep Repo coordinates.
 76  
      * @throws IOException - If something goes wrong.
 77  
      */
 78  
     MkReferences(
 79  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 80  
         @NotNull(message = "login can't be NULL") final String login,
 81  
         @NotNull(message = "rep can't be NULL") final Coordinates rep
 82  17
     ) throws IOException {
 83  17
         this.storage = stg;
 84  17
         this.self = login;
 85  17
         this.coords = rep;
 86  17
         this.storage.apply(
 87  
             new Directives().xpath(
 88  
                 String.format(
 89  
                     "/github/repos/repo[@coords='%s']/git",
 90  
                     this.coords
 91  
                 )
 92  
             ).addIf("refs")
 93  
         );
 94  17
     }
 95  
 
 96  
     @Override
 97  
     @NotNull(message = "Repository can't be NULL")
 98  
     public Repo repo() {
 99  2
         return new MkRepo(this.storage, this.self, this.coords);
 100  
     }
 101  
 
 102  
     @Override
 103  
     @NotNull(message = "created ref can't be NULL")
 104  
     public Reference create(
 105  
         @NotNull(message = "ref can't be NULL") final String ref,
 106  
         @NotNull(message = "sha can't be NULL") final String sha
 107  
     ) throws IOException {
 108  36
         this.storage.apply(
 109  
             new Directives().xpath(this.xpath()).add("reference")
 110  
                 .add("ref").set(ref).up()
 111  
                 .add("sha").set(sha).up()
 112  
         );
 113  18
         return this.get(ref);
 114  
     }
 115  
 
 116  
     @Override
 117  
     @NotNull(message = "Reference is never NULL")
 118  
     public Reference get(
 119  
         @NotNull(message = "identifier can't be NULL") final String identifier
 120  
     ) {
 121  54
         return new MkReference(
 122  
             this.storage, this.self, this.coords, identifier
 123  
         );
 124  
     }
 125  
 
 126  
     @Override
 127  
     @NotNull(message = "Iterable of references can't be NULL")
 128  
     public Iterable<Reference> iterate() {
 129  6
         return new MkIterable<Reference>(
 130  
             this.storage,
 131  
             String.format("%s/reference", this.xpath()),
 132  8
             new MkIterable.Mapping<Reference>() {
 133  
                 @Override
 134  
                 public Reference map(final XML xml) {
 135  5
                     return MkReferences.this.get(
 136  
                         xml.xpath("ref/text()").get(0)
 137  
                     );
 138  
                 }
 139  
             }
 140  
         );
 141  
     }
 142  
 
 143  
     @Override
 144  
     @NotNull(message = "Iterable of references can't be NULL")
 145  
     public Iterable<Reference> iterate(
 146  
         @NotNull(message = "subnamespace can't be NULL")
 147  
         final String subnamespace
 148  
     ) {
 149  8
         return new MkIterable<Reference>(
 150  
             this.storage,
 151  
             String.format(
 152  
                 "%s/reference/ref[starts-with(., 'refs/%s')]", this.xpath(),
 153  
                 subnamespace
 154  
             ),
 155  8
             new MkIterable.Mapping<Reference>() {
 156  
                 @Override
 157  
                 public Reference map(final XML xml) {
 158  4
                     return MkReferences.this.get(
 159  
                         xml.xpath("text()").get(0)
 160  
                     );
 161  
                 }
 162  
             }
 163  
         );
 164  
     }
 165  
 
 166  
     @Override
 167  
     @NotNull(message = "Iterable of references is never NULL")
 168  
     public Iterable<Reference> tags() {
 169  2
         return this.iterate("tags");
 170  
     }
 171  
 
 172  
     @Override
 173  
     @NotNull(message = "Iterable of references is never NULL")
 174  
     public Iterable<Reference> heads() {
 175  2
         return this.iterate("heads");
 176  
     }
 177  
 
 178  
     @Override
 179  
     public void remove(
 180  
         @NotNull(message = "identifier shouldn't be NULL")
 181  
         final String identifier
 182  
     ) throws IOException {
 183  2
         this.storage.apply(
 184  
             new Directives().xpath(
 185  
                 String.format(
 186  
                     "%s/reference[ref='%s']", this.xpath(), identifier
 187  
                 )
 188  
             ).remove()
 189  
         );
 190  1
     }
 191  
 
 192  
     /**
 193  
      * XPath of this element in XML tree.
 194  
      * @return XPath
 195  
      */
 196  
     @NotNull(message = "Xpath is never NULL")
 197  
     private String xpath() {
 198  26
         return String.format(
 199  
             "/github/repos/repo[@coords='%s']/git/refs",
 200  
             this.coords
 201  
         );
 202  
     }
 203  
 
 204  
 }