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.mock.MkAnswer;
33  import com.jcabi.http.mock.MkContainer;
34  import com.jcabi.http.mock.MkGrizzlyContainer;
35  import com.jcabi.http.request.JdkRequest;
36  import java.net.HttpURLConnection;
37  import javax.json.Json;
38  import javax.json.JsonValue;
39  import org.hamcrest.MatcherAssert;
40  import org.hamcrest.Matchers;
41  import org.junit.Rule;
42  import org.junit.Test;
43  import org.mockito.Mockito;
44  
45  /**
46   * Test case for {@link RtAssignees}.
47   * @author Paul Polishchuk (ppol@ua.fm)
48   * @version $Id: ad2094e1901ff724a530ca148f657f3deb9a5a48 $
49   * @since 0.7
50   * @checkstyle MultipleStringLiterals (500 lines)
51   */
52  public final class RtAssigneesTest {
53  
54      /**
55       * The rule for skipping test if there's BindException.
56       * @checkstyle VisibilityModifierCheck (3 lines)
57       */
58      @Rule
59      public final transient RandomPort resource = new RandomPort();
60  
61      /**
62       * RtAssignees can iterate over assignees.
63       * @throws Exception Exception If some problem inside
64       */
65      @Test
66      public void iteratesAssignees() throws Exception {
67          try (
68              final MkContainer container = new MkGrizzlyContainer().next(
69                  new MkAnswer.Simple(
70                      HttpURLConnection.HTTP_OK,
71                      Json.createArrayBuilder()
72                          .add(RtAssigneesTest.json("octocat"))
73                          .add(RtAssigneesTest.json("dummy"))
74                          .build().toString()
75                  )
76              ).start(this.resource.port());
77          ) {
78              final Assignees users = new RtAssignees(
79                  new JdkRequest(container.home()),
80                  this.repo()
81              );
82              MatcherAssert.assertThat(
83                  users.iterate(),
84                  Matchers.<User>iterableWithSize(2)
85              );
86          }
87      }
88  
89      /**
90       * RtAssignees can check if user is assignee for this repo.
91       * @throws Exception Exception If some problem inside
92       */
93      @Test
94      public void checkUserIsAssigneeForRepo() throws Exception {
95          try (
96              final MkContainer container = new MkGrizzlyContainer().next(
97                  new MkAnswer.Simple(
98                      HttpURLConnection.HTTP_NO_CONTENT,
99                      Json.createArrayBuilder()
100                         .add(RtAssigneesTest.json("octocat2"))
101                         .add(RtAssigneesTest.json("dummy"))
102                         .build().toString()
103                 )
104             ).start(this.resource.port())) {
105             final Assignees users = new RtAssignees(
106                 new JdkRequest(container.home()),
107                 this.repo()
108             );
109             MatcherAssert.assertThat(
110                 users.check("octocat2"),
111                 Matchers.equalTo(true)
112             );
113         }
114     }
115 
116     /**
117      * RtAssignees can check if user is NOT assignee for this repo.
118      * @throws Exception Exception If some problem inside
119      */
120     @Test
121     public void checkUserIsNotAssigneeForRepo() throws Exception {
122         try (
123             final MkContainer container = new MkGrizzlyContainer().next(
124                 new MkAnswer.Simple(
125                     HttpURLConnection.HTTP_NOT_FOUND,
126                     Json.createArrayBuilder()
127                         .add(RtAssigneesTest.json("octocat3"))
128                         .add(RtAssigneesTest.json("dummy"))
129                         .build().toString()
130                 )
131             ).start(this.resource.port());
132         ) {
133             final Assignees users = new RtAssignees(
134                 new JdkRequest(container.home()),
135                 this.repo()
136             );
137             MatcherAssert.assertThat(
138                 users.check("octocat33"),
139                 Matchers.equalTo(false)
140             );
141         }
142     }
143 
144     /**
145      * Create and return JsonObject to test.
146      * @param login Username to login
147      * @return JsonObject
148      */
149     private static JsonValue json(final String login) {
150         return Json.createObjectBuilder()
151             .add("login", login)
152             .build();
153     }
154 
155     /**
156      * Create and return repo for testing.
157      * @return Repo
158      */
159     private Repo repo() {
160         final Repo repo = Mockito.mock(Repo.class);
161         Mockito.doReturn(new Coordinates.Simple("test", "assignee"))
162             .when(repo).coordinates();
163         Mockito.doReturn(Mockito.mock(Github.class)).when(repo).github();
164         return repo;
165     }
166 }