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 jakarta.json.Json;
8   import java.io.IOException;
9   import java.net.MalformedURLException;
10  import java.net.URI;
11  import java.net.URISyntaxException;
12  import java.nio.charset.StandardCharsets;
13  import org.hamcrest.MatcherAssert;
14  import org.hamcrest.Matchers;
15  import org.junit.jupiter.api.Test;
16  import org.mockito.Mockito;
17  
18  /**
19   * Test case for {@link Content}.
20   * @since 0.8
21   * @checkstyle MultipleStringLiterals (500 lines)
22   */
23  @SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"})
24  final class ContentTest {
25      @Test
26      void fetchesType() throws IOException {
27          final Content content = Mockito.mock(Content.class);
28          final String prop = "this is some type";
29          Mockito.doReturn(
30              Json.createObjectBuilder()
31                  .add("type", prop)
32                  .build()
33          ).when(content).json();
34          MatcherAssert.assertThat(
35              "Values are not equal",
36              new Content.Smart(content).type(),
37              Matchers.is(prop)
38          );
39      }
40  
41      @Test
42      void fetchesSize() throws IOException {
43          final Content content = Mockito.mock(Content.class);
44          final int prop = 5555;
45          Mockito.doReturn(
46              Json.createObjectBuilder()
47                  // @checkstyle MagicNumber (1 line)
48                  .add("size", prop)
49                  .build()
50          ).when(content).json();
51          MatcherAssert.assertThat(
52              "Values are not equal",
53              new Content.Smart(content).size(),
54              // @checkstyle MagicNumber (1 line)
55              Matchers.is(prop)
56          );
57      }
58  
59      @Test
60      void fetchesName() throws IOException {
61          final Content content = Mockito.mock(Content.class);
62          final String prop = "this is some name";
63          Mockito.doReturn(
64              Json.createObjectBuilder()
65                  .add("name", prop)
66                  .build()
67          ).when(content).json();
68          MatcherAssert.assertThat(
69              "Values are not equal",
70              new Content.Smart(content).name(),
71              Matchers.is(prop)
72          );
73      }
74  
75      @Test
76      void fetchesPath() {
77          final Content content = Mockito.mock(Content.class);
78          final String path = "this is some path";
79          Mockito.doReturn(path).when(content).path();
80          MatcherAssert.assertThat(
81              "Values are not equal",
82              new Content.Smart(content).path(),
83              Matchers.is(path)
84          );
85      }
86  
87      @Test
88      void fetchesSha() throws IOException {
89          final Content content = Mockito.mock(Content.class);
90          final String prop = "this is some sha";
91          Mockito.doReturn(
92              Json.createObjectBuilder()
93                  .add("sha", prop)
94                  .build()
95          ).when(content).json();
96          MatcherAssert.assertThat(
97              "Values are not equal",
98              new Content.Smart(content).sha(),
99              Matchers.is(prop)
100         );
101     }
102 
103     @Test
104     void fetchesUrl()
105         throws IOException, MalformedURLException, URISyntaxException {
106         final Content content = Mockito.mock(Content.class);
107         final String prop = String.join(
108             "",
109             "https://api.github.com/repos/pengwynn/",
110             "octokit/contents/README.md"
111         );
112         Mockito.doReturn(
113             Json.createObjectBuilder()
114                 .add("url", prop)
115                 .build()
116         ).when(content).json();
117         MatcherAssert.assertThat(
118             "Values are not equal",
119             new Content.Smart(content).url(),
120             Matchers.is(new URI(prop).toURL())
121         );
122     }
123 
124     @Test
125     void fetchesGitUrl()
126         throws IOException, MalformedURLException, URISyntaxException {
127         final Content content = Mockito.mock(Content.class);
128         final String prop = String.join(
129             "",
130             "https://api.github.com/repos/pengwynn/octokit/git/blobs/",
131             "3d21ec53a331a6f037a91c368710b99387d012c1"
132         );
133         Mockito.doReturn(
134             Json.createObjectBuilder()
135                 .add("git_url", prop)
136                 .build()
137         ).when(content).json();
138         MatcherAssert.assertThat(
139             "Values are not equal",
140             new Content.Smart(content).gitUrl(),
141             Matchers.is(new URI(prop).toURL())
142         );
143     }
144 
145     @Test
146     void fetchesHtmlUrl()
147         throws IOException, MalformedURLException, URISyntaxException {
148         final Content content = Mockito.mock(Content.class);
149         final String prop = String.join(
150             "",
151             "https://github.com/pengwynn/octokit/",
152             "blob/master/README.md"
153         );
154         Mockito.doReturn(
155             Json.createObjectBuilder()
156                 .add("html_url", prop)
157                 .build()
158         ).when(content).json();
159         MatcherAssert.assertThat(
160             "Values are not equal",
161             new Content.Smart(content).htmlUrl(),
162             Matchers.is(new URI(prop).toURL())
163         );
164     }
165 
166     @Test
167     void fetchesContent() throws IOException {
168         final Content content = Mockito.mock(Content.class);
169         final String prop = "dGVzdCBlbmNvZGU=";
170         Mockito.doReturn(
171             Json.createObjectBuilder()
172                 .add("content", prop)
173                 .build()
174         ).when(content).json();
175         MatcherAssert.assertThat(
176             "Values are not equal",
177             new Content.Smart(content).content(),
178             Matchers.is(prop)
179         );
180     }
181 
182     @Test
183     void fetchesDecoded() throws IOException {
184         final Content content = Mockito.mock(Content.class);
185         final String prop = "dGVzdCBlbmNvZGXigqw=";
186         Mockito.doReturn(
187             Json.createObjectBuilder()
188                 .add("content", prop)
189                 .build()
190         ).when(content).json();
191         MatcherAssert.assertThat(
192             "Values are not equal",
193             new String(
194                 new Content.Smart(content).decoded(), StandardCharsets.UTF_8
195             ),
196             Matchers.is("test encode\u20ac")
197         );
198     }
199 
200     @Test
201     void smartCanGetUnderlyingRepo() {
202         final Content content = Mockito.mock(Content.class);
203         final Repo repo = Mockito.mock(Repo.class);
204         Mockito.doReturn(repo).when(content).repo();
205         MatcherAssert.assertThat(
206             "Values are not equal",
207             new Content.Smart(content).repo(),
208             Matchers.is(repo)
209         );
210     }
211 }