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