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 com.jcabi.http.request.FakeRequest;
14  import com.jcabi.http.request.JdkRequest;
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.junit.jupiter.api.Test;
22  import org.junit.jupiter.api.extension.ExtendWith;
23  import org.mockito.Mockito;
24  
25  /**
26   * Test case for {@link RtReleases}.
27   * @since 0.8
28   * @checkstyle MultipleStringLiterals (500 lines)
29   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
30   */
31  @ExtendWith(RandomPort.class)
32  final class RtReleasesTest {
33  
34      /**
35       * The rule for skipping test if there's BindException.
36       * @checkstyle VisibilityModifierCheck (3 lines)
37       */
38      @Test
39      void canFetchEmptyListOfReleases() {
40          final Releases releases = new RtReleases(
41              new FakeRequest().withBody("[]"),
42              RtReleasesTest.repo()
43          );
44          MatcherAssert.assertThat(
45              "Collection is not empty",
46              releases.iterate(),
47              Matchers.emptyIterable()
48          );
49      }
50  
51      @Test
52      void canFetchNonEmptyListOfReleases() {
53          final int number = 1;
54          final Releases releases = new RtReleases(
55              new FakeRequest().withBody(
56                  Json.createArrayBuilder().add(
57                      Json.createObjectBuilder()
58                          .add("id", number)
59                          .add("tag_name", "v1.0.0")
60                          .add("name", "v1.0.0")
61                          .add("body", "Release")
62                  ).build().toString()
63              ),
64              RtReleasesTest.repo()
65          );
66          MatcherAssert.assertThat(
67              "Values are not equal",
68              releases.iterate().iterator().next().number(),
69              Matchers.equalTo(number)
70          );
71      }
72  
73      @Test
74      void canFetchSingleRelease() {
75          final Releases releases = new RtReleases(
76              new FakeRequest(), RtReleasesTest.repo()
77          );
78          MatcherAssert.assertThat(
79              "Value is null", releases.get(1), Matchers.notNullValue()
80          );
81      }
82  
83      @Test
84      void canCreateRelease() throws IOException {
85          final String tag = "v1.0.0";
86          final String rel = RtReleasesTest.release(tag).toString();
87          try (
88              MkContainer container = new MkGrizzlyContainer().next(
89                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, rel)
90              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, rel))
91                  .start(RandomPort.port())
92          ) {
93              final RtReleases releases = new RtReleases(
94                  new JdkRequest(container.home()),
95                  RtReleasesTest.repo()
96              );
97              final Release release = releases.create(tag);
98              MatcherAssert.assertThat(
99                  "Values are not equal",
100                 container.take().method(),
101                 Matchers.equalTo(Request.POST)
102             );
103             MatcherAssert.assertThat(
104                 "Values are not equal",
105                 release.json().getString("tag_name"),
106                 Matchers.equalTo(tag)
107             );
108             container.stop();
109         }
110     }
111 
112     @Test
113     void canDeleteRelease() throws IOException {
114         try (
115             MkContainer container = new MkGrizzlyContainer().next(
116                 new MkAnswer.Simple(
117                     HttpURLConnection.HTTP_NO_CONTENT,
118                     ""
119                 )
120             ).start(RandomPort.port())
121         ) {
122             final Releases releases = new RtReleases(
123                 new ApacheRequest(container.home()),
124                 RtReleasesTest.repo()
125             );
126             releases.remove(1);
127             final MkQuery query = container.take();
128             MatcherAssert.assertThat(
129                 "String does not end with expected value",
130                 query.uri().toString(),
131                 Matchers.endsWith("/releases/1")
132             );
133             MatcherAssert.assertThat(
134                 "Values are not equal",
135                 query.method(),
136                 Matchers.equalTo(Request.DELETE)
137             );
138             container.stop();
139         }
140     }
141 
142     /**
143      * Create and return repo for testing.
144      * @return Repo
145      */
146     private static Repo repo() {
147         final Repo repo = Mockito.mock(Repo.class);
148         Mockito.doReturn(new Coordinates.Simple("test", "releases"))
149             .when(repo).coordinates();
150         return repo;
151     }
152 
153     /**
154      * Create and return JsonObject to test.
155      * @param tag The tag name of the release
156      * @return JsonObject
157      */
158     private static JsonObject release(final String tag) {
159         return Json.createObjectBuilder()
160             .add("id", 1)
161             .add("tag_name", tag)
162             .build();
163     }
164 }