Coverage Report - com.jcabi.github.mock.MkHooks
 
Classes in this File Line Coverage Branch Coverage Complexity
MkHooks
80%
20/25
4%
1/24
1.125
MkHooks$1
100%
2/2
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  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.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 javax.validation.constraints.NotNull;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 import org.xembly.Directives;
 45  
 
 46  
 /**
 47  
  * Mock Github hooks.
 48  
  * @author Paul Polishchuk (ppol@ua.fm)
 49  
  * @version $Id$
 50  
  * @since 0.8
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @ToString
 55  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 56  
 final class MkHooks implements Hooks {
 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  
     public MkHooks(
 81  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 82  
         @NotNull(message = "login can't be NULL") final String login,
 83  
         @NotNull(message = "rep can't be NULL") final Coordinates rep
 84  5
     ) throws IOException {
 85  5
         this.storage = stg;
 86  5
         this.self = login;
 87  5
         this.coords = rep;
 88  5
         this.storage.apply(
 89  
             new Directives().xpath(
 90  
                 String.format(
 91  
                     "/github/repos/repo[@coords='%s']",
 92  
                     this.coords
 93  
                 )
 94  
             ).addIf("hooks")
 95  
         );
 96  5
     }
 97  
 
 98  
     @Override
 99  
     @NotNull(message = "Repo can't be NULL")
 100  
     public Repo repo() {
 101  0
         return new MkRepo(this.storage, this.self, this.coords);
 102  
     }
 103  
 
 104  
     @Override
 105  
     @NotNull(message = "Iterable of hooks can't be NULL")
 106  
     public Iterable<Hook> iterate() {
 107  10
         return new MkIterable<Hook>(
 108  
             this.storage,
 109  
             String.format("%s/hook", this.xpath()),
 110  9
             new MkIterable.Mapping<Hook>() {
 111  
                 @Override
 112  
                 public Hook map(final XML xml) {
 113  4
                     return MkHooks.this.get(
 114  
                         Integer.parseInt(xml.xpath("id/text()").get(0))
 115  
                     );
 116  
                 }
 117  
             }
 118  
         );
 119  
     }
 120  
 
 121  
     @Override
 122  
     @NotNull(message = "hook is never NULL")
 123  
     public Hook get(final int number) {
 124  20
         return new MkHook(this.storage, this.self, this.coords, number);
 125  
     }
 126  
 
 127  
     @Override
 128  
     @NotNull(message = "created hook is never NULL")
 129  
     public Hook create(
 130  
         @NotNull(message = "name can't be NULL") final String name,
 131  
         @NotNull(message = "config can't be NULL")
 132  
         final Map<String, String> config
 133  
     ) throws IOException {
 134  10
         this.storage.lock();
 135  
         final int number;
 136  
         try {
 137  5
             number = 1 + this.storage.xml().xpath(
 138  
                 String.format("%s/hook/id/text()", this.xpath())
 139  
             ).size();
 140  5
             final Directives dirs = new Directives().xpath(this.xpath())
 141  
                 .add("hook")
 142  
                 .add("id").set(String.valueOf(number)).up()
 143  
                 .add("name").set(name).up()
 144  
                 .add("active").set("true").up()
 145  
                 .add("events").up()
 146  
                 .add("config");
 147  5
             for (final Map.Entry<String, String> entr : config.entrySet()) {
 148  0
                 dirs.add(entr.getKey()).set(entr.getValue()).up();
 149  0
             }
 150  5
             this.storage.apply(dirs);
 151  
         } finally {
 152  5
             this.storage.unlock();
 153  5
         }
 154  5
         return this.get(number);
 155  
     }
 156  
 
 157  
     @Override
 158  
     public void remove(final int number) throws IOException {
 159  2
         this.storage.apply(
 160  
             new Directives().xpath(
 161  
                 String.format("%s/hook[id='%d']", this.xpath(), number)
 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  16
         return String.format(
 173  
             "/github/repos/repo[@coords='%s']/hooks",
 174  
             this.coords
 175  
         );
 176  
     }
 177  
 
 178  
 }