Coverage Report - com.jcabi.github.RtIssues
 
Classes in this File Line Coverage Branch Coverage Complexity
RtIssues
76%
16/21
4%
1/24
1.125
RtIssues$1
100%
2/2
N/A
1.125
RtIssues$AjcClosure1
0%
0/1
N/A
1.125
RtIssues$AjcClosure3
100%
1/1
N/A
1.125
RtIssues$AjcClosure5
100%
1/1
N/A
1.125
RtIssues$AjcClosure7
100%
1/1
N/A
1.125
RtIssues$AjcClosure9
100%
1/1
N/A
1.125
 
 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.EnumMap;
 40  
 import java.util.HashMap;
 41  
 import java.util.Map;
 42  
 import javax.json.Json;
 43  
 import javax.json.JsonObject;
 44  
 import javax.json.JsonStructure;
 45  
 import javax.validation.constraints.NotNull;
 46  
 import lombok.EqualsAndHashCode;
 47  
 
 48  
 /**
 49  
  * Github issues.
 50  
  *
 51  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 52  
  * @version $Id$
 53  
  * @since 0.1
 54  
  * @checkstyle MultipleStringLiterals (500 lines)
 55  
  */
 56  
 @Immutable
 57  
 @Loggable(Loggable.DEBUG)
 58  0
 @EqualsAndHashCode(of = { "entry", "request", "owner" })
 59  
 final class RtIssues implements Issues {
 60  
 
 61  
     /**
 62  
      * API entry point.
 63  
      */
 64  
     private final transient Request entry;
 65  
 
 66  
     /**
 67  
      * RESTful request.
 68  
      */
 69  
     private final transient Request request;
 70  
 
 71  
     /**
 72  
      * Repository.
 73  
      */
 74  
     private final transient Repo owner;
 75  
 
 76  
     /**
 77  
      * Public ctor.
 78  
      * @param req Request
 79  
      * @param repo Repository
 80  
      */
 81  6
     RtIssues(final Request req, final Repo repo) {
 82  6
         this.entry = req;
 83  6
         final Coordinates coords = repo.coordinates();
 84  6
         this.request = this.entry.uri()
 85  
             .path("/repos")
 86  
             .path(coords.user())
 87  
             .path(coords.repo())
 88  
             .path("/issues")
 89  
             .back();
 90  6
         this.owner = repo;
 91  6
     }
 92  
 
 93  
     @Override
 94  
     @NotNull(message = "toString is never NULL")
 95  
     public String toString() {
 96  0
         return this.request.uri().get().toString();
 97  
     }
 98  
 
 99  
     @Override
 100  
     @NotNull(message = "repository is never NULL")
 101  
     public Repo repo() {
 102  0
         return this.owner;
 103  
     }
 104  
 
 105  
     @Override
 106  
     @NotNull(message = "Issue is never NULL")
 107  
     public Issue get(final int number) {
 108  14
         return new RtIssue(this.entry, this.owner, number);
 109  
     }
 110  
 
 111  
     @Override
 112  
     @NotNull(message = "Issue is never NULL")
 113  
     public Issue create(
 114  
         @NotNull(message = "title can't be NULL") final String title,
 115  
         @NotNull(message = "body can't be NULL")final String body)
 116  
         throws IOException {
 117  2
         final JsonStructure json = Json.createObjectBuilder()
 118  
             .add("title", title)
 119  
             .add("body", body)
 120  
             .build();
 121  1
         return this.get(
 122  
             this.request.method(Request.POST)
 123  
                 .body().set(json).back()
 124  
                 .fetch().as(RestResponse.class)
 125  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 126  
                 .as(JsonResponse.class)
 127  
                 .json().readObject().getInt("number")
 128  
         );
 129  
     }
 130  
 
 131  
     @Override
 132  
     @NotNull(message = "Iterable of issues is never NULL")
 133  
     public Iterable<Issue> iterate(
 134  
         @NotNull(message = "map or params can't be NULL")
 135  
         final Map<String, String> params) {
 136  4
         return new RtPagination<Issue>(
 137  
             this.request.uri().queryParams(params).back(),
 138  6
             new RtValuePagination.Mapping<Issue, JsonObject>() {
 139  
                 @Override
 140  
                 public Issue map(final JsonObject object) {
 141  4
                     return RtIssues.this.get(object.getInt("number"));
 142  
                 }
 143  
             }
 144  
         );
 145  
     }
 146  
 
 147  
     @Override
 148  
     @NotNull(message = "Iterable of issues is never NULL")
 149  
     @SuppressWarnings("PMD.UseConcurrentHashMap")
 150  
     public Iterable<Issue> search(
 151  
         @NotNull(message = "Sort field can't be NULL") final Sort sort,
 152  
         @NotNull(message = "Sort direction can't be NULL")
 153  
         final Search.Order direction,
 154  
         @NotNull(message = "Search qualifiers can't be NULL")
 155  
         final EnumMap<Qualifier, String> qualifiers)
 156  
         throws IOException {
 157  2
         final Map<String, String> params = new HashMap<String, String>();
 158  1
         for (final EnumMap.Entry<Qualifier, String> pair : qualifiers
 159  
             .entrySet()) {
 160  0
             params.put(pair.getKey().identifier(), pair.getValue());
 161  0
         }
 162  1
         params.put("sort", sort.identifier());
 163  1
         params.put("direction", direction.identifier());
 164  1
         return this.iterate(params);
 165  
     }
 166  
 }