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.References;
8   import com.jcabi.github.Repo;
9   import java.io.IOException;
10  import org.hamcrest.MatcherAssert;
11  import org.hamcrest.Matchers;
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * Testcase for {@link MkReferences}.
16   * @since 0.1
17   * @checkstyle MultipleStringLiterals (500 lines)
18   */
19  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
20  final class MkReferencesTest {
21  
22      @Test
23      void createsMkReference() throws IOException {
24          final References refs = new MkGitHub().randomRepo()
25              .git().references();
26          MatcherAssert.assertThat(
27              "Value is null",
28              refs.create("refs/heads/branch1", "abcderf122"),
29              Matchers.notNullValue()
30          );
31      }
32  
33      @Test
34      void returnsRepo() throws IOException {
35          final References refs = new MkGitHub().randomRepo()
36              .git().references();
37          MatcherAssert.assertThat(
38              "Value is null",
39              refs.repo(),
40              Matchers.notNullValue()
41          );
42      }
43  
44      @Test
45      void iteratesReferences() throws IOException {
46          final Repo owner = new MkGitHub().randomRepo();
47          final References refs = owner.git().references();
48          refs.create("refs/heads/br", "qweqwe");
49          refs.create("refs/tags/t1", "111t222");
50          MatcherAssert.assertThat(
51              "Collection size is incorrect",
52              refs.iterate(),
53              Matchers.iterableWithSize(2)
54          );
55      }
56  
57      /**
58       * MkReferences can iterate over references in sub-namespace.
59       */
60      @Test
61      void iteratesReferencesInSubNamespace() throws IOException {
62          final Repo owner = new MkGitHub().randomRepo();
63          final References refs = owner.git().references();
64          refs.create("refs/heads/br", "qweqwe");
65          refs.create("refs/tags/t1", "111t222");
66          MatcherAssert.assertThat(
67              "Collection size is incorrect",
68              refs.iterate("heads"),
69              Matchers.iterableWithSize(1)
70          );
71          MatcherAssert.assertThat(
72              "Collection size is incorrect",
73              refs.iterate("tags"),
74              Matchers.iterableWithSize(1)
75          );
76      }
77  
78      /**
79       * MkReferences can iterate over references in Tagsub-namespace.
80       */
81      @Test
82      void iteratesTags() throws IOException {
83          final Repo owner = new MkGitHub().randomRepo();
84          final References refs = owner.git().references();
85          refs.create("refs/tags/t2", "2322f34");
86          MatcherAssert.assertThat(
87              "Collection size is incorrect",
88              refs.tags(),
89              Matchers.iterableWithSize(1)
90          );
91      }
92  
93      /**
94       * MkReferences can iterate over references in Tagsub-namespace.
95       */
96      @Test
97      void iteratesHeads() throws IOException {
98          final Repo owner = new MkGitHub().randomRepo();
99          final References refs = owner.git().references();
100         refs.create("refs/heads/branch2", "blahblah");
101         MatcherAssert.assertThat(
102             "Collection size is incorrect",
103             refs.heads(),
104             Matchers.iterableWithSize(1)
105         );
106     }
107 
108     @Test
109     void removesReference() throws IOException {
110         final Repo owner = new MkGitHub().randomRepo();
111         final References refs = owner.git().references();
112         refs.create("refs/heads/testbr", "qweqwe22");
113         refs.create("refs/tags/t2", "111teee");
114         MatcherAssert.assertThat(
115             "Collection size is incorrect",
116             refs.iterate(),
117             Matchers.iterableWithSize(2)
118         );
119         refs.remove("refs/tags/t2");
120         MatcherAssert.assertThat(
121             "Collection size is incorrect",
122             refs.iterate(),
123             Matchers.iterableWithSize(1)
124         );
125     }
126 }