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.mock.MkQuery;
37 import com.jcabi.http.request.JdkRequest;
38 import java.net.HttpURLConnection;
39 import javax.json.Json;
40 import javax.json.JsonObject;
41 import org.hamcrest.MatcherAssert;
42 import org.hamcrest.Matchers;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.mockito.Mockito;
46
47
48
49
50
51
52
53 public final class RtLabelsTest {
54
55
56
57
58 @Rule
59 public final transient RandomPort resource = new RandomPort();
60
61
62
63
64
65 @Test
66 public void createLabel() throws Exception {
67 final String name = "API";
68 final String color = "FFFFFF";
69 final String body = label(name, color).toString();
70 try (
71 final MkContainer container = new MkGrizzlyContainer().next(
72 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
73 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
74 .start(this.resource.port())
75 ) {
76 final RtLabels labels = new RtLabels(
77 new JdkRequest(container.home()),
78 repo()
79 );
80 final Label label = labels.create(name, color);
81 MatcherAssert.assertThat(
82 container.take().method(),
83 Matchers.equalTo(Request.POST)
84 );
85 MatcherAssert.assertThat(
86 new Label.Smart(label).name(),
87 Matchers.equalTo(name)
88 );
89 MatcherAssert.assertThat(
90 new Label.Smart(label).color(),
91 Matchers.equalTo(color)
92 );
93 container.stop();
94 }
95 }
96
97
98
99
100
101
102 @Test
103 public void getSingleLabel() throws Exception {
104 final String name = "bug";
105 final String color = "f29513";
106 try (
107 final MkContainer container = new MkGrizzlyContainer().next(
108 new MkAnswer.Simple(
109 HttpURLConnection.HTTP_OK,
110 label(name, color).toString()
111 )
112 ).start(this.resource.port())
113 ) {
114 final RtLabels issues = new RtLabels(
115 new JdkRequest(container.home()),
116 repo()
117 );
118 final Label label = issues.get(name);
119 MatcherAssert.assertThat(
120 new Label.Smart(label).color(),
121 Matchers.equalTo(color)
122 );
123 container.stop();
124 }
125 }
126
127
128
129
130
131 @Test
132 public void deleteLabel() throws Exception {
133 try (
134 final MkContainer container = new MkGrizzlyContainer().next(
135 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
136 ).start(this.resource.port())
137 ) {
138 final RtLabels issues = new RtLabels(
139 new JdkRequest(container.home()),
140 repo()
141 );
142 issues.delete("issue");
143 final MkQuery query = container.take();
144 MatcherAssert.assertThat(
145 query.method(),
146 Matchers.equalTo(Request.DELETE)
147 );
148 MatcherAssert.assertThat(
149 query.body(),
150 Matchers.is(Matchers.emptyOrNullString())
151 );
152 container.stop();
153 }
154 }
155
156
157
158
159
160 @Test
161 public void iterateLabels() throws Exception {
162 try (
163 final MkContainer container = new MkGrizzlyContainer().next(
164 new MkAnswer.Simple(
165 HttpURLConnection.HTTP_OK,
166 Json.createArrayBuilder()
167 .add(label("new issue", "f29512"))
168 .add(label("new bug", "f29522"))
169 .build().toString()
170 )
171 ).start(this.resource.port())
172 ) {
173 final RtLabels labels = new RtLabels(
174 new JdkRequest(container.home()),
175 repo()
176 );
177 MatcherAssert.assertThat(
178 labels.iterate(),
179 Matchers.<Label>iterableWithSize(2)
180 );
181 container.stop();
182 }
183 }
184
185
186
187
188
189
190
191 private static JsonObject label(
192 final String name, final String color) {
193 return Json.createObjectBuilder()
194 .add("name", name)
195 .add("color", color)
196 .build();
197 }
198
199
200
201
202
203 private static Repo repo() {
204 final Repo repo = Mockito.mock(Repo.class);
205 Mockito.doReturn(new Coordinates.Simple("mark", "test"))
206 .when(repo).coordinates();
207 return repo;
208 }
209 }