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.mock;
31  
32  import com.jcabi.github.Coordinates;
33  import java.util.Arrays;
34  import java.util.stream.Collectors;
35  import javax.json.JsonValue;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.collection.IsIterableContainingInAnyOrder;
38  import org.hamcrest.core.IsEqual;
39  import org.junit.Test;
40  import org.xembly.Directives;
41  
42  /**
43   * Tests for {@link MkHook}.
44   * @author Paulo Lobo (pauloeduardolobo@gmail.com)
45   * @version $Id: be10f6aa5ffaafd1a95ffed22da8eede9481f563 $
46   * @since 0.42
47   * @checkstyle MultipleStringLiteralsCheck (500 lines)
48   */
49  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
50  public final class MkHookTest {
51      /**
52       * Test if {@link MkHook} is being created with the correct number.
53       */
54      @Test
55      public void createWithCorrectNumber() {
56          final int number = 5;
57          MatcherAssert.assertThat(
58              "Hook returned wrong number",
59              new MkHook(null, null, null, number).number(),
60              new IsEqual<>(number)
61          );
62      }
63  
64      /**
65       * Test if {@link MkHook} is being created with the correct repository.
66       * @throws Exception If something goes wrong
67       */
68      @Test
69      public void createWithCorrectRepo() throws Exception {
70          final MkStorage storage = new MkStorage.InFile();
71          final String login = "login";
72          final Coordinates coords = new Coordinates.Simple("user/repo");
73          MatcherAssert.assertThat(
74              "Hook returned wrong repository",
75              new MkHook(storage, login, coords, 0).repo(),
76              new IsEqual<>(new MkRepo(storage, login, coords))
77          );
78      }
79  
80      /**
81       * Test if {@link MkHook} is being created with the correct id.
82       * @throws Exception If something goes wrong
83       */
84      @Test
85      public void createWithCorrectId() throws Exception {
86          final int number = 5;
87          final MkStorage storage = new MkStorage.InFile();
88          final Coordinates coords = new Coordinates.Simple("user/repo");
89          storage.apply(
90              this.hookDirs(number, coords)
91          );
92          MatcherAssert.assertThat(
93              "Hook json returned wrong id",
94              new MkHook(storage, "login", coords, number).json().getString("id"),
95              new IsEqual<>(String.valueOf(number))
96          );
97      }
98  
99      /**
100      * Test if {@link MkHook} is being created with the correct url.
101      * @throws Exception If something goes wrong
102      */
103     @Test
104     public void createWithCorrectUrl() throws Exception {
105         final String url = "https://github.com/user/repo/hooks/hook/5";
106         final int number = 5;
107         final Coordinates coords = new Coordinates.Simple("user/repo");
108         final MkStorage storage = new MkStorage.InFile();
109         storage.apply(
110             this.hookDirs(number, coords)
111                 .add("url").set(url).up()
112         );
113         MatcherAssert.assertThat(
114             "Hook json returned wrong url",
115             new MkHook(
116                 storage, "login", coords, number
117             ).json().getString("url"),
118             new IsEqual<>(url)
119         );
120     }
121 
122     /**
123      * Test if {@link MkHook} is being created with the correct test url.
124      * @throws Exception If something goes wrong
125      */
126     @Test
127     public void createWithCorrectTestUrl() throws Exception {
128         final String test = "https://github.com/user/repo/hooks/hook/5";
129         final int number = 5;
130         final Coordinates coords = new Coordinates.Simple("user/repo");
131         final MkStorage storage = new MkStorage.InFile();
132         storage.apply(
133             this.hookDirs(number, coords)
134                 .add("test_url").set(test).up()
135         );
136         MatcherAssert.assertThat(
137             "Hook json returned wrong test_url",
138             new MkHook(
139                 storage, "login", coords, number
140             ).json().getString("test_url"),
141             new IsEqual<>(test)
142         );
143     }
144 
145     /**
146      * Test if {@link MkHook} is being created with the correct ping url.
147      * @throws Exception If something goes wrong
148      */
149     @Test
150     public void createWithCorrectPingUrl() throws Exception {
151         final String ping = "https://github.com/user/repo/hooks/hook/5";
152         final int number = 5;
153         final Coordinates coords = new Coordinates.Simple("user/repo");
154         final MkStorage storage = new MkStorage.InFile();
155         storage.apply(
156             this.hookDirs(number, coords)
157                 .add("ping_url").set(ping).up()
158         );
159         MatcherAssert.assertThat(
160             "Hook json returned wrong ping_url",
161             new MkHook(
162                 storage, "login", coords, number
163             ).json().getString("ping_url"),
164             new IsEqual<>(ping)
165         );
166     }
167 
168     /**
169      * MkHook.json() should return the "events" json array with the given
170      * event names.
171      * @throws Exception If something goes wrong
172      */
173     @Test
174     public void createWithCorrectEvents() throws Exception {
175         final Iterable<String> events = Arrays.asList("event1", "event2");
176         final int number = 123;
177         final Coordinates coords = new Coordinates.Simple("user/repo");
178         final Directives xml = this.hookDirs(number, coords).add("events")
179             .attr("array", "true");
180         events.forEach(e -> xml.add("event").set(e).up());
181         final MkStorage storage = new MkStorage.InFile();
182         storage.apply(xml);
183         MatcherAssert.assertThat(
184             "",
185             new MkHook(
186                 storage, "", coords, number
187             ).json().getJsonArray("events")
188                 .stream()
189                 .map(JsonValue::toString)
190                 .map(event -> event.replace("\"", ""))
191                 .collect(Collectors.toList()),
192                 new IsIterableContainingInAnyOrder<>(
193                     Arrays.asList(
194                         new IsEqual<>("event1"),
195                         new IsEqual<>("event2")
196                     )
197                 )
198         );
199     }
200 
201     /**
202      * Directives to build a hook XML that only includes its id.
203      * @param number Hook number
204      * @param coords Repo coords
205      * @return Hook directives
206      */
207     private Directives hookDirs(final int number, final Coordinates coords) {
208         return new Directives().xpath("/github")
209             .add("repos").add("repo").attr("coords", coords.toString())
210             .add("hooks")
211             .add("hook")
212             .add("id").set(String.valueOf(number)).up();
213     }
214 }