1
2
3
4
5 package com.jcabi.github;
6
7 import com.jcabi.http.mock.MkAnswer;
8 import com.jcabi.http.mock.MkContainer;
9 import com.jcabi.http.mock.MkGrizzlyContainer;
10 import com.jcabi.http.request.ApacheRequest;
11 import jakarta.json.JsonValue;
12 import java.io.IOException;
13 import java.net.HttpURLConnection;
14 import java.util.Arrays;
15 import java.util.stream.Collectors;
16 import org.hamcrest.MatcherAssert;
17 import org.hamcrest.Matchers;
18 import org.hamcrest.collection.IsIterableContainingInAnyOrder;
19 import org.hamcrest.core.IsEqual;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.Mockito;
23
24
25
26
27
28
29 @ExtendWith(RandomPort.class)
30 final class RtHookTest {
31
32
33
34
35 @Test
36 void performsValidRequest() throws IOException {
37 try (
38 MkContainer container = new MkGrizzlyContainer().next(
39 new MkAnswer.Simple(
40 HttpURLConnection.HTTP_OK,
41 "{\"test\":\"hook\"}"
42 )
43 ).start(RandomPort.port())
44 ) {
45 final Hook hook = new RtHook(
46 new ApacheRequest(container.home()),
47 RtHookTest.repo(),
48 1
49 );
50 MatcherAssert.assertThat(
51 "Value is null",
52 hook.json(),
53 Matchers.notNullValue()
54 );
55 MatcherAssert.assertThat(
56 "String does not end with expected value",
57 container.take().uri().toString(),
58 Matchers.endsWith("/repos/test/repo/hooks/1")
59 );
60 container.stop();
61 }
62 }
63
64
65
66
67 @Test
68 void returnsEvents() throws IOException {
69 try (
70 MkContainer container = new MkGrizzlyContainer().next(
71 new MkAnswer.Simple(
72 HttpURLConnection.HTTP_OK,
73 "{ \"id\": 1, \"events\": [ \"push\", \"pull_request\" ] }"
74 )
75 ).start(RandomPort.port())
76 ) {
77 MatcherAssert.assertThat(
78 "Assertion failed",
79 new RtHook(
80 new ApacheRequest(container.home()),
81 RtHookTest.repo(),
82 1
83 ).json().getJsonArray("events")
84 .stream()
85 .map(JsonValue::toString)
86 .map(event -> event.replace("\"", ""))
87 .collect(Collectors.toList()),
88 new IsIterableContainingInAnyOrder<>(
89 Arrays.asList(
90 new IsEqual<>("push"),
91 new IsEqual<>("pull_request")
92 )
93 )
94 );
95 container.stop();
96 }
97 }
98
99
100
101
102
103 private static Repo repo() {
104 final Repo repo = Mockito.mock(Repo.class);
105 Mockito.doReturn(new Coordinates.Simple("test", "repo"))
106 .when(repo).coordinates();
107 return repo;
108 }
109
110 }