View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcabi.github.mock.MkGitHub;
8   import com.jcabi.http.mock.MkAnswer;
9   import com.jcabi.http.mock.MkContainer;
10  import com.jcabi.http.mock.MkGrizzlyContainer;
11  import com.jcabi.http.request.ApacheRequest;
12  import jakarta.json.Json;
13  import jakarta.ws.rs.core.MediaType;
14  import java.io.IOException;
15  import java.net.HttpURLConnection;
16  import org.apache.http.HttpHeaders;
17  import org.hamcrest.MatcherAssert;
18  import org.hamcrest.Matchers;
19  import org.junit.jupiter.api.Test;
20  import org.junit.jupiter.api.extension.ExtendWith;
21  
22  /**
23   * Test case for {@link RtMarkdown}.
24   * @since 0.8
25   * @checkstyle MultipleStringLiteralsCheck (100 lines)
26   */
27  @ExtendWith(RandomPort.class)
28  final class RtMarkdownTest {
29  
30      /**
31       * The rule for skipping test if there's BindException.
32       * @checkstyle VisibilityModifierCheck (3 lines)
33       */
34      @Test
35      void returnsJsonOutput() throws IOException {
36          try (
37              MkContainer container = new MkGrizzlyContainer().next(
38                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "{\"a\":\"b\"}")
39                      .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML)
40              ).start(RandomPort.port())
41          ) {
42              final RtMarkdown markdown = new RtMarkdown(
43                  new MkGitHub(),
44                  new ApacheRequest(container.home())
45              );
46              MatcherAssert.assertThat(
47                  "Values are not equal",
48                  markdown.render(
49                      Json.createObjectBuilder().add("hello", "world").build()
50                  ),
51                  Matchers.equalTo("{\"a\":\"b\"}")
52              );
53              MatcherAssert.assertThat(
54                  "Values are not equal",
55                  container.take().body(),
56                  Matchers.equalTo("{\"hello\":\"world\"}")
57              );
58              container.stop();
59          }
60      }
61  
62      @Test
63      void returnsRawOutput() throws IOException {
64          try (
65              MkContainer container = new MkGrizzlyContainer().next(
66                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "Test Output")
67                      .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML)
68              ).start(RandomPort.port())
69          ) {
70              final RtMarkdown markdown = new RtMarkdown(
71                  new MkGitHub(),
72                  new ApacheRequest(container.home())
73              );
74              MatcherAssert.assertThat(
75                  "Values are not equal",
76                  markdown.raw("Hello World!"),
77                  Matchers.equalTo("Test Output")
78              );
79              MatcherAssert.assertThat(
80                  "Values are not equal",
81                  container.take().body(),
82                  Matchers.equalTo("Hello World!")
83              );
84              container.stop();
85          }
86      }
87  
88  }