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