View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github.mock;
6   
7   import com.jcabi.github.Hook;
8   import com.jcabi.github.Hooks;
9   import java.io.IOException;
10  import java.util.Collections;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link MkHooks}.
17   * @since 0.8
18   */
19  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
20  final class MkHooksTest {
21      /**
22       * Type of hook to create and use for tests.
23       */
24      private static final String HOOK_TYPE = "web";
25  
26      /**
27       * MkHooks can fetch empty list of hooks.
28       * @throws Exception if some problem inside
29       */
30      @Test
31      void canFetchEmptyListOfHooks() throws Exception {
32          final Hooks hooks = MkHooksTest.newHooks();
33          MatcherAssert.assertThat(
34              "Collection is not empty",
35              hooks.iterate(),
36              Matchers.emptyIterable()
37          );
38      }
39  
40      /**
41       * MkHooks can delete a single hook by ID.
42       * @throws Exception if something goes wrong.
43       */
44      @Test
45      void canDeleteSingleHook() throws Exception {
46          final Hooks hooks = MkHooksTest.newHooks();
47          final Hook hook = hooks.create(
48              MkHooksTest.HOOK_TYPE,
49              Collections.emptyMap(),
50              Collections.emptyList(),
51              true
52          );
53          MatcherAssert.assertThat(
54              "Collection size is incorrect",
55              hooks.iterate(),
56              Matchers.iterableWithSize(1)
57          );
58          hooks.remove(hook.number());
59          MatcherAssert.assertThat(
60              "Collection is not empty",
61              hooks.iterate(),
62              Matchers.emptyIterable()
63          );
64      }
65  
66      /**
67       * MkHooks can fetch single hook.
68       * @throws Exception if some problem inside
69       */
70      @Test
71      void canFetchSingleHook() throws Exception {
72          final Hooks hooks = MkHooksTest.newHooks();
73          final Hook hook = hooks.create(
74              MkHooksTest.HOOK_TYPE,
75              Collections.emptyMap(),
76              Collections.emptyList(),
77              true
78          );
79          MatcherAssert.assertThat(
80              "Value is null",
81              hooks.get(hook.number()),
82              Matchers.notNullValue()
83          );
84      }
85  
86      /**
87       * MkHooks can fetch non empty list of hooks.
88       * @throws Exception If some problem inside
89       */
90      @Test
91      void canFetchNonEmptyListOfHooks() throws Exception {
92          final Hooks hooks = MkHooksTest.newHooks();
93          hooks.create(
94              MkHooksTest.HOOK_TYPE,
95              Collections.emptyMap(),
96              Collections.emptyList(),
97              true
98          );
99          hooks.create(
100             MkHooksTest.HOOK_TYPE,
101             Collections.emptyMap(),
102             Collections.emptyList(),
103             true
104         );
105         MatcherAssert.assertThat(
106             "Collection size is incorrect",
107             hooks.iterate(),
108             Matchers.iterableWithSize(2)
109         );
110     }
111 
112     /**
113      * MkHooks can create a hook.
114      * @throws Exception If some problem inside
115      */
116     @Test
117     void canCreateHook() throws Exception {
118         final Hooks hooks = MkHooksTest.newHooks();
119         final Hook hook = hooks.create(
120             MkHooksTest.HOOK_TYPE,
121             Collections.emptyMap(),
122             Collections.emptyList(),
123             true
124         );
125         MatcherAssert.assertThat(
126             "Values are not equal",
127             hooks.iterate().iterator().next().number(),
128             Matchers.equalTo(hook.number())
129         );
130     }
131 
132     /**
133      * Create hooks to work with.
134      * @return Hooks
135      */
136     private static Hooks newHooks() throws IOException {
137         return new MkGitHub().randomRepo().hooks();
138     }
139 }