View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcabi.http.Request;
8   import com.jcabi.http.mock.MkAnswer;
9   import com.jcabi.http.mock.MkContainer;
10  import com.jcabi.http.mock.MkGrizzlyContainer;
11  import com.jcabi.http.request.JdkRequest;
12  import jakarta.json.Json;
13  import jakarta.json.JsonObject;
14  import java.io.IOException;
15  import java.net.HttpURLConnection;
16  import org.hamcrest.MatcherAssert;
17  import org.hamcrest.Matchers;
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.extension.ExtendWith;
20  import org.mockito.Mockito;
21  
22  /**
23   * Test case for {@link RtGistComments}.
24   * @since 0.1
25   */
26  @ExtendWith(RandomPort.class)
27  final class RtGistCommentsTest {
28  
29      /**
30       * The rule for skipping test if there's BindException.
31       * @checkstyle VisibilityModifierCheck (3 lines)
32       */
33      @Test
34      void getComment() throws IOException {
35          final String body = "Just commenting";
36          try (
37              MkContainer container = new MkGrizzlyContainer().next(
38                  new MkAnswer.Simple(
39                      HttpURLConnection.HTTP_OK,
40                      RtGistCommentsTest.comment(body).toString()
41                  )
42              ).start(RandomPort.port())
43          ) {
44              final Gist gist = Mockito.mock(Gist.class);
45              Mockito.doReturn("1").when(gist).identifier();
46              final RtGistComments comments = new RtGistComments(
47                  new JdkRequest(container.home()),
48                  gist
49              );
50              final GistComment comment = comments.get(1);
51              MatcherAssert.assertThat(
52                  "Values are not equal",
53                  new GistComment.Smart(comment).body(),
54                  Matchers.equalTo(body)
55              );
56          }
57      }
58  
59      @Test
60      void iterateComments() throws IOException {
61          try (MkContainer container = new MkGrizzlyContainer().next(
62              new MkAnswer.Simple(
63                  HttpURLConnection.HTTP_OK,
64                  Json.createArrayBuilder()
65                      .add(RtGistCommentsTest.comment("comment 1"))
66                      .add(RtGistCommentsTest.comment("comment 2"))
67                      .build().toString()
68              )
69          ).start(RandomPort.port())) {
70              final Gist gist = Mockito.mock(Gist.class);
71              Mockito.doReturn("2").when(gist).identifier();
72              final RtGistComments comments = new RtGistComments(
73                  new JdkRequest(container.home()),
74                  gist
75              );
76              MatcherAssert.assertThat(
77                  "Collection size is incorrect",
78                  comments.iterate(),
79                  Matchers.iterableWithSize(2)
80              );
81          }
82      }
83  
84      @Test
85      void postComment() throws IOException {
86          final String body = "new commenting";
87          final MkAnswer answer = new MkAnswer.Simple(
88              HttpURLConnection.HTTP_OK,
89              RtGistCommentsTest.comment(body).toString()
90          );
91          try (MkContainer container = new MkGrizzlyContainer().next(
92              new MkAnswer.Simple(
93                  HttpURLConnection.HTTP_CREATED,
94                  RtGistCommentsTest.comment(body).toString()
95              )
96          ).next(answer).start(RandomPort.port())
97          ) {
98              final Gist gist = Mockito.mock(Gist.class);
99              Mockito.doReturn("3").when(gist).identifier();
100             final RtGistComments comments = new RtGistComments(
101                 new JdkRequest(container.home()),
102                 gist
103             );
104             final GistComment comment = comments.post(body);
105             MatcherAssert.assertThat(
106                 "Values are not equal",
107                 container.take().method(),
108                 Matchers.equalTo(Request.POST)
109             );
110             MatcherAssert.assertThat(
111                 "Values are not equal",
112                 new GistComment.Smart(comment).body(),
113                 Matchers.equalTo(body)
114             );
115         }
116     }
117 
118     /**
119      * Create and return JsonObject to test.
120      * @param body The body of the comment
121      * @return JsonObject
122      */
123     private static JsonObject comment(final String body) {
124         return Json.createObjectBuilder()
125             .add("id", 1)
126             .add("body", body)
127             .build();
128     }
129 }