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