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 com.jcabi.http.request.FakeRequest;
39 import com.jcabi.http.request.JdkRequest;
40 import java.net.HttpURLConnection;
41 import javax.json.Json;
42 import javax.json.JsonObject;
43 import org.hamcrest.MatcherAssert;
44 import org.hamcrest.Matchers;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.mockito.Mockito;
48
49 /**
50 * Test case for {@link RtReleases}.
51 * @author Paul Polishchuk (ppol@ua.fm)
52 * @author Paulo Lobo (pauloeduardolobo@gmail.com)
53 * @version $Id: 4fbbf61206a702ef0c6a845b39d9fda938f88ee0 $
54 * @checkstyle MultipleStringLiterals (500 lines)
55 * @since 0.8
56 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
57 */
58 public final class RtReleasesTest {
59
60 /**
61 * The rule for skipping test if there's BindException.
62 * @checkstyle VisibilityModifierCheck (3 lines)
63 */
64 @Rule
65 public final transient RandomPort resource = new RandomPort();
66
67 /**
68 * RtReleases can fetch empty list of releases.
69 */
70 @Test
71 public void canFetchEmptyListOfReleases() {
72 final Releases releases = new RtReleases(
73 new FakeRequest().withBody("[]"),
74 RtReleasesTest.repo()
75 );
76 MatcherAssert.assertThat(
77 releases.iterate(),
78 Matchers.emptyIterable()
79 );
80 }
81
82 /**
83 * RtReleases can fetch non empty list of releases.
84 */
85 @Test
86 public void canFetchNonEmptyListOfReleases() {
87 final int number = 1;
88 final Releases releases = new RtReleases(
89 new FakeRequest().withBody(
90 Json.createArrayBuilder().add(
91 Json.createObjectBuilder()
92 .add("id", number)
93 .add("tag_name", "v1.0.0")
94 .add("name", "v1.0.0")
95 .add("body", "Release")
96 ).build().toString()
97 ),
98 RtReleasesTest.repo()
99 );
100 MatcherAssert.assertThat(
101 releases.iterate().iterator().next().number(),
102 Matchers.equalTo(number)
103 );
104 }
105
106 /**
107 * RtReleases can fetch a single release.
108 */
109 @Test
110 public void canFetchSingleRelease() {
111 final Releases releases = new RtReleases(
112 new FakeRequest(), RtReleasesTest.repo()
113 );
114 MatcherAssert.assertThat(releases.get(1), Matchers.notNullValue());
115 }
116
117 /**
118 * RtReleases can create a release.
119 * @throws Exception If some problem inside
120 */
121 @Test
122 public void canCreateRelease() throws Exception {
123 final String tag = "v1.0.0";
124 final String rel = release(tag).toString();
125 try (
126 final MkContainer container = new MkGrizzlyContainer().next(
127 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, rel)
128 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, rel))
129 .start(this.resource.port())
130 ) {
131 final RtReleases releases = new RtReleases(
132 new JdkRequest(container.home()),
133 repo()
134 );
135 final Release release = releases.create(tag);
136 MatcherAssert.assertThat(
137 container.take().method(),
138 Matchers.equalTo(Request.POST)
139 );
140 MatcherAssert.assertThat(
141 release.json().getString("tag_name"),
142 Matchers.equalTo(tag)
143 );
144 container.stop();
145 }
146 }
147
148 /**
149 * RtReleases can delete a release.
150 * @throws Exception If some problem inside
151 */
152 @Test
153 public void canDeleteRelease() throws Exception {
154 try (
155 final MkContainer container = new MkGrizzlyContainer().next(
156 new MkAnswer.Simple(
157 HttpURLConnection.HTTP_NO_CONTENT,
158 ""
159 )
160 ).start(this.resource.port());
161 ) {
162 final Releases releases = new RtReleases(
163 new ApacheRequest(container.home()),
164 RtReleasesTest.repo()
165 );
166 releases.remove(1);
167 final MkQuery query = container.take();
168 MatcherAssert.assertThat(
169 query.uri().toString(),
170 Matchers.endsWith("/releases/1")
171 );
172 MatcherAssert.assertThat(
173 query.method(),
174 Matchers.equalTo(Request.DELETE)
175 );
176 container.stop();
177 }
178 }
179
180 /**
181 * Create and return repo for testing.
182 * @return Repo
183 */
184 private static Repo repo() {
185 final Repo repo = Mockito.mock(Repo.class);
186 Mockito.doReturn(new Coordinates.Simple("test", "releases"))
187 .when(repo).coordinates();
188 return repo;
189 }
190
191 /**
192 * Create and return JsonObject to test.
193 * @param tag The tag name of the release
194 * @return JsonObject
195 */
196 private static JsonObject release(final String tag) {
197 return Json.createObjectBuilder()
198 .add("id", 1)
199 .add("tag_name", tag)
200 .build();
201 }
202 }