View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcabi.aspects.Immutable;
8   import com.jcabi.http.Request;
9   import com.jcabi.http.mock.MkAnswer;
10  import com.jcabi.http.mock.MkContainer;
11  import com.jcabi.http.mock.MkGrizzlyContainer;
12  import com.jcabi.http.mock.MkQuery;
13  import com.jcabi.http.request.JdkRequest;
14  import jakarta.json.Json;
15  import jakarta.json.JsonObject;
16  import jakarta.json.JsonObjectBuilder;
17  import java.io.IOException;
18  import java.net.HttpURLConnection;
19  import java.util.Collections;
20  import java.util.Map;
21  import java.util.concurrent.ConcurrentHashMap;
22  import org.hamcrest.MatcherAssert;
23  import org.hamcrest.Matchers;
24  import org.junit.jupiter.api.Test;
25  import org.junit.jupiter.api.extension.ExtendWith;
26  import org.mockito.Mockito;
27  
28  /**
29   * Test case for {@link RtHooks}.
30   * @since 0.8
31   * @checkstyle MultipleStringLiterals (500 lines)
32   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
33   */
34  @Immutable
35  @ExtendWith(RandomPort.class)
36  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
37  final class RtHooksTest {
38  
39      /**
40       * The rule for skipping test if there's BindException.
41       * @checkstyle VisibilityModifierCheck (3 lines)
42       */
43      @Test
44      void canFetchEmptyListOfHooks() throws IOException {
45          try (
46              MkContainer container = new MkGrizzlyContainer().next(
47                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "[]")
48              ).start(RandomPort.port())
49          ) {
50              final Hooks hooks = new RtHooks(
51                  new JdkRequest(container.home()),
52                  RtHooksTest.repo()
53              );
54              MatcherAssert.assertThat(
55                  "Collection is not empty",
56                  hooks.iterate(),
57                  Matchers.emptyIterable()
58              );
59              container.stop();
60          }
61      }
62  
63      @Test
64      void canFetchNonEmptyListOfHooks() throws IOException {
65          try (
66              MkContainer container = new MkGrizzlyContainer().next(
67                  new MkAnswer.Simple(
68                      HttpURLConnection.HTTP_OK,
69                      Json.createArrayBuilder()
70                          .add(
71                              RtHooksTest.hook(
72                                  "hook 1",
73                                  Collections.emptyMap()
74                              )
75                          )
76                          .add(
77                              RtHooksTest.hook(
78                                  "hook 2",
79                                  Collections.emptyMap()
80                              )
81                          )
82                          .build().toString()
83                  )
84              ).start(RandomPort.port())
85          ) {
86              final RtHooks hooks = new RtHooks(
87                  new JdkRequest(container.home()),
88                  RtHooksTest.repo()
89              );
90              MatcherAssert.assertThat(
91                  "Collection size is incorrect",
92                  hooks.iterate(),
93                  Matchers.iterableWithSize(2)
94              );
95              container.stop();
96          }
97      }
98  
99      @Test
100     void canFetchSingleHook() throws IOException {
101         final String name = "hook name";
102         try (
103             MkContainer container = new MkGrizzlyContainer().next(
104                 new MkAnswer.Simple(
105                     HttpURLConnection.HTTP_OK,
106                     RtHooksTest.hook(
107                         name,
108                         Collections.emptyMap()
109                     ).toString()
110                 )
111             ).start(RandomPort.port())
112         ) {
113             final Hooks hooks = new RtHooks(
114                 new JdkRequest(container.home()),
115                 RtHooksTest.repo()
116             );
117             final Hook hook = hooks.get(1);
118             MatcherAssert.assertThat(
119                 "Values are not equal",
120                 new Hook.Smart(hook).name(),
121                 Matchers.equalTo(name)
122             );
123             container.stop();
124         }
125     }
126 
127     @Test
128     void canCreateHook() throws IOException {
129         final ConcurrentHashMap<String, String> config =
130             new ConcurrentHashMap<>(2);
131         config.put("url", "http://example.com");
132         config.put("content_type", "json");
133         final String name = "hook name";
134         final String body = RtHooksTest.hook(name, config).toString();
135         try (
136             MkContainer container = new MkGrizzlyContainer().next(
137                 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
138             ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
139                 .start(RandomPort.port())
140         ) {
141             final Hooks hooks = new RtHooks(
142                 new JdkRequest(container.home()),
143                 RtHooksTest.repo()
144             );
145             final Hook hook = hooks.create(
146                 name, config, Collections.emptyList(), true
147             );
148             MatcherAssert.assertThat(
149                 "Values are not equal",
150                 container.take().method(),
151                 Matchers.equalTo(Request.POST)
152             );
153             MatcherAssert.assertThat(
154                 "Values are not equal",
155                 new Hook.Smart(hook).name(),
156                 Matchers.equalTo(name)
157             );
158             container.stop();
159         }
160     }
161 
162     @Test
163     void canDeleteHook() throws IOException {
164         try (
165             MkContainer container = new MkGrizzlyContainer().next(
166                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
167             ).start(RandomPort.port())
168         ) {
169             final Hooks hooks = new RtHooks(
170                 new JdkRequest(container.home()),
171                 RtHooksTest.repo()
172             );
173             hooks.remove(1);
174             final MkQuery query = container.take();
175             MatcherAssert.assertThat(
176                 "Values are not equal",
177                 query.method(),
178                 Matchers.equalTo(Request.DELETE)
179             );
180             MatcherAssert.assertThat(
181                 "Values are not equal",
182                 query.body(),
183                 Matchers.is(Matchers.emptyString())
184             );
185             container.stop();
186         }
187     }
188 
189     /**
190      * Create and return JsonObject to test.
191      * @param name Name of the hook
192      * @param config Config of hook
193      * @return JsonObject
194      */
195     private static JsonObject hook(final String name,
196         final Map<String, String> config) {
197         final JsonObjectBuilder builder = Json.createObjectBuilder();
198         for (final Map.Entry<String, String> entry : config.entrySet()) {
199             builder.add(entry.getKey(), entry.getValue());
200         }
201         return Json.createObjectBuilder()
202             .add("id", 1)
203             .add("name", name)
204             .add("config", builder)
205             .build();
206     }
207 
208     /**
209      * Create and return repo for testing.
210      * @return Repo
211      */
212     private static Repo repo() {
213         final Repo repo = Mockito.mock(Repo.class);
214         Mockito.doReturn(new Coordinates.Simple("test", "hooks"))
215             .when(repo).coordinates();
216         return repo;
217     }
218 }