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.http.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.request.JdkRequest;
37 import java.net.HttpURLConnection;
38 import javax.json.Json;
39 import javax.json.JsonObject;
40 import org.hamcrest.MatcherAssert;
41 import org.hamcrest.Matchers;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.mockito.Mockito;
45
46
47
48
49
50
51
52 public final class RtGistCommentsTest {
53
54
55
56
57
58 @Rule
59 public final transient RandomPort resource = new RandomPort();
60
61
62
63
64
65 @Test
66 public void getComment() throws Exception {
67 final String body = "Just commenting";
68 try (
69 final MkContainer container = new MkGrizzlyContainer().next(
70 new MkAnswer.Simple(
71 HttpURLConnection.HTTP_OK,
72 comment(body).toString()
73 )
74 ).start(this.resource.port())) {
75 final Gist gist = Mockito.mock(Gist.class);
76 Mockito.doReturn("1").when(gist).identifier();
77 final RtGistComments comments = new RtGistComments(
78 new JdkRequest(container.home()),
79 gist
80 );
81 final GistComment comment = comments.get(1);
82 MatcherAssert.assertThat(
83 new GistComment.Smart(comment).body(),
84 Matchers.equalTo(body)
85 );
86 }
87 }
88
89
90
91
92
93 @Test
94 public void iterateComments() throws Exception {
95 try (final MkContainer container = new MkGrizzlyContainer().next(
96 new MkAnswer.Simple(
97 HttpURLConnection.HTTP_OK,
98 Json.createArrayBuilder()
99 .add(comment("comment 1"))
100 .add(comment("comment 2"))
101 .build().toString()
102 )
103 ).start(this.resource.port())) {
104 final Gist gist = Mockito.mock(Gist.class);
105 Mockito.doReturn("2").when(gist).identifier();
106 final RtGistComments comments = new RtGistComments(
107 new JdkRequest(container.home()),
108 gist
109 );
110 MatcherAssert.assertThat(
111 comments.iterate(),
112 Matchers.<GistComment>iterableWithSize(2)
113 );
114 }
115 }
116
117
118
119
120
121 @Test
122 public void postComment() throws Exception {
123 final String body = "new commenting";
124 final MkAnswer answer = new MkAnswer.Simple(
125 HttpURLConnection.HTTP_OK,
126 comment(body).toString()
127 );
128 try (final MkContainer container = new MkGrizzlyContainer().next(
129 new MkAnswer.Simple(
130 HttpURLConnection.HTTP_CREATED,
131 comment(body).toString()
132 )
133 ).next(answer).start(this.resource.port())) {
134 final Gist gist = Mockito.mock(Gist.class);
135 Mockito.doReturn("3").when(gist).identifier();
136 final RtGistComments comments = new RtGistComments(
137 new JdkRequest(container.home()),
138 gist
139 );
140 final GistComment comment = comments.post(body);
141 MatcherAssert.assertThat(
142 container.take().method(),
143 Matchers.equalTo(Request.POST)
144 );
145 MatcherAssert.assertThat(
146 new GistComment.Smart(comment).body(),
147 Matchers.equalTo(body)
148 );
149 }
150 }
151
152
153
154
155
156
157 private static JsonObject comment(final String body) {
158 return Json.createObjectBuilder()
159 .add("id", 1)
160 .add("body", body)
161 .build();
162 }
163 }