View Javadoc
1   /**
2    * Copyright (c) 2013-2023, 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.Event;
33  import com.jcabi.github.Hook;
34  import com.jcabi.github.Hooks;
35  import java.util.Collections;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  
40  /**
41   * Test case for {@link MkHooks}.
42   * @author Paul Polishchuk (ppol@ua.fm)
43   * @version $Id: 6aa5b26032bfbe816279f1414fcd936b9e21b381 $
44   * @since 0.8
45   */
46  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
47  public final class MkHooksTest {
48      /**
49       * Type of hook to create and use for tests.
50       */
51      private static final String HOOK_TYPE = "web";
52  
53      /**
54       * MkHooks can fetch empty list of hooks.
55       * @throws Exception if some problem inside
56       */
57      @Test
58      public void canFetchEmptyListOfHooks() throws Exception {
59          final Hooks hooks = MkHooksTest.newHooks();
60          MatcherAssert.assertThat(
61              hooks.iterate(),
62              Matchers.emptyIterable()
63          );
64      }
65  
66      /**
67       * MkHooks can delete a single hook by ID.
68       *
69       * @throws Exception if something goes wrong.
70       */
71      @Test
72      public void canDeleteSingleHook() throws Exception {
73          final Hooks hooks = MkHooksTest.newHooks();
74          final Hook hook = hooks.create(
75              HOOK_TYPE,
76              Collections.<String, String>emptyMap(),
77              Collections.<Event>emptyList(),
78              true
79          );
80          MatcherAssert.assertThat(
81              hooks.iterate(),
82              Matchers.<Hook>iterableWithSize(1)
83          );
84          hooks.remove(hook.number());
85          MatcherAssert.assertThat(
86              hooks.iterate(),
87              Matchers.emptyIterable()
88          );
89      }
90  
91      /**
92       * MkHooks can fetch single hook.
93       * @throws Exception if some problem inside
94       */
95      @Test
96      public void canFetchSingleHook() throws Exception {
97          final Hooks hooks = MkHooksTest.newHooks();
98          final Hook hook = hooks.create(
99              HOOK_TYPE,
100             Collections.<String, String>emptyMap(),
101             Collections.<Event>emptyList(),
102             true
103         );
104         MatcherAssert.assertThat(
105             hooks.get(hook.number()),
106             Matchers.notNullValue()
107         );
108     }
109 
110     /**
111      * MkHooks can fetch non empty list of hooks.
112      * @throws Exception If some problem inside
113      */
114     @Test
115     public void canFetchNonEmptyListOfHooks() throws Exception {
116         final Hooks hooks = MkHooksTest.newHooks();
117         hooks.create(
118             HOOK_TYPE,
119             Collections.<String, String>emptyMap(),
120             Collections.<Event>emptyList(),
121             true
122         );
123         hooks.create(
124             HOOK_TYPE,
125             Collections.<String, String>emptyMap(),
126             Collections.<Event>emptyList(),
127             true
128         );
129         MatcherAssert.assertThat(
130             hooks.iterate(),
131             Matchers.<Hook>iterableWithSize(2)
132         );
133     }
134 
135     /**
136      * MkHooks can create a hook.
137      * @throws Exception If some problem inside
138      */
139     @Test
140     public void canCreateHook() throws Exception {
141         final Hooks hooks = MkHooksTest.newHooks();
142         final Hook hook = hooks.create(
143             HOOK_TYPE,
144             Collections.<String, String>emptyMap(),
145             Collections.<Event>emptyList(),
146             true
147         );
148         MatcherAssert.assertThat(
149             hooks.iterate().iterator().next().number(),
150             Matchers.equalTo(hook.number())
151         );
152     }
153 
154     /**
155      * Create hooks to work with.
156      * @return Hooks
157      * @throws Exception If some problem inside
158      */
159     private static Hooks newHooks() throws Exception {
160         return new MkGithub().randomRepo().hooks();
161     }
162 }