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