Coverage Report - com.jcabi.github.mock.MkUserEmails
 
Classes in this File Line Coverage Branch Coverage Complexity
MkUserEmails
94%
32/34
16%
4/24
1.25
MkUserEmails$1
100%
2/2
N/A
1.25
MkUserEmails$AjcClosure1
100%
1/1
N/A
1.25
MkUserEmails$AjcClosure3
100%
1/1
N/A
1.25
MkUserEmails$AjcClosure5
100%
1/1
N/A
1.25
MkUserEmails$AjcClosure7
100%
1/1
N/A
1.25
 
 1  8
 /**
 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.UserEmails;
 35  
 import com.jcabi.xml.XML;
 36  
 import java.io.IOException;
 37  
 import javax.json.JsonObject;
 38  
 import lombok.EqualsAndHashCode;
 39  
 import lombok.ToString;
 40  
 import org.xembly.Directives;
 41  
 
 42  
 /**
 43  
  * Mock Github User Emails.
 44  
  *
 45  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 46  
  * @version $Id: 698f1a74b867cc87ec38d42184c6428d440696ee $
 47  
  * @since 0.8
 48  
  */
 49  
 @Immutable
 50  
 @Loggable(Loggable.DEBUG)
 51  0
 @ToString
 52  0
 @EqualsAndHashCode(of = { "storage", "self" })
 53  
 final class MkUserEmails implements UserEmails {
 54  
 
 55  
     /**
 56  
      * Mapping.
 57  
      */
 58  1
     private static final MkIterable.Mapping<String> MAPPING =
 59  10
         new MkIterable.Mapping<String>() {
 60  
             @Override
 61  
             public String map(final XML xml) {
 62  9
                 return xml.xpath("./text()").get(0);
 63  
             }
 64  
         };
 65  
 
 66  
     /**
 67  
      * Storage.
 68  
      */
 69  
     private final transient MkStorage storage;
 70  
 
 71  
     /**
 72  
      * Login of the user logged in.
 73  
      */
 74  
     private final transient String self;
 75  
 
 76  
     /**
 77  
      * Public ctor.
 78  
      * @param stg Storage
 79  
      * @param login User to login
 80  
      * @throws IOException If there is any I/O problem
 81  
      */
 82  
     MkUserEmails(
 83  
         final MkStorage stg,
 84  
         final String login
 85  4
     ) throws IOException {
 86  4
         this.storage = stg;
 87  4
         this.self = login;
 88  8
         this.storage.apply(
 89  4
             new Directives().xpath(this.userXpath()).addIf("emails")
 90  
         );
 91  4
     }
 92  
 
 93  
     @Override
 94  
     public JsonObject json() throws IOException {
 95  3
         return new JsonNode(
 96  1
             this.storage.xml().nodes(this.xpath()).get(0)
 97  1
         ).json();
 98  
     }
 99  
 
 100  
     @Override
 101  
     public Iterable<String> iterate() {
 102  6
         return new MkIterable<String>(
 103  
             this.storage,
 104  2
             String.format("%s/email", this.xpath()),
 105  
             MkUserEmails.MAPPING
 106  
         );
 107  
     }
 108  
 
 109  
     @Override
 110  
     public Iterable<String> add(
 111  
         final Iterable<String> emails
 112  
     ) throws IOException {
 113  8
         this.storage.lock();
 114  
         try {
 115  4
             final Directives directives = new Directives().xpath(this.xpath());
 116  4
             for (final String email : emails) {
 117  6
                 directives.add("email").set(email).up();
 118  6
             }
 119  4
             this.storage.apply(directives);
 120  
         } finally {
 121  4
             this.storage.unlock();
 122  4
         }
 123  4
         return emails;
 124  
     }
 125  
 
 126  
     @Override
 127  
     public void remove(
 128  
         final Iterable<String> emails
 129  
     ) throws IOException {
 130  2
         final Directives directives = new Directives();
 131  1
         for (final String email : emails) {
 132  2
             directives.xpath(
 133  1
                 String.format("%s/email[.='%s']", this.xpath(), email)
 134  1
             ).remove();
 135  1
         }
 136  1
         this.storage.apply(directives);
 137  1
     }
 138  
 
 139  
     /**
 140  
      * XPath of user element in XML tree.
 141  
      * @return XPath
 142  
      */
 143  
     private String userXpath() {
 144  12
         return String.format("/github/users/user[login='%s']", this.self);
 145  
     }
 146  
 
 147  
     /**
 148  
      * XPath of user emails element in XML tree.
 149  
      * @return XPath
 150  
      */
 151  
     private String xpath() {
 152  8
         return String.format("%s/emails", this.userXpath());
 153  
     }
 154  
 }