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.github.mock.MkGithub;
33  import com.jcabi.http.Request;
34  import com.jcabi.http.mock.MkAnswer;
35  import com.jcabi.http.mock.MkContainer;
36  import com.jcabi.http.mock.MkGrizzlyContainer;
37  import com.jcabi.http.request.ApacheRequest;
38  import com.jcabi.http.request.FakeRequest;
39  import java.io.IOException;
40  import java.net.HttpURLConnection;
41  import javax.json.Json;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  
47  /**
48   * Test case for {@link RtGistComment}.
49   * @author Giang Le (giang@vn-smartsolutions.com)
50   * @version $Id: f709d92c044f4fedd09e069fe32f4800b7899de0 $
51   * @checkstyle ClassDataAbstractionCouplingCheck (150 lines)
52   */
53  public class RtGistCommentTest {
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       * RtGistComment can patch comment and return new json.
64       * @throws IOException if has some problems with json parsing.
65       */
66      @Test
67      public final void patchAndCheckJsonGistComment() throws IOException {
68          final int identifier = 1;
69          final String idString = "id";
70          final String bodyString = "body";
71          final String body = "somebody";
72          final String patchedBody = "some patchedbody";
73          final MkAnswer first = new MkAnswer.Simple(
74              HttpURLConnection.HTTP_OK,
75              Json.createObjectBuilder()
76                  .add(bodyString, body)
77                  .add(idString, identifier)
78                  .build().toString()
79          );
80          final MkAnswer second = new MkAnswer.Simple(
81              HttpURLConnection.HTTP_OK,
82              Json.createObjectBuilder()
83                  .add(bodyString, patchedBody)
84                  .add(idString, identifier)
85                  .build().toString()
86          );
87          final MkAnswer third = new MkAnswer.Simple(
88              HttpURLConnection.HTTP_OK,
89              Json.createObjectBuilder()
90                  .add(bodyString, body)
91                  .add(idString, identifier)
92                  .build().toString()
93          );
94          try (
95              final MkContainer container =
96                  new MkGrizzlyContainer().next(first).next(second).next(third)
97                      .start(this.resource.port());
98              final MkContainer gistContainer = new MkGrizzlyContainer()
99                  .start(this.resource.port())) {
100             final RtGist gist =
101                 new RtGist(
102                     new MkGithub(),
103                     new ApacheRequest(gistContainer.home()), "someName"
104                 );
105             final RtGistComment comment = new RtGistComment(
106                 new ApacheRequest(container.home()), gist, identifier
107             );
108             comment.patch(Json.createObjectBuilder()
109                 .add(bodyString, patchedBody)
110                 .add(idString, identifier)
111                 .build()
112             );
113             MatcherAssert.assertThat(
114                 comment.json().getString(bodyString),
115                 Matchers.equalTo(patchedBody)
116             );
117             container.stop();
118             gistContainer.stop();
119         }
120     }
121 
122     /**
123      * RtGistComment can remove comment.
124      * @throws IOException if has some problems with json parsing.
125      */
126     @Test
127     public final void removeGistComment() throws IOException {
128         final int identifier = 1;
129         try (
130             final MkContainer container = new MkGrizzlyContainer().next(
131                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
132             ).start(this.resource.port())) {
133             final RtGist gist = new RtGist(
134                 new MkGithub(),
135                 new FakeRequest().withStatus(HttpURLConnection.HTTP_NO_CONTENT),
136                 "gistName"
137             );
138             final RtGistComment comment = new RtGistComment(
139                 new ApacheRequest(container.home()), gist, identifier
140             );
141             comment.remove();
142             MatcherAssert.assertThat(
143                 container.take().method(),
144                 Matchers.equalTo(Request.DELETE)
145             );
146             container.stop();
147         }
148     }
149 }