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.mock.MkQuery;
12  import com.jcabi.http.request.ApacheRequest;
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.hamcrest.core.IsEqual;
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 Repos}.
26   * @since 0.8
27   * @checkstyle ClassDataAbstractionCoupling (500 lines)
28   */
29  @ExtendWith(RandomPort.class)
30  final class RtReposTest {
31  
32      /**
33       * RepoRule.
34       * @checkstyle VisibilityModifierCheck (3 lines)
35       */
36      public final transient RepoRule rule = new RepoRule();
37  
38      @Test
39      void createRepo() throws IOException {
40          final String owner = "test-owner";
41          final String name = "test-repo";
42          final String response = RtReposTest.response(owner, name).toString();
43          try (
44              MkContainer container = new MkGrizzlyContainer().next(
45                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, response)
46              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, response))
47                  .start(RandomPort.port())
48          ) {
49              final RtRepos repos = new RtRepos(
50                  Mockito.mock(GitHub.class),
51                  new ApacheRequest(container.home())
52              );
53              final Repo repo = this.rule.repo(repos);
54              MatcherAssert.assertThat(
55                  "Values are not equal",
56                  container.take().method(),
57                  Matchers.equalTo(Request.POST)
58              );
59              MatcherAssert.assertThat(
60                  "Assertion failed",
61                  repo.coordinates(),
62                  new IsEqual<>(new Coordinates.Simple(owner, name))
63              );
64              container.stop();
65          }
66      }
67  
68      @Test
69      void iterateRepos() throws IOException {
70          final String identifier = "1";
71          try (
72              MkContainer container = new MkGrizzlyContainer().next(
73                  new MkAnswer.Simple(
74                      HttpURLConnection.HTTP_OK,
75                      Json.createArrayBuilder()
76                          .add(RtReposTest.response("octocat", identifier))
77                          .add(RtReposTest.response("dummy", "2"))
78                          .build().toString()
79                  )
80              ).start(RandomPort.port())
81          ) {
82              final RtRepos repos = new RtRepos(
83                  Mockito.mock(GitHub.class),
84                  new ApacheRequest(container.home())
85              );
86              MatcherAssert.assertThat(
87                  "Collection size is incorrect",
88                  repos.iterate(identifier),
89                  Matchers.iterableWithSize(2)
90              );
91              container.stop();
92          }
93      }
94  
95      @Test
96      void removeRepo() throws IOException {
97          try (
98              MkContainer container = new MkGrizzlyContainer().next(
99                  new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
100             ).start(RandomPort.port())
101         ) {
102             final Repos repos = new RtRepos(
103                 Mockito.mock(GitHub.class),
104                 new ApacheRequest(container.home())
105             );
106             repos.remove(new Coordinates.Simple("", ""));
107             final MkQuery query = container.take();
108             MatcherAssert.assertThat(
109                 "Values are not equal",
110                 query.method(),
111                 Matchers.equalTo(Request.DELETE)
112             );
113             MatcherAssert.assertThat(
114                 "Values are not equal",
115                 query.body(),
116                 Matchers.is(Matchers.emptyString())
117             );
118             container.stop();
119         }
120     }
121 
122     /**
123      * Create and return JsonObject to test response.
124      * @param owner Owner name
125      * @param name Repo name
126      * @return JsonObject
127      */
128     private static JsonObject response(
129         final String owner, final String name) {
130         return Json.createObjectBuilder()
131             .add("name", name)
132             .add("full_name", String.format("%s/%s", owner, name))
133             .add(
134                 "owner",
135                 Json.createObjectBuilder().add("login", owner).build()
136             )
137             .build();
138     }
139 
140 }