Coverage Report - com.jcabi.github.RtStatuses
 
Classes in this File Line Coverage Branch Coverage Complexity
RtStatuses
69%
9/13
N/A
1.167
 
 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  
 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.Json;
 38  
 import javax.json.JsonObject;
 39  
 import javax.json.JsonStructure;
 40  
 import javax.validation.constraints.NotNull;
 41  
 
 42  
 /**
 43  
  * Github statuses for a given commit.
 44  
  * @author Marcin Cylke (maracin.cylke+github@gmail.com)
 45  
  * @version $Id$
 46  
  * @since 0.23
 47  
  * @checkstyle MultipleStringLiteralsCheck (500 lines)
 48  
  */
 49  
 public class RtStatuses implements Statuses {
 50  
 
 51  
     /**
 52  
      * RESTful request.
 53  
      */
 54  
     private final transient Request request;
 55  
 
 56  
     /**
 57  
      * Commit cmmt.
 58  
      */
 59  
     private final transient Commit cmmt;
 60  
 
 61  
     /**
 62  
      * Create a new status-aware object based on given commit.
 63  
      * @param req Http request
 64  
      * @param commit Specific commit
 65  
      */
 66  1
     RtStatuses(final Request req, final Commit commit) {
 67  1
         final Coordinates coords = commit.repo().coordinates();
 68  1
         this.request = req.uri()
 69  
             .path("/repos")
 70  
             .path(coords.user())
 71  
             .path(coords.repo())
 72  
             .path("/statuses")
 73  
             .path(commit.sha())
 74  
             .back();
 75  1
         this.cmmt = commit;
 76  1
     }
 77  
 
 78  
     /**
 79  
      * Generate string representation.
 80  
      * @return String representation
 81  
      */
 82  
     @Override
 83  
     @NotNull(message = "toString is never NULL")
 84  
     public final String toString() {
 85  0
         return this.request.uri().get().toString();
 86  
     }
 87  
 
 88  
     /**
 89  
      * Get commit object.
 90  
      * @return Commit object
 91  
      */
 92  
     @Override
 93  
     @NotNull(message = "Commit can't be NULL")
 94  
     public final Commit commit() {
 95  0
         return this.cmmt;
 96  
     }
 97  
 
 98  
     /**
 99  
      * Create new status for a commit.
 100  
      * @param status Add this status
 101  
      * @return Returned status
 102  
      * @throws IOException In case of any I/O problems
 103  
      */
 104  
     @Override
 105  
     public final Status create(
 106  
         @NotNull(message = "status can't be NULL") final Status status
 107  
     ) throws IOException {
 108  1
         final JsonStructure json = Json.createObjectBuilder()
 109  
             .add("state", status.state().name())
 110  
             .add("target_url", status.targetUrl())
 111  
             .add("description", status.description())
 112  
             .add("context", status.context())
 113  
             .build();
 114  1
         final JsonObject response = this.request.method(Request.POST)
 115  
             .body().set(json).back()
 116  
             .fetch()
 117  
             .as(RestResponse.class)
 118  
             .assertStatus(HttpURLConnection.HTTP_CREATED)
 119  
             .as(JsonResponse.class)
 120  
             .json().readObject();
 121  1
         return new RtStatus(
 122  
             Status.State.forValue(response.getString("state")),
 123  
             response.getString("target_url"),
 124  
             response.getString("description"),
 125  
             response.getString("context")
 126  
         );
 127  
     }
 128  
 
 129  
     /**
 130  
      * Get all status messages for current commit.
 131  
      * @param ref It can be a SHA, a branch name, or a tag name.
 132  
      * @return Full list of statuses for this commit.
 133  
      */
 134  
     @Override
 135  
     @NotNull(message = "list of statuses can't be NULL")
 136  
     public final Iterable<Statuses> list(
 137  
         @NotNull(message = "ref can't be NULL") final String ref
 138  
     ) {
 139  0
         throw new UnsupportedOperationException("Not implemented");
 140  
     }
 141  
 
 142  
     /**
 143  
      * JSON object for this request.
 144  
      * @return Json object
 145  
      * @throws IOException In case of I/O problems
 146  
      */
 147  
     @Override
 148  
     @NotNull(message = "JSON can't be NULL")
 149  
     public final JsonObject json() throws IOException {
 150  0
         return new RtJson(this.request).fetch();
 151  
     }
 152  
 }