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