Coverage Report - com.jcabi.github.mock.MkUser
 
Classes in this File Line Coverage Branch Coverage Complexity
MkUser
74%
35/47
45%
10/22
1.909
MkUser$AjcClosure1
0%
0/1
N/A
1.909
MkUser$AjcClosure11
100%
1/1
N/A
1.909
MkUser$AjcClosure13
100%
1/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.Notifications;
 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 com.jcabi.xml.XML;
 41  
 import java.io.IOException;
 42  
 import java.util.Date;
 43  
 import javax.json.Json;
 44  
 import javax.json.JsonObject;
 45  
 import lombok.EqualsAndHashCode;
 46  
 import lombok.ToString;
 47  
 import org.xembly.Directives;
 48  
 
 49  
 /**
 50  
  * Github user.
 51  
  *
 52  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 53  
  * @version $Id: c259b216dffd6603eecd4884b7cf0cf02460abc2 $
 54  
  * @since 0.5
 55  
  * @checkstyle ClassDataAbstractionCouplingCheck (8 lines)
 56  
  */
 57  
 @Immutable
 58  
 @Loggable(Loggable.DEBUG)
 59  3
 @ToString
 60  6
 @EqualsAndHashCode(of = { "storage", "self" })
 61  
 final class MkUser implements User {
 62  
 
 63  
     /**
 64  
      * Storage.
 65  
      */
 66  
     private final transient MkStorage storage;
 67  
 
 68  
     /**
 69  
      * Login of the user logged in.
 70  
      */
 71  
     private final transient String self;
 72  
 
 73  
     /**
 74  
      * Public ctor.
 75  
      * @param stg Storage
 76  
      * @param login User to login
 77  
      * @throws IOException If there is any I/O problem
 78  
      */
 79  56
     MkUser(final MkStorage stg, final String login) throws IOException {
 80  56
         this.storage = stg;
 81  56
         this.self = login;
 82  112
         this.storage.apply(
 83  
             new Directives()
 84  56
                 .xpath(
 85  56
                     String.format(
 86  
                         "/github/users[not(user[login='%s'])]", login
 87  
                     )
 88  
                 )
 89  56
                 .add("user")
 90  56
                 .add("login").set(login).up()
 91  56
                 .add("type").set("User").up()
 92  56
                 .add("name").set(login).up()
 93  56
                 .add("notifications").up()
 94  
         );
 95  56
     }
 96  
 
 97  
     @Override
 98  
     public Github github() {
 99  0
         return new MkGithub(this.storage, this.self);
 100  
     }
 101  
 
 102  
     @Override
 103  
     public String login() {
 104  76
         return this.self;
 105  
     }
 106  
 
 107  
     @Override
 108  
     public UserOrganizations organizations() {
 109  
         try {
 110  4
             return new MkUserOrganizations(this.storage, this.self);
 111  0
         } catch (final IOException ex) {
 112  0
             throw new IllegalStateException(ex);
 113  
         }
 114  
     }
 115  
 
 116  
     @Override
 117  
     public PublicKeys keys() {
 118  
         try {
 119  11
             return new MkPublicKeys(this.storage, this.self);
 120  0
         } catch (final IOException ex) {
 121  0
             throw new IllegalStateException(ex);
 122  
         }
 123  
     }
 124  
 
 125  
     @Override
 126  
     public UserEmails emails() {
 127  
         try {
 128  8
             return new MkUserEmails(this.storage, this.self);
 129  0
         } catch (final IOException ex) {
 130  0
             throw new IllegalStateException(ex);
 131  
         }
 132  
     }
 133  
 
 134  
     @Override
 135  
     public Notifications notifications() {
 136  3
         return new MkNotifications(
 137  
             this.storage,
 138  1
             this.xpath().concat("/notifications/notification")
 139  
         );
 140  
     }
 141  
 
 142  
     @Override
 143  
     public void markAsRead(final Date lastread) throws IOException {
 144  3
         final Iterable<XML> ids = this.storage.xml().nodes(
 145  2
             this.xpath() + String.format(
 146  
                 "/notifications/notification[date <= %s]/id",
 147  1
                 lastread.getTime()
 148  
             )
 149  
         );
 150  1
         final JsonPatch json = new JsonPatch(this.storage);
 151  1
         final JsonObject read = Json.createObjectBuilder()
 152  1
             .add("read", true).build();
 153  1
         for (final XML nid : ids) {
 154  2
             json.patch(
 155  1
                 String.format(
 156  1
                     this.xpath().concat("/notifications/notification[id = %s]"),
 157  1
                     nid.xpath("text()").get(0)
 158  
                 ),
 159  
                 read
 160  
             );
 161  1
         }
 162  1
     }
 163  
 
 164  
     @Override
 165  
     public void patch(
 166  
         final JsonObject json
 167  
     ) throws IOException {
 168  0
         new JsonPatch(this.storage).patch(this.xpath(), json);
 169  0
     }
 170  
 
 171  
     @Override
 172  
     public JsonObject json() throws IOException {
 173  0
         return new JsonNode(
 174  0
             this.storage.xml().nodes(this.xpath()).get(0)
 175  0
         ).json();
 176  
     }
 177  
 
 178  
     /**
 179  
      * XPath of this element in XML tree.
 180  
      * @return XPath
 181  
      */
 182  
     private String xpath() {
 183  3
         return String.format("/github/users/user[login='%s']", this.self);
 184  
     }
 185  
 
 186  
 }