Coverage Report - com.jcabi.github.RtMilestones
 
Classes in this File Line Coverage Branch Coverage Complexity
RtMilestones
56%
9/16
0%
0/22
1
RtMilestones$1
0%
0/2
N/A
1
RtMilestones$AjcClosure1
0%
0/1
N/A
1
RtMilestones$AjcClosure3
0%
0/1
N/A
1
RtMilestones$AjcClosure5
0%
0/1
N/A
1
RtMilestones$AjcClosure7
100%
1/1
N/A
1
RtMilestones$AjcClosure9
0%
0/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.JsonResponse;
 36  
 import com.jcabi.http.response.RestResponse;
 37  
 import java.io.IOException;
 38  
 import java.net.HttpURLConnection;
 39  
 import java.util.Map;
 40  
 import javax.json.Json;
 41  
 import javax.json.JsonObject;
 42  
 import javax.json.JsonStructure;
 43  
 import javax.validation.constraints.NotNull;
 44  
 import lombok.EqualsAndHashCode;
 45  
 
 46  
 /**
 47  
  * Github milestones.
 48  
  * @author Paul Polishchuk (ppol@ua.fm)
 49  
  * @version $Id$
 50  
  * @since 0.7
 51  
  * @checkstyle MultipleStringLiterals (500 lines)
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @EqualsAndHashCode(of = {"entry", "request", "owner" })
 56  
 final class RtMilestones implements Milestones {
 57  
     /**
 58  
      * API entry point.
 59  
      */
 60  
     private final transient Request entry;
 61  
 
 62  
     /**
 63  
      * RESTful request.
 64  
      */
 65  
     private final transient Request request;
 66  
 
 67  
     /**
 68  
      * Repository.
 69  
      */
 70  
     private final transient Repo owner;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param req Request
 75  
      * @param repo Repository
 76  
      */
 77  1
     RtMilestones(final Request req, final Repo repo) {
 78  1
         this.entry = req;
 79  1
         final Coordinates coords = repo.coordinates();
 80  1
         this.request = this.entry.uri()
 81  
             .path("/repos")
 82  
             .path(coords.user())
 83  
             .path(coords.repo())
 84  
             .path("/milestones")
 85  
             .back();
 86  1
         this.owner = repo;
 87  1
     }
 88  
 
 89  
     @Override
 90  
     @NotNull(message = "toString is never NULL")
 91  
     public String toString() {
 92  0
         return this.request.uri().get().toString();
 93  
     }
 94  
 
 95  
     @Override
 96  
     @NotNull(message = "repository is never NULL")
 97  
     public Repo repo() {
 98  0
         return this.owner;
 99  
     }
 100  
 
 101  
     @Override
 102  
     @NotNull(message = "milestone is never NULL")
 103  
     public Milestone create(
 104  
         @NotNull(message = "title can't be NULL") final String title)
 105  
         throws IOException {
 106  0
         final JsonStructure json = Json.createObjectBuilder()
 107  
             .add("title", title)
 108  
             .build();
 109  0
         return this.get(
 110  
             this.request.method(Request.POST)
 111  
                 .body().set(json).back()
 112  
                 .fetch().as(RestResponse.class)
 113  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 114  
                 .as(JsonResponse.class)
 115  
                 .json().readObject().getInt("number")
 116  
         );
 117  
     }
 118  
 
 119  
     @Override
 120  
     @NotNull(message = "Milestone is never NULL")
 121  
     public Milestone get(final int number) {
 122  0
         return new RtMilestone(this.entry, this.owner, number);
 123  
     }
 124  
 
 125  
     @Override
 126  
     public void remove(final int number) throws IOException {
 127  2
         this.request.method(Request.DELETE)
 128  
             .uri().path(Integer.toString(number)).back()
 129  
             .fetch()
 130  
             .as(RestResponse.class)
 131  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 132  1
     }
 133  
 
 134  
     @Override
 135  
     @NotNull(message = "Iterable of Milestones is never NULL")
 136  
     public Iterable<Milestone> iterate(
 137  
         @NotNull(message = "map or params can't be NULL")
 138  
         final Map<String, String> params) {
 139  0
         return new RtPagination<Milestone>(
 140  
             this.request.uri().queryParams(params).back(),
 141  0
             new RtValuePagination.Mapping<Milestone, JsonObject>() {
 142  
                 @Override
 143  
                 public Milestone map(final JsonObject object) {
 144  0
                     return RtMilestones.this.get(object.getInt("number"));
 145  
                 }
 146  
             }
 147  
         );
 148  
     }
 149  
 }