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.mock;
31
32 import com.jcabi.github.Coordinates;
33 import java.util.Arrays;
34 import java.util.stream.Collectors;
35 import javax.json.JsonValue;
36 import org.hamcrest.MatcherAssert;
37 import org.hamcrest.collection.IsIterableContainingInAnyOrder;
38 import org.hamcrest.core.IsEqual;
39 import org.junit.Test;
40 import org.xembly.Directives;
41
42
43
44
45
46
47
48
49 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
50 public final class MkHookTest {
51
52
53
54 @Test
55 public void createWithCorrectNumber() {
56 final int number = 5;
57 MatcherAssert.assertThat(
58 "Hook returned wrong number",
59 new MkHook(null, null, null, number).number(),
60 new IsEqual<>(number)
61 );
62 }
63
64
65
66
67
68 @Test
69 public void createWithCorrectRepo() throws Exception {
70 final MkStorage storage = new MkStorage.InFile();
71 final String login = "login";
72 final Coordinates coords = new Coordinates.Simple("user/repo");
73 MatcherAssert.assertThat(
74 "Hook returned wrong repository",
75 new MkHook(storage, login, coords, 0).repo(),
76 new IsEqual<>(new MkRepo(storage, login, coords))
77 );
78 }
79
80
81
82
83
84 @Test
85 public void createWithCorrectId() throws Exception {
86 final int number = 5;
87 final MkStorage storage = new MkStorage.InFile();
88 final Coordinates coords = new Coordinates.Simple("user/repo");
89 storage.apply(
90 this.hookDirs(number, coords)
91 );
92 MatcherAssert.assertThat(
93 "Hook json returned wrong id",
94 new MkHook(storage, "login", coords, number).json().getString("id"),
95 new IsEqual<>(String.valueOf(number))
96 );
97 }
98
99
100
101
102
103 @Test
104 public void createWithCorrectUrl() throws Exception {
105 final String url = "https://github.com/user/repo/hooks/hook/5";
106 final int number = 5;
107 final Coordinates coords = new Coordinates.Simple("user/repo");
108 final MkStorage storage = new MkStorage.InFile();
109 storage.apply(
110 this.hookDirs(number, coords)
111 .add("url").set(url).up()
112 );
113 MatcherAssert.assertThat(
114 "Hook json returned wrong url",
115 new MkHook(
116 storage, "login", coords, number
117 ).json().getString("url"),
118 new IsEqual<>(url)
119 );
120 }
121
122
123
124
125
126 @Test
127 public void createWithCorrectTestUrl() throws Exception {
128 final String test = "https://github.com/user/repo/hooks/hook/5";
129 final int number = 5;
130 final Coordinates coords = new Coordinates.Simple("user/repo");
131 final MkStorage storage = new MkStorage.InFile();
132 storage.apply(
133 this.hookDirs(number, coords)
134 .add("test_url").set(test).up()
135 );
136 MatcherAssert.assertThat(
137 "Hook json returned wrong test_url",
138 new MkHook(
139 storage, "login", coords, number
140 ).json().getString("test_url"),
141 new IsEqual<>(test)
142 );
143 }
144
145
146
147
148
149 @Test
150 public void createWithCorrectPingUrl() throws Exception {
151 final String ping = "https://github.com/user/repo/hooks/hook/5";
152 final int number = 5;
153 final Coordinates coords = new Coordinates.Simple("user/repo");
154 final MkStorage storage = new MkStorage.InFile();
155 storage.apply(
156 this.hookDirs(number, coords)
157 .add("ping_url").set(ping).up()
158 );
159 MatcherAssert.assertThat(
160 "Hook json returned wrong ping_url",
161 new MkHook(
162 storage, "login", coords, number
163 ).json().getString("ping_url"),
164 new IsEqual<>(ping)
165 );
166 }
167
168
169
170
171
172
173 @Test
174 public void createWithCorrectEvents() throws Exception {
175 final Iterable<String> events = Arrays.asList("event1", "event2");
176 final int number = 123;
177 final Coordinates coords = new Coordinates.Simple("user/repo");
178 final Directives xml = this.hookDirs(number, coords).add("events")
179 .attr("array", "true");
180 events.forEach(e -> xml.add("event").set(e).up());
181 final MkStorage storage = new MkStorage.InFile();
182 storage.apply(xml);
183 MatcherAssert.assertThat(
184 "",
185 new MkHook(
186 storage, "", coords, number
187 ).json().getJsonArray("events")
188 .stream()
189 .map(JsonValue::toString)
190 .map(event -> event.replace("\"", ""))
191 .collect(Collectors.toList()),
192 new IsIterableContainingInAnyOrder<>(
193 Arrays.asList(
194 new IsEqual<>("event1"),
195 new IsEqual<>("event2")
196 )
197 )
198 );
199 }
200
201
202
203
204
205
206
207 private Directives hookDirs(final int number, final Coordinates coords) {
208 return new Directives().xpath("/github")
209 .add("repos").add("repo").attr("coords", coords.toString())
210 .add("hooks")
211 .add("hook")
212 .add("id").set(String.valueOf(number)).up();
213 }
214 }