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