1
2
3
4
5 package com.jcabi.github;
6
7 import com.jcabi.http.Request;
8 import com.jcabi.http.mock.MkAnswer;
9 import com.jcabi.http.mock.MkContainer;
10 import com.jcabi.http.mock.MkGrizzlyContainer;
11 import com.jcabi.http.mock.MkQuery;
12 import com.jcabi.http.request.JdkRequest;
13 import jakarta.json.Json;
14 import jakarta.json.JsonObject;
15 import java.io.IOException;
16 import java.net.HttpURLConnection;
17 import org.hamcrest.MatcherAssert;
18 import org.hamcrest.Matchers;
19 import org.junit.jupiter.api.Test;
20 import org.junit.jupiter.api.extension.ExtendWith;
21 import org.mockito.Mockito;
22
23
24
25
26
27 @ExtendWith(RandomPort.class)
28 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
29 final class RtLabelsTest {
30
31
32
33
34 @Test
35 void createLabel() throws IOException {
36 final String name = "API";
37 final String color = "FFFFFF";
38 final String body = RtLabelsTest.label(name, color).toString();
39 try (
40 MkContainer container = new MkGrizzlyContainer().next(
41 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
42 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
43 .start(RandomPort.port())
44 ) {
45 final RtLabels labels = new RtLabels(
46 new JdkRequest(container.home()),
47 RtLabelsTest.repo()
48 );
49 final Label label = labels.create(name, color);
50 MatcherAssert.assertThat(
51 "Values are not equal",
52 container.take().method(),
53 Matchers.equalTo(Request.POST)
54 );
55 MatcherAssert.assertThat(
56 "Values are not equal",
57 new Label.Smart(label).name(),
58 Matchers.equalTo(name)
59 );
60 MatcherAssert.assertThat(
61 "Values are not equal",
62 new Label.Smart(label).color(),
63 Matchers.equalTo(color)
64 );
65 container.stop();
66 }
67 }
68
69 @Test
70 void getSingleLabel() throws IOException {
71 final String name = "bug";
72 final String color = "f29513";
73 try (
74 MkContainer container = new MkGrizzlyContainer().next(
75 new MkAnswer.Simple(
76 HttpURLConnection.HTTP_OK,
77 RtLabelsTest.label(name, color).toString()
78 )
79 ).start(RandomPort.port())
80 ) {
81 final RtLabels issues = new RtLabels(
82 new JdkRequest(container.home()),
83 RtLabelsTest.repo()
84 );
85 final Label label = issues.get(name);
86 MatcherAssert.assertThat(
87 "Values are not equal",
88 new Label.Smart(label).color(),
89 Matchers.equalTo(color)
90 );
91 container.stop();
92 }
93 }
94
95 @Test
96 void deleteLabel() throws IOException {
97 try (
98 MkContainer container = new MkGrizzlyContainer().next(
99 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
100 ).start(RandomPort.port())
101 ) {
102 final RtLabels issues = new RtLabels(
103 new JdkRequest(container.home()),
104 RtLabelsTest.repo()
105 );
106 issues.delete("issue");
107 final MkQuery query = container.take();
108 MatcherAssert.assertThat(
109 "Values are not equal",
110 query.method(),
111 Matchers.equalTo(Request.DELETE)
112 );
113 MatcherAssert.assertThat(
114 "Values are not equal",
115 query.body(),
116 Matchers.is(Matchers.emptyOrNullString())
117 );
118 container.stop();
119 }
120 }
121
122 @Test
123 void iterateLabels() throws IOException {
124 try (
125 MkContainer container = new MkGrizzlyContainer().next(
126 new MkAnswer.Simple(
127 HttpURLConnection.HTTP_OK,
128 Json.createArrayBuilder()
129 .add(RtLabelsTest.label("new issue", "f29512"))
130 .add(RtLabelsTest.label("new bug", "f29522"))
131 .build().toString()
132 )
133 ).start(RandomPort.port())
134 ) {
135 final RtLabels labels = new RtLabels(
136 new JdkRequest(container.home()),
137 RtLabelsTest.repo()
138 );
139 MatcherAssert.assertThat(
140 "Collection size is incorrect",
141 labels.iterate(),
142 Matchers.iterableWithSize(2)
143 );
144 container.stop();
145 }
146 }
147
148
149
150
151
152
153
154 private static JsonObject label(
155 final String name, final String color) {
156 return Json.createObjectBuilder()
157 .add("name", name)
158 .add("color", color)
159 .build();
160 }
161
162
163
164
165
166 private static Repo repo() {
167 final Repo repo = Mockito.mock(Repo.class);
168 Mockito.doReturn(new Coordinates.Simple("mark", "test"))
169 .when(repo).coordinates();
170 return repo;
171 }
172 }