Coverage Report - com.jcabi.github.mock.MkDeployKey
 
Classes in this File Line Coverage Branch Coverage Complexity
MkDeployKey
66%
10/15
44%
8/18
1
MkDeployKey$AjcClosure1
100%
1/1
N/A
1
MkDeployKey$AjcClosure3
100%
1/1
N/A
1
MkDeployKey$AjcClosure5
0%
0/1
N/A
1
MkDeployKey$AjcClosure7
0%
0/1
N/A
1
 
 1  0
 /**
 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.DeployKey;
 35  
 import com.jcabi.github.Repo;
 36  
 import java.io.IOException;
 37  
 import javax.json.JsonObject;
 38  
 import javax.validation.constraints.NotNull;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import lombok.ToString;
 41  
 import org.xembly.Directives;
 42  
 
 43  
 /**
 44  
  * Mock Github deploy key.
 45  
  * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
 46  
  * @version $Id$
 47  
  */
 48  
 @Immutable
 49  
 @Loggable(Loggable.DEBUG)
 50  0
 @ToString
 51  3
 @EqualsAndHashCode(of = { "storage", "owner", "key" })
 52  
 final class MkDeployKey implements DeployKey {
 53  
 
 54  
     /**
 55  
      * Storage.
 56  
      */
 57  
     private final transient MkStorage storage;
 58  
 
 59  
     /**
 60  
      * Repository.
 61  
      */
 62  
     private final transient Repo owner;
 63  
 
 64  
     /**
 65  
      * Id.
 66  
      */
 67  
     private final transient int key;
 68  
 
 69  
     /**
 70  
      * Public ctor.
 71  
      * @param stg Storage
 72  
      * @param number Id
 73  
      * @param repo Repository
 74  
      */
 75  
     MkDeployKey(
 76  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 77  
         final int number,
 78  
         @NotNull(message = "repo can't be NULL") final Repo repo
 79  7
     ) {
 80  7
         this.storage = stg;
 81  7
         this.key = number;
 82  7
         this.owner = repo;
 83  7
     }
 84  
 
 85  
     @Override
 86  
     public int number() {
 87  8
         return this.key;
 88  
     }
 89  
 
 90  
     @Override
 91  
     @NotNull(message = "JSON is never NULL")
 92  
     public JsonObject json() throws IOException {
 93  2
         return new JsonNode(
 94  
             this.storage.xml().nodes(this.xpath()).get(0)
 95  
         ).json();
 96  
     }
 97  
 
 98  
     @Override
 99  
     public void remove() throws IOException {
 100  0
         this.storage.apply(
 101  
             new Directives().xpath(this.xpath()).strict(1).remove()
 102  
         );
 103  0
     }
 104  
 
 105  
     @Override
 106  
     public void patch(
 107  
         @NotNull(message = "json shouldn't be NULL") final JsonObject json
 108  
     ) throws IOException {
 109  0
         new JsonPatch(this.storage).patch(this.xpath(), json);
 110  0
     }
 111  
 
 112  
     /**
 113  
      * XPath of this element in XML tree.
 114  
      * @return XPath
 115  
      */
 116  
     @NotNull(message = "Xpath is never NULL")
 117  
     private String xpath() {
 118  1
         return String.format(
 119  
             "/github/repos/repo[@coords='%s']/deploykeys/deploykey[id='%d']",
 120  
             this.owner.coordinates(),
 121  
             this.key
 122  
         );
 123  
     }
 124  
 
 125  
 }