Coverage Report - com.jcabi.github.RtHooks
 
Classes in this File Line Coverage Branch Coverage Complexity
RtHooks
89%
17/19
8%
2/24
1.143
RtHooks$1
100%
2/2
N/A
1.143
RtHooks$AjcClosure1
0%
0/1
N/A
1.143
RtHooks$AjcClosure3
100%
1/1
N/A
1.143
RtHooks$AjcClosure5
100%
1/1
N/A
1.143
RtHooks$AjcClosure7
100%
1/1
N/A
1.143
RtHooks$AjcClosure9
100%
1/1
N/A
1.143
 
 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 java.util.Map;
 40  
 import javax.json.Json;
 41  
 import javax.json.JsonObject;
 42  
 import javax.json.JsonObjectBuilder;
 43  
 import javax.json.JsonStructure;
 44  
 import javax.validation.constraints.NotNull;
 45  
 import lombok.EqualsAndHashCode;
 46  
 
 47  
 /**
 48  
  * Github hooks.
 49  
  * @author Paul Polishchuk (ppol@ua.fm)
 50  
  * @version $Id$
 51  
  * @since 0.8
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @EqualsAndHashCode(of = { "entry", "owner", "request" })
 56  
 final class RtHooks implements Hooks {
 57  
     /**
 58  
      * API entry point.
 59  
      */
 60  
     private final transient Request entry;
 61  
 
 62  
     /**
 63  
      * RESTful request.
 64  
      */
 65  
     private final transient Request request;
 66  
 
 67  
     /**
 68  
      * Repository.
 69  
      */
 70  
     private final transient Repo owner;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param req Request
 75  
      * @param repo Repository
 76  
      */
 77  6
     public RtHooks(final Request req, final Repo repo) {
 78  6
         this.entry = req;
 79  6
         final Coordinates coords = repo.coordinates();
 80  6
         this.request = this.entry.uri()
 81  
             .path("/repos")
 82  
             .path(coords.user())
 83  
             .path(coords.repo())
 84  
             .path("/hooks")
 85  
             .back();
 86  6
         this.owner = repo;
 87  6
     }
 88  
 
 89  
     @Override
 90  
     @NotNull(message = "repository is never NULL")
 91  
     public Repo repo() {
 92  0
         return this.owner;
 93  
     }
 94  
 
 95  
     @Override
 96  
     @NotNull(message = "iterable of hooks is never NULL")
 97  
     public Iterable<Hook> iterate() {
 98  4
         return new RtPagination<Hook>(
 99  
             this.request,
 100  4
             new RtValuePagination.Mapping<Hook, JsonObject>() {
 101  
                 @Override
 102  
                 public Hook map(final JsonObject object) {
 103  
                     // @checkstyle MultipleStringLiterals (1 line)
 104  2
                     return RtHooks.this.get(object.getInt("id"));
 105  
                 }
 106  
             }
 107  
         );
 108  
     }
 109  
 
 110  
     @Override
 111  
     public void remove(final int number) throws IOException {
 112  2
         this.request.method(Request.DELETE)
 113  
             .uri().path(Integer.toString(number)).back()
 114  
             .fetch()
 115  
             .as(RestResponse.class)
 116  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 117  1
     }
 118  
 
 119  
     @Override
 120  
     @NotNull(message = "Hook is never NULL")
 121  
     public Hook get(final int number) {
 122  8
         return new RtHook(this.entry, this.owner, number);
 123  
     }
 124  
 
 125  
     @Override
 126  
     @NotNull(message = "Hook is never NULL")
 127  
     public Hook create(
 128  
         @NotNull(message = "name can't be NULL") final String name,
 129  
         @NotNull(message = "config can't be NULL")
 130  
         final Map<String, String> config) throws IOException {
 131  2
         final JsonObjectBuilder builder = Json.createObjectBuilder();
 132  1
         for (final Map.Entry<String, String> entr : config.entrySet()) {
 133  2
             builder.add(entr.getKey(), entr.getValue());
 134  2
         }
 135  1
         final JsonStructure json = Json.createObjectBuilder()
 136  
             .add("name", name)
 137  
             .add("config", builder)
 138  
             .build();
 139  1
         return this.get(
 140  
             this.request.method(Request.POST)
 141  
                 .body().set(json).back()
 142  
                 .fetch().as(RestResponse.class)
 143  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 144  
                 .as(JsonResponse.class)
 145  
                 .json().readObject().getInt("id")
 146  
         );
 147  
     }
 148  
 }