Coverage Report - com.jcabi.github.RtStatuses
 
Classes in this File Line Coverage Branch Coverage Complexity
RtStatuses
86%
20/23
N/A
1.167
 
 1  
 /**
 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;
 31  
 
 32  
 import com.jcabi.http.Request;
 33  
 import com.jcabi.http.response.JsonResponse;
 34  
 import com.jcabi.http.response.RestResponse;
 35  
 import java.io.IOException;
 36  
 import java.net.HttpURLConnection;
 37  
 import javax.json.JsonObject;
 38  
 
 39  
 /**
 40  
  * Github statuses for a given commit.
 41  
  * @author Marcin Cylke (maracin.cylke+github@gmail.com)
 42  
  * @version $Id: cb39eed922c4662c6a0f98df53ea7716693ae99b $
 43  
  * @since 0.23
 44  
  * @checkstyle MultipleStringLiteralsCheck (500 lines)
 45  
  */
 46  
 public class RtStatuses implements Statuses {
 47  
 
 48  
     /**
 49  
      * RESTful request.
 50  
      */
 51  
     private final transient Request request;
 52  
 
 53  
     /**
 54  
      * Commit cmmt.
 55  
      */
 56  
     private final transient Commit cmmt;
 57  
 
 58  
     /**
 59  
      * Create a new status-aware object based on given commit.
 60  
      * @param req Http request
 61  
      * @param commit Specific commit
 62  
      */
 63  2
     RtStatuses(final Request req, final Commit commit) {
 64  2
         final Coordinates coords = commit.repo().coordinates();
 65  2
         this.request = req.uri()
 66  2
             .path("/repos")
 67  2
             .path(coords.user())
 68  2
             .path(coords.repo())
 69  2
             .path("/statuses")
 70  2
             .path(commit.sha())
 71  2
             .back();
 72  2
         this.cmmt = commit;
 73  2
     }
 74  
 
 75  
     /**
 76  
      * Generate string representation.
 77  
      * @return String representation
 78  
      */
 79  
     @Override
 80  
     public final String toString() {
 81  0
         return this.request.uri().get().toString();
 82  
     }
 83  
 
 84  
     /**
 85  
      * Get commit object.
 86  
      * @return Commit object
 87  
      */
 88  
     @Override
 89  
     public final Commit commit() {
 90  1
         return this.cmmt;
 91  
     }
 92  
 
 93  
     /**
 94  
      * Create new status for a commit.
 95  
      * @param status Add this status
 96  
      * @return Returned status
 97  
      * @throws IOException In case of any I/O problems
 98  
      */
 99  
     @Override
 100  
     public final Status create(
 101  
         final StatusCreate status
 102  
     ) throws IOException {
 103  1
         final JsonObject response = this.request.method(Request.POST)
 104  1
             .body().set(status.json()).back()
 105  1
             .fetch()
 106  1
             .as(RestResponse.class)
 107  1
             .assertStatus(HttpURLConnection.HTTP_CREATED)
 108  1
             .as(JsonResponse.class)
 109  1
             .json().readObject();
 110  1
         return new RtStatus(this.cmmt, response);
 111  
     }
 112  
 
 113  
     /**
 114  
      * Get all status messages for a given commit.
 115  
      * @param ref It can be a SHA, a branch name, or a tag name.
 116  
      * @return Full list of statuses for this commit.
 117  
      * @todo #1126:30min Implement this method which gets all status messages for a given commit.
 118  
      */
 119  
     @Override
 120  
     public final Iterable<Status> list(
 121  
         final String ref
 122  
     ) {
 123  0
         throw new UnsupportedOperationException("Not implemented");
 124  
     }
 125  
 
 126  
     /**
 127  
      * JSON object for this request.
 128  
      * @return Json object
 129  
      * @throws IOException In case of I/O problems
 130  
      */
 131  
     @Override
 132  
     public final JsonObject json() throws IOException {
 133  0
         return new RtJson(this.request).fetch();
 134  
     }
 135  
 }