Coverage Report - com.jcabi.github.mock.MkUser
 
Classes in this File Line Coverage Branch Coverage Complexity
MkUser
44%
11/25
12%
2/16
2
MkUser$AjcClosure1
0%
0/1
N/A
2
MkUser$AjcClosure11
0%
0/1
N/A
2
MkUser$AjcClosure13
0%
0/1
N/A
2
MkUser$AjcClosure15
0%
0/1
N/A
2
MkUser$AjcClosure17
0%
0/1
N/A
2
MkUser$AjcClosure3
100%
1/1
N/A
2
MkUser$AjcClosure5
100%
1/1
N/A
2
MkUser$AjcClosure7
100%
1/1
N/A
2
MkUser$AjcClosure9
100%
1/1
N/A
2
 
 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.Github;
 35  
 import com.jcabi.github.Notification;
 36  
 import com.jcabi.github.Organizations;
 37  
 import com.jcabi.github.PublicKeys;
 38  
 import com.jcabi.github.User;
 39  
 import com.jcabi.github.UserEmails;
 40  
 import java.io.IOException;
 41  
 import java.util.Date;
 42  
 import java.util.List;
 43  
 import javax.json.JsonObject;
 44  
 import javax.validation.constraints.NotNull;
 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$
 55  
  * @since 0.5
 56  
  * @todo #913:30min Implement notifications(), 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  0
 @ToString
 63  1
 @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  
     MkUser(
 83  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 84  
         @NotNull(message = "login can't be NULL") final String login
 85  32
     ) throws IOException {
 86  32
         this.storage = stg;
 87  32
         this.self = login;
 88  32
         this.storage.apply(
 89  
             new Directives().xpath(
 90  
                 String.format("/github/users[not(user[login='%s'])]", login)
 91  
             ).add("user").add("login").set(login)
 92  
         );
 93  32
     }
 94  
 
 95  
     @Override
 96  
     @NotNull(message = "github is never NULL")
 97  
     public Github github() {
 98  0
         return new MkGithub(this.storage, this.self);
 99  
     }
 100  
 
 101  
     @Override
 102  
     @NotNull(message = "login is never NULL")
 103  
     public String login() {
 104  32
         return this.self;
 105  
     }
 106  
 
 107  
     @Override
 108  
     @NotNull(message = "orgs is never NULL")
 109  
     public Organizations organizations() {
 110  
         try {
 111  2
             return new MkOrganizations(this.storage, this.self);
 112  0
         } catch (final IOException ex) {
 113  0
             throw new IllegalStateException(ex);
 114  
         }
 115  
     }
 116  
 
 117  
     @Override
 118  
     @NotNull(message = "public keys is never NULL")
 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  
     @NotNull(message = "emails is never NULL")
 129  
     public UserEmails emails() {
 130  
         try {
 131  8
             return new MkUserEmails(this.storage, this.self);
 132  0
         } catch (final IOException ex) {
 133  0
             throw new IllegalStateException(ex);
 134  
         }
 135  
     }
 136  
 
 137  
     @Override
 138  
     public List<Notification> notifications() {
 139  0
         throw new NotImplementedException("MkNotifications#notifications");
 140  
     }
 141  
 
 142  
     @Override
 143  
     public void markAsRead(final Date lastread) {
 144  0
         throw new NotImplementedException("MkNotifications#markAsRead");
 145  
     }
 146  
 
 147  
     @Override
 148  
     public void patch(
 149  
         @NotNull(message = "json can't be NULL") final JsonObject json
 150  
     ) throws IOException {
 151  0
         new JsonPatch(this.storage).patch(this.xpath(), json);
 152  0
     }
 153  
 
 154  
     @Override
 155  
     @NotNull(message = "JSON is never NULL")
 156  
     public JsonObject json() throws IOException {
 157  0
         return new JsonNode(
 158  
             this.storage.xml().nodes(this.xpath()).get(0)
 159  
         ).json();
 160  
     }
 161  
 
 162  
     /**
 163  
      * XPath of this element in XML tree.
 164  
      * @return XPath
 165  
      */
 166  
     @NotNull(message = "Xpath is never NULL")
 167  
     private String xpath() {
 168  0
         return String.format("/github/users/user[login='%s']", this.self);
 169  
     }
 170  
 
 171  
 }