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.aspects.Tv;
33  import javax.json.Json;
34  import javax.json.JsonObject;
35  import org.apache.commons.lang3.RandomStringUtils;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  import org.mockito.Mockito;
40  
41  /**
42   * Test case for {@link PullComment}.
43   *
44   * @author Andrej Istomin (andrej.istomin.ikeen@gmail.com)
45   * @version $Id: 1f64be84db9acb8c06e6c34a4c74906d560829b8 $
46   */
47  public final class PullCommentTest {
48  
49      /**
50       * Id field's name in JSON.
51       */
52      private static final String ID = "id";
53  
54      /**
55       * Commit id field's name in JSON.
56       */
57      private static final String COMMIT_ID = "commit_id";
58  
59      /**
60       * Url field's name in JSON.
61       */
62      private static final String URL = "url";
63  
64      /**
65       * Body field's name in JSON.
66       */
67      private static final String BODY = "body";
68  
69      /**
70       * PullComment.Smart can fetch the id value from PullComment.
71       * @throws Exception If a problem occurs.
72       */
73      @Test
74      public void fetchesId() throws Exception {
75          final PullComment comment = Mockito.mock(PullComment.class);
76          final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
77          Mockito.doReturn(
78              Json.createObjectBuilder().add(ID, value).build()
79          ).when(comment).json();
80          MatcherAssert.assertThat(
81              new PullComment.Smart(comment).identifier(),
82              Matchers.is(value)
83          );
84      }
85  
86      /**
87       * PullComment.Smart can update the id value of PullComment.
88       * @throws Exception If a problem occurs.
89       */
90      @Test
91      public void updatesId() throws Exception {
92          final PullComment comment = Mockito.mock(PullComment.class);
93          final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
94          new PullComment.Smart(comment).identifier(value);
95          Mockito.verify(comment).patch(
96              Json.createObjectBuilder().add(ID, value).build()
97          );
98      }
99  
100     /**
101      * PullComment.Smart can fetch the commit id value from PullComment.
102      * @throws Exception If a problem occurs.
103      */
104     @Test
105     public void fetchesCommitId() throws Exception {
106         final PullComment comment = Mockito.mock(PullComment.class);
107         final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
108         Mockito.doReturn(
109             Json.createObjectBuilder().add(COMMIT_ID, value).build()
110         ).when(comment).json();
111         MatcherAssert.assertThat(
112             new PullComment.Smart(comment).commitId(),
113             Matchers.is(value)
114         );
115     }
116 
117     /**
118      * PullComment.Smart can update the commit id value of PullComment.
119      * @throws Exception If a problem occurs.
120      */
121     @Test
122     public void updatesCommitId() throws Exception {
123         final PullComment comment = Mockito.mock(PullComment.class);
124         final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
125         new PullComment.Smart(comment).commitId(value);
126         Mockito.verify(comment).patch(
127             Json.createObjectBuilder().add(COMMIT_ID, value).build()
128         );
129     }
130 
131     /**
132      * PullComment.Smart can fetch the url value from PullComment.
133      * @throws Exception If a problem occurs.
134      */
135     @Test
136     public void fetchesUrl() throws Exception {
137         final PullComment comment = Mockito.mock(PullComment.class);
138         final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
139         Mockito.doReturn(
140             Json.createObjectBuilder().add(URL, value).build()
141         ).when(comment).json();
142         MatcherAssert.assertThat(
143             new PullComment.Smart(comment).url(),
144             Matchers.is(value)
145         );
146     }
147 
148     /**
149      * PullComment.Smart can update the url value of PullComment.
150      * @throws Exception If a problem occurs.
151      */
152     @Test
153     public void updatesUrl() throws Exception {
154         final PullComment comment = Mockito.mock(PullComment.class);
155         final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
156         new PullComment.Smart(comment).url(value);
157         Mockito.verify(comment).patch(
158             Json.createObjectBuilder().add(URL, value).build()
159         );
160     }
161 
162     /**
163      * PullComment.Smart can fetch the body value from PullComment.
164      * @throws Exception If a problem occurs.
165      */
166     @Test
167     public void fetchesBody() throws Exception {
168         final PullComment comment = Mockito.mock(PullComment.class);
169         final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
170         Mockito.doReturn(
171             Json.createObjectBuilder().add(BODY, value).build()
172         ).when(comment).json();
173         MatcherAssert.assertThat(
174             new PullComment.Smart(comment).body(),
175             Matchers.is(value)
176         );
177     }
178 
179     /**
180      * PullComment.Smart can update the body value of PullComment.
181      * @throws Exception If a problem occurs.
182      */
183     @Test
184     public void updatesBody() throws Exception {
185         final PullComment comment = Mockito.mock(PullComment.class);
186         final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
187         new PullComment.Smart(comment).body(value);
188         Mockito.verify(comment).patch(
189             Json.createObjectBuilder().add(BODY, value).build()
190         );
191     }
192 
193     /**
194      * PullComment.Smart can retrieve who is the comment author.
195      * @throws Exception If a problem occurs.
196      */
197     @Test
198     public void retrievesAuthor() throws Exception {
199         final PullComment comment = Mockito.mock(PullComment.class);
200         final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
201         final JsonObject user = Json.createObjectBuilder()
202             .add("login", value).build();
203         Mockito.doReturn(
204             Json.createObjectBuilder().add("user", user).build()
205         ).when(comment).json();
206         MatcherAssert.assertThat(
207             new PullComment.Smart(comment).author(),
208             Matchers.is(value)
209         );
210     }
211 
212 }