Coverage Report - com.jcabi.github.mock.MkOrganizations
 
Classes in this File Line Coverage Branch Coverage Complexity
MkOrganizations
63%
14/22
0%
0/16
1.375
MkOrganizations$1
40%
4/10
0%
0/4
1.375
MkOrganizations$2
50%
1/2
N/A
1.375
MkOrganizations$3
50%
1/2
N/A
1.375
MkOrganizations$AjcClosure1
0%
0/1
N/A
1.375
MkOrganizations$AjcClosure3
0%
0/1
N/A
1.375
MkOrganizations$AjcClosure5
100%
1/1
N/A
1.375
MkOrganizations$AjcClosure7
100%
1/1
N/A
1.375
MkOrganizations$AjcClosure9
100%
1/1
N/A
1.375
 
 1  1
 /**
 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.Organization;
 36  
 import com.jcabi.github.Organizations;
 37  
 import com.jcabi.github.User;
 38  
 import com.jcabi.xml.XML;
 39  
 import java.io.IOException;
 40  
 import java.security.SecureRandom;
 41  
 import java.util.Random;
 42  
 import javax.json.Json;
 43  
 import javax.json.JsonObject;
 44  
 import javax.validation.constraints.NotNull;
 45  
 import lombok.EqualsAndHashCode;
 46  
 import lombok.ToString;
 47  
 import org.xembly.Directives;
 48  
 
 49  
 /**
 50  
  * Github organizations.
 51  
  * @author Paul Polishchuk (ppol@ua.fm)
 52  
  * @version $Id$
 53  
  * @see <a href="http://developer.github.com/v3/orgs/">Organizations API</a>
 54  
  * @since 0.7
 55  
  * @checkstyle MultipleStringLiteralsCheck (200 lines)
 56  
  * @checkstyle ClassDataAbstractionCoupling (200 lines)
 57  
  */
 58  
 @Immutable
 59  
 @Loggable(Loggable.DEBUG)
 60  0
 @ToString
 61  0
 @EqualsAndHashCode(of = { "storage", "self" })
 62  
 @SuppressWarnings("PMD.TooManyMethods")
 63  21
 final class MkOrganizations implements Organizations {
 64  
 
 65  
     /**
 66  
      * Random generator.
 67  
      */
 68  1
     private static final Random RAND = new SecureRandom();
 69  
 
 70  
     /**
 71  
      * Storage.
 72  
      */
 73  
     private final transient MkStorage storage;
 74  
 
 75  
     /**
 76  
      * Login of the user logged in.
 77  
      */
 78  
     private final transient String self;
 79  
 
 80  
     /**
 81  
      * Public ctor.
 82  
      * @param stg Storage
 83  
      * @param login User to login
 84  
      * @throws IOException If there is any I/O problem
 85  
      */
 86  
     MkOrganizations(
 87  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 88  
         @NotNull(message = "login can't be NULL") final String login
 89  
     )
 90  5
         throws IOException {
 91  5
         this.storage = stg;
 92  5
         this.self = login;
 93  5
         this.storage.apply(
 94  
             new Directives().xpath("/github").addIf("orgs")
 95  
         );
 96  5
     }
 97  
 
 98  
     @Override
 99  
     @NotNull(message = "Github is never NULL")
 100  
     public Github github() {
 101  0
         return new MkGithub(this.storage, this.self);
 102  
     }
 103  
 
 104  
     @Override
 105  
     @NotNull(message = "User is never NULL")
 106  
     public User user() {
 107  
         try {
 108  0
             return new MkUser(this.storage, this.self);
 109  0
         } catch (final IOException ex) {
 110  0
             throw new IllegalStateException(ex);
 111  
         }
 112  
     }
 113  
 
 114  
     @Override
 115  
     @NotNull(message = "org is never NULL")
 116  
     public Organization get(
 117  
         @NotNull(message = "login is never NULl") final String login
 118  
     ) {
 119  
         try {
 120  12
             this.storage.apply(
 121  
                 new Directives().xpath(
 122  
                     String.format("/github/orgs[not(org[login='%s'])]", login)
 123  
                 ).add("org").add("login").set(login)
 124  
             );
 125  0
         } catch (final IOException ex) {
 126  0
             throw new IllegalStateException(ex);
 127  6
         }
 128  
         // @checkstyle AnonInnerLength (50 lines)
 129  6
         return new Organization() {
 130  
             @Override
 131  
             public Github github() {
 132  0
                 return new MkGithub(MkOrganizations.this.storage, login);
 133  
             }
 134  
             @Override
 135  
             public String login() {
 136  1
                 return login;
 137  
             }
 138  
             @Override
 139  
             public JsonObject json() {
 140  3
                 return Json.createObjectBuilder()
 141  
                     .add("login", login)
 142  
                     .add("id", Integer.toString(RAND.nextInt()))
 143  
                     .add("name", "github")
 144  
                     .add("company", "GitHub")
 145  
                     .add("blog", "https://github.com/blog")
 146  
                     .add("location", "San Francisco")
 147  
                     .add("email", "octocat@github.com")
 148  
                     .add("public_repos", MkOrganizations.RAND.nextInt())
 149  
                     .add("public_gists", MkOrganizations.RAND.nextInt())
 150  
                     .add("total_private_repos", MkOrganizations.RAND.nextInt())
 151  
                     .add("owned_private_repos", MkOrganizations.RAND.nextInt())
 152  
                     .add("followers", MkOrganizations.RAND.nextInt())
 153  
                     .add("following", MkOrganizations.RAND.nextInt())
 154  
                     .add("url", "https://github.com/orgs/cat")
 155  
                     .add("repos_url", "https://github.com/orgs/cat/repos")
 156  
                     .add("events_url", "https://github.com/orgs/cat/events")
 157  
                     .add("html_url", "https://github.com/cat")
 158  
                     .add("created_at", new Github.Time().toString())
 159  
                     .add("type", "Organization")
 160  
                     .build();
 161  
             }
 162  
             @Override
 163  
             public boolean equals(final Object obj) {
 164  0
                 return obj instanceof Organization
 165  
                     && login.equals(Organization.class.cast(obj).login());
 166  
             }
 167  
             @Override
 168  
             public int hashCode() {
 169  0
                 return login.hashCode();
 170  
             }
 171  
             @Override
 172  
             public int compareTo(final Organization obj) {
 173  0
                 return this.login().compareTo(obj.login());
 174  
             }
 175  
             @Override
 176  
             public void patch(final JsonObject json) throws IOException {
 177  0
                 new JsonPatch(MkOrganizations.this.storage)
 178  
                     .patch(MkOrganizations.this.xpath(), json);
 179  0
             }
 180  
         };
 181  
     }
 182  
 
 183  
     @Override
 184  
     @NotNull(message = "Iterable of orgs is never NULL")
 185  
     public Iterable<Organization> iterate() {
 186  2
         return new MkIterable<Organization>(
 187  
             this.storage,
 188  
             String.format("%s/org", this.xpath()),
 189  1
             new MkIterable.Mapping<Organization>() {
 190  
                 @Override
 191  
                 public Organization map(final XML xml) {
 192  0
                     return MkOrganizations.this.get(
 193  
                         xml.xpath("login/text()").get(0)
 194  
                     );
 195  
                 }
 196  
             }
 197  
         );
 198  
     }
 199  
 
 200  
     @Override
 201  
     @NotNull(message = "Iterable of orgs is never NULL")
 202  
     public Iterable<Organization> iterate(
 203  
         @NotNull(message = "login is never NULL") final String login
 204  
     ) {
 205  2
         return new MkIterable<Organization>(
 206  
             this.storage,
 207  
             String.format("%s/org/login", this.xpath()),
 208  1
             new MkIterable.Mapping<Organization>() {
 209  
                 @Override
 210  
                 public Organization map(final XML xml) {
 211  0
                     return MkOrganizations.this.get(
 212  
                         xml.xpath("login/text()").get(0)
 213  
                     );
 214  
                 }
 215  
             }
 216  
         );
 217  
     }
 218  
 
 219  
     /**
 220  
      * XPath of this element in XML tree.
 221  
      * @return XPath
 222  
      */
 223  
     @NotNull(message = "Xpath is never NULL")
 224  
     private String xpath() {
 225  2
         return "/github/orgs";
 226  
     }
 227  
 }