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  
31  package com.jcabi.github.mock;
32  
33  import com.jcabi.github.Reference;
34  import com.jcabi.github.References;
35  import com.jcabi.github.Repo;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  
40  /**
41   * Testcase for {@link MkReferences}.
42   * @author Mihai Andronache (amihaiemil@gmail.com)
43   * @version $Id: c73951677dc719f257481242dcc105c6ce56ece1 $
44   * @checkstyle MultipleStringLiterals (500 lines)
45   */
46  public final class MkReferencesTest {
47  
48      /**
49       * MkReferences can create a MkReference.
50       * @throws Exception - If something goes wrong.
51       */
52      @Test
53      public void createsMkReference() throws Exception {
54          final References refs = new MkGithub().randomRepo()
55              .git().references();
56          MatcherAssert.assertThat(
57              refs.create("refs/heads/branch1", "abcderf122"),
58              Matchers.notNullValue()
59          );
60      }
61  
62      /**
63       * MkReference can return its owner.
64       * @throws Exception - If something goes wrong.
65       */
66      @Test
67      public void returnsRepo() throws Exception {
68          final References refs = new MkGithub().randomRepo()
69              .git().references();
70          MatcherAssert.assertThat(
71              refs.repo(),
72              Matchers.notNullValue()
73          );
74      }
75  
76      /**
77       * MkReferences can iterate over references.
78       * @throws Exception - If something goes wrong.
79       */
80      @Test
81      public void iteratesReferences() throws Exception {
82          final Repo owner = new MkGithub().randomRepo();
83          final References refs = owner.git().references();
84          refs.create("refs/heads/br", "qweqwe");
85          refs.create("refs/tags/t1", "111t222");
86          MatcherAssert.assertThat(
87              refs.iterate(),
88              Matchers.<Reference>iterableWithSize(2)
89          );
90      }
91  
92      /**
93       * MkReferences can iterate over references in sub-namespace.
94       * @throws Exception - If something goes wrong.
95       */
96      @Test
97      public void iteratesReferencesInSubNamespace() throws Exception {
98          final Repo owner = new MkGithub().randomRepo();
99          final References refs = owner.git().references();
100         refs.create("refs/heads/br", "qweqwe");
101         refs.create("refs/tags/t1", "111t222");
102         MatcherAssert.assertThat(
103             refs.iterate("heads"),
104             Matchers.<Reference>iterableWithSize(1)
105         );
106         MatcherAssert.assertThat(
107             refs.iterate("tags"),
108             Matchers.<Reference>iterableWithSize(1)
109         );
110     }
111 
112     /**
113      * MkReferences can iterate over references in Tagsub-namespace.
114      * @throws Exception - If something goes wrong.
115      */
116     @Test
117     public void iteratesTags() throws Exception {
118         final Repo owner = new MkGithub().randomRepo();
119         final References refs = owner.git().references();
120         refs.create("refs/tags/t2", "2322f34");
121         MatcherAssert.assertThat(
122             refs.tags(),
123             Matchers.<Reference>iterableWithSize(1)
124         );
125     }
126 
127     /**
128      * MkReferences can iterate over references in Tagsub-namespace.
129      * @throws Exception - If something goes wrong.
130      */
131     @Test
132     public void iteratesHeads() throws Exception {
133         final Repo owner = new MkGithub().randomRepo();
134         final References refs = owner.git().references();
135         refs.create("refs/heads/branch2", "blahblah");
136         MatcherAssert.assertThat(
137             refs.heads(),
138             Matchers.<Reference>iterableWithSize(1)
139         );
140     }
141 
142     /**
143      * MkReferences can remove a Reference.
144      * @throws Exception - If something goes wrong.
145      */
146     @Test
147     public void removesReference() throws Exception {
148         final Repo owner = new MkGithub().randomRepo();
149         final References refs = owner.git().references();
150         refs.create("refs/heads/testbr", "qweqwe22");
151         refs.create("refs/tags/t2", "111teee");
152         MatcherAssert.assertThat(
153             refs.iterate(),
154             Matchers.<Reference>iterableWithSize(2)
155         );
156         refs.remove("refs/tags/t2");
157         MatcherAssert.assertThat(
158             refs.iterate(),
159             Matchers.<Reference>iterableWithSize(1)
160         );
161     }
162 }