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