Coverage Report - com.jcabi.github.RtPublicKeys
 
Classes in this File Line Coverage Branch Coverage Complexity
RtPublicKeys
78%
11/14
0%
0/22
1
RtPublicKeys$1
100%
2/2
N/A
1
RtPublicKeys$AjcClosure1
0%
0/1
N/A
1
RtPublicKeys$AjcClosure3
100%
1/1
N/A
1
RtPublicKeys$AjcClosure5
100%
1/1
N/A
1
RtPublicKeys$AjcClosure7
100%
1/1
N/A
1
RtPublicKeys$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.validation.constraints.NotNull;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Github public keys.
 46  
  *
 47  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 48  
  * @version $Id$
 49  
  * @see <a href="http://developer.github.com/v3/users/keys/">Public Keys API</a>
 50  
  * @checkstyle MultipleStringLiteralsCheck (200 lines)
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = { "entry", "request", "owner" })
 55  
 final class RtPublicKeys implements PublicKeys {
 56  
 
 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  
      * User we're in.
 69  
      */
 70  
     private final transient User owner;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      *
 75  
      * @param req Request
 76  
      * @param user User
 77  
      */
 78  4
     public RtPublicKeys(final Request req, final User user) {
 79  4
         this.entry = req;
 80  4
         this.owner = user;
 81  4
         this.request = this.entry.uri().path("/user").path("/keys").back();
 82  4
     }
 83  
 
 84  
     @Override
 85  
     @NotNull(message = "user is never NULL")
 86  
     public User user() {
 87  0
         return this.owner;
 88  
     }
 89  
 
 90  
     @Override
 91  
     @NotNull(message = "Iterable of public keys is never NULL")
 92  
     public Iterable<PublicKey> iterate() {
 93  2
         return new RtPagination<PublicKey>(
 94  
             this.request,
 95  3
             new RtValuePagination.Mapping<PublicKey, JsonObject>() {
 96  
                 @Override
 97  
                 public PublicKey map(final JsonObject object) {
 98  2
                     return RtPublicKeys.this.get(object.getInt("id"));
 99  
                 }
 100  
             }
 101  
         );
 102  
     }
 103  
 
 104  
     @Override
 105  
     @NotNull(message = "public key is never NULL")
 106  
     public PublicKey create(
 107  
         @NotNull(message = "title key can't be NULL") final String title,
 108  
         @NotNull(message = "key can't be NULL") final String key
 109  
     ) throws IOException {
 110  2
         return this.get(
 111  
             this.request.method(Request.POST)
 112  
                 .body().set(
 113  
                     Json.createObjectBuilder()
 114  
                         .add("title", title)
 115  
                         .add("key", key)
 116  
                         .build()
 117  
                 ).back()
 118  
                 .fetch().as(RestResponse.class)
 119  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 120  
                 .as(JsonResponse.class)
 121  
                 .json().readObject().getInt("id")
 122  
         );
 123  
     }
 124  
 
 125  
     @Override
 126  
     @NotNull(message = "public key is never NULL")
 127  
     public PublicKey get(final int number) {
 128  8
         return new RtPublicKey(this.entry, this.owner, number);
 129  
     }
 130  
 
 131  
     @Override
 132  
     public void remove(final int number) throws IOException {
 133  2
         this.request.method(Request.DELETE)
 134  
             .uri().path(Integer.toString(number)).back()
 135  
             .fetch()
 136  
             .as(RestResponse.class)
 137  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 138  1
     }
 139  
 
 140  
     @Override
 141  
     @NotNull(message = "toString is never NULL")
 142  
     public String toString() {
 143  0
         return this.request.uri().get().toString();
 144  
     }
 145  
 
 146  
 }