View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.jcabi.aspects.Immutable;
33  import com.jcabi.http.Request;
34  import com.jcabi.http.mock.MkAnswer;
35  import com.jcabi.http.mock.MkContainer;
36  import com.jcabi.http.mock.MkGrizzlyContainer;
37  import com.jcabi.http.mock.MkQuery;
38  import com.jcabi.http.request.JdkRequest;
39  import java.net.HttpURLConnection;
40  import java.util.Collections;
41  import java.util.Map;
42  import java.util.concurrent.ConcurrentHashMap;
43  import javax.json.Json;
44  import javax.json.JsonObject;
45  import javax.json.JsonObjectBuilder;
46  import org.hamcrest.MatcherAssert;
47  import org.hamcrest.Matchers;
48  import org.junit.Rule;
49  import org.junit.Test;
50  import org.mockito.Mockito;
51  
52  /**
53   * Test case for {@link RtHooks}.
54   * @author Paul Polishchuk (ppol@ua.fm)
55   * @version $Id: 4dc49007100d31d2b5e5ca98890b0d41b518ca3a $
56   * @since 0.8
57   * @checkstyle MultipleStringLiterals (500 lines)
58   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
59   */
60  @Immutable
61  public final class RtHooksTest {
62  
63      /**
64       * The rule for skipping test if there's BindException.
65       * @checkstyle VisibilityModifierCheck (3 lines)
66       */
67      @Rule
68      public final transient RandomPort resource = new RandomPort();
69  
70      /**
71       * RtHooks can fetch empty list of hooks.
72       * @throws Exception if some problem inside
73       */
74      @Test
75      public void canFetchEmptyListOfHooks() throws Exception {
76          try (
77              final MkContainer container = new MkGrizzlyContainer().next(
78                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "[]")
79              ).start(this.resource.port())
80          ) {
81              final Hooks hooks = new RtHooks(
82                  new JdkRequest(container.home()),
83                  RtHooksTest.repo()
84              );
85              MatcherAssert.assertThat(
86                  hooks.iterate(),
87                  Matchers.emptyIterable()
88              );
89              container.stop();
90          }
91      }
92  
93      /**
94       * RtHooks can fetch non empty list of hooks.
95       * @throws Exception if some problem inside
96       */
97      @Test
98      public void canFetchNonEmptyListOfHooks() throws Exception {
99          try (
100             final MkContainer container = new MkGrizzlyContainer().next(
101                 new MkAnswer.Simple(
102                     HttpURLConnection.HTTP_OK,
103                     Json.createArrayBuilder()
104                         .add(
105                             hook(
106                                 "hook 1",
107                                 Collections.<String, String>emptyMap()
108                             )
109                         )
110                         .add(
111                             hook(
112                                 "hook 2",
113                                 Collections.<String, String>emptyMap()
114                             )
115                         )
116                         .build().toString()
117                 )
118             ).start(this.resource.port())
119         ) {
120             final RtHooks hooks = new RtHooks(
121                 new JdkRequest(container.home()),
122                 RtHooksTest.repo()
123             );
124             MatcherAssert.assertThat(
125                 hooks.iterate(),
126                 Matchers.<Hook>iterableWithSize(2)
127             );
128             container.stop();
129         }
130     }
131 
132     /**
133      * RtHooks can fetch single hook.
134      * @throws Exception if some problem inside
135      */
136     @Test
137     public void canFetchSingleHook() throws Exception {
138         final String name = "hook name";
139         try (
140             final MkContainer container = new MkGrizzlyContainer().next(
141                 new MkAnswer.Simple(
142                     HttpURLConnection.HTTP_OK,
143                     RtHooksTest.hook(
144                         name,
145                         Collections.<String, String>emptyMap()
146                     ).toString()
147                 )
148             ).start(this.resource.port())
149         ) {
150             final Hooks hooks = new RtHooks(
151                 new JdkRequest(container.home()),
152                 RtHooksTest.repo()
153             );
154             final Hook hook = hooks.get(1);
155             MatcherAssert.assertThat(
156                 new Hook.Smart(hook).name(),
157                 Matchers.equalTo(name)
158             );
159             container.stop();
160         }
161     }
162 
163     /**
164      * RtHooks can create a hook.
165      *
166      * @throws Exception if something goes wrong.
167      */
168     @Test
169     public void canCreateHook() throws Exception {
170         final String name = "hook name";
171         final ConcurrentHashMap<String, String> config =
172             new ConcurrentHashMap<String, String>(2);
173         config.put("url", "http://example.com");
174         config.put("content_type", "json");
175         final String body = RtHooksTest.hook(name, config).toString();
176         try (
177             final MkContainer container = new MkGrizzlyContainer().next(
178                 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
179             ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
180                 .start(this.resource.port())
181         ) {
182             final Hooks hooks = new RtHooks(
183                 new JdkRequest(container.home()),
184                 RtHooksTest.repo()
185             );
186             final Hook hook = hooks.create(
187                 name, config, Collections.<Event>emptyList(), true
188             );
189             MatcherAssert.assertThat(
190                 container.take().method(),
191                 Matchers.equalTo(Request.POST)
192             );
193             MatcherAssert.assertThat(
194                 new Hook.Smart(hook).name(),
195                 Matchers.equalTo(name)
196             );
197             container.stop();
198         }
199     }
200 
201     /**
202      * RtHooks can delete a hook.
203      *
204      * @throws Exception if something goes wrong.
205      */
206     @Test
207     public void canDeleteHook() throws Exception {
208         try (
209             final MkContainer container = new MkGrizzlyContainer().next(
210                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
211             ).start(this.resource.port())
212         ) {
213             final Hooks hooks = new RtHooks(
214                 new JdkRequest(container.home()),
215                 RtHooksTest.repo()
216             );
217             hooks.remove(1);
218             final MkQuery query = container.take();
219             MatcherAssert.assertThat(
220                 query.method(),
221                 Matchers.equalTo(Request.DELETE)
222             );
223             MatcherAssert.assertThat(
224                 query.body(),
225                 Matchers.isEmptyString()
226             );
227             container.stop();
228         }
229     }
230 
231     /**
232      * Create and return JsonObject to test.
233      * @param name Name of the hook
234      * @param config Config of hook
235      * @return JsonObject
236      * @throws Exception If some problem inside
237      */
238     private static JsonObject hook(final String name,
239         final Map<String, String> config)
240         throws Exception {
241         final JsonObjectBuilder builder = Json.createObjectBuilder();
242         for (final Map.Entry<String, String> entry : config.entrySet()) {
243             builder.add(entry.getKey(), entry.getValue());
244         }
245         return Json.createObjectBuilder()
246             .add("id", 1)
247             .add("name", name)
248             .add("config", builder)
249             .build();
250     }
251 
252     /**
253      * Create and return repo for testing.
254      * @return Repo
255      */
256     private static Repo repo() {
257         final Repo repo = Mockito.mock(Repo.class);
258         Mockito.doReturn(new Coordinates.Simple("test", "hooks"))
259             .when(repo).coordinates();
260         return repo;
261     }
262 }