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