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;
32  
33  import com.jcabi.github.mock.MkGithub;
34  import com.jcabi.http.Request;
35  import com.jcabi.http.mock.MkAnswer;
36  import com.jcabi.http.mock.MkContainer;
37  import com.jcabi.http.mock.MkGrizzlyContainer;
38  import com.jcabi.http.request.ApacheRequest;
39  import java.net.HttpURLConnection;
40  import javax.json.Json;
41  import org.hamcrest.MatcherAssert;
42  import org.hamcrest.Matchers;
43  import org.junit.Rule;
44  import org.junit.Test;
45  
46  /**
47   * Test case for {@link RtReference}.
48   *
49   * @author Mihai Andronache (amihaiemil@gmail.com)
50   * @author Paulo Lobo (pauloeduardolobo@gmail.com)
51   * @version $Id: 5967fb4a4740248189334b68ba3ed902e5934c36 $
52   * @checkstyle MultipleStringLiterals (500 lines)
53   */
54  public final class RtReferenceTest {
55  
56      /**
57       * The rule for skipping test if there's BindException.
58       * @checkstyle VisibilityModifierCheck (3 lines)
59       */
60      @Rule
61      public final transient RandomPort resource = new RandomPort();
62  
63      /**
64       * RtReference should be able to execute patch.
65       * @throws Exception - If something goes wrong.
66       */
67      @Test
68      public void patchesContent() throws Exception {
69          try (
70              final MkContainer container = new MkGrizzlyContainer().next(
71                  new MkAnswer.Simple(
72                      HttpURLConnection.HTTP_OK,
73                      "{\"ref\":\"refs/heads/featureA\"}"
74                  )
75              ).start(this.resource.port())
76          ) {
77              final Reference reference = new RtReference(
78                  new ApacheRequest(container.home()),
79                  new MkGithub().randomRepo(),
80                  "refs/heads/featureA"
81              );
82              reference.patch(
83                  Json.createObjectBuilder().add("sha", "abcdef12345")
84                  .add("force", "false").build()
85              );
86              MatcherAssert.assertThat(
87                  container.take().method(),
88                  Matchers.equalTo(Request.PATCH)
89              );
90              container.stop();
91          }
92      }
93  
94      /**
95       * RtReference should be able to fetch its json.
96       * @throws Exception - if something goes wrong.
97       */
98      @Test
99      public void fetchesContent() throws Exception {
100         try (
101             final MkContainer container = new MkGrizzlyContainer().next(
102                 new MkAnswer.Simple(
103                     HttpURLConnection.HTTP_OK,
104                     "{\"ref\":\"refs/heads/featureB\"}"
105                 )
106             ).start(this.resource.port())
107         ) {
108             final Reference reference = new RtReference(
109                 new ApacheRequest(container.home()),
110                 new MkGithub().randomRepo(),
111                 "refs/heads/featureB"
112             );
113             MatcherAssert.assertThat(
114                 reference.json().getString("ref"),
115                 Matchers.is("refs/heads/featureB")
116             );
117             container.stop();
118         }
119     }
120 
121     /**
122      * RtReference should be able to return its ref.
123      * @throws Exception - If something goes wrong.
124      */
125     @Test
126     public void returnsRef() throws Exception {
127         try (
128             final MkContainer container = new MkGrizzlyContainer().next(
129                 new MkAnswer.Simple(
130                     HttpURLConnection.HTTP_OK,
131                     "{\"ref\":\"refs/heads/featureC\"}"
132                 )
133             ).start(this.resource.port())
134         ) {
135             final Reference reference = new RtReference(
136                 new ApacheRequest(container.home()),
137                 new MkGithub().randomRepo(),
138                 "refs/heads/featureC"
139             );
140             MatcherAssert.assertThat(
141                 reference.ref(),
142                 Matchers.is("refs/heads/featureC")
143             );
144             container.stop();
145         }
146     }
147 
148     /**
149      * RtReference should be able to return its owner repo.
150      * @throws Exception - If something goes wrong.
151      */
152     @Test
153     public void returnsOwner() throws Exception {
154         final Repo owner = new MkGithub().randomRepo();
155         try (
156             final MkContainer container = new MkGrizzlyContainer().next(
157                 new MkAnswer.Simple(
158                     HttpURLConnection.HTTP_OK,
159                     "{\"ref\":\"refs/heads/featureD\"}"
160                 )
161             ).start(this.resource.port());
162         ) {
163             final Reference reference = new RtReference(
164                 new ApacheRequest(container.home()),
165                 owner,
166                 "refs/heads/featureD"
167             );
168             MatcherAssert.assertThat(
169                 reference.repo(),
170                 Matchers.is(owner)
171             );
172             container.stop();
173         }
174     }
175 }