View Javadoc
1   /**
2    * Copyright (c) 2013-2023, 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.http.Request;
33  import com.jcabi.http.mock.MkAnswer;
34  import com.jcabi.http.mock.MkContainer;
35  import com.jcabi.http.mock.MkGrizzlyContainer;
36  import com.jcabi.http.mock.MkQuery;
37  import com.jcabi.http.request.ApacheRequest;
38  import java.net.HttpURLConnection;
39  import java.net.URI;
40  import javax.json.Json;
41  import javax.json.JsonObject;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.After;
45  import org.junit.Before;
46  import org.junit.Rule;
47  import org.junit.Test;
48  import org.mockito.Mockito;
49  
50  /**
51   * Test case for {@link RtRelease}.
52   * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
53   * @version $Id: c1b4daf1815aefa5f37275bd9eec5318a083c97f $
54   */
55  public class RtReleaseTest {
56      /**
57       * An empty JSON string.
58       */
59      private static final String EMPTY_JSON = "{}";
60  
61      /**
62       * The rule for skipping test if there's BindException.
63       * @checkstyle VisibilityModifierCheck (3 lines)
64       */
65      @Rule
66      public final transient RandomPort resource = new RandomPort();
67  
68      /**
69       * A mock container used in test to mimic the Github server.
70       */
71      private transient MkContainer container;
72  
73      /**
74       * Setting up the test fixture.
75       */
76      @Before
77      public final void setUp() {
78          this.container = new MkGrizzlyContainer();
79      }
80  
81      /**
82       * Tear down the test fixture to return to the original state.
83       */
84      @After
85      public final void tearDown() {
86          this.container.stop();
87      }
88  
89      /**
90       * RtRelease can edit a release.
91       * @throws Exception If any problem during test execution occurs.
92       */
93      @Test
94      public final void editRelease() throws Exception {
95          this.container.next(
96              new MkAnswer.Simple(HttpURLConnection.HTTP_OK, EMPTY_JSON)
97          ).start(this.resource.port());
98          final RtRelease release = RtReleaseTest.release(this.container.home());
99          final JsonObject json = Json.createObjectBuilder()
100             .add("tag_name", "v1.0.0")
101             .build();
102         release.patch(json);
103         final MkQuery query = this.container.take();
104         MatcherAssert.assertThat(
105             query.method(),
106             Matchers.equalTo(Request.PATCH)
107         );
108         MatcherAssert.assertThat(
109             query.body(),
110             Matchers.equalTo(json.toString())
111         );
112     }
113 
114     /**
115      * RtRelease can delete a release.
116      * @throws Exception If any problems in the test occur.
117      */
118     @Test
119     public final void deleteRelease() throws Exception {
120         this.container.next(
121             new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, EMPTY_JSON)
122         ).start(this.resource.port());
123         final RtRelease release = RtReleaseTest.release(this.container.home());
124         release.delete();
125         MatcherAssert.assertThat(
126             this.container.take().method(),
127             Matchers.equalTo(Request.DELETE)
128         );
129     }
130 
131     /**
132      * RtRelese can execute PATCH request.
133      * @throws Exception if there is any problem
134      */
135     @Test
136     public final void executePatchRequest() throws Exception {
137         this.container.next(
138             new MkAnswer.Simple(HttpURLConnection.HTTP_OK, EMPTY_JSON)
139         ).start(this.resource.port());
140         final RtRelease release = RtReleaseTest.release(this.container.home());
141         release.patch(Json.createObjectBuilder().add("name", "v1")
142             .build()
143         );
144         MatcherAssert.assertThat(
145             this.container.take().method(),
146             Matchers.equalTo(Request.PATCH)
147         );
148     }
149 
150     /**
151      * Create a test release.
152      * @param uri REST API entry point.
153      * @return A test release.
154      */
155     private static RtRelease release(final URI uri) {
156         final Repo repo = Mockito.mock(Repo.class);
157         final Coordinates coords = Mockito.mock(Coordinates.class);
158         Mockito.doReturn(coords).when(repo).coordinates();
159         Mockito.doReturn("tstuser").when(coords).user();
160         Mockito.doReturn("tstbranch").when(coords).repo();
161         return new RtRelease(
162             new ApacheRequest(uri),
163             repo,
164             2
165         );
166     }
167 
168 }