Coverage Report - com.jcabi.github.mock.MkCollaborators
 
Classes in this File Line Coverage Branch Coverage Complexity
MkCollaborators
90%
18/20
100%
2/2
1.333
MkCollaborators$1
50%
2/4
N/A
1.333
 
 1  2
 /**
 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.github.Collaborators;
 34  
 import com.jcabi.github.Coordinates;
 35  
 import com.jcabi.github.Repo;
 36  
 import com.jcabi.github.User;
 37  
 import com.jcabi.xml.XML;
 38  
 import java.io.IOException;
 39  
 import javax.validation.constraints.NotNull;
 40  
 import org.xembly.Directives;
 41  
 
 42  
 /**
 43  
  * Mock Github repository collaborators.
 44  
  *
 45  
  * @author Andrej Istomin (andrej.istomin.ikeen@gmail.com)
 46  
  * @version $Id$
 47  
  */
 48  
 @Immutable
 49  2
 final class MkCollaborators implements Collaborators {
 50  
 
 51  
     /**
 52  
      * Storage.
 53  
      */
 54  
     private final transient MkStorage storage;
 55  
 
 56  
     /**
 57  
      * Login of the user logged in.
 58  
      */
 59  
     private final transient String self;
 60  
 
 61  
     /**
 62  
      * Repo name.
 63  
      */
 64  
     private final transient Coordinates coords;
 65  
 
 66  
     /**
 67  
      * Public ctor.
 68  
      * @param stg Storage
 69  
      * @param login User to login
 70  
      * @param crds Coordinates
 71  
      * @throws IOException If there is any I/O problem
 72  
      */
 73  
     public MkCollaborators(
 74  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 75  
         @NotNull(message = "login can't be NULL") final String login,
 76  
         @NotNull(message = "crds can't be NULL") final Coordinates crds
 77  3
     ) throws IOException {
 78  3
         this.storage = stg;
 79  3
         this.self = login;
 80  3
         this.coords = crds;
 81  3
         this.storage.apply(
 82  
             new Directives().xpath(
 83  
                 String.format(
 84  
                     "/github/repos/repo[@coords='%s']", this.coords
 85  
                 )
 86  
             ).addIf("collaborators")
 87  
         );
 88  3
     }
 89  
 
 90  
     @Override
 91  
     @NotNull(message = "Repository is never NULL")
 92  
     public Repo repo() {
 93  0
         return new MkRepo(this.storage, this.self, this.coords);
 94  
     }
 95  
 
 96  
     @Override
 97  
     public boolean isCollaborator(
 98  
         @NotNull(message = "User is never null") final String user
 99  
     ) throws IOException {
 100  2
         return !this.storage.xml().xpath(
 101  
             String.format("%s/user[login='%s']/text()", this.xpath(), user)
 102  
         ).isEmpty();
 103  
     }
 104  
 
 105  
     @Override
 106  
     public void add(
 107  
         @NotNull(message = "User is never null") final String user
 108  
     ) throws IOException {
 109  3
         this.storage.lock();
 110  
         try {
 111  3
             this.storage.apply(
 112  
                 new Directives().xpath(this.xpath()).add("user").add("login")
 113  
                     .set(user)
 114  
             );
 115  
         } finally {
 116  3
             this.storage.unlock();
 117  3
         }
 118  3
     }
 119  
 
 120  
     @Override
 121  
     public void remove(
 122  
         @NotNull(message = "user should not be NULL") final String user
 123  
     ) throws IOException {
 124  1
         this.storage.apply(
 125  
             new Directives().xpath(
 126  
                 String.format("%s/user[login='%s']", this.xpath(), user)
 127  
             ).remove()
 128  
         );
 129  1
     }
 130  
 
 131  
     @Override
 132  
     @NotNull(message = "Iterable of users is never NULL")
 133  
     public Iterable<User> iterate() {
 134  3
         return new MkIterable<User>(
 135  
             this.storage, String.format("%s/user", this.xpath()),
 136  5
             new MkIterable.Mapping<User>() {
 137  
                 @Override
 138  
                 public User map(final XML xml) {
 139  
                     try {
 140  2
                         return new MkUser(
 141  
                             MkCollaborators.this.storage,
 142  
                             xml.xpath("login/text()").get(0)
 143  
                         );
 144  0
                     } catch (final IOException ex) {
 145  0
                         throw new IllegalStateException(ex);
 146  
                     }
 147  
                 }
 148  
             }
 149  
         );
 150  
     }
 151  
 
 152  
     /**
 153  
      * Gets a mocked User.
 154  
      * @param login User login
 155  
      * @return Mocked User
 156  
      * @throws IOException If there is any I/O problem
 157  
      */
 158  
     @NotNull(message = "User is never NULL")
 159  
     public User get(
 160  
         @NotNull(message = "login should not be NULL") final String login
 161  
     ) throws IOException {
 162  0
         return new MkUser(this.storage, login);
 163  
     }
 164  
 
 165  
     /**
 166  
      * XPath of this element in XML tree.
 167  
      * @return XPath
 168  
      */
 169  
     @NotNull(message = "Xpath is never NULL")
 170  
     private String xpath() {
 171  9
         return String.format(
 172  
             "/github/repos/repo[@coords='%s']/collaborators",
 173  
             this.coords
 174  
         );
 175  
     }
 176  
 }