Coverage Report - com.jcabi.github.mock.MkUser
 
Classes in this File Line Coverage Branch Coverage Complexity
MkUser
57%
19/33
40%
8/20
1.909
MkUser$AjcClosure1
0%
0/1
N/A
1.909
MkUser$AjcClosure11
100%
1/1
N/A
1.909
MkUser$AjcClosure13
0%
0/1
N/A
1.909
MkUser$AjcClosure15
0%
0/1
N/A
1.909
MkUser$AjcClosure17
0%
0/1
N/A
1.909
MkUser$AjcClosure3
100%
1/1
N/A
1.909
MkUser$AjcClosure5
100%
1/1
N/A
1.909
MkUser$AjcClosure7
100%
1/1
N/A
1.909
MkUser$AjcClosure9
100%
1/1
N/A
1.909
 
 1  12
 /**
 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.Github;
 35  
 import com.jcabi.github.Notification;
 36  
 import com.jcabi.github.PublicKeys;
 37  
 import com.jcabi.github.User;
 38  
 import com.jcabi.github.UserEmails;
 39  
 import com.jcabi.github.UserOrganizations;
 40  
 import java.io.IOException;
 41  
 import java.util.ArrayList;
 42  
 import java.util.Date;
 43  
 import java.util.List;
 44  
 import javax.json.JsonObject;
 45  
 import lombok.EqualsAndHashCode;
 46  
 import lombok.ToString;
 47  
 import org.apache.commons.lang3.NotImplementedException;
 48  
 import org.xembly.Directives;
 49  
 
 50  
 /**
 51  
  * Github user.
 52  
  *
 53  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 54  
  * @version $Id: d3f16efe26a6aa7f790141c8d559f727b5b40669 $
 55  
  * @since 0.5
 56  
  * @todo #1305:30min Implement markAsRead(final Date lastread)
 57  
  *  operations in MkUser. Don't forget about unit tests.
 58  
  * @checkstyle ClassDataAbstractionCouplingCheck (8 lines)
 59  
  */
 60  
 @Immutable
 61  
 @Loggable(Loggable.DEBUG)
 62  3
 @ToString
 63  6
 @EqualsAndHashCode(of = { "storage", "self" })
 64  
 final class MkUser implements User {
 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  54
     MkUser(final MkStorage stg, final String login) throws IOException {
 83  55
         this.storage = stg;
 84  55
         this.self = login;
 85  110
         this.storage.apply(
 86  
             new Directives()
 87  54
                 .xpath(
 88  54
                     String.format(
 89  
                         "/github/users[not(user[login='%s'])]", login
 90  
                     )
 91  
                 )
 92  54
                 .add("user")
 93  55
                 .add("login").set(login).up()
 94  55
                 .add("type").set("User").up()
 95  55
                 .add("name").set(login).up()
 96  
         );
 97  55
     }
 98  
 
 99  
     @Override
 100  
     public Github github() {
 101  0
         return new MkGithub(this.storage, this.self);
 102  
     }
 103  
 
 104  
     @Override
 105  
     public String login() {
 106  76
         return this.self;
 107  
     }
 108  
 
 109  
     @Override
 110  
     public UserOrganizations organizations() {
 111  
         try {
 112  4
             return new MkUserOrganizations(this.storage, this.self);
 113  0
         } catch (final IOException ex) {
 114  0
             throw new IllegalStateException(ex);
 115  
         }
 116  
     }
 117  
 
 118  
     @Override
 119  
     public PublicKeys keys() {
 120  
         try {
 121  12
             return new MkPublicKeys(this.storage, this.self);
 122  0
         } catch (final IOException ex) {
 123  0
             throw new IllegalStateException(ex);
 124  
         }
 125  
     }
 126  
 
 127  
     @Override
 128  
     public UserEmails emails() {
 129  
         try {
 130  8
             return new MkUserEmails(this.storage, this.self);
 131  0
         } catch (final IOException ex) {
 132  0
             throw new IllegalStateException(ex);
 133  
         }
 134  
     }
 135  
 
 136  
     @Override
 137  
     public List<Notification> notifications() {
 138  2
         return new ArrayList<Notification>(0);
 139  
     }
 140  
 
 141  
     @Override
 142  
     public void markAsRead(final Date lastread) {
 143  0
         throw new NotImplementedException("MkNotifications#markAsRead");
 144  
     }
 145  
 
 146  
     @Override
 147  
     public void patch(
 148  
         final JsonObject json
 149  
     ) throws IOException {
 150  0
         new JsonPatch(this.storage).patch(this.xpath(), json);
 151  0
     }
 152  
 
 153  
     @Override
 154  
     public JsonObject json() throws IOException {
 155  0
         return new JsonNode(
 156  0
             this.storage.xml().nodes(this.xpath()).get(0)
 157  0
         ).json();
 158  
     }
 159  
 
 160  
     /**
 161  
      * XPath of this element in XML tree.
 162  
      * @return XPath
 163  
      */
 164  
     private String xpath() {
 165  0
         return String.format("/github/users/user[login='%s']", this.self);
 166  
     }
 167  
 
 168  
 }