Coverage Report - com.jcabi.github.RtLabels
 
Classes in this File Line Coverage Branch Coverage Complexity
RtLabels
83%
15/18
0%
0/16
1
RtLabels$1
100%
2/2
N/A
1
RtLabels$AjcClosure1
0%
0/1
N/A
1
RtLabels$AjcClosure3
100%
1/1
N/A
1
RtLabels$AjcClosure5
100%
1/1
N/A
1
RtLabels$AjcClosure7
100%
1/1
N/A
1
RtLabels$AjcClosure9
100%
1/1
N/A
1
 
 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.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.JsonObject;
 41  
 import javax.json.JsonStructure;
 42  
 import javax.validation.constraints.NotNull;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github labels of a repo.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id$
 50  
  * @since 0.6
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = { "entry", "owner" })
 55  4
 final class RtLabels implements Labels {
 56  
 
 57  
     /**
 58  
      * RESTful entry.
 59  
      */
 60  
     private final transient Request entry;
 61  
 
 62  
     /**
 63  
      * RESTful request.
 64  
      */
 65  
     private final transient Request request;
 66  
 
 67  
     /**
 68  
      * Github.
 69  
      */
 70  
     private final transient Repo owner;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param req Request
 75  
      * @param repo Repo we're in
 76  
      */
 77  5
     RtLabels(final Request req, final Repo repo) {
 78  5
         this.owner = repo;
 79  5
         final Coordinates coords = repo.coordinates();
 80  5
         this.entry = req;
 81  5
         this.request = req.uri()
 82  
             .path("/repos")
 83  
             .path(coords.user())
 84  
             .path(coords.repo())
 85  
             .path("/labels")
 86  
             .back();
 87  5
     }
 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 = "label is never NULL")
 103  
     public Label create(
 104  
         @NotNull(message = "label name can't be NULL") final String name,
 105  
         @NotNull(message = "label color can't be NULL") final String color)
 106  
         throws IOException {
 107  2
         final JsonStructure json = Json.createObjectBuilder()
 108  
             // @checkstyle MultipleStringLiterals (1 line)
 109  
             .add("name", name)
 110  
             .add("color", color)
 111  
             .build();
 112  1
         this.request.method(Request.POST)
 113  
             .body().set(json).back()
 114  
             .fetch()
 115  
             .as(RestResponse.class)
 116  
             .assertStatus(HttpURLConnection.HTTP_CREATED)
 117  
             .as(JsonResponse.class)
 118  
             .json();
 119  1
         return new RtLabel(this.entry, this.owner, name);
 120  
     }
 121  
 
 122  
     @Override
 123  
     @NotNull(message = "Label is never NULL")
 124  
     public Label get(@NotNull(message = "label name can't be NULL")
 125  
         final String name) {
 126  2
         return new RtLabel(this.entry, this.owner, name);
 127  
     }
 128  
 
 129  
     @Override
 130  
     public void delete(@NotNull(message = "label name can't be NULL")
 131  
         final String name) throws IOException {
 132  2
         this.request.method(Request.DELETE)
 133  
             .uri().path(name).back()
 134  
             .fetch()
 135  
             .as(RestResponse.class)
 136  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 137  1
     }
 138  
 
 139  
     @Override
 140  
     @NotNull(message = "Iterable of labels is never NULL")
 141  
     public Iterable<Label> iterate() {
 142  2
         return new RtPagination<Label>(
 143  
             this.request,
 144  3
             new RtValuePagination.Mapping<Label, JsonObject>() {
 145  
                 @Override
 146  
                 public Label map(final JsonObject object) {
 147  2
                     return new RtLabel(
 148  
                         RtLabels.this.entry,
 149  
                         RtLabels.this.owner,
 150  
                         object.getString("name")
 151  
                     );
 152  
                 }
 153  
             }
 154  
         );
 155  
     }
 156  
 
 157  
 }