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.FakeRequest;
12  import com.jcabi.http.request.JdkRequest;
13  import jakarta.json.Json;
14  import jakarta.json.JsonObject;
15  import java.io.IOException;
16  import java.net.HttpURLConnection;
17  import org.apache.commons.lang3.RandomStringUtils;
18  import org.hamcrest.MatcherAssert;
19  import org.hamcrest.Matchers;
20  import org.junit.jupiter.api.Test;
21  import org.junit.jupiter.api.extension.ExtendWith;
22  import org.mockito.Mockito;
23  
24  /**
25   * Test case for {@link RtForks}.
26   * @since 0.1
27   */
28  @ExtendWith(RandomPort.class)
29  final class RtForksTest {
30  
31      /**
32       * Fork's organization name in JSON object.
33       */
34      public static final String ORGANIZATION = "organization";
35  
36      /**
37       * The rule for skipping test if there's BindException.
38       * @checkstyle VisibilityModifierCheck (3 lines)
39       */
40      @Test
41      void retrievesForks() {
42          final RtForks forks = new RtForks(
43              new FakeRequest()
44                  .withBody("[]"), RtForksTest.repo()
45          );
46          MatcherAssert.assertThat(
47              "Collection size is incorrect",
48              forks.iterate("newest"),
49              Matchers.iterableWithSize(0)
50          );
51      }
52  
53      @Test
54      void createsFork() throws IOException {
55          final String organization = RandomStringUtils.secure().nextAlphanumeric(10);
56          final MkAnswer answer = new MkAnswer.Simple(
57              HttpURLConnection.HTTP_OK,
58              RtForksTest.fork(organization).toString()
59          );
60          try (
61              MkContainer container = new MkGrizzlyContainer().next(
62                  new MkAnswer.Simple(
63                      HttpURLConnection.HTTP_ACCEPTED,
64                      RtForksTest.fork(organization).toString()
65                  )
66              ).next(answer).start(RandomPort.port())) {
67              final Repo owner = Mockito.mock(Repo.class);
68              final Coordinates coordinates = new Coordinates.Simple(
69                  "test_user", "test_repo"
70              );
71              Mockito.doReturn(coordinates).when(owner).coordinates();
72              final RtForks forks = new RtForks(
73                  new JdkRequest(container.home()),
74                  owner
75              );
76              final Fork fork = forks.create(organization);
77              MatcherAssert.assertThat(
78                  "Values are not equal",
79                  container.take().method(),
80                  Matchers.equalTo(Request.POST)
81              );
82              MatcherAssert.assertThat(
83                  "Values are not equal",
84                  fork.json().getString(RtForksTest.ORGANIZATION),
85                  Matchers.equalTo(organization)
86              );
87          }
88      }
89  
90      /**
91       * Create and return repo for testing.
92       * @return Repo
93       */
94      private static Repo repo() {
95          final Repo repo = Mockito.mock(Repo.class);
96          Mockito.doReturn(new Coordinates.Simple("test", "forks"))
97              .when(repo).coordinates();
98          return repo;
99      }
100 
101     /**
102      * Create and return JsonObject to test.
103      * @param organization The organization of the fork
104      * @return JsonObject
105      */
106     private static JsonObject fork(
107         final String organization) {
108         return Json.createObjectBuilder()
109             .add("id", 1)
110             .add(RtForksTest.ORGANIZATION, organization)
111             .build();
112     }
113 }