1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.github;
31
32 import com.jcabi.aspects.Immutable;
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.mock.MkQuery;
38 import com.jcabi.http.request.ApacheRequest;
39 import com.jcabi.http.request.FakeRequest;
40 import java.io.InputStream;
41 import java.net.HttpURLConnection;
42 import java.nio.charset.StandardCharsets;
43 import javax.json.Json;
44 import javax.ws.rs.core.HttpHeaders;
45 import org.apache.commons.io.IOUtils;
46 import org.hamcrest.MatcherAssert;
47 import org.hamcrest.Matchers;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.mockito.Mockito;
51
52
53
54
55
56
57
58 @Immutable
59 public final class RtContentTest {
60
61
62
63
64
65 @Rule
66 public final transient RandomPort resource = new RandomPort();
67
68
69
70
71
72
73 @Test
74 public void fetchContentAsJson() throws Exception {
75 final RtContent content = new RtContent(
76 new FakeRequest().withBody("{\"content\":\"json\"}"),
77 this.repo(),
78 "blah"
79 );
80 MatcherAssert.assertThat(
81 content.json().getString("content"),
82 Matchers.equalTo("json")
83 );
84 }
85
86
87
88
89
90
91 @Test
92 public void patchWithJson() throws Exception {
93 try (final MkContainer container = new MkGrizzlyContainer().next(
94 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
95 ).start(this.resource.port())) {
96 final RtContent content = new RtContent(
97 new ApacheRequest(container.home()),
98 this.repo(),
99 "path"
100 );
101 content.patch(
102 Json.createObjectBuilder().add("patch", "test").build()
103 );
104 final MkQuery query = container.take();
105 MatcherAssert.assertThat(
106 query.method(),
107 Matchers.equalTo(Request.PATCH)
108 );
109 MatcherAssert.assertThat(
110 query.body(),
111 Matchers.equalTo("{\"patch\":\"test\"}")
112 );
113 }
114 }
115
116
117
118
119
120 @Test
121 public void canCompareInstances() {
122 final RtContent less = new RtContent(
123 new FakeRequest(),
124 this.repo(),
125 "aaa"
126 );
127 final RtContent greater = new RtContent(
128 new FakeRequest(),
129 this.repo(),
130 "zzz"
131 );
132 MatcherAssert.assertThat(
133 less.compareTo(greater), Matchers.lessThan(0)
134 );
135 MatcherAssert.assertThat(
136 greater.compareTo(less), Matchers.greaterThan(0)
137 );
138 MatcherAssert.assertThat(
139 greater.compareTo(greater), Matchers.is(0)
140 );
141 }
142
143
144
145
146
147
148 @Test
149 public void fetchesRawContent() throws Exception {
150 final String raw = "the raw \u20ac";
151 try (final MkContainer container = new MkGrizzlyContainer().next(
152 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, raw)
153 ).start(this.resource.port())) {
154 final InputStream stream = new RtContent(
155 new ApacheRequest(container.home()),
156 this.repo(),
157 "raw"
158 ).raw();
159 MatcherAssert.assertThat(
160 IOUtils.toString(stream, StandardCharsets.UTF_8),
161 Matchers.is(raw)
162 );
163 MatcherAssert.assertThat(
164 container.take().headers().get(HttpHeaders.ACCEPT).get(0),
165 Matchers.is("application/vnd.github.v3.raw")
166 );
167 }
168 }
169
170
171
172
173
174 private Repo repo() {
175 final Repo repo = Mockito.mock(Repo.class);
176 final Coordinates coords = Mockito.mock(Coordinates.class);
177 Mockito.doReturn(coords).when(repo).coordinates();
178 Mockito.doReturn("user").when(coords).user();
179 Mockito.doReturn("repo").when(coords).repo();
180 return repo;
181 }
182 }