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 java.io.IOException;
17  import java.net.HttpURLConnection;
18  import org.hamcrest.MatcherAssert;
19  import org.hamcrest.Matchers;
20  import org.hamcrest.core.IsEqual;
21  import org.junit.jupiter.api.Test;
22  import org.junit.jupiter.api.extension.ExtendWith;
23  import org.mockito.Mockito;
24  
25  /**
26   * Test case for {@link RtIssue}.
27   * @since 0.1
28   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
29   */
30  @ExtendWith(RandomPort.class)
31  final class RtIssueTest {
32  
33      /**
34       * The rule for skipping test if there's BindException.
35       * @checkstyle VisibilityModifierCheck (3 lines)
36       */
37      @Test
38      void fetchesComments() {
39          final RtIssue issue = new RtIssue(new FakeRequest(), RtIssueTest.repo(), 1);
40          MatcherAssert.assertThat(
41              "Value is null",
42              issue.comments(),
43              Matchers.notNullValue()
44          );
45      }
46  
47      @Test
48      void fetchesLabels() {
49          final RtIssue issue = new RtIssue(new FakeRequest(), RtIssueTest.repo(), 1);
50          MatcherAssert.assertThat(
51              "Value is null",
52              issue.labels(),
53              Matchers.notNullValue()
54          );
55      }
56  
57      @Test
58      void fetchesEvents() {
59          final RtIssue issue = new RtIssue(new FakeRequest(), RtIssueTest.repo(), 1);
60          MatcherAssert.assertThat(
61              "Value is null",
62              issue.events(),
63              Matchers.notNullValue()
64          );
65      }
66  
67      @Test
68      void fetchIssueAsJson() throws IOException {
69          final RtIssue issue = new RtIssue(
70              new FakeRequest().withBody("{\"issue\":\"json\"}"),
71              RtIssueTest.repo(),
72              1
73          );
74          MatcherAssert.assertThat(
75              "Values are not equal",
76              issue.json().getString("issue"),
77              Matchers.equalTo("json")
78          );
79      }
80  
81      @Test
82      void patchWithJson() throws IOException {
83          try (
84              MkContainer container = new MkGrizzlyContainer().next(
85                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
86              ).start(RandomPort.port())
87          ) {
88              final RtIssue issue = new RtIssue(
89                  new ApacheRequest(container.home()),
90                  RtIssueTest.repo(),
91                  1
92              );
93              issue.patch(
94                  Json.createObjectBuilder().add("patch", "test").build()
95              );
96              final MkQuery query = container.take();
97              MatcherAssert.assertThat(
98                  "Values are not equal",
99                  query.method(),
100                 Matchers.equalTo(Request.PATCH)
101             );
102             MatcherAssert.assertThat(
103                 "Values are not equal",
104                 query.body(),
105                 Matchers.equalTo("{\"patch\":\"test\"}")
106             );
107             container.stop();
108         }
109     }
110 
111     @Test
112     void canCompareInstances() {
113         final RtIssue less = new RtIssue(new FakeRequest(), RtIssueTest.repo(), 1);
114         final RtIssue greater = new RtIssue(new FakeRequest(), RtIssueTest.repo(), 2);
115         MatcherAssert.assertThat(
116             "Value is not less than expected",
117             less.compareTo(greater), Matchers.lessThan(0)
118         );
119         MatcherAssert.assertThat(
120             "Value is not greater than expected",
121             greater.compareTo(less), Matchers.greaterThan(0)
122         );
123     }
124 
125     @Test
126     void reacts() throws IOException {
127         try (
128             MkContainer container = new MkGrizzlyContainer().next(
129                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
130             ).start(RandomPort.port())) {
131             final Repo repo = new MkGitHub().randomRepo();
132             final Issue issue = new RtIssue(
133                 new ApacheRequest(container.home()),
134                 repo,
135                 10
136             );
137             issue.react(new Reaction.Simple(Reaction.HEART));
138             final MkQuery query = container.take();
139             MatcherAssert.assertThat(
140                 "Issue was unable to react",
141                 query.method(),
142                 new IsEqual<>(Request.POST)
143             );
144         }
145     }
146 
147     /**
148      * Mock repo for GhIssue creation.
149      * @return The mock repo.
150      */
151     private static Repo repo() {
152         final Repo repo = Mockito.mock(Repo.class);
153         final Coordinates coords = Mockito.mock(Coordinates.class);
154         Mockito.doReturn(coords).when(repo).coordinates();
155         Mockito.doReturn("user").when(coords).user();
156         Mockito.doReturn("repo").when(coords).repo();
157         return repo;
158     }
159 
160 }