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.mock.MkQuery;
37  import com.jcabi.http.request.JdkRequest;
38  import java.net.HttpURLConnection;
39  import javax.json.Json;
40  import javax.json.JsonObject;
41  import org.hamcrest.MatcherAssert;
42  import org.hamcrest.Matchers;
43  import org.junit.Rule;
44  import org.junit.Test;
45  import org.mockito.Mockito;
46  
47  /**
48   * Test case for {@link RtLabels}.
49   *
50   * @author Giang Le (giang@vn-smartsolutions.com)
51   * @version $Id: 9e83ad66d28880aa6493364e4910915bfafc79b4 $
52   */
53  public final class RtLabelsTest {
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       * RtLabels can create a label.
63       * @throws Exception if some problem inside
64       */
65      @Test
66      public void createLabel() throws Exception {
67          final String name = "API";
68          final String color = "FFFFFF";
69          final String body = label(name, color).toString();
70          try (
71              final MkContainer container = new MkGrizzlyContainer().next(
72                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
73              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
74                  .start(this.resource.port())
75          ) {
76              final RtLabels labels = new RtLabels(
77                  new JdkRequest(container.home()),
78                  repo()
79              );
80              final Label label = labels.create(name, color);
81              MatcherAssert.assertThat(
82                  container.take().method(),
83                  Matchers.equalTo(Request.POST)
84              );
85              MatcherAssert.assertThat(
86                  new Label.Smart(label).name(),
87                  Matchers.equalTo(name)
88              );
89              MatcherAssert.assertThat(
90                  new Label.Smart(label).color(),
91                  Matchers.equalTo(color)
92              );
93              container.stop();
94          }
95      }
96  
97      /**
98       * RtLabels can get a single label.
99       *
100      * @throws Exception if some problem inside
101      */
102     @Test
103     public void getSingleLabel() throws Exception {
104         final String name = "bug";
105         final String color = "f29513";
106         try (
107             final MkContainer container = new MkGrizzlyContainer().next(
108                 new MkAnswer.Simple(
109                     HttpURLConnection.HTTP_OK,
110                     label(name, color).toString()
111                 )
112             ).start(this.resource.port())
113         ) {
114             final RtLabels issues = new RtLabels(
115                 new JdkRequest(container.home()),
116                 repo()
117             );
118             final Label label = issues.get(name);
119             MatcherAssert.assertThat(
120                 new Label.Smart(label).color(),
121                 Matchers.equalTo(color)
122             );
123             container.stop();
124         }
125     }
126 
127     /**
128      * RtLabels can delete a label.
129      * @throws Exception if some problem inside
130      */
131     @Test
132     public void deleteLabel() throws Exception {
133         try (
134             final MkContainer container = new MkGrizzlyContainer().next(
135                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
136             ).start(this.resource.port())
137         ) {
138             final RtLabels issues = new RtLabels(
139                 new JdkRequest(container.home()),
140                 repo()
141             );
142             issues.delete("issue");
143             final MkQuery query = container.take();
144             MatcherAssert.assertThat(
145                 query.method(),
146                 Matchers.equalTo(Request.DELETE)
147             );
148             MatcherAssert.assertThat(
149                 query.body(),
150                 Matchers.isEmptyOrNullString()
151             );
152             container.stop();
153         }
154     }
155 
156     /**
157      * RtLabels can iterate labels.
158      * @throws Exception if there is any error
159      */
160     @Test
161     public void iterateLabels() throws Exception {
162         try (
163             final MkContainer container = new MkGrizzlyContainer().next(
164                 new MkAnswer.Simple(
165                     HttpURLConnection.HTTP_OK,
166                     Json.createArrayBuilder()
167                         .add(label("new issue", "f29512"))
168                         .add(label("new bug", "f29522"))
169                         .build().toString()
170                 )
171             ).start(this.resource.port())
172         ) {
173             final RtLabels labels = new RtLabels(
174                 new JdkRequest(container.home()),
175                 repo()
176             );
177             MatcherAssert.assertThat(
178                 labels.iterate(),
179                 Matchers.<Label>iterableWithSize(2)
180             );
181             container.stop();
182         }
183     }
184 
185     /**
186      * Create and return JsonObject to test.
187      * @param name The name of the label
188      * @param color A 6 character hex code, identifying the color
189      * @return JsonObject
190      * @throws Exception If some problem inside
191      */
192     private static JsonObject label(
193         final String name, final String color) throws Exception {
194         return Json.createObjectBuilder()
195             .add("name", name)
196             .add("color", color)
197             .build();
198     }
199 
200     /**
201      * Create and return repo to test.
202      * @return Repo
203      * @throws Exception If some problem inside
204      */
205     private static Repo repo() throws Exception {
206         final Repo repo = Mockito.mock(Repo.class);
207         Mockito.doReturn(new Coordinates.Simple("mark", "test"))
208             .when(repo).coordinates();
209         return repo;
210     }
211 }