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.github.mock.MkGithub;
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 java.net.HttpURLConnection;
40 import java.util.Collections;
41 import org.hamcrest.MatcherAssert;
42 import org.hamcrest.Matchers;
43 import org.junit.Rule;
44 import org.junit.Test;
45
46
47
48
49
50
51
52 public final class RtGistsTest {
53
54
55
56
57
58 @Rule
59 public final transient RandomPort resource = new RandomPort();
60
61
62
63
64
65
66 @Test
67 public void canCreateFiles() throws Exception {
68 try (
69 final MkContainer container = new MkGrizzlyContainer().next(
70 new MkAnswer.Simple(
71 HttpURLConnection.HTTP_CREATED,
72 "{\"id\":\"1\"}"
73 )
74 ).start(this.resource.port())) {
75 final Gists gists = new RtGists(
76 new MkGithub(),
77 new ApacheRequest(container.home())
78 );
79 MatcherAssert.assertThat(
80 gists.create(Collections.singletonMap("test", ""), false),
81 Matchers.notNullValue()
82 );
83 MatcherAssert.assertThat(
84 container.take().body(),
85 Matchers.startsWith("{\"files\":{\"test\":{\"content\":")
86 );
87 container.stop();
88 }
89 }
90
91
92
93
94
95
96 @Test
97 public void canRetrieveSpecificGist() throws Exception {
98 try (
99 final MkContainer container = new MkGrizzlyContainer().next(
100 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "testing")
101 ).start(this.resource.port())
102 ) {
103 final Gists gists = new RtGists(
104 new MkGithub(),
105 new ApacheRequest(container.home())
106 );
107 MatcherAssert.assertThat(
108 gists.get("gist"),
109 Matchers.notNullValue()
110 );
111 container.stop();
112 }
113 }
114
115
116
117
118
119
120 @Test
121 public void canIterateThrouRtGists() throws Exception {
122 try (
123 final MkContainer container = new MkGrizzlyContainer().next(
124 new MkAnswer.Simple(
125 HttpURLConnection.HTTP_OK,
126 "[{\"id\":\"hello\"}]"
127 )
128 ).start(this.resource.port())
129 ) {
130 final Gists gists = new RtGists(
131 new MkGithub(),
132 new ApacheRequest(container.home())
133 );
134 MatcherAssert.assertThat(
135 gists.iterate().iterator().next(),
136 Matchers.notNullValue()
137 );
138 container.stop();
139 }
140 }
141
142
143
144
145 @Test
146 public void removesGistByName() throws Exception {
147 try (
148 final MkContainer container = new MkGrizzlyContainer().next(
149 new MkAnswer.Simple(
150 HttpURLConnection.HTTP_NO_CONTENT,
151 ""
152 )
153 ).start(this.resource.port())) {
154 final Gists gists = new RtGists(
155 new MkGithub(),
156 new ApacheRequest(container.home())
157 );
158 gists.remove("12234");
159 final MkQuery query = container.take();
160 MatcherAssert.assertThat(
161 query.method(),
162 Matchers.equalTo(Request.DELETE)
163 );
164 container.stop();
165 }
166 }
167 }