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