1
2
3
4
5 package com.jcabi.github.mock;
6
7 import com.jcabi.github.Coordinates;
8 import jakarta.json.JsonValue;
9 import java.io.IOException;
10 import java.util.Arrays;
11 import java.util.stream.Collectors;
12 import org.hamcrest.MatcherAssert;
13 import org.hamcrest.collection.IsIterableContainingInAnyOrder;
14 import org.hamcrest.core.IsEqual;
15 import org.junit.jupiter.api.Test;
16 import org.xembly.Directives;
17
18
19
20
21
22
23 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
24 final class MkHookTest {
25
26
27
28 @Test
29 void createWithCorrectNumber() {
30 final int number = 5;
31 MatcherAssert.assertThat(
32 "Hook returned wrong number",
33 new MkHook(null, null, null, number).number(),
34 new IsEqual<>(number)
35 );
36 }
37
38
39
40
41 @Test
42 void createWithCorrectRepo() throws IOException {
43 final MkStorage storage = new MkStorage.InFile();
44 final String login = "login";
45 final Coordinates coords = new Coordinates.Simple("user/repo");
46 MatcherAssert.assertThat(
47 "Hook returned wrong repository",
48 new MkHook(storage, login, coords, 0).repo(),
49 new IsEqual<>(new MkRepo(storage, login, coords))
50 );
51 }
52
53
54
55
56 @Test
57 void createWithCorrectId() throws IOException {
58 final int number = 5;
59 final MkStorage storage = new MkStorage.InFile();
60 final Coordinates coords = new Coordinates.Simple("user/repo");
61 storage.apply(
62 MkHookTest.hookDirs(number, coords)
63 );
64 MatcherAssert.assertThat(
65 "Hook json returned wrong id",
66 new MkHook(storage, "login", coords, number).json().getString("id"),
67 new IsEqual<>(String.valueOf(number))
68 );
69 }
70
71
72
73
74 @Test
75 void createWithCorrectUrl() throws IOException {
76 final String url = "https://github.com/user/repo/hooks/hook/5";
77 final int number = 5;
78 final Coordinates coords = new Coordinates.Simple("user/repo");
79 final MkStorage storage = new MkStorage.InFile();
80 storage.apply(
81 MkHookTest.hookDirs(number, coords)
82 .add("url").set(url).up()
83 );
84 MatcherAssert.assertThat(
85 "Hook json returned wrong url",
86 new MkHook(
87 storage, "login", coords, number
88 ).json().getString("url"),
89 new IsEqual<>(url)
90 );
91 }
92
93
94
95
96 @Test
97 void createWithCorrectTestUrl() throws IOException {
98 final String test = "https://github.com/user/repo/hooks/hook/5";
99 final int number = 5;
100 final Coordinates coords = new Coordinates.Simple("user/repo");
101 final MkStorage storage = new MkStorage.InFile();
102 storage.apply(
103 MkHookTest.hookDirs(number, coords)
104 .add("test_url").set(test).up()
105 );
106 MatcherAssert.assertThat(
107 "Hook json returned wrong test_url",
108 new MkHook(
109 storage, "login", coords, number
110 ).json().getString("test_url"),
111 new IsEqual<>(test)
112 );
113 }
114
115
116
117
118 @Test
119 void createWithCorrectPingUrl() throws IOException {
120 final String ping = "https://github.com/user/repo/hooks/hook/5";
121 final int number = 5;
122 final Coordinates coords = new Coordinates.Simple("user/repo");
123 final MkStorage storage = new MkStorage.InFile();
124 storage.apply(
125 MkHookTest.hookDirs(number, coords)
126 .add("ping_url").set(ping).up()
127 );
128 MatcherAssert.assertThat(
129 "Hook json returned wrong ping_url",
130 new MkHook(
131 storage, "login", coords, number
132 ).json().getString("ping_url"),
133 new IsEqual<>(ping)
134 );
135 }
136
137
138
139
140
141 @Test
142 void createWithCorrectEvents() throws IOException {
143 final Iterable<String> events = Arrays.asList("event1", "event2");
144 final int number = 123;
145 final Coordinates coords = new Coordinates.Simple("user/repo");
146 final Directives xml = MkHookTest.hookDirs(number, coords).add("events")
147 .attr("array", "true");
148 events.forEach(e -> xml.add("event").set(e).up());
149 final MkStorage storage = new MkStorage.InFile();
150 storage.apply(xml);
151 MatcherAssert.assertThat(
152 "Hook events are incorrect",
153 new MkHook(
154 storage, "", coords, number
155 ).json().getJsonArray("events")
156 .stream()
157 .map(JsonValue::toString)
158 .map(event -> event.replace("\"", ""))
159 .collect(Collectors.toList()),
160 new IsIterableContainingInAnyOrder<>(
161 Arrays.asList(
162 new IsEqual<>("event1"),
163 new IsEqual<>("event2")
164 )
165 )
166 );
167 }
168
169
170
171
172
173
174
175 private static Directives hookDirs(final int number, final Coordinates coords) {
176 return new Directives().xpath("/github")
177 .add("repos").add("repo").attr("coords", coords.toString())
178 .add("hooks")
179 .add("hook")
180 .add("id").set(String.valueOf(number)).up();
181 }
182 }