Coverage Report - com.jcabi.github.RtStars
 
Classes in this File Line Coverage Branch Coverage Complexity
RtStars
84%
11/13
11%
2/18
1
RtStars$AjcClosure1
0%
0/1
N/A
1
RtStars$AjcClosure3
100%
1/1
N/A
1
RtStars$AjcClosure5
100%
1/1
N/A
1
RtStars$AjcClosure7
100%
1/1
N/A
1
 
 1  0
 /**
 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;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.response.RestResponse;
 36  
 import java.io.IOException;
 37  
 import java.net.HttpURLConnection;
 38  
 import javax.validation.constraints.NotNull;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import org.hamcrest.Matchers;
 41  
 
 42  
 /**
 43  
  * Github starring API.
 44  
  *
 45  
  * @author Paul Polishchuk (ppol@ua.fm)
 46  
  * @version $Id$
 47  
  * @since 0.15
 48  
  * @see <a href="https://developer.github.com/v3/activity/starring/">Starring API</a>
 49  
  */
 50  
 @Immutable
 51  
 @Loggable(Loggable.DEBUG)
 52  0
 @EqualsAndHashCode(of = { "owner", "request" })
 53  
 final class RtStars implements Stars {
 54  
 
 55  
     /**
 56  
      * RESTful request.
 57  
      */
 58  
     private final transient Request request;
 59  
 
 60  
     /**
 61  
      * Repository.
 62  
      */
 63  
     private final transient Repo owner;
 64  
 
 65  
     /**
 66  
      * Public ctor.
 67  
      * @param req Request
 68  
      * @param repo Repository
 69  
      */
 70  5
     RtStars(final Request req, final Repo repo) {
 71  5
         final Coordinates coords = repo.coordinates();
 72  5
         this.request = req.uri()
 73  
             .path("/user/starred")
 74  
             .path(coords.user())
 75  
             .path(coords.repo())
 76  
             .back();
 77  5
         this.owner = repo;
 78  5
     }
 79  
 
 80  
     @Override
 81  
     @NotNull(message = "repository is never NULL")
 82  
     public Repo repo() {
 83  0
         return this.owner;
 84  
     }
 85  
 
 86  
     @Override
 87  
     public boolean starred() throws IOException {
 88  4
         return this.request
 89  
             .fetch().as(RestResponse.class)
 90  
             .assertStatus(
 91  
                 Matchers.isOneOf(
 92  
                     HttpURLConnection.HTTP_NO_CONTENT,
 93  
                     HttpURLConnection.HTTP_NOT_FOUND
 94  
             )
 95  
         ).status() == HttpURLConnection.HTTP_NO_CONTENT;
 96  
     }
 97  
 
 98  
     @Override
 99  
     public void star() throws IOException {
 100  2
         this.request
 101  
             .method(Request.PUT)
 102  
             .fetch().as(RestResponse.class)
 103  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 104  1
     }
 105  
 
 106  
     @Override
 107  
     public void unstar() throws IOException {
 108  2
         this.request
 109  
             .method(Request.DELETE)
 110  
             .fetch().as(RestResponse.class)
 111  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 112  1
     }
 113  
 }