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