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 java.net.HttpURLConnection;
38  import javax.json.Json;
39  import javax.json.JsonObject;
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 RtGistComments}.
48   *
49   * @author Giang Le (giang@vn-smartsolutions.com)
50   * @version $Id: 499bf7a537f46ce6f012badcf9d87b6255bbc304 $
51   */
52  public final class RtGistCommentsTest {
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       * RtGistComments can get a single comment.
63       * @throws Exception if some problem inside
64       */
65      @Test
66      public void getComment() throws Exception {
67          final String body = "Just commenting";
68          try (
69              final MkContainer container = new MkGrizzlyContainer().next(
70                  new MkAnswer.Simple(
71                      HttpURLConnection.HTTP_OK,
72                      comment(body).toString()
73                  )
74              ).start(this.resource.port())) {
75              final Gist gist = Mockito.mock(Gist.class);
76              Mockito.doReturn("1").when(gist).identifier();
77              final RtGistComments comments = new RtGistComments(
78                  new JdkRequest(container.home()),
79                  gist
80              );
81              final GistComment comment = comments.get(1);
82              MatcherAssert.assertThat(
83                  new GistComment.Smart(comment).body(),
84                  Matchers.equalTo(body)
85              );
86          }
87      }
88  
89      /**
90       * RtGistComments can iterate comments.
91       * @throws Exception if there is any error
92       */
93      @Test
94      public void iterateComments() throws Exception {
95          try (final MkContainer container = new MkGrizzlyContainer().next(
96              new MkAnswer.Simple(
97                  HttpURLConnection.HTTP_OK,
98                  Json.createArrayBuilder()
99                      .add(comment("comment 1"))
100                     .add(comment("comment 2"))
101                     .build().toString()
102             )
103         ).start(this.resource.port())) {
104             final Gist gist = Mockito.mock(Gist.class);
105             Mockito.doReturn("2").when(gist).identifier();
106             final RtGistComments comments = new RtGistComments(
107                 new JdkRequest(container.home()),
108                 gist
109             );
110             MatcherAssert.assertThat(
111                 comments.iterate(),
112                 Matchers.<GistComment>iterableWithSize(2)
113             );
114         }
115     }
116 
117     /**
118      * RtGistComments can create a comment.
119      * @throws Exception if there is any error
120      */
121     @Test
122     public void postComment() throws Exception {
123         final String body = "new commenting";
124         final MkAnswer answer = new MkAnswer.Simple(
125             HttpURLConnection.HTTP_OK,
126             comment(body).toString()
127         );
128         try (final MkContainer container = new MkGrizzlyContainer().next(
129             new MkAnswer.Simple(
130                 HttpURLConnection.HTTP_CREATED,
131                 comment(body).toString()
132             )
133         ).next(answer).start(this.resource.port())) {
134             final Gist gist = Mockito.mock(Gist.class);
135             Mockito.doReturn("3").when(gist).identifier();
136             final RtGistComments comments = new RtGistComments(
137                 new JdkRequest(container.home()),
138                 gist
139             );
140             final GistComment comment = comments.post(body);
141             MatcherAssert.assertThat(
142                 container.take().method(),
143                 Matchers.equalTo(Request.POST)
144             );
145             MatcherAssert.assertThat(
146                 new GistComment.Smart(comment).body(),
147                 Matchers.equalTo(body)
148             );
149         }
150     }
151 
152     /**
153      * Create and return JsonObject to test.
154      * @param body The body of the comment
155      * @return JsonObject
156      * @throws Exception If some problem inside
157      */
158     private static JsonObject comment(final String body) throws Exception {
159         return Json.createObjectBuilder()
160             .add("id", 1)
161             .add("body", body)
162             .build();
163     }
164 }