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.Request;
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 com.jcabi.immutable.ArrayMap;
13  import jakarta.json.Json;
14  import jakarta.json.JsonObject;
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  import org.mockito.Mockito;
22  
23  /**
24   * Test case for {@link RtPulls}.
25   * @since 0.7
26   * @checkstyle ClassDataAbstractionCouplingCheck (200 lines)
27   */
28  @ExtendWith(RandomPort.class)
29  final class RtPullsTest {
30  
31      /**
32       * The rule for skipping test if there's BindException.
33       * @checkstyle VisibilityModifierCheck (3 lines)
34       */
35      @Test
36      void createPull() throws IOException {
37          final String title = "new feature";
38          final String body = RtPullsTest.pull(title).toString();
39          try (
40              MkContainer container = new MkGrizzlyContainer().next(
41                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
42              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
43                  .start(RandomPort.port())
44          ) {
45              final RtPulls pulls = new RtPulls(
46                  new ApacheRequest(container.home()),
47                  RtPullsTest.repo()
48              );
49              final Pull pull = pulls.create(title, "octocat", "master");
50              MatcherAssert.assertThat(
51                  "Values are not equal",
52                  container.take().method(),
53                  Matchers.equalTo(Request.POST)
54              );
55              MatcherAssert.assertThat(
56                  "Values are not equal",
57                  new Pull.Smart(pull).title(),
58                  Matchers.equalTo(title)
59              );
60              container.stop();
61          }
62      }
63  
64      @Test
65      void getSinglePull() throws IOException {
66          final String title = "new-feature";
67          try (
68              MkContainer container = new MkGrizzlyContainer().next(
69                  new MkAnswer.Simple(
70                      HttpURLConnection.HTTP_OK,
71                      RtPullsTest.pull(title).toString()
72                  )
73              ).start(RandomPort.port())
74          ) {
75              final RtPulls pulls = new RtPulls(
76                  new ApacheRequest(container.home()),
77                  RtPullsTest.repo()
78              );
79              final Pull pull = pulls.get(1_000_000_000);
80              MatcherAssert.assertThat(
81                  "Values are not equal",
82                  new Pull.Smart(pull).title(),
83                  Matchers.equalTo(title)
84              );
85              container.stop();
86          }
87      }
88  
89      @Test
90      void iteratePulls() throws IOException {
91          try (
92              MkContainer container = new MkGrizzlyContainer().next(
93                  new MkAnswer.Simple(
94                      HttpURLConnection.HTTP_OK,
95                      Json.createArrayBuilder()
96                          .add(RtPullsTest.pull("new-topic"))
97                          .add(RtPullsTest.pull("Amazing new feature"))
98                          .build().toString()
99                  )
100             ).start(RandomPort.port())
101         ) {
102             final RtPulls pulls = new RtPulls(
103                 new ApacheRequest(container.home()),
104                 RtPullsTest.repo()
105             );
106             MatcherAssert.assertThat(
107                 "Collection size is incorrect",
108                 pulls.iterate(new ArrayMap<>()),
109                 Matchers.iterableWithSize(2)
110             );
111             container.stop();
112         }
113     }
114 
115     /**
116      * Create and return JsonObject to test.
117      * @param title The title of the pull request
118      * @return JsonObject
119      */
120     private static JsonObject pull(final String title) {
121         return Json.createObjectBuilder()
122             .add("number", 1_000_000_000)
123             .add("state", Issue.OPEN_STATE)
124             .add("title", title)
125             .build();
126     }
127 
128     /**
129      * Create and return repo to test.
130      * @return Repo
131      */
132     private static Repo repo() {
133         final Repo repo = Mockito.mock(Repo.class);
134         Mockito.doReturn(new Coordinates.Simple("mark", "test"))
135             .when(repo).coordinates();
136         return repo;
137     }
138 }