Coverage Report - com.jcabi.github.RtUserEmails
 
Classes in this File Line Coverage Branch Coverage Complexity
RtUserEmails
89%
26/29
44%
8/18
1.667
RtUserEmails$AjcClosure1
100%
1/1
N/A
1.667
RtUserEmails$AjcClosure3
100%
1/1
N/A
1.667
RtUserEmails$AjcClosure5
100%
1/1
N/A
1.667
RtUserEmails$AjcClosure7
0%
0/1
N/A
1.667
 
 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 java.util.ArrayList;
 40  
 import java.util.Collection;
 41  
 import java.util.List;
 42  
 import javax.json.Json;
 43  
 import javax.json.JsonArrayBuilder;
 44  
 import javax.json.JsonObject;
 45  
 import javax.validation.constraints.NotNull;
 46  
 import lombok.EqualsAndHashCode;
 47  
 
 48  
 /**
 49  
  * Github user's emails.
 50  
  * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
 51  
  * @version $Id$
 52  
  * @since 0.8
 53  
  */
 54  
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @EqualsAndHashCode(of = "request")
 57  
 final class RtUserEmails implements UserEmails {
 58  
 
 59  
     /**
 60  
      * RESTful API request for the emails.
 61  
      */
 62  
     private final transient Request request;
 63  
 
 64  
     /**
 65  
      * Ctor.
 66  
      * @param req RESTful API entry point
 67  
      */
 68  4
     RtUserEmails(@NotNull(message = "req can't be NULL") final Request req) {
 69  4
         this.request = req.header("Accept", "application/vnd.github.v3")
 70  
             .uri().path("/user/emails").back();
 71  4
     }
 72  
 
 73  
     @Override
 74  
     @NotNull(message = "iterable is never NULL")
 75  
     public Iterable<String> iterate() throws IOException {
 76  2
         final List<JsonObject> array = this.request.method(Request.GET)
 77  
             .fetch().as(RestResponse.class)
 78  
             .assertStatus(HttpURLConnection.HTTP_OK)
 79  
             .as(JsonResponse.class)
 80  
             .json().readArray().getValuesAs(JsonObject.class);
 81  1
         final Collection<String> emails = new ArrayList<String>(array.size());
 82  1
         for (final JsonObject obj : array) {
 83  
             // @checkstyle MultipleStringLiterals (1 line)
 84  1
             emails.add(obj.getString("email"));
 85  1
         }
 86  1
         return emails;
 87  
     }
 88  
 
 89  
     @Override
 90  
     @NotNull(message = "iterable is never NULL")
 91  
     public Iterable<String> add(@NotNull(message = "emails is never NULL")
 92  
         final Iterable<String> emails) throws IOException {
 93  2
         final JsonArrayBuilder json = Json.createArrayBuilder();
 94  1
         for (final String email : emails) {
 95  1
             json.add(email);
 96  1
         }
 97  1
         final List<JsonObject> array = this.request.method(Request.POST)
 98  
             .body().set(json.build()).back()
 99  
             .fetch().as(RestResponse.class)
 100  
             .assertStatus(HttpURLConnection.HTTP_CREATED)
 101  
             .as(JsonResponse.class)
 102  
             .json().readArray().getValuesAs(JsonObject.class);
 103  1
         final Collection<String> result = new ArrayList<String>(array.size());
 104  1
         for (final JsonObject obj : array) {
 105  1
             result.add(obj.getString("email"));
 106  1
         }
 107  1
         return result;
 108  
     }
 109  
 
 110  
     @Override
 111  
     public void remove(@NotNull(message = "emails is never NULL")
 112  
         final Iterable<String> emails) throws IOException {
 113  2
         final JsonArrayBuilder json = Json.createArrayBuilder();
 114  1
         for (final String email : emails) {
 115  1
             json.add(email);
 116  1
         }
 117  1
         this.request.method(Request.DELETE)
 118  
             .body().set(json.build()).back()
 119  
             .fetch().as(RestResponse.class)
 120  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 121  1
     }
 122  
 
 123  
     @Override
 124  
     public String toString() {
 125  0
         return this.request.uri().get().toString();
 126  
     }
 127  
 
 128  
     @Override
 129  
     @NotNull(message = "JSON is never NULL")
 130  
     public JsonObject json() throws IOException {
 131  0
         return new RtJson(this.request).fetch();
 132  
     }
 133  
 
 134  
 }