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.http.mock.MkAnswer;
33  import com.jcabi.http.mock.MkContainer;
34  import com.jcabi.http.mock.MkGrizzlyContainer;
35  import com.jcabi.http.request.ApacheRequest;
36  import java.net.HttpURLConnection;
37  import java.util.Arrays;
38  import java.util.stream.Collectors;
39  import javax.json.JsonValue;
40  import org.hamcrest.MatcherAssert;
41  import org.hamcrest.Matchers;
42  import org.hamcrest.collection.IsIterableContainingInAnyOrder;
43  import org.hamcrest.core.IsEqual;
44  import org.junit.Rule;
45  import org.junit.Test;
46  import org.mockito.Mockito;
47  
48  /**
49   * Test case for {@link RtHook}.
50   *
51   * @author Carlos Miranda (miranda.cma@gmail.com)
52   * @version $Id: d5c6cc6c79068708ac4ffd75dc216f86c9436b2e $
53   * @checkstyle ClassDataAbstractionCouplingCheck (2 lines)
54   */
55  public final class RtHookTest {
56      /**
57       * The rule for skipping test if there's BindException.
58       * @checkstyle VisibilityModifierCheck (3 lines)
59       */
60      @Rule
61      public final transient RandomPort resource = new RandomPort();
62  
63      /**
64       * RtHook should perform a JSON request to "/repos/:owner/:repo/hooks/:id".
65       *
66       * @throws Exception If a problem occurs.
67       */
68      @Test
69      public void performsValidRequest() throws Exception {
70          try (
71              final MkContainer container = new MkGrizzlyContainer().next(
72                  new MkAnswer.Simple(
73                      HttpURLConnection.HTTP_OK,
74                      "{\"test\":\"hook\"}"
75                  )
76              ).start(this.resource.port())
77          ) {
78              final Hook hook = new RtHook(
79                  new ApacheRequest(container.home()),
80                  repo(),
81                  1
82              );
83              MatcherAssert.assertThat(
84                  hook.json(),
85                  Matchers.notNullValue()
86              );
87              MatcherAssert.assertThat(
88                  container.take().uri().toString(),
89                  Matchers.endsWith("/repos/test/repo/hooks/1")
90              );
91              container.stop();
92          }
93      }
94  
95      /**
96       * RtHook.json() should return a json array with the hook's events.
97       *
98       * @throws Exception If a problem occurs.
99       */
100     @Test
101     public void returnsEvents() throws Exception {
102         try (
103             final MkContainer container = new MkGrizzlyContainer().next(
104                 new MkAnswer.Simple(
105                     HttpURLConnection.HTTP_OK,
106                     "{ \"id\": 1, \"events\": [ \"push\", \"pull_request\" ] }"
107                 )
108             ).start(this.resource.port())
109         ) {
110             MatcherAssert.assertThat(
111                 new RtHook(
112                     new ApacheRequest(container.home()),
113                     repo(),
114                     1
115                 ).json().getJsonArray("events")
116                     .stream()
117                     .map(JsonValue::toString)
118                     .map(event -> event.replace("\"", ""))
119                     .collect(Collectors.toList()),
120                 new IsIterableContainingInAnyOrder<>(
121                     Arrays.asList(
122                         new IsEqual<>("push"),
123                         new IsEqual<>("pull_request")
124                     )
125                 )
126             );
127             container.stop();
128         }
129     }
130 
131     /**
132      * Create and return repo for testing.
133      * @return Repo
134      */
135     private static Repo repo() {
136         final Repo repo = Mockito.mock(Repo.class);
137         Mockito.doReturn(new Coordinates.Simple("test", "repo"))
138             .when(repo).coordinates();
139         return repo;
140     }
141 
142 }