Coverage Report - com.jcabi.github.mock.MkLabels
 
Classes in this File Line Coverage Branch Coverage Complexity
MkLabels
85%
29/34
3%
1/30
1.25
MkLabels$1
100%
3/3
N/A
1.25
MkLabels$AjcClosure1
0%
0/1
N/A
1.25
MkLabels$AjcClosure3
100%
1/1
N/A
1.25
MkLabels$AjcClosure5
100%
1/1
N/A
1.25
MkLabels$AjcClosure7
100%
1/1
N/A
1.25
MkLabels$AjcClosure9
100%
1/1
N/A
1.25
 
 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.Label;
 36  
 import com.jcabi.github.Labels;
 37  
 import com.jcabi.github.Repo;
 38  
 import com.jcabi.xml.XML;
 39  
 import java.io.IOException;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 import org.xembly.Directives;
 43  
 
 44  
 /**
 45  
  * Mock Github labels.
 46  
  *
 47  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 48  
  * @version $Id: f67bcfa474513375e3e977b00010b2a8978aac79 $
 49  
  * @since 0.5
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 55  
 final class MkLabels implements Labels {
 56  
 
 57  
     /**
 58  
      * Storage.
 59  
      */
 60  
     private final transient MkStorage storage;
 61  
 
 62  
     /**
 63  
      * Login of the user logged in.
 64  
      */
 65  
     private final transient String self;
 66  
 
 67  
     /**
 68  
      * Repo name.
 69  
      */
 70  
     private final transient Coordinates coords;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param stg Storage
 75  
      * @param login User to login
 76  
      * @param rep Repo
 77  
      * @throws IOException If there is any I/O problem
 78  
      */
 79  
     MkLabels(final MkStorage stg,
 80  
         final String login,
 81  
         final Coordinates rep
 82  15
     ) throws IOException {
 83  15
         this.storage = stg;
 84  15
         this.self = login;
 85  15
         this.coords = rep;
 86  30
         this.storage.apply(
 87  15
             new Directives().xpath(
 88  15
                 String.format(
 89  
                     "/github/repos/repo[@coords='%s']",
 90  
                     this.coords
 91  
                 )
 92  15
             ).addIf("labels")
 93  
         );
 94  15
     }
 95  
 
 96  
     @Override
 97  
     public Repo repo() {
 98  0
         return new MkRepo(this.storage, this.self, this.coords);
 99  
     }
 100  
 
 101  
     @Override
 102  
     public Label get(final String name
 103  
     ) {
 104  28
         return new MkLabel(this.storage, this.self, this.coords, name);
 105  
     }
 106  
 
 107  
     @Override
 108  
     public Label create(final String name,
 109  
         final String color
 110  
     ) throws IOException {
 111  16
         if (!color.matches("[0-9a-f]{6}")) {
 112  0
             throw new IllegalArgumentException(
 113  0
                 String.format(
 114  
                     "color '%s' is in wrong format, six hex letters expected",
 115  
                     color
 116  
                 )
 117  
             );
 118  
         }
 119  16
         this.storage.apply(
 120  8
             new Directives().xpath(this.xpath()).add("label")
 121  8
                 .add("name").set(name).up()
 122  8
                 .add("color").set(color).up()
 123  
         );
 124  8
         return this.get(name);
 125  
     }
 126  
 
 127  
     @Override
 128  
     public Iterable<Label> iterate() {
 129  9
         return new MkIterable<Label>(
 130  
             this.storage,
 131  3
             String.format("%s/label", this.xpath()),
 132  4
             new MkIterable.Mapping<Label>() {
 133  
                 @Override
 134  
                 public Label map(final XML xml) {
 135  2
                     return MkLabels.this.get(
 136  1
                         xml.xpath("name/text()").get(0)
 137  
                     );
 138  
                 }
 139  
             }
 140  
         );
 141  
     }
 142  
 
 143  
     @Override
 144  
     public void delete(final String name
 145  
     ) throws IOException {
 146  3
         this.storage.apply(
 147  1
             new Directives().xpath(this.xpath())
 148  1
                 .xpath(String.format("label[name='%s']", name))
 149  1
                 .remove()
 150  1
                 .xpath("/github/repos")
 151  1
                 .xpath(String.format("repo[@coords='%s']", this.coords))
 152  1
                 .xpath(String.format("issues/issue/labels/label[.='%s']", name))
 153  1
                 .remove()
 154  
         );
 155  1
     }
 156  
 
 157  
     /**
 158  
      * XPath of this element in XML tree.
 159  
      * @return XPath
 160  
      */
 161  
     private String xpath() {
 162  12
         return String.format(
 163  
             "/github/repos/repo[@coords='%s']/labels",
 164  
             this.coords
 165  
         );
 166  
     }
 167  
 }