Coverage Report - com.jcabi.github.RtDeployKeys
 
Classes in this File Line Coverage Branch Coverage Complexity
RtDeployKeys
81%
9/11
0%
0/22
1
RtDeployKeys$1
100%
2/2
N/A
1
RtDeployKeys$AjcClosure1
0%
0/1
N/A
1
RtDeployKeys$AjcClosure3
100%
1/1
N/A
1
RtDeployKeys$AjcClosure5
100%
1/1
N/A
1
RtDeployKeys$AjcClosure7
100%
1/1
N/A
1
 
 1  8
 /**
 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.validation.constraints.NotNull;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Github deploy keys.
 46  
  *
 47  
  * @author Andres Candal (andres.candal@rollasolution.com)
 48  
  * @version $Id$
 49  
  * @since 0.8
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "request", "owner", "entry" })
 54  
 final class RtDeployKeys implements DeployKeys {
 55  
     /**
 56  
      * Repository.
 57  
      */
 58  
     private final transient Repo owner;
 59  
 
 60  
     /**
 61  
      * RESTful API entry point.
 62  
      */
 63  
     private final transient Request entry;
 64  
 
 65  
     /**
 66  
      * RESTful API request for these deploy keys.
 67  
      */
 68  
     private final transient Request request;
 69  
 
 70  
     /**
 71  
      * Public ctor.
 72  
      * @param req RESTful API entry point
 73  
      * @param repo Repository
 74  
      */
 75  5
     RtDeployKeys(final Request req, final Repo repo) {
 76  5
         this.owner = repo;
 77  5
         this.entry = req;
 78  5
         this.request = req.uri()
 79  
             .path("/repos")
 80  
             .path(repo.coordinates().user())
 81  
             .path(repo.coordinates().repo())
 82  
             .path("/keys")
 83  
             .back();
 84  5
     }
 85  
 
 86  
     @Override
 87  
     @NotNull(message = "repository is never NULL")
 88  
     public Repo repo() {
 89  0
         return this.owner;
 90  
     }
 91  
 
 92  
     @Override
 93  
     @NotNull(message = "Iterable of DeployKey can't be NULL")
 94  
     public Iterable<DeployKey> iterate() {
 95  4
         return new RtPagination<DeployKey>(
 96  
             this.request,
 97  4
             new RtValuePagination.Mapping<DeployKey, JsonObject>() {
 98  
                 @Override
 99  
                 public DeployKey map(final JsonObject object) {
 100  
                     //@checkstyle MultipleStringLiteralsCheck (1 line)
 101  2
                     return RtDeployKeys.this.get(object.getInt("id"));
 102  
                 }
 103  
             }
 104  
         );
 105  
     }
 106  
 
 107  
     @Override
 108  
     @NotNull(message = "DeployKey can't be NULL")
 109  
     public DeployKey get(final int number) {
 110  8
         return new RtDeployKey(this.entry, number, this.owner);
 111  
     }
 112  
 
 113  
     @Override
 114  
     @NotNull(message = "DeployKey is never NULL")
 115  
     public DeployKey create(
 116  
         @NotNull(message = "title can't be NULL") final String title,
 117  
         @NotNull(message = "key can't be NULL") final String key
 118  
     )
 119  
         throws IOException {
 120  2
         return this.get(
 121  
             this.request.method(Request.POST)
 122  
                 .body().set(
 123  
                     Json.createObjectBuilder()
 124  
                         .add("title", title)
 125  
                         .add("key", key)
 126  
                         .build()
 127  
                 ).back()
 128  
                 .fetch().as(RestResponse.class)
 129  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 130  
                 .as(JsonResponse.class)
 131  
                 .json().readObject().getInt("id")
 132  
         );
 133  
     }
 134  
 }