Coverage Report - com.jcabi.github.mock.MkPublicKeys
 
Classes in this File Line Coverage Branch Coverage Complexity
MkPublicKeys
78%
18/23
0%
0/16
1.333
MkPublicKeys$1
100%
2/2
N/A
1.333
MkPublicKeys$AjcClosure1
0%
0/1
N/A
1.333
MkPublicKeys$AjcClosure3
100%
1/1
N/A
1.333
MkPublicKeys$AjcClosure5
100%
1/1
N/A
1.333
MkPublicKeys$AjcClosure7
100%
1/1
N/A
1.333
MkPublicKeys$AjcClosure9
100%
1/1
N/A
1.333
 
 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.PublicKey;
 35  
 import com.jcabi.github.PublicKeys;
 36  
 import com.jcabi.github.User;
 37  
 import com.jcabi.xml.XML;
 38  
 import java.io.IOException;
 39  
 import javax.validation.constraints.NotNull;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 import org.xembly.Directives;
 43  
 
 44  
 /**
 45  
  * Mock github public keys.
 46  
  *
 47  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 48  
  * @version $Id$
 49  
  * @checkstyle MultipleStringLiteralsCheck (200 lines)
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = { "storage", "self" })
 55  
 final class MkPublicKeys implements PublicKeys {
 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  
      * Public ctor.
 69  
      * @param stg Storage
 70  
      * @param login User to login
 71  
      * @throws IOException If there is any I/O problem
 72  
      */
 73  
     public MkPublicKeys(
 74  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 75  
         @NotNull(message = "login can't be NULL") final String login
 76  6
     ) throws IOException {
 77  6
         this.storage = stg;
 78  6
         this.self = login;
 79  6
         this.storage.apply(
 80  
             new Directives().xpath(this.userXpath()).addIf("keys")
 81  
         );
 82  6
     }
 83  
 
 84  
     @Override
 85  
     @NotNull(message = "User is never NULL")
 86  
     public User user() {
 87  
         try {
 88  0
             return new MkUser(this.storage, this.self);
 89  0
         } catch (final IOException ex) {
 90  0
             throw new IllegalStateException(ex);
 91  
         }
 92  
     }
 93  
 
 94  
     @Override
 95  
     @NotNull(message = "Iterable of keys is never NULL")
 96  
     public Iterable<PublicKey> iterate() {
 97  6
         return new MkIterable<PublicKey>(
 98  
             this.storage,
 99  
             String.format("%s/key", this.xpath()),
 100  5
             new MkIterable.Mapping<PublicKey>() {
 101  
                 @Override
 102  
                 public PublicKey map(final XML xml) {
 103  2
                     return MkPublicKeys.this.get(
 104  
                         Integer.parseInt(xml.xpath("id/text()").get(0))
 105  
                     );
 106  
                 }
 107  
             }
 108  
         );
 109  
     }
 110  
 
 111  
     @Override
 112  
     @NotNull(message = "key is never NULL")
 113  
     public PublicKey get(final int number) {
 114  18
         return new MkPublicKey(this.storage, this.self, number);
 115  
     }
 116  
 
 117  
     @Override
 118  
     @NotNull(message = "created public key is never NULL")
 119  
     public PublicKey create(
 120  
         @NotNull(message = "title cannot be NULL") final String title,
 121  
         @NotNull(message = "key cannot be NULL") final String key
 122  
     ) throws IOException {
 123  10
         this.storage.lock();
 124  
         final int number;
 125  
         try {
 126  5
             number = 1 + this.storage.xml().xpath(
 127  
                 String.format("%s/key/id/text()", this.xpath())
 128  
             ).size();
 129  5
             this.storage.apply(
 130  
                 new Directives().xpath(this.xpath())
 131  
                     .add("key")
 132  
                     .add("id").set(String.valueOf(number)).up()
 133  
                     .add("title").set(title).up()
 134  
                     .add("key").set(key)
 135  
             );
 136  
         } finally {
 137  5
             this.storage.unlock();
 138  5
         }
 139  5
         return this.get(number);
 140  
     }
 141  
 
 142  
     @Override
 143  
     public void remove(final int number) throws IOException {
 144  2
         this.storage.apply(
 145  
             new Directives().xpath(
 146  
                 String.format("%s/key[id='%d']", this.xpath(), number)
 147  
             ).remove()
 148  
         );
 149  1
     }
 150  
 
 151  
     /**
 152  
      * XPath of user element in XML tree.
 153  
      * @return XPath
 154  
      */
 155  
     @NotNull(message = "Xpath of the user is never NULL")
 156  
     private String userXpath() {
 157  20
         return String.format("/github/users/user[login='%s']", this.self);
 158  
     }
 159  
 
 160  
     /**
 161  
      * XPath of user element in XML tree.
 162  
      * @return XPath
 163  
      */
 164  
     @NotNull(message = "Xpath is never NULL")
 165  
     private String xpath() {
 166  14
         return String.format("%s/keys", this.userXpath());
 167  
     }
 168  
 }