Coverage Report - com.jcabi.github.mock.MkOrganization
 
Classes in this File Line Coverage Branch Coverage Complexity
MkOrganization
82%
39/47
5%
1/20
1.222
MkOrganization$AjcClosure1
100%
1/1
N/A
1.222
MkOrganization$AjcClosure11
100%
1/1
N/A
1.222
MkOrganization$AjcClosure13
100%
1/1
N/A
1.222
MkOrganization$AjcClosure3
100%
1/1
N/A
1.222
MkOrganization$AjcClosure5
100%
1/1
N/A
1.222
MkOrganization$AjcClosure7
0%
0/1
N/A
1.222
MkOrganization$AjcClosure9
0%
0/1
N/A
1.222
 
 1  6
 /**
 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.Organization;
 36  
 import com.jcabi.github.PublicMembers;
 37  
 import com.jcabi.github.User;
 38  
 import java.io.IOException;
 39  
 import java.security.SecureRandom;
 40  
 import java.util.Random;
 41  
 import javax.json.Json;
 42  
 import javax.json.JsonObject;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Mock GitHub organization.
 49  
  *
 50  
  * @author Paul Polishchuk (ppol@ua.fm)
 51  
  * @version $Id: f7a85adbf5298f6b3f8c907d34e3ebbaffae12d9 $
 52  
  * @see <a href="http://developer.github.com/v3/orgs/">Organizations API</a>
 53  
  * @since 0.24
 54  
  */
 55  0
 @Immutable
 56  
 @Loggable(Loggable.DEBUG)
 57  0
 @ToString
 58  1
 @EqualsAndHashCode(of = { "storage", "self" })
 59  
 public final class MkOrganization implements Organization {
 60  
     /**
 61  
      * Random generator.
 62  
      */
 63  1
     private static final Random RAND = new SecureRandom();
 64  
 
 65  
     /**
 66  
      * Login key in key-value pair.
 67  
      */
 68  
     private static final String LOGIN_KEY = "login";
 69  
 
 70  
     /**
 71  
      * Storage.
 72  
      */
 73  
     private final transient MkStorage storage;
 74  
 
 75  
     /**
 76  
      * Username of the organization.
 77  
      */
 78  
     private final transient String self;
 79  
 
 80  
     /**
 81  
      * Public ctor.
 82  
      * @param stg Storage
 83  
      * @param login Username of organization
 84  
      */
 85  
     public MkOrganization(
 86  
         final MkStorage stg,
 87  
         final String login
 88  19
     ) {
 89  19
         this.storage = stg;
 90  19
         this.self = login;
 91  19
     }
 92  
 
 93  
     @Override
 94  
     public Github github() {
 95  8
         return new MkGithub(this.storage, this.self);
 96  
     }
 97  
 
 98  
     @Override
 99  
     public String login() {
 100  56
         return this.self;
 101  
     }
 102  
 
 103  
     @Override
 104  
     public JsonObject json() {
 105  9
         return Json.createObjectBuilder()
 106  3
             .add(LOGIN_KEY, this.self)
 107  3
             .add("id", Integer.toString(RAND.nextInt()))
 108  3
             .add("name", "github")
 109  3
             .add("company", "GitHub")
 110  3
             .add("blog", "https://github.com/blog")
 111  3
             .add("location", "San Francisco")
 112  3
             .add("email", "octocat@github.com")
 113  3
             .add("public_repos", RAND.nextInt())
 114  3
             .add("public_gists", RAND.nextInt())
 115  3
             .add("total_private_repos", RAND.nextInt())
 116  3
             .add("owned_private_repos", RAND.nextInt())
 117  3
             .add("followers", RAND.nextInt())
 118  3
             .add("following", RAND.nextInt())
 119  3
             .add("url", "https://github.com/orgs/cat")
 120  3
             .add("repos_url", "https://github.com/orgs/cat/repos")
 121  3
             .add("events_url", "https://github.com/orgs/cat/events")
 122  3
             .add("html_url", "https://github.com/cat")
 123  3
             .add("created_at", new Github.Time().toString())
 124  3
             .add("type", "Organization")
 125  3
             .build();
 126  
     }
 127  
 
 128  
     @Override
 129  
     public int compareTo(final Organization obj) {
 130  0
         return this.login().compareTo(obj.login());
 131  
     }
 132  
 
 133  
     @Override
 134  
     public void patch(final JsonObject json) throws IOException {
 135  0
         new JsonPatch(this.storage)
 136  0
             .patch(this.xpath(), json);
 137  0
     }
 138  
 
 139  
     @Override
 140  
     public PublicMembers publicMembers() {
 141  8
         return new MkPublicMembers(this.storage, this);
 142  
     }
 143  
 
 144  
     /**
 145  
      * Add the given user to this organization.
 146  
      * @param user User to add to the organization
 147  
      * @todo #1107:30min Implement the "Add team membership" API (see
 148  
      *  https://developer.github.com/v3/orgs/teams/#add-team-membership )
 149  
      *  (per https://developer.github.com/v3/orgs/members/#add-a-member ,
 150  
      *  you can't add a user directly to an org; you instead add them to one
 151  
      *  of that org's teams) and replace uses of this method with uses of that
 152  
      *  API (or downgrade this method to a convenience method for unit tests).
 153  
      */
 154  
     public void addMember(final User user) {
 155  
         try {
 156  9
             this.storage.apply(
 157  
                 new Directives()
 158  3
                     .xpath(String.format("%s/members", this.xpath()))
 159  3
                     .add("member")
 160  3
                     .add(LOGIN_KEY).set(user.login()).up()
 161  3
                     .add("public").set("false").up()
 162  
             );
 163  0
         } catch (final IOException ex) {
 164  0
             throw new IllegalStateException(ex);
 165  3
         }
 166  3
     }
 167  
 
 168  
     /**
 169  
      * XPath of this element in XML tree.
 170  
      * @return XPath
 171  
      */
 172  
     private String xpath() {
 173  3
         return String.format("/github/orgs/org[login='%s']", this.self);
 174  
     }
 175  
 }