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