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.Request;
9   import com.jcabi.http.mock.MkAnswer;
10  import com.jcabi.http.mock.MkContainer;
11  import com.jcabi.http.mock.MkGrizzlyContainer;
12  import com.jcabi.http.request.ApacheRequest;
13  import com.jcabi.http.request.FakeRequest;
14  import jakarta.json.Json;
15  import java.io.IOException;
16  import java.net.HttpURLConnection;
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 RtGistComment}.
24   * @since 0.8
25   * @checkstyle ClassDataAbstractionCouplingCheck (150 lines)
26   */
27  @ExtendWith(RandomPort.class)
28  final class RtGistCommentTest {
29  
30      /**
31       * RtGistComment can patch comment and return new json.
32       * @throws IOException if has some problems with json parsing.
33       */
34      @Test
35      void patchAndCheckJsonGistComment() throws IOException {
36          final int identifier = 1;
37          final String idprop = "id";
38          final String bodyprop = "body";
39          final String body = "somebody";
40          final String patched = "some patchedbody";
41          final MkAnswer first = new MkAnswer.Simple(
42              HttpURLConnection.HTTP_OK,
43              Json.createObjectBuilder()
44                  .add(bodyprop, body)
45                  .add(idprop, identifier)
46                  .build().toString()
47          );
48          final MkAnswer second = new MkAnswer.Simple(
49              HttpURLConnection.HTTP_OK,
50              Json.createObjectBuilder()
51                  .add(bodyprop, patched)
52                  .add(idprop, identifier)
53                  .build().toString()
54          );
55          final MkAnswer third = new MkAnswer.Simple(
56              HttpURLConnection.HTTP_OK,
57              Json.createObjectBuilder()
58                  .add(bodyprop, body)
59                  .add(idprop, identifier)
60                  .build().toString()
61          );
62          try (
63              MkContainer container =
64                  new MkGrizzlyContainer().next(first).next(second).next(third)
65                      .start(RandomPort.port());
66              MkContainer gistContainer = new MkGrizzlyContainer()
67                  .start(RandomPort.port())
68          ) {
69              final RtGist gist =
70                  new RtGist(
71                      new MkGitHub(),
72                      new ApacheRequest(gistContainer.home()), "someName"
73                  );
74              final RtGistComment comment = new RtGistComment(
75                  new ApacheRequest(container.home()), gist, identifier
76              );
77              comment.patch(Json.createObjectBuilder()
78                  .add(bodyprop, patched)
79                  .add(idprop, identifier)
80                  .build()
81              );
82              MatcherAssert.assertThat(
83                  "Values are not equal",
84                  comment.json().getString(bodyprop),
85                  Matchers.equalTo(patched)
86              );
87              container.stop();
88              gistContainer.stop();
89          }
90      }
91  
92      /**
93       * RtGistComment can remove comment.
94       * @throws IOException if has some problems with json parsing.
95       */
96      @Test
97      void removeGistComment() throws IOException {
98          try (
99              MkContainer container = new MkGrizzlyContainer().next(
100                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
101             ).start(RandomPort.port())) {
102             final RtGist gist = new RtGist(
103                 new MkGitHub(),
104                 new FakeRequest().withStatus(HttpURLConnection.HTTP_NO_CONTENT),
105                 "gistName"
106             );
107             final int identifier = 1;
108             final RtGistComment comment = new RtGistComment(
109                 new ApacheRequest(container.home()), gist, identifier
110             );
111             comment.remove();
112             MatcherAssert.assertThat(
113                 "Values are not equal",
114                 container.take().method(),
115                 Matchers.equalTo(Request.DELETE)
116             );
117             container.stop();
118         }
119     }
120 }