Coverage Report - com.jcabi.github.mock.MkHooks
 
Classes in this File Line Coverage Branch Coverage Complexity
MkHooks
87%
35/40
3%
1/30
1.125
MkHooks$1
100%
3/3
N/A
1.125
MkHooks$AjcClosure1
0%
0/1
N/A
1.125
MkHooks$AjcClosure3
100%
1/1
N/A
1.125
MkHooks$AjcClosure5
100%
1/1
N/A
1.125
MkHooks$AjcClosure7
100%
1/1
N/A
1.125
MkHooks$AjcClosure9
100%
1/1
N/A
1.125
 
 1  20
 /**
 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.Hook;
 36  
 import com.jcabi.github.Hooks;
 37  
 import com.jcabi.github.Repo;
 38  
 import com.jcabi.xml.XML;
 39  
 import java.io.IOException;
 40  
 import java.util.Map;
 41  
 import lombok.EqualsAndHashCode;
 42  
 import lombok.ToString;
 43  
 import org.xembly.Directives;
 44  
 
 45  
 /**
 46  
  * Mock Github hooks.
 47  
  * @author Paul Polishchuk (ppol@ua.fm)
 48  
  * @version $Id: d86e4823f92a537c19fd203b00bda7196231ffac $
 49  
  * @since 0.8
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 55  
 final class MkHooks implements Hooks {
 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  
     public MkHooks(
 80  
         final MkStorage stg,
 81  
         final String login,
 82  
         final Coordinates rep
 83  5
     ) throws IOException {
 84  5
         this.storage = stg;
 85  5
         this.self = login;
 86  5
         this.coords = rep;
 87  9
         this.storage.apply(
 88  4
             new Directives().xpath(
 89  4
                 String.format(
 90  
                     "/github/repos/repo[@coords='%s']",
 91  
                     this.coords
 92  
                 )
 93  4
             ).addIf("hooks")
 94  
         );
 95  5
     }
 96  
 
 97  
     @Override
 98  
     public Repo repo() {
 99  0
         return new MkRepo(this.storage, this.self, this.coords);
 100  
     }
 101  
 
 102  
     @Override
 103  
     public Iterable<Hook> iterate() {
 104  15
         return new MkIterable<Hook>(
 105  
             this.storage,
 106  5
             String.format("%s/hook", this.xpath()),
 107  9
             new MkIterable.Mapping<Hook>() {
 108  
                 @Override
 109  
                 public Hook map(final XML xml) {
 110  8
                     return MkHooks.this.get(
 111  4
                         Integer.parseInt(xml.xpath("id/text()").get(0))
 112  
                     );
 113  
                 }
 114  
             }
 115  
         );
 116  
     }
 117  
 
 118  
     @Override
 119  
     public Hook get(final int number) {
 120  20
         return new MkHook(this.storage, this.self, this.coords, number);
 121  
     }
 122  
 
 123  
     @Override
 124  
     public Hook create(
 125  
         final String name,
 126  
         final Map<String, String> config,
 127  
         final boolean active
 128  
     ) throws IOException {
 129  10
         this.storage.lock();
 130  
         final int number;
 131  
         try {
 132  9
             number = 1 + this.storage.xml().xpath(
 133  4
                 String.format("%s/hook/id/text()", this.xpath())
 134  4
             ).size();
 135  4
             final Directives dirs = new Directives().xpath(this.xpath())
 136  4
                 .add("hook")
 137  5
                 .add("id").set(String.valueOf(number)).up()
 138  4
                 .add("name").set(name).up()
 139  5
                 .add("active").set(Boolean.toString(active)).up()
 140  4
                 .add("events").up()
 141  4
                 .add("config");
 142  4
             for (final Map.Entry<String, String> entr : config.entrySet()) {
 143  0
                 dirs.add(entr.getKey()).set(entr.getValue()).up();
 144  0
             }
 145  4
             this.storage.apply(dirs);
 146  
         } finally {
 147  5
             this.storage.unlock();
 148  5
         }
 149  5
         return this.get(number);
 150  
     }
 151  
 
 152  
     @Override
 153  
     public void remove(final int number) throws IOException {
 154  3
         this.storage.apply(
 155  1
             new Directives().xpath(
 156  1
                 String.format("%s/hook[id='%d']", this.xpath(), number)
 157  1
             ).remove()
 158  
         );
 159  1
     }
 160  
 
 161  
     /**
 162  
      * XPath of this element in XML tree.
 163  
      * @return XPath
 164  
      */
 165  
     private String xpath() {
 166  14
         return String.format(
 167  
             "/github/repos/repo[@coords='%s']/hooks",
 168  
             this.coords
 169  
         );
 170  
     }
 171  
 
 172  
 }