1
2
3
4
5 package com.jcabi.github;
6
7 import com.jcabi.aspects.Immutable;
8 import com.jcabi.http.Request;
9 import com.jcabi.http.mock.MkAnswer;
10 import com.jcabi.http.mock.MkContainer;
11 import com.jcabi.http.mock.MkGrizzlyContainer;
12 import com.jcabi.http.mock.MkQuery;
13 import com.jcabi.http.request.ApacheRequest;
14 import com.jcabi.http.request.FakeRequest;
15 import jakarta.json.Json;
16 import jakarta.ws.rs.core.HttpHeaders;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.HttpURLConnection;
20 import java.nio.charset.StandardCharsets;
21 import org.apache.commons.io.IOUtils;
22 import org.hamcrest.MatcherAssert;
23 import org.hamcrest.Matchers;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mockito;
27
28
29
30
31
32 @Immutable
33 @ExtendWith(RandomPort.class)
34 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
35 final class RtContentTest {
36
37
38
39
40
41 @Test
42 void fetchContentAsJson() throws IOException {
43 final RtContent content = new RtContent(
44 new FakeRequest().withBody("{\"content\":\"json\"}"),
45 RtContentTest.repo(),
46 "blah"
47 );
48 MatcherAssert.assertThat(
49 "Values are not equal",
50 content.json().getString("content"),
51 Matchers.equalTo("json")
52 );
53 }
54
55 @Test
56 void patchWithJson() throws IOException {
57 try (MkContainer container = new MkGrizzlyContainer().next(
58 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
59 ).start(RandomPort.port())) {
60 final RtContent content = new RtContent(
61 new ApacheRequest(container.home()),
62 RtContentTest.repo(),
63 "path"
64 );
65 content.patch(
66 Json.createObjectBuilder().add("patch", "test").build()
67 );
68 final MkQuery query = container.take();
69 MatcherAssert.assertThat(
70 "Values are not equal",
71 query.method(),
72 Matchers.equalTo(Request.PATCH)
73 );
74 MatcherAssert.assertThat(
75 "Values are not equal",
76 query.body(),
77 Matchers.equalTo("{\"patch\":\"test\"}")
78 );
79 }
80 }
81
82 @Test
83 void canCompareInstances() {
84 final RtContent less = new RtContent(
85 new FakeRequest(),
86 RtContentTest.repo(),
87 "aaa"
88 );
89 final RtContent greater = new RtContent(
90 new FakeRequest(),
91 RtContentTest.repo(),
92 "zzz"
93 );
94 MatcherAssert.assertThat(
95 "Value is not less than expected",
96 less.compareTo(greater), Matchers.lessThan(0)
97 );
98 MatcherAssert.assertThat(
99 "Value is not greater than expected",
100 greater.compareTo(less), Matchers.greaterThan(0)
101 );
102 MatcherAssert.assertThat(
103 "Values are not equal",
104 greater.compareTo(greater), Matchers.is(0)
105 );
106 }
107
108 @Test
109 void fetchesRawContent() throws IOException {
110 final String raw = "the raw \u20ac";
111 try (MkContainer container = new MkGrizzlyContainer().next(
112 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, raw)
113 ).start(RandomPort.port())) {
114 try (
115 InputStream stream = new RtContent(
116 new ApacheRequest(container.home()),
117 RtContentTest.repo(),
118 "raw"
119 ).raw()
120 ) {
121 MatcherAssert.assertThat(
122 "Values are not equal",
123 IOUtils.toString(stream, StandardCharsets.UTF_8),
124 Matchers.is(raw)
125 );
126 }
127 MatcherAssert.assertThat(
128 "Values are not equal",
129 container.take().headers().get(HttpHeaders.ACCEPT).get(0),
130 Matchers.is("application/vnd.github.v3.raw")
131 );
132 }
133 }
134
135
136
137
138
139 private static Repo repo() {
140 final Repo repo = Mockito.mock(Repo.class);
141 final Coordinates coords = Mockito.mock(Coordinates.class);
142 Mockito.doReturn(coords).when(repo).coordinates();
143 Mockito.doReturn("user").when(coords).user();
144 Mockito.doReturn("repo").when(coords).repo();
145 return repo;
146 }
147 }