Coverage Report - com.jcabi.github.mock.MkDeployKeys
 
Classes in this File Line Coverage Branch Coverage Complexity
MkDeployKeys
89%
17/19
0%
0/22
1
MkDeployKeys$AjcClosure1
100%
1/1
N/A
1
MkDeployKeys$AjcClosure3
100%
1/1
N/A
1
MkDeployKeys$AjcClosure5
100%
1/1
N/A
1
MkDeployKeys$AjcClosure7
100%
1/1
N/A
1
 
 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.DeployKey;
 36  
 import com.jcabi.github.DeployKeys;
 37  
 import com.jcabi.github.Repo;
 38  
 import java.io.IOException;
 39  
 import java.util.Collections;
 40  
 import javax.validation.constraints.NotNull;
 41  
 import lombok.EqualsAndHashCode;
 42  
 import lombok.ToString;
 43  
 import org.xembly.Directives;
 44  
 
 45  
 /**
 46  
  * Mock Github deploy keys.
 47  
  *
 48  
  * @author Andres Candal (andres.candal@rollasolution.com)
 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 MkDeployKeys implements DeployKeys {
 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  
     MkDeployKeys(
 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("/github/repos/repo[@coords='%s']", this.coords)
 91  
             ).addIf("deploykeys")
 92  
         );
 93  5
     }
 94  
 
 95  
     @Override
 96  
     @NotNull(message = "repo is never NULL")
 97  
     public Repo repo() {
 98  14
         return new MkRepo(this.storage, this.self, this.coords);
 99  
     }
 100  
 
 101  
     @Override
 102  
     @NotNull(message = "Iterable of deploy keys is never NULL")
 103  
     public Iterable<DeployKey> iterate() {
 104  2
         return Collections.emptyList();
 105  
     }
 106  
 
 107  
     @Override
 108  
     @NotNull(message = "deploy key is never NULL")
 109  
     public DeployKey get(final int number) {
 110  14
         return new MkDeployKey(this.storage, number, this.repo());
 111  
     }
 112  
 
 113  
     @Override
 114  
     @NotNull(message = "created key is never NULL")
 115  
     public DeployKey create(
 116  
         @NotNull(message = "title can't be NULL") final String title,
 117  
         @NotNull(message = "key can't be NULL") final String key
 118  
     )
 119  
         throws IOException {
 120  10
         this.storage.lock();
 121  
         final int number;
 122  
         try {
 123  5
             number = 1 + this.storage.xml().xpath(
 124  
                 String.format("%s/deploykey/id/text()", this.xpath())
 125  
             ).size();
 126  5
             this.storage.apply(
 127  
                 new Directives().xpath(this.xpath())
 128  
                     .add("deploykey")
 129  
                     .add("id").set(String.valueOf(number)).up()
 130  
                     .add("title").set(title).up()
 131  
                     .add("key").set(key)
 132  
             );
 133  
         } finally {
 134  5
             this.storage.unlock();
 135  5
         }
 136  5
         return this.get(number);
 137  
     }
 138  
 
 139  
     /**
 140  
      * XPath of this element in XML tree.
 141  
      * @return XPath
 142  
      */
 143  
     @NotNull(message = "Xpath is never NULL")
 144  
     private String xpath() {
 145  10
         return String.format(
 146  
             "/github/repos/repo[@coords='%s']/deploykeys",
 147  
             this.coords
 148  
         );
 149  
     }
 150  
 
 151  
 }