Coverage Report - com.jcabi.github.mock.MkPublicMembers
 
Classes in this File Line Coverage Branch Coverage Complexity
MkPublicMembers
96%
27/28
75%
3/4
1.556
MkPublicMembers$1
66%
4/6
N/A
1.556
 
 1  
 /**
 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.github.Organization;
 33  
 import com.jcabi.github.PublicMembers;
 34  
 import com.jcabi.github.User;
 35  
 import com.jcabi.xml.XML;
 36  
 import java.io.IOException;
 37  
 import org.xembly.Directives;
 38  
 
 39  
 /**
 40  
  * Mock for public members of a GitHub organization.
 41  
  *
 42  
  * @author Chris Rebert (github@chrisrebert.com)
 43  
  * @version $Id: a72c4b22caa67378dd31a93491e09141aaabb7dc $
 44  
  * @see <a href="https://developer.github.com/v3/orgs/members/">Organization Members API</a>
 45  
  * @since 0.24
 46  
  */
 47  6
 public final class MkPublicMembers implements PublicMembers {
 48  
     /**
 49  
      * Storage.
 50  
      */
 51  
     private final transient MkStorage storage;
 52  
 
 53  
     /**
 54  
      * Organization.
 55  
      */
 56  
     private final transient Organization organization;
 57  
 
 58  
     /**
 59  
      * Public ctor.
 60  
      * @param stg Storage
 61  
      * @param organ Organization
 62  
      */
 63  
     public MkPublicMembers(
 64  
         final MkStorage stg,
 65  
         final Organization organ
 66  4
     ) {
 67  4
         this.storage = stg;
 68  4
         this.organization = organ;
 69  4
     }
 70  
 
 71  
     @Override
 72  
     public Organization org() {
 73  1
         return this.organization;
 74  
     }
 75  
 
 76  
     @Override
 77  
     public void conceal(
 78  
         final User user
 79  
     ) throws IOException {
 80  6
         this.storage.apply(
 81  
             new Directives()
 82  3
                 .xpath(this.xpath(user))
 83  3
                 .set("false")
 84  
         );
 85  3
     }
 86  
 
 87  
     @Override
 88  
     public void publicize(
 89  
         final User user
 90  
     ) throws IOException {
 91  8
         this.storage.apply(
 92  
             new Directives()
 93  4
                 .xpath(this.xpath(user))
 94  4
                 .set("true")
 95  
         );
 96  4
     }
 97  
 
 98  
     @Override
 99  
     public Iterable<User> iterate() {
 100  28
         return new MkIterable<User>(
 101  
             this.storage,
 102  14
             String.format("%s/member[public='true']/login", this.xpath()),
 103  20
             new MkIterable.Mapping<User>() {
 104  
                 @Override
 105  
                 public User map(final XML xml) {
 106  
                     try {
 107  12
                         return new MkUser(
 108  6
                             MkPublicMembers.this.storage,
 109  6
                             xml.xpath("text()").get(0)
 110  
                         );
 111  0
                     } catch (final IOException exc) {
 112  0
                         throw new IllegalStateException(exc);
 113  
                     }
 114  
                 }
 115  
             }
 116  
         );
 117  
     }
 118  
 
 119  
     @Override
 120  
     public boolean contains(
 121  
         final User user
 122  
     ) throws IOException {
 123  7
         boolean result = false;
 124  7
         for (final User member : this.iterate()) {
 125  3
             if (member.equals(user)) {
 126  3
                 result = true;
 127  3
                 break;
 128  
             }
 129  0
         }
 130  7
         return result;
 131  
     }
 132  
 
 133  
     /**
 134  
      * XPath of publicity of user's membership in the organization.
 135  
      * @param user User
 136  
      * @return XPath
 137  
      * @throws IOException If there is an I/O problem
 138  
      */
 139  
     private String xpath(final User user) throws IOException {
 140  14
         return String.format(
 141  
             "%s/member[login='%s']/public",
 142  7
             this.xpath(),
 143  7
             user.login()
 144  
         );
 145  
     }
 146  
 
 147  
     /**
 148  
      * XPath of this element in XML tree.
 149  
      * @return XPath
 150  
      */
 151  
     private String xpath() {
 152  42
         return String.format(
 153  
             "/github/orgs/org[login='%s']/members",
 154  21
             this.organization.login()
 155  
         );
 156  
     }
 157  
 }