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 java.net.URI;
18  import org.hamcrest.MatcherAssert;
19  import org.hamcrest.Matchers;
20  import org.junit.jupiter.api.AfterEach;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.api.extension.ExtendWith;
24  import org.mockito.Mockito;
25  
26  /**
27   * Test case for {@link RtRelease}.
28   * @since 0.8
29   */
30  @ExtendWith(RandomPort.class)
31  final class RtReleaseTest {
32      /**
33       * An empty JSON string.
34       */
35      private static final String EMPTY_JSON = "{}";
36  
37      /**
38       * A mock container used in test to mimic the GitHub server.
39       */
40      private transient MkContainer container;
41  
42      /**
43       * Setting up the test fixture.
44       */
45      @BeforeEach
46      void setUp() {
47          this.container = new MkGrizzlyContainer();
48      }
49  
50      /**
51       * Tear down the test fixture to return to the original state.
52       */
53      @AfterEach
54      void tearDown() {
55          this.container.stop();
56      }
57  
58      @Test
59      void editRelease() throws IOException {
60          this.container.next(
61              new MkAnswer.Simple(HttpURLConnection.HTTP_OK, RtReleaseTest.EMPTY_JSON)
62          ).start(RandomPort.port());
63          final RtRelease release = RtReleaseTest.release(this.container.home());
64          final JsonObject json = Json.createObjectBuilder()
65              .add("tag_name", "v1.0.0")
66              .build();
67          release.patch(json);
68          final MkQuery query = this.container.take();
69          MatcherAssert.assertThat(
70              "Values are not equal",
71              query.method(),
72              Matchers.equalTo(Request.PATCH)
73          );
74          MatcherAssert.assertThat(
75              "Values are not equal",
76              query.body(),
77              Matchers.equalTo(json.toString())
78          );
79      }
80  
81      @Test
82      void deleteRelease() throws IOException {
83          this.container.next(
84              new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, RtReleaseTest.EMPTY_JSON)
85          ).start(RandomPort.port());
86          final RtRelease release = RtReleaseTest.release(this.container.home());
87          release.delete();
88          MatcherAssert.assertThat(
89              "Values are not equal",
90              this.container.take().method(),
91              Matchers.equalTo(Request.DELETE)
92          );
93      }
94  
95      @Test
96      void executePatchRequest() throws IOException {
97          this.container.next(
98              new MkAnswer.Simple(HttpURLConnection.HTTP_OK, RtReleaseTest.EMPTY_JSON)
99          ).start(RandomPort.port());
100         final RtRelease release = RtReleaseTest.release(this.container.home());
101         release.patch(Json.createObjectBuilder().add("name", "v1")
102             .build()
103         );
104         MatcherAssert.assertThat(
105             "Values are not equal",
106             this.container.take().method(),
107             Matchers.equalTo(Request.PATCH)
108         );
109     }
110 
111     /**
112      * Create a test release.
113      * @param uri REST API entry point.
114      * @return A test release.
115      */
116     private static RtRelease release(final URI uri) {
117         final Repo repo = Mockito.mock(Repo.class);
118         final Coordinates coords = Mockito.mock(Coordinates.class);
119         Mockito.doReturn(coords).when(repo).coordinates();
120         Mockito.doReturn("tstuser").when(coords).user();
121         Mockito.doReturn("tstbranch").when(coords).repo();
122         return new RtRelease(
123             new ApacheRequest(uri),
124             repo,
125             2
126         );
127     }
128 
129 }