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.github.mock.MkGithub;
33  import com.jcabi.http.Request;
34  import com.jcabi.http.mock.MkAnswer;
35  import com.jcabi.http.mock.MkContainer;
36  import com.jcabi.http.mock.MkGrizzlyContainer;
37  import com.jcabi.http.request.ApacheRequest;
38  import java.net.HttpURLConnection;
39  import org.hamcrest.MatcherAssert;
40  import org.hamcrest.Matchers;
41  import org.junit.Rule;
42  import org.junit.Test;
43  
44  /**
45   * Test case for {@link RtReferences}.
46   *
47   * @author Mihai Andronache (amihaiemil@gmail.com)
48   * @version $Id: a51abea6c9500ce46644958c7ed16cc3a004e4cb $
49   * @checkstyle MultipleStringLiterals (500 lines)
50   */
51  public final class RtReferencesTest {
52  
53      /**
54       * The rule for skipping test if there's BindException.
55       * @checkstyle VisibilityModifierCheck (3 lines)
56       */
57      @Rule
58      public final transient RandomPort resource = new RandomPort();
59  
60      /**
61       * RtReferences should create and return a Reference.
62       * @throws Exception - if something goes wrong.
63       */
64      @Test
65      public void createsReference() throws Exception {
66          try (
67              final MkContainer container = new MkGrizzlyContainer().next(
68                  new MkAnswer.Simple(
69                      HttpURLConnection.HTTP_CREATED,
70                      "{\"ref\":\"refs/heads/feature-a\"}"
71                  )
72              ).start(this.resource.port())
73          ) {
74              final References refs = new RtReferences(
75                  new ApacheRequest(container.home()),
76                  new MkGithub().randomRepo()
77              );
78              MatcherAssert.assertThat(
79                  refs.create("abceefgh3456", "refs/heads/feature-a"),
80                  Matchers.instanceOf(Reference.class)
81              );
82              MatcherAssert.assertThat(
83                  container.take().method(),
84                  Matchers.equalTo(Request.POST)
85              );
86              container.stop();
87          }
88      }
89  
90      /**
91       * RtReferences should be able to iterate over References.
92       * @throws Exception - If something goes wrong.
93       */
94      @Test
95      public void iteratesReferences() throws Exception {
96          try (
97              final MkContainer container = new MkGrizzlyContainer().next(
98                  new MkAnswer.Simple(
99                      HttpURLConnection.HTTP_OK,
100                     "{\"ref\":\"refs/heads/feature-a\"}"
101                 )
102             ).start(this.resource.port())
103         ) {
104             final References refs = new RtReferences(
105                 new ApacheRequest(container.home()),
106                 new MkGithub().randomRepo()
107             );
108             MatcherAssert.assertThat(
109                 refs.iterate(),
110                 Matchers.notNullValue()
111             );
112             container.stop();
113         }
114     }
115 
116     /**
117      * RtReferences should be able to remove a Reference.
118      * @throws Exception - If somethins goes wrong.
119      */
120     @Test
121     public void removesReference() throws Exception {
122         try (
123             final MkContainer container = new MkGrizzlyContainer().next(
124                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
125             ).start(this.resource.port())
126         ) {
127             final References refs = new RtReferences(
128                 new ApacheRequest(container.home()),
129                 new MkGithub().randomRepo()
130             );
131             refs.remove("heads/feature-a");
132             MatcherAssert.assertThat(
133                 container.take().method(),
134                 Matchers.equalTo(Request.DELETE)
135             );
136             container.stop();
137         }
138     }
139 
140     /**
141      * RtReferences should be able to iterate over tags.
142      * @throws Exception - If something goes wrong.
143      */
144     @Test
145     public void iteratesTags() throws Exception {
146         try (
147             final MkContainer container = new MkGrizzlyContainer().next(
148                 new MkAnswer.Simple(
149                     HttpURLConnection.HTTP_OK,
150                     "[{\"ref\":\"refs/tags/feature-b\"}]"
151                 )
152             ).start(this.resource.port())
153         ) {
154             final References refs = new RtReferences(
155                 new ApacheRequest(container.home()),
156                 new MkGithub().randomRepo()
157             );
158             MatcherAssert.assertThat(
159                 refs.tags(),
160                 Matchers.<Reference>iterableWithSize(1)
161             );
162             MatcherAssert.assertThat(
163                 container.take().uri().toString(),
164                 Matchers.endsWith("/git/refs/tags")
165             );
166             container.stop();
167         }
168     }
169 
170     /**
171      * RtReferences should be able to iterate over heads.
172      * @throws Exception - If something goes wrong.
173      */
174     @Test
175     public void iteratesHeads() throws Exception {
176         try (
177             final MkContainer container = new MkGrizzlyContainer().next(
178                 new MkAnswer.Simple(
179                     HttpURLConnection.HTTP_OK,
180                     "[{\"ref\":\"refs/heads/feature-c\"}]"
181                 )
182             ).start(this.resource.port())
183         ) {
184             final References refs = new RtReferences(
185                 new ApacheRequest(container.home()),
186                 new MkGithub().randomRepo()
187             );
188             MatcherAssert.assertThat(
189                 refs.heads(),
190                 Matchers.<Reference>iterableWithSize(1)
191             );
192             MatcherAssert.assertThat(
193                 container.take().uri().toString(),
194                 Matchers.endsWith("/git/refs/heads")
195             );
196             container.stop();
197         }
198     }
199 }