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