Coverage Report - com.jcabi.github.RtTrees
 
Classes in this File Line Coverage Branch Coverage Complexity
RtTrees
81%
9/11
0%
0/22
1
RtTrees$AjcClosure1
0%
0/1
N/A
1
RtTrees$AjcClosure3
100%
1/1
N/A
1
RtTrees$AjcClosure5
100%
1/1
N/A
1
RtTrees$AjcClosure7
100%
1/1
N/A
1
 
 1  6
 /**
 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.JsonObject;
 40  
 import javax.validation.constraints.NotNull;
 41  
 import lombok.EqualsAndHashCode;
 42  
 
 43  
 /**
 44  
  * Github trees.
 45  
  * @version $Id$
 46  
  * @author Alexander Lukashevich (sanai56967@gmail.com)
 47  
  */
 48  
 @Immutable
 49  
 @Loggable(Loggable.DEBUG)
 50  0
 @EqualsAndHashCode(of = { "request", "owner", "entry" })
 51  
 final class RtTrees implements Trees {
 52  
 
 53  
     /**
 54  
      * RESTful API entry point.
 55  
      */
 56  
     private final transient Request entry;
 57  
 
 58  
     /**
 59  
      * Repository.
 60  
      */
 61  
     private final transient Repo owner;
 62  
 
 63  
     /**
 64  
      * RESTful request.
 65  
      */
 66  
     private final transient Request request;
 67  
 
 68  
     /**
 69  
      * Public ctor.
 70  
      * @param req Entry point of API
 71  
      * @param repo Repository
 72  
      */
 73  3
     RtTrees(final Request req, final Repo repo) {
 74  3
         this.entry = req;
 75  3
         this.owner = repo;
 76  3
         this.request = req.uri()
 77  
             .path("/repos")
 78  
             .path(repo.coordinates().user())
 79  
             .path(repo.coordinates().repo())
 80  
             .path("/git")
 81  
             .path("/trees")
 82  
             .back();
 83  3
     }
 84  
 
 85  
     @Override
 86  
     @NotNull(message = "Repo is never NULL")
 87  
     public Repo repo() {
 88  0
         return this.owner;
 89  
     }
 90  
 
 91  
     @Override
 92  
     @NotNull(message = "Tree is never NULL")
 93  
     public Tree get(@NotNull(message = "sha can't be NULL") final String sha) {
 94  6
         return new RtTree(this.entry, this.owner, sha);
 95  
     }
 96  
 
 97  
     @Override
 98  
     @NotNull(message = "tree is never NULL")
 99  
     public Tree getRec(
 100  
         @NotNull(message = "sha can't be NULL") final String sha
 101  
     ) {
 102  2
         return new RtTree(
 103  
             this.entry.uri().queryParam("recursive", "1").back(),
 104  
             this.owner, sha
 105  
         );
 106  
     }
 107  
 
 108  
     @Override
 109  
     @NotNull(message = "tree is never NULL")
 110  
     public Tree create(
 111  
         @NotNull(message = "params can't be NULL") final JsonObject params
 112  
     ) throws IOException {
 113  2
         return this.get(
 114  
             this.request.method(Request.POST)
 115  
                 .body().set(params).back()
 116  
                 .fetch().as(RestResponse.class)
 117  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 118  
                 .as(JsonResponse.class)
 119  
                 .json().readObject().getString("sha")
 120  
         );
 121  
     }
 122  
 }