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