Coverage Report - com.jcabi.github.mock.MkIssueLabels
 
Classes in this File Line Coverage Branch Coverage Complexity
MkIssueLabels
80%
36/45
28%
9/32
1.7
MkIssueLabels$1
100%
2/2
N/A
1.7
MkIssueLabels$AjcClosure1
100%
1/1
N/A
1.7
MkIssueLabels$AjcClosure11
0%
0/1
N/A
1.7
MkIssueLabels$AjcClosure3
100%
1/1
N/A
1.7
MkIssueLabels$AjcClosure5
0%
0/1
N/A
1.7
MkIssueLabels$AjcClosure7
100%
1/1
N/A
1.7
MkIssueLabels$AjcClosure9
100%
1/1
N/A
1.7
 
 1  0
 /**
 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.google.common.base.Optional;
 33  
 import com.jcabi.aspects.Immutable;
 34  
 import com.jcabi.aspects.Loggable;
 35  
 import com.jcabi.github.Coordinates;
 36  
 import com.jcabi.github.Event;
 37  
 import com.jcabi.github.Issue;
 38  
 import com.jcabi.github.IssueLabels;
 39  
 import com.jcabi.github.Label;
 40  
 import com.jcabi.xml.XML;
 41  
 import java.io.IOException;
 42  
 import java.util.Collection;
 43  
 import java.util.HashSet;
 44  
 import java.util.Set;
 45  
 import javax.validation.constraints.NotNull;
 46  
 import lombok.EqualsAndHashCode;
 47  
 import lombok.ToString;
 48  
 import org.xembly.Directives;
 49  
 
 50  
 /**
 51  
  * Mock Github labels.
 52  
  *
 53  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 54  
  * @version $Id$
 55  
  * @since 0.5
 56  
  */
 57  
 @Immutable
 58  
 @Loggable(Loggable.DEBUG)
 59  0
 @ToString
 60  0
 @EqualsAndHashCode(of = { "storage", "repo", "ticket" })
 61  12
 final class MkIssueLabels implements IssueLabels {
 62  
 
 63  
     /**
 64  
      * Storage.
 65  
      */
 66  
     private final transient MkStorage storage;
 67  
 
 68  
     /**
 69  
      * Login of the user logged in.
 70  
      */
 71  
     private final transient String self;
 72  
 
 73  
     /**
 74  
      * Repo name.
 75  
      */
 76  
     private final transient Coordinates repo;
 77  
 
 78  
     /**
 79  
      * Issue number.
 80  
      */
 81  
     private final transient int ticket;
 82  
 
 83  
     /**
 84  
      * Public ctor.
 85  
      * @param stg Storage
 86  
      * @param login Login
 87  
      * @param rep Repo
 88  
      * @param issue Issue number
 89  
      * @throws IOException If fails
 90  
      * @checkstyle ParameterNumber (5 lines)
 91  
      */
 92  
     MkIssueLabels(
 93  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 94  
         @NotNull(message = "login can't be NULL") final String login,
 95  
         @NotNull(message = "rep can't be NULL") final Coordinates rep,
 96  
         final int issue
 97  40
     ) throws IOException {
 98  40
         this.storage = stg;
 99  40
         this.self = login;
 100  40
         this.repo = rep;
 101  40
         this.ticket = issue;
 102  40
         this.storage.apply(
 103  
             new Directives().xpath(
 104  
                 String.format(
 105  
                     // @checkstyle LineLength (1 line)
 106  
                     "/github/repos/repo[@coords='%s']/issues/issue[number='%d']",
 107  
                     rep, this.ticket
 108  
                 )
 109  
             ).addIf("labels")
 110  
         );
 111  40
     }
 112  
 
 113  
     @Override
 114  
     @NotNull(message = "Issue is never NULL")
 115  
     public Issue issue() {
 116  2
         return new MkIssue(this.storage, this.self, this.repo, this.ticket);
 117  
     }
 118  
 
 119  
     @Override
 120  
     public void add(@NotNull(message = "labels can't be NULL")
 121  
         final Iterable<String> labels
 122  
     ) throws IOException {
 123  12
         final Collection<String> existing = this.labels();
 124  6
         final Set<String> added = new HashSet<String>();
 125  6
         final Directives dirs = new Directives().xpath(this.xpath());
 126  6
         for (final String label : labels) {
 127  6
             dirs.add("label").set(label).up();
 128  6
             if (!existing.contains(label)) {
 129  6
                 added.add(label);
 130  
             }
 131  6
         }
 132  6
         this.storage.apply(dirs);
 133  6
         if (!added.isEmpty()) {
 134  6
             final MkIssueEvents events = new MkIssueEvents(
 135  
                 this.storage,
 136  
                 this.self,
 137  
                 this.repo
 138  
             );
 139  6
             for (final String label : added) {
 140  6
                 events.create(
 141  
                     Event.LABELED,
 142  
                     this.ticket,
 143  
                     this.self,
 144  
                     Optional.of(label)
 145  
                 );
 146  6
             }
 147  
         }
 148  6
     }
 149  
 
 150  
     @Override
 151  
     public void replace(@NotNull(message = "labels should not be NULL")
 152  
         final Iterable<String> labels
 153  
     ) throws IOException {
 154  0
         this.clear();
 155  0
         this.add(labels);
 156  0
     }
 157  
 
 158  
     @Override
 159  
     @NotNull(message = "Iterable of labels is never NULL")
 160  
     public Iterable<Label> iterate() {
 161  82
         return new MkIterable<Label>(
 162  
             this.storage,
 163  
             String.format("%s/*", this.xpath()),
 164  45
             new MkIterable.Mapping<Label>() {
 165  
                 @Override
 166  
                 public Label map(final XML xml) {
 167  4
                     return new MkLabel(
 168  
                         MkIssueLabels.this.storage,
 169  
                         MkIssueLabels.this.self,
 170  
                         MkIssueLabels.this.repo,
 171  
                         xml.xpath("./text()").get(0)
 172  
                     );
 173  
                 }
 174  
             }
 175  
         );
 176  
     }
 177  
 
 178  
     @Override
 179  
     public void remove(@NotNull(message = "name cannot be NULL")
 180  
         final String name
 181  
     ) throws IOException {
 182  2
         if (this.labels().contains(name)) {
 183  1
             this.storage.apply(
 184  
                 new Directives().xpath(
 185  
                     String.format("%s/label[.='%s']", this.xpath(), name)
 186  
                 ).remove()
 187  
             );
 188  1
             new MkIssueEvents(
 189  
                 this.storage,
 190  
                 this.self,
 191  
                 this.repo
 192  
             ).create(
 193  
                 Event.UNLABELED,
 194  
                 this.ticket,
 195  
                 this.self,
 196  
                 Optional.of(name)
 197  
             );
 198  
         }
 199  1
     }
 200  
 
 201  
     @Override
 202  
     public void clear() throws IOException {
 203  0
         for (final String label : this.labels()) {
 204  0
             this.remove(label);
 205  0
         }
 206  0
     }
 207  
 
 208  
     /**
 209  
      * XPath of this element in XML tree.
 210  
      * @return XPath
 211  
      */
 212  
     @NotNull(message = "Xpath is never NULL")
 213  
     private String xpath() {
 214  48
         return String.format(
 215  
             "/github/repos/repo[@coords='%s']/issues/issue[number='%d']/labels",
 216  
             this.repo, this.ticket
 217  
         );
 218  
     }
 219  
 
 220  
     /**
 221  
      * Returns a set of all of the issue's labels.
 222  
      * @return Set of label names
 223  
      */
 224  
     private Collection<String> labels() {
 225  7
         final Set<String> labels = new HashSet<String>();
 226  7
         for (final Label label : this.iterate()) {
 227  1
             labels.add(label.name());
 228  1
         }
 229  7
         return labels;
 230  
     }
 231  
 }