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.mock.MkQuery;
13  import com.jcabi.http.request.ApacheRequest;
14  import com.jcabi.http.request.FakeRequest;
15  import jakarta.json.Json;
16  import jakarta.json.JsonObject;
17  import java.io.IOException;
18  import java.net.HttpURLConnection;
19  import org.hamcrest.MatcherAssert;
20  import org.hamcrest.Matchers;
21  import org.hamcrest.collection.IsCollectionWithSize;
22  import org.hamcrest.core.IsEqual;
23  import org.junit.jupiter.api.Disabled;
24  import org.junit.jupiter.api.Test;
25  import org.junit.jupiter.api.extension.ExtendWith;
26  import org.mockito.Mockito;
27  
28  /**
29   * Test case for {@link RtPullComment}.
30   * @since 0.8
31   * @checkstyle MultipleStringLiteralsCheck (200 lines)
32   * @checkstyle ClassDataAbstractionCouplingCheck (3 lines)
33   */
34  @ExtendWith(RandomPort.class)
35  final class RtPullCommentTest {
36  
37      /**
38       * The rule for skipping test if there's BindException.
39       * @checkstyle VisibilityModifierCheck (3 lines)
40       */
41      @Test
42      void canCompareInstances() throws IOException {
43          final Pull pull = Mockito.mock(Pull.class);
44          Mockito.doReturn(new MkGitHub().randomRepo()).when(pull).repo();
45          final RtPullComment less =
46              new RtPullComment(new FakeRequest(), pull, 1);
47          final RtPullComment greater =
48              new RtPullComment(new FakeRequest(), pull, 2);
49          MatcherAssert.assertThat(
50              "Value is not less than expected",
51              less.compareTo(greater), Matchers.lessThan(0)
52          );
53          MatcherAssert.assertThat(
54              "Value is not greater than expected",
55              greater.compareTo(less), Matchers.greaterThan(0)
56          );
57          MatcherAssert.assertThat(
58              "Values are not equal",
59              less.compareTo(less), Matchers.equalTo(0)
60          );
61      }
62  
63      /**
64       * RtPullComment can return its JSON description.
65       * @throws Exception If a problem occurs.
66       */
67      @Test
68      void canDescribeAsJson() throws Exception {
69          final String body = "{\"body\":\"test\"}";
70          try (
71              MkContainer container = new MkGrizzlyContainer().next(
72                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body)
73              ).start(RandomPort.port())
74          ) {
75              final Pull pull = Mockito.mock(Pull.class);
76              Mockito.doReturn(RtPullCommentTest.repo()).when(pull).repo();
77              final RtPullComment comment =
78                  new RtPullComment(new ApacheRequest(container.home()), pull, 1);
79              final JsonObject json = comment.json();
80              MatcherAssert.assertThat(
81                  "Values are not equal",
82                  json.getString("body"),
83                  Matchers.is("test")
84              );
85              MatcherAssert.assertThat(
86                  "String does not end with expected value",
87                  container.take().uri().toString(),
88                  Matchers.endsWith("/repos/joe/blueharvest/pulls/comments/1")
89              );
90              container.stop();
91          }
92      }
93  
94      /**
95       * RtPullComment can create a patch request.
96       * @throws Exception If a problem occurs.
97       */
98      @Test
99      void patchesComment() throws Exception {
100         try (
101             MkContainer container = new MkGrizzlyContainer().next(
102                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
103             ).start(RandomPort.port())
104         ) {
105             final Pull pull = Mockito.mock(Pull.class);
106             Mockito.doReturn(RtPullCommentTest.repo()).when(pull).repo();
107             final RtPullComment comment =
108                 new RtPullComment(new ApacheRequest(container.home()), pull, 2);
109             final JsonObject json = Json.createObjectBuilder()
110                 .add("body", "test comment").build();
111             comment.patch(json);
112             final MkQuery query = container.take();
113             MatcherAssert.assertThat(
114                 "Values are not equal",
115                 query.method(), Matchers.equalTo(Request.PATCH)
116             );
117             MatcherAssert.assertThat(
118                 "String does not contain expected value",
119                 query.body(),
120                 Matchers.containsString("{\"body\":\"test comment\"}")
121             );
122             MatcherAssert.assertThat(
123                 "String does not end with expected value",
124                 query.uri().toString(),
125                 Matchers.endsWith("/repos/joe/blueharvest/pulls/comments/2")
126             );
127             container.stop();
128         }
129     }
130 
131     @Test
132     @Disabled
133     void reacts() throws IOException {
134         try (
135             MkContainer container = new MkGrizzlyContainer().next(
136                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
137             ).start(RandomPort.port())) {
138             final Repo repo = new MkGitHub().randomRepo();
139             final Pull pull = repo.pulls().create(
140                 "Reaction adding test",
141                 "This is a test for adding a reaction",
142                 "Base"
143             );
144             final RtPullComment comment = new RtPullComment(
145                 new ApacheRequest(container.home()), pull, 2
146             );
147             comment.react(new Reaction.Simple(Reaction.HEART));
148             MatcherAssert.assertThat(
149                 "Pull comment was unable to react",
150                 comment.reactions(),
151                 new IsCollectionWithSize<>(
152                     new IsEqual<>(1)
153                 )
154             );
155         }
156     }
157 
158     /**
159      * This method returns a Repo for testing.
160      * @return Repo - a repo to be used for test.
161      */
162     private static Repo repo() throws IOException {
163         return new MkGitHub("joe").repos().create(
164             new Repos.RepoCreate("blueharvest", false)
165         );
166     }
167 }