Coverage Report - com.jcabi.github.RtIssueLabels
 
Classes in this File Line Coverage Branch Coverage Complexity
RtIssueLabels
23%
7/30
0%
0/14
1.222
RtIssueLabels$1
0%
0/2
N/A
1.222
RtIssueLabels$AjcClosure1
0%
0/1
N/A
1.222
RtIssueLabels$AjcClosure11
0%
0/1
N/A
1.222
RtIssueLabels$AjcClosure3
0%
0/1
N/A
1.222
RtIssueLabels$AjcClosure5
0%
0/1
N/A
1.222
RtIssueLabels$AjcClosure7
0%
0/1
N/A
1.222
RtIssueLabels$AjcClosure9
0%
0/1
N/A
1.222
 
 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 javax.json.Json;
 40  
 import javax.json.JsonArrayBuilder;
 41  
 import javax.json.JsonObject;
 42  
 import javax.json.JsonStructure;
 43  
 import javax.validation.constraints.NotNull;
 44  
 import lombok.EqualsAndHashCode;
 45  
 
 46  
 /**
 47  
  * Github get labels.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 50  
  * @version $Id$
 51  
  * @since 0.1
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @EqualsAndHashCode(of = "entry")
 56  0
 final class RtIssueLabels implements IssueLabels {
 57  
 
 58  
     /**
 59  
      * RESTful entry.
 60  
      */
 61  
     private final transient Request entry;
 62  
 
 63  
     /**
 64  
      * RESTful entry.
 65  
      */
 66  
     private final transient Request request;
 67  
 
 68  
     /**
 69  
      * Which issue we belong to.
 70  
      */
 71  
     private final transient Issue owner;
 72  
 
 73  
     /**
 74  
      * Public ctor.
 75  
      * @param req Request
 76  
      * @param issue Issue we're in
 77  
      */
 78  1
     RtIssueLabels(final Request req, final Issue issue) {
 79  1
         this.owner = issue;
 80  1
         final Coordinates coords = issue.repo().coordinates();
 81  1
         this.entry = req;
 82  1
         this.request = req.uri()
 83  
             .path("/repos")
 84  
             .path(coords.user())
 85  
             .path(coords.repo())
 86  
             .path("/issues")
 87  
             .path(Integer.toString(issue.number()))
 88  
             .path("/labels")
 89  
             .back();
 90  1
     }
 91  
 
 92  
     @Override
 93  
     @NotNull(message = "toString is never NULL")
 94  
     public String toString() {
 95  0
         return this.request.uri().get().toString();
 96  
     }
 97  
 
 98  
     @Override
 99  
     @NotNull(message = "issue is never NULL")
 100  
     public Issue issue() {
 101  0
         return this.owner;
 102  
     }
 103  
 
 104  
     @Override
 105  
     public void add(@NotNull(message = "iterable of labels can't be NULL")
 106  
         final Iterable<String> labels) throws IOException {
 107  0
         JsonArrayBuilder builder = Json.createArrayBuilder();
 108  0
         for (final String label : labels) {
 109  0
             builder = builder.add(label);
 110  0
         }
 111  0
         final JsonStructure json = builder.build();
 112  0
         this.request.method(Request.POST)
 113  
             .body().set(json).back()
 114  
             .fetch()
 115  
             .as(RestResponse.class)
 116  
             .assertStatus(HttpURLConnection.HTTP_OK)
 117  
             .as(JsonResponse.class)
 118  
             .json().readArray();
 119  0
     }
 120  
 
 121  
     @Override
 122  
     public void replace(@NotNull(message = "iterable of labels can't be NULL")
 123  
         final Iterable<String> labels) throws IOException {
 124  0
         JsonArrayBuilder builder = Json.createArrayBuilder();
 125  0
         for (final String label : labels) {
 126  0
             builder = builder.add(label);
 127  0
         }
 128  0
         final JsonStructure json = builder.build();
 129  0
         this.request.method(Request.PUT)
 130  
             .body().set(json).back()
 131  
             .fetch()
 132  
             .as(RestResponse.class)
 133  
             .assertStatus(HttpURLConnection.HTTP_OK)
 134  
             .as(JsonResponse.class)
 135  
             .json().readArray();
 136  0
     }
 137  
 
 138  
     @Override
 139  
     public void remove(@NotNull(message = "label name can't be NULL")
 140  
         final String name) throws IOException {
 141  0
         this.request.method(Request.DELETE)
 142  
             .uri().path(name).back()
 143  
             .fetch()
 144  
             .as(RestResponse.class)
 145  
             .assertStatus(HttpURLConnection.HTTP_OK);
 146  0
     }
 147  
 
 148  
     @Override
 149  
     public void clear() throws IOException {
 150  0
         this.request.method(Request.DELETE)
 151  
             .fetch()
 152  
             .as(RestResponse.class)
 153  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 154  0
     }
 155  
 
 156  
     @Override
 157  
     @NotNull(message = "Iterable of labels is never NULL")
 158  
     public Iterable<Label> iterate() {
 159  0
         return new RtPagination<Label>(
 160  
             this.request,
 161  0
             new RtValuePagination.Mapping<Label, JsonObject>() {
 162  
                 @Override
 163  
                 public Label map(final JsonObject object) {
 164  0
                     return new RtLabel(
 165  
                         RtIssueLabels.this.entry,
 166  
                         RtIssueLabels.this.owner.repo(),
 167  
                         object.getString("name")
 168  
                     );
 169  
                 }
 170  
             }
 171  
         );
 172  
     }
 173  
 
 174  
 }