Coverage Report - com.jcabi.github.RtCollaborators
 
Classes in this File Line Coverage Branch Coverage Complexity
RtCollaborators
87%
14/16
4%
1/24
1
RtCollaborators$1
100%
2/2
N/A
1
RtCollaborators$AjcClosure1
0%
0/1
N/A
1
RtCollaborators$AjcClosure3
100%
1/1
N/A
1
RtCollaborators$AjcClosure5
100%
1/1
N/A
1
RtCollaborators$AjcClosure7
100%
1/1
N/A
1
RtCollaborators$AjcClosure9
100%
1/1
N/A
1
 
 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  
 
 31  
 package com.jcabi.github;
 32  
 
 33  
 import com.jcabi.aspects.Immutable;
 34  
 import com.jcabi.aspects.Loggable;
 35  
 import com.jcabi.http.Request;
 36  
 import com.jcabi.http.response.RestResponse;
 37  
 import java.io.IOException;
 38  
 import java.net.HttpURLConnection;
 39  
 import javax.json.JsonObject;
 40  
 import javax.validation.constraints.NotNull;
 41  
 import lombok.EqualsAndHashCode;
 42  
 import org.hamcrest.Matchers;
 43  
 
 44  
 /**
 45  
  * Implementation of Collaborators.
 46  
  * @author Aleksey Popov (alopen@yandex.ru)
 47  
  * @version $Id$
 48  
  * @since 0.8
 49  
  */
 50  
 @Immutable
 51  
 @Loggable(Loggable.DEBUG)
 52  0
 @EqualsAndHashCode(of = { "entry", "request", "owner" })
 53  
 @SuppressWarnings("PMD.SingularField")
 54  2
 final class RtCollaborators implements Collaborators {
 55  
 
 56  
     /**
 57  
      * API entry point.
 58  
      */
 59  
     private final transient Request entry;
 60  
 
 61  
     /**
 62  
      * RESTful request.
 63  
      */
 64  
     private final transient Request request;
 65  
 
 66  
     /**
 67  
      * Repository we're in.
 68  
      */
 69  
     private final transient Repo owner;
 70  
 
 71  
     /**
 72  
      * Public ctor.
 73  
      * @param repo Repo
 74  
      * @param req Request
 75  
      */
 76  4
     RtCollaborators(final Request req, final Repo repo) {
 77  4
         this.entry = req;
 78  4
         final Coordinates coords = repo.coordinates();
 79  4
         this.request = this.entry.uri()
 80  
             .path("/repos")
 81  
             .path(coords.user())
 82  
             .path(coords.repo())
 83  
             .path("/collaborators")
 84  
             .back();
 85  4
         this.owner = repo;
 86  4
     }
 87  
 
 88  
     @Override
 89  
     @NotNull(message = "repository is never NULL")
 90  
     public Repo repo() {
 91  0
         return this.owner;
 92  
     }
 93  
 
 94  
     @Override
 95  
     public boolean isCollaborator(
 96  
         @NotNull(message = "User is never null") final String user)
 97  
         throws IOException {
 98  2
         return this.request
 99  
             .method(Request.GET)
 100  
             .uri().path(user).back()
 101  
             .fetch()
 102  
             .as(RestResponse.class)
 103  
             .assertStatus(
 104  
                 Matchers.isOneOf(
 105  
                     HttpURLConnection.HTTP_NO_CONTENT,
 106  
                     HttpURLConnection.HTTP_NOT_FOUND
 107  
                 )
 108  
             ).status() == HttpURLConnection.HTTP_NO_CONTENT;
 109  
     }
 110  
 
 111  
     @Override
 112  
     public void add(
 113  
         @NotNull(message = "User is never null") final String user)
 114  
         throws IOException {
 115  2
         this.request.method(Request.PUT)
 116  
             .uri().path(user).back()
 117  
             .fetch()
 118  
             .as(RestResponse.class)
 119  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 120  1
     }
 121  
 
 122  
     @Override
 123  
     public void remove(
 124  
         @NotNull(message = "user is never NULL") final String user
 125  
     )
 126  
         throws IOException {
 127  2
         this.request.method(Request.DELETE)
 128  
             .uri().path(user).back()
 129  
             .fetch()
 130  
             .as(RestResponse.class)
 131  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 132  1
     }
 133  
 
 134  
     @Override
 135  
     @NotNull(message = "iterable of users is never NULL")
 136  
     public Iterable<User> iterate() {
 137  2
         return new RtPagination<User>(
 138  
             this.request,
 139  3
             new RtValuePagination.Mapping<User, JsonObject>() {
 140  
                 @Override
 141  
                 public User map(final JsonObject object) {
 142  2
                     return RtCollaborators.this.owner.github().users()
 143  
                         .get(object.getString("login"));
 144  
                 }
 145  
             }
 146  
         );
 147  
     }
 148  
 }