View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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.request.JdkRequest;
37  import com.jcabi.immutable.ArrayMap;
38  import java.net.HttpURLConnection;
39  import java.util.EnumMap;
40  import javax.json.Json;
41  import javax.json.JsonObject;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  import org.mockito.Mockito;
47  
48  /**
49   * Test case for {@link RtIssues}.
50   *
51   * @author Giang Le (giang@vn-smartsolutions.com)
52   * @version $Id: 6dcb23a1972ed956226d3d1d812c883c4b7596b1 $
53   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
54   */
55  public final class RtIssuesTest {
56  
57      /**
58       * The rule for skipping test if there's BindException.
59       * @checkstyle VisibilityModifierCheck (3 lines)
60       */
61      @Rule
62      public final transient RandomPort resource = new RandomPort();
63  
64      /**
65       * RtIssues can create an issue.
66       *
67       * @throws Exception if some problem inside
68       */
69      @Test
70      public void createIssue() throws Exception {
71          final String title = "Found a bug";
72          final String body = issue(title).toString();
73          try (
74              final MkContainer container = new MkGrizzlyContainer().next(
75                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
76              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
77                  .start(this.resource.port())
78          ) {
79              final RtIssues issues = new RtIssues(
80                  new JdkRequest(container.home()),
81                  repo()
82              );
83              final Issue issue = issues.create(
84                  title, "having a problem with it."
85              );
86              MatcherAssert.assertThat(
87                  container.take().method(),
88                  Matchers.equalTo(Request.POST)
89              );
90              MatcherAssert.assertThat(
91                  new Issue.Smart(issue).title(),
92                  Matchers.equalTo(title)
93              );
94              container.stop();
95          }
96      }
97  
98      /**
99       * RtIssues can get a single issue.
100      * @throws Exception if some problem inside
101      */
102     @Test
103     public void getSingleIssue() throws Exception {
104         final String title = "Unit test";
105         try (
106             final MkContainer container = new MkGrizzlyContainer().next(
107                 new MkAnswer.Simple(
108                     HttpURLConnection.HTTP_OK,
109                     issue(title).toString()
110                 )
111             ).start(this.resource.port())
112         ) {
113             final RtIssues issues = new RtIssues(
114                 new JdkRequest(container.home()),
115                 repo()
116             );
117             final Issue issue = issues.get(1);
118             MatcherAssert.assertThat(
119                 new Issue.Smart(issue).title(),
120                 Matchers.equalTo(title)
121             );
122             container.stop();
123         }
124     }
125 
126     /**
127      * RtIssues can iterate issues.
128      * @throws Exception if there is any error
129      */
130     @Test
131     public void iterateIssues() throws Exception {
132         try (
133             final MkContainer container = new MkGrizzlyContainer().next(
134                 new MkAnswer.Simple(
135                     HttpURLConnection.HTTP_OK,
136                     Json.createArrayBuilder()
137                         .add(issue("new issue"))
138                         .add(issue("code issue"))
139                         .build().toString()
140                 )
141             ).start(this.resource.port())
142         ) {
143             final RtIssues issues = new RtIssues(
144                 new JdkRequest(container.home()),
145                 repo()
146             );
147             MatcherAssert.assertThat(
148                 issues.iterate(new ArrayMap<String, String>()),
149                 Matchers.<Issue>iterableWithSize(2)
150             );
151             container.stop();
152         }
153     }
154 
155     /**
156      * RtIssues can search issues within a repository.
157      * @throws Exception if there is any error
158      */
159     @Test
160     public void searchIssues() throws Exception {
161         try (
162             final MkContainer container = new MkGrizzlyContainer().next(
163                 new MkAnswer.Simple(
164                     HttpURLConnection.HTTP_OK,
165                     Json.createArrayBuilder()
166                         .add(issue("some issue"))
167                         .add(issue("some other issue"))
168                         .build().toString()
169                 )
170             ).start(this.resource.port())
171         ) {
172             final RtIssues issues = new RtIssues(
173                 new JdkRequest(container.home()),
174                 repo()
175             );
176             MatcherAssert.assertThat(
177                 issues.search(
178                     Issues.Sort.UPDATED,
179                     Search.Order.ASC,
180                     new EnumMap<Issues.Qualifier, String>(
181                         Issues.Qualifier.class
182                     )
183                 ),
184                 Matchers.<Issue>iterableWithSize(2)
185             );
186             container.stop();
187         }
188     }
189 
190     /**
191      * Create and return JsonObject to test.
192      * @param title The title of the issue
193      * @return JsonObject
194      * @throws Exception If some problem inside
195      */
196     private static JsonObject issue(final String title) throws Exception {
197         return Json.createObjectBuilder()
198             .add("number", 1)
199             .add("state", Issue.OPEN_STATE)
200             .add("title", title)
201             .build();
202     }
203 
204     /**
205      * Create and return repo to test.
206      * @return Repo
207      * @throws Exception If some problem inside
208      */
209     private static Repo repo() throws Exception {
210         final Repo repo = Mockito.mock(Repo.class);
211         Mockito.doReturn(new Coordinates.Simple("mark", "test"))
212             .when(repo).coordinates();
213         return repo;
214     }
215 }