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;
31  
32  import com.jcabi.aspects.Tv;
33  import com.jcabi.github.OAuthScope.Scope;
34  import org.apache.commons.lang3.RandomStringUtils;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  
41  /**
42   * Test case for {@link RtReferences}.
43   * @author Mihai Andronache (amihaiemil@gmail.com)
44   * @version $Id: 851f2f81f4e61890c3303b7d0f59ebe8353b271d $
45   * @checkstyle MultipleStringLiterals (500 lines)
46   */
47  @OAuthScope(Scope.REPO)
48  @SuppressWarnings("PMD.ConsecutiveLiteralAppends")
49  public final class RtReferencesITCase {
50  
51      /**
52       * RepoRule.
53       * @checkstyle VisibilityModifierCheck (3 lines)
54       */
55      private static RepoRule rule = new RepoRule();
56  
57      /**
58       * Test repos.
59       */
60      private static Repos repos;
61  
62      /**
63       * Test repo.
64       */
65      private static Repo repo;
66  
67      /**
68       * Set up test fixtures.
69       * @throws Exception If some errors occurred.
70       */
71      @BeforeClass
72      public static void setUp() throws Exception {
73          final Github github = new GithubIT().connect();
74          repos = github.repos();
75          repo = rule.repo(repos);
76      }
77  
78      /**
79       * Tear down test fixtures.
80       * @throws Exception If some errors occurred.
81       */
82      @AfterClass
83      public static void tearDown() throws Exception {
84          if (repos != null && repo != null) {
85              repos.remove(repo.coordinates());
86          }
87      }
88  
89      /**
90       * RtReference can create a reference.
91       * @throws Exception - If something goes wrong.
92       */
93      @Test
94      public void createsReference() throws Exception {
95          final References refs = repo.git().references();
96          final String name = RandomStringUtils.randomAlphanumeric(Tv.TEN);
97          final StringBuilder builder = new StringBuilder("refs/tags/")
98              .append(name);
99          final Reference reference = refs.create(
100             builder.toString(),
101             refs.get("refs/heads/master").json().getJsonObject("object")
102                 .getString("sha")
103         );
104         MatcherAssert.assertThat(
105             reference,
106             Matchers.notNullValue()
107         );
108         builder.delete(0, builder.length());
109         builder.append("tags/").append(name);
110         refs.remove(builder.toString());
111     }
112 
113     /**
114      * RtReference can iterate over references.
115      * @throws Exception - If something goes wrong.
116      */
117     @Test
118     public void iteratesReferences() throws Exception {
119         final References refs = repo.git().references();
120         final String name = RandomStringUtils.randomAlphanumeric(Tv.TEN);
121         final StringBuilder builder = new StringBuilder("refs/heads/")
122             .append(name);
123         refs.create(
124             builder.toString(),
125             refs.get("refs/heads/master").json().getJsonObject("object")
126                 .getString("sha")
127         );
128         MatcherAssert.assertThat(
129             refs.iterate(),
130             Matchers.notNullValue()
131         );
132         builder.delete(0, builder.length());
133         builder.append("heads/").append(name);
134         refs.remove(builder.toString());
135     }
136 
137     /**
138      * RtReference can iterate over references in sub-namespace.
139      * @throws Exception - If something goes wrong.
140      */
141     @Test
142     public void iteratesReferencesInSubNamespace() throws Exception {
143         final References refs = repo.git().references();
144         final String name = RandomStringUtils.randomAlphanumeric(Tv.TEN);
145         final StringBuilder builder = new StringBuilder("refs/heads/")
146             .append(name);
147         refs.create(
148             builder.toString(),
149             refs.get("refs/heads/master").json().getJsonObject("object")
150                 .getString("sha")
151         );
152         MatcherAssert.assertThat(
153             refs.iterate("heads"),
154             Matchers.notNullValue()
155         );
156         builder.delete(0, builder.length());
157         builder.append("heads/").append(name);
158         refs.remove(builder.toString());
159     }
160 
161 }