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.http.mock.MkAnswer;
8   import com.jcabi.http.mock.MkContainer;
9   import com.jcabi.http.mock.MkGrizzlyContainer;
10  import com.jcabi.http.request.ApacheRequest;
11  import jakarta.json.Json;
12  import java.io.IOException;
13  import java.net.HttpURLConnection;
14  import org.hamcrest.MatcherAssert;
15  import org.hamcrest.Matchers;
16  import org.junit.jupiter.api.Test;
17  import org.junit.jupiter.api.extension.ExtendWith;
18  
19  /**
20   * Test case for {@link RtJson}.
21   * @since 0.1
22   */
23  @ExtendWith(RandomPort.class)
24  final class RtJsonTest {
25      /**
26       * The rule for skipping test if there's BindException.
27       * @checkstyle VisibilityModifierCheck (3 lines)
28       */
29      @Test
30      void sendHttpRequest() throws IOException {
31          try (
32              MkContainer container = new MkGrizzlyContainer().next(
33                  new MkAnswer.Simple(
34                      HttpURLConnection.HTTP_OK,
35                      "{\"body\":\"hi\"}"
36                  )
37              ).start(RandomPort.port())
38          ) {
39              final RtJson json = new RtJson(new ApacheRequest(container.home()));
40              MatcherAssert.assertThat(
41                  "Values are not equal",
42                  json.fetch().getString("body"),
43                  Matchers.equalTo("hi")
44              );
45              container.stop();
46          }
47      }
48  
49      @Test
50      void executePatchRequest() throws IOException {
51          try (
52              MkContainer container = new MkGrizzlyContainer().next(
53                  new MkAnswer.Simple(
54                      HttpURLConnection.HTTP_OK,
55                      "{\"body\":\"hj\"}"
56                  )
57              ).start(RandomPort.port())
58          ) {
59              final RtJson json = new RtJson(new ApacheRequest(container.home()));
60              json.patch(
61                  Json.createObjectBuilder()
62                      .add("content", "hi you!")
63                      .build()
64              );
65              MatcherAssert.assertThat(
66                  "Values are not equal",
67                  container.take().method(),
68                  Matchers.equalTo("PATCH")
69              );
70              container.stop();
71          }
72      }
73  }