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.ws.rs.core.UriBuilder;
14  import jakarta.ws.rs.core.UriBuilderException;
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 RtStars}.
25   * @since 0.8
26   */
27  @ExtendWith(RandomPort.class)
28  final class RtStarsTest {
29  
30      /**
31       * The rule for skipping test if there's BindException.
32       * @checkstyle VisibilityModifierCheck (3 lines)
33       */
34      @Test
35      void checkIfRepoStarred() throws IOException {
36          try (
37              MkContainer container = new MkGrizzlyContainer().next(
38                  new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
39              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_NOT_FOUND))
40                  .start(RandomPort.port())
41          ) {
42              final Stars starred = new RtStars(
43                  new ApacheRequest(container.home()),
44                  RtStarsTest.repo("someuser", "starredrepo")
45              );
46              MatcherAssert.assertThat(
47                  "Values are not equal",
48                  starred.starred(), Matchers.is(true)
49              );
50              final Stars unstarred = new RtStars(
51                  new ApacheRequest(container.home()),
52                  RtStarsTest.repo("otheruser", "notstarredrepo")
53              );
54              MatcherAssert.assertThat(
55                  "Values are not equal",
56                  unstarred.starred(), Matchers.is(false)
57              );
58              container.stop();
59          }
60      }
61  
62      @Test
63      @SuppressWarnings("PMD.AvoidUncheckedExceptionsInSignatures")
64      void starRepository() throws IOException, IllegalArgumentException, UriBuilderException {
65          try (
66              MkContainer container = new MkGrizzlyContainer().next(
67                  new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
68              ).start(RandomPort.port())
69          ) {
70              final String user = "staruser";
71              final String repo = "starrepo";
72              final Stars stars = new RtStars(
73                  new ApacheRequest(container.home()),
74                  RtStarsTest.repo(user, repo)
75              );
76              stars.star();
77              final MkQuery query = container.take();
78              MatcherAssert.assertThat(
79                  "Values are not equal",
80                  query.method(),
81                  Matchers.equalTo(Request.PUT)
82              );
83              MatcherAssert.assertThat(
84                  "String does not contain expected value",
85                  query.uri().getPath(),
86                  Matchers.containsString(
87                      UriBuilder.fromPath(user)
88                          .path(repo)
89                          .build()
90                          .getPath()
91                  )
92              );
93              container.stop();
94          }
95      }
96  
97      @Test
98      @SuppressWarnings("PMD.AvoidUncheckedExceptionsInSignatures")
99      void unstarRepository()
100         throws IOException, IllegalArgumentException, UriBuilderException {
101         try (
102             MkContainer container = new MkGrizzlyContainer().next(
103                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
104             ).start(RandomPort.port())
105         ) {
106             final String user = "unstaruser";
107             final String repo = "unstarrepo";
108             final Stars stars = new RtStars(
109                 new ApacheRequest(container.home()),
110                 RtStarsTest.repo(user, repo)
111             );
112             stars.unstar();
113             final MkQuery query = container.take();
114             MatcherAssert.assertThat(
115                 "Values are not equal",
116                 query.method(),
117                 Matchers.equalTo(Request.DELETE)
118             );
119             MatcherAssert.assertThat(
120                 "String does not contain expected value",
121                 query.uri().getPath(),
122                 Matchers.containsString(
123                     UriBuilder.fromPath(user)
124                         .path(repo)
125                         .build()
126                         .getPath()
127                 )
128             );
129             container.stop();
130         }
131     }
132 
133     /**
134      * Create and return repo for testing.
135      * @param user User
136      * @param reponame Repository
137      * @return Repo
138      */
139     private static Repo repo(final String user, final String reponame) {
140         final Repo repo = Mockito.mock(Repo.class);
141         Mockito.doReturn(new Coordinates.Simple(user, reponame))
142             .when(repo).coordinates();
143         return repo;
144     }
145 }