Coverage Report - com.jcabi.github.mock.MkDeployKeys
 
Classes in this File Line Coverage Branch Coverage Complexity
MkDeployKeys
93%
27/29
0%
0/28
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  10
 /**
 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.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 lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 import org.xembly.Directives;
 43  
 
 44  
 /**
 45  
  * Mock Github deploy keys.
 46  
  *
 47  
  * @author Andres Candal (andres.candal@rollasolution.com)
 48  
  * @version $Id: 7f19f9d6cabc68da4bdd83ea12cf13da87d2a2e4 $
 49  
  * @since 0.8
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 55  
 final class MkDeployKeys implements DeployKeys {
 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  
     MkDeployKeys(
 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  10
         this.storage.apply(
 88  5
             new Directives().xpath(
 89  5
                 String.format("/github/repos/repo[@coords='%s']", this.coords)
 90  5
             ).addIf("deploykeys")
 91  
         );
 92  5
     }
 93  
 
 94  
     @Override
 95  
     public Repo repo() {
 96  14
         return new MkRepo(this.storage, this.self, this.coords);
 97  
     }
 98  
 
 99  
     @Override
 100  
     public Iterable<DeployKey> iterate() {
 101  2
         return Collections.emptyList();
 102  
     }
 103  
 
 104  
     @Override
 105  
     public DeployKey get(final int number) {
 106  14
         return new MkDeployKey(this.storage, number, this.repo());
 107  
     }
 108  
 
 109  
     @Override
 110  
     public DeployKey create(
 111  
         final String title,
 112  
         final String key
 113  
     )
 114  
         throws IOException {
 115  10
         this.storage.lock();
 116  
         final int number;
 117  
         try {
 118  10
             number = 1 + this.storage.xml().xpath(
 119  5
                 String.format("%s/deploykey/id/text()", this.xpath())
 120  5
             ).size();
 121  10
             this.storage.apply(
 122  5
                 new Directives().xpath(this.xpath())
 123  5
                     .add("deploykey")
 124  5
                     .add("id").set(String.valueOf(number)).up()
 125  5
                     .add("title").set(title).up()
 126  5
                     .add("key").set(key)
 127  
             );
 128  
         } finally {
 129  5
             this.storage.unlock();
 130  5
         }
 131  5
         return this.get(number);
 132  
     }
 133  
 
 134  
     /**
 135  
      * XPath of this element in XML tree.
 136  
      * @return XPath
 137  
      */
 138  
     private String xpath() {
 139  10
         return String.format(
 140  
             "/github/repos/repo[@coords='%s']/deploykeys",
 141  
             this.coords
 142  
         );
 143  
     }
 144  
 
 145  
 }