View Javadoc
1   /**
2    * Copyright (c) 2013-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.jcabi.http.Request;
33  import com.jcabi.http.mock.MkAnswer;
34  import com.jcabi.http.mock.MkContainer;
35  import com.jcabi.http.mock.MkGrizzlyContainer;
36  import com.jcabi.http.mock.MkQuery;
37  import com.jcabi.http.request.ApacheRequest;
38  import java.net.HttpURLConnection;
39  import javax.ws.rs.core.UriBuilder;
40  import org.hamcrest.MatcherAssert;
41  import org.hamcrest.Matchers;
42  import org.junit.Rule;
43  import org.junit.Test;
44  import org.mockito.Mockito;
45  
46  /**
47   * Test case for {@link RtStars}.
48   *
49   * @author Paul Polishchuk (ppol@ua.fm)
50   * @author Paulo Lobo (pauloeduardolobo@gmail.com)
51   * @version $Id: a36e50e5b5802404361cf7c499dcc5b6922df03e $
52   */
53  public final class RtStarsTest {
54  
55      /**
56       * The rule for skipping test if there's BindException.
57       * @checkstyle VisibilityModifierCheck (3 lines)
58       */
59      @Rule
60      public final transient RandomPort resource = new RandomPort();
61  
62      /**
63       * RtStars can check if repo is starred.
64       *
65       * @throws Exception If something goes wrong.
66       */
67      @Test
68      public void checkIfRepoStarred() throws Exception {
69          try (
70              final MkContainer container = new MkGrizzlyContainer().next(
71                      new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
72                  ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_NOT_FOUND))
73                  .start(this.resource.port())
74          ) {
75              final Stars starred = new RtStars(
76                  new ApacheRequest(container.home()),
77                  RtStarsTest.repo("someuser", "starredrepo")
78              );
79              MatcherAssert.assertThat(
80                  starred.starred(), Matchers.is(true)
81              );
82              final Stars unstarred = new RtStars(
83                  new ApacheRequest(container.home()),
84                  RtStarsTest.repo("otheruser", "notstarredrepo")
85              );
86              MatcherAssert.assertThat(
87                  unstarred.starred(), Matchers.is(false)
88              );
89              container.stop();
90          }
91      }
92  
93      /**
94       * RtStars can star repository.
95       *
96       * @throws Exception If something goes wrong.
97       */
98      @Test
99      public void starRepository() throws Exception {
100         try (
101             final MkContainer container = new MkGrizzlyContainer().next(
102                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
103             ).start(this.resource.port());
104         ) {
105             final String user = "staruser";
106             final String repo = "starrepo";
107             final Stars stars = new RtStars(
108                 new ApacheRequest(container.home()),
109                 RtStarsTest.repo(user, repo)
110             );
111             stars.star();
112             final MkQuery query = container.take();
113             MatcherAssert.assertThat(
114                 query.method(),
115                 Matchers.equalTo(Request.PUT)
116             );
117             MatcherAssert.assertThat(
118                 query.uri().getPath(),
119                 Matchers.containsString(
120                     UriBuilder.fromPath(user)
121                         .path(repo)
122                         .build()
123                         .getPath()
124                 )
125             );
126             container.stop();
127         }
128     }
129 
130     /**
131      * RtStars can unstar repository.
132      *
133      * @throws Exception If something goes wrong.
134      */
135     @Test
136     public void unstarRepository() throws Exception {
137         try (
138             final MkContainer container = new MkGrizzlyContainer().next(
139                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
140             ).start(this.resource.port())
141         ) {
142             final String user = "unstaruser";
143             final String repo = "unstarrepo";
144             final Stars stars = new RtStars(
145                 new ApacheRequest(container.home()),
146                 RtStarsTest.repo(user, repo)
147             );
148             stars.unstar();
149             final MkQuery query = container.take();
150             MatcherAssert.assertThat(
151                 query.method(),
152                 Matchers.equalTo(Request.DELETE)
153             );
154             MatcherAssert.assertThat(
155                 query.uri().getPath(),
156                 Matchers.containsString(
157                     UriBuilder.fromPath(user)
158                         .path(repo)
159                         .build()
160                         .getPath()
161                 )
162             );
163             container.stop();
164         }
165     }
166 
167     /**
168      * Create and return repo for testing.
169      * @param user User
170      * @param reponame Repository
171      * @return Repo
172      */
173     private static Repo repo(final String user, final String reponame) {
174         final Repo repo = Mockito.mock(Repo.class);
175         Mockito.doReturn(new Coordinates.Simple(user, reponame))
176             .when(repo).coordinates();
177         return repo;
178     }
179 }