Coverage Report - com.jcabi.github.RtMarkdown
 
Classes in this File Line Coverage Branch Coverage Complexity
RtMarkdown
75%
9/12
0%
0/16
1
RtMarkdown$AjcClosure1
0%
0/1
N/A
1
RtMarkdown$AjcClosure3
100%
1/1
N/A
1
RtMarkdown$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.RestResponse;
 36  
 import java.io.IOException;
 37  
 import java.io.StringWriter;
 38  
 import java.net.HttpURLConnection;
 39  
 import javax.json.Json;
 40  
 import javax.json.JsonObject;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import javax.ws.rs.core.HttpHeaders;
 43  
 import javax.ws.rs.core.MediaType;
 44  
 import lombok.EqualsAndHashCode;
 45  
 import org.hamcrest.Matchers;
 46  
 
 47  
 /**
 48  
  * Github markdown.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 51  
  * @version $Id$
 52  
  * @since 0.6
 53  
  */
 54  
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @EqualsAndHashCode(of = { "ghub", "request" })
 57  
 final class RtMarkdown implements Markdown {
 58  
 
 59  
     /**
 60  
      * Github.
 61  
      */
 62  
     private final transient Github ghub;
 63  
 
 64  
     /**
 65  
      * RESTful request.
 66  
      */
 67  
     private final transient Request request;
 68  
 
 69  
     /**
 70  
      * Public ctor.
 71  
      * @param github Github
 72  
      * @param req Request
 73  
      */
 74  3
     RtMarkdown(final Github github, final Request req) {
 75  3
         this.ghub = github;
 76  3
         this.request = req.uri().path("markdown").back().method(Request.POST);
 77  3
     }
 78  
 
 79  
     @Override
 80  
     @NotNull(message = "toString is never NULL")
 81  
     public String toString() {
 82  0
         return this.request.uri().get().toString();
 83  
     }
 84  
 
 85  
     @Override
 86  
     @NotNull(message = "Github is never NULL")
 87  
     public Github github() {
 88  0
         return this.ghub;
 89  
     }
 90  
 
 91  
     @Override
 92  
     @NotNull(message = "render is never NULL")
 93  
     public String render(
 94  
         @NotNull(message = "JSON can't be NULL") final JsonObject json)
 95  
         throws IOException {
 96  2
         final StringWriter output = new StringWriter();
 97  1
         Json.createWriter(output).writeObject(json);
 98  1
         return this.request
 99  
             .body()
 100  
             .set(output.toString())
 101  
             .back()
 102  
             .fetch()
 103  
             .as(RestResponse.class)
 104  
             .assertStatus(HttpURLConnection.HTTP_OK)
 105  
             .assertHeader(
 106  
                 HttpHeaders.CONTENT_TYPE,
 107  
                 Matchers.everyItem(
 108  
                     Matchers.startsWith(MediaType.TEXT_HTML)
 109  
                 )
 110  
             )
 111  
             .body();
 112  
     }
 113  
 
 114  
     @Override
 115  
     @NotNull(message = "raw is never NULL")
 116  
     public String raw(
 117  
         @NotNull(message = "Markdown can't be NULL") final String text)
 118  
         throws IOException {
 119  2
         return this.request
 120  
             .body()
 121  
             .set(text)
 122  
             .back()
 123  
             .uri().path("raw").back()
 124  
             .reset(HttpHeaders.CONTENT_TYPE)
 125  
             .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
 126  
             .fetch()
 127  
             .as(RestResponse.class)
 128  
             .assertStatus(HttpURLConnection.HTTP_OK)
 129  
             .assertHeader(
 130  
                 HttpHeaders.CONTENT_TYPE,
 131  
                 Matchers.everyItem(
 132  
                     Matchers.startsWith(MediaType.TEXT_HTML)
 133  
                 )
 134  
             )
 135  
             .body();
 136  
     }
 137  
 
 138  
 }