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.aspects.Tv;
33 import com.jcabi.github.OAuthScope.Scope;
34 import javax.json.Json;
35 import org.apache.commons.lang3.RandomStringUtils;
36 import org.hamcrest.MatcherAssert;
37 import org.hamcrest.Matchers;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41
42 /**
43 * Integration test for {@link RtReleaseAsset}.
44 *
45 * @author Carlos Miranda (miranda.cma@gmail.com)
46 * @version $Id: 55a0fc54b5749b36862f51c5ce8d185ee6ed9c84 $
47 * @since 0.8
48 * @checkstyle MultipleStringLiterals (300 lines)
49 */
50 @OAuthScope(Scope.REPO)
51 public final class RtReleaseAssetITCase {
52
53 /**
54 * Test repos.
55 */
56 private static Repos repos;
57
58 /**
59 * Test repo.
60 */
61 private static Repo repo;
62
63 /**
64 * RepoRule.
65 * @checkstyle VisibilityModifierCheck (3 lines)
66 */
67 private static RepoRule rule = new RepoRule();
68
69 /**
70 * Set up test fixtures.
71 * @throws Exception If some errors occurred.
72 */
73 @BeforeClass
74 public static void setUp() throws Exception {
75 final Github github = new GithubIT().connect();
76 repos = github.repos();
77 repo = rule.repo(repos);
78 repo.releases().create(
79 RandomStringUtils.randomAlphanumeric(Tv.TEN)
80 );
81 }
82
83 /**
84 * Tear down test fixtures.
85 * @throws Exception If some errors occurred.
86 */
87 @AfterClass
88 public static void tearDown() throws Exception {
89 if (repos != null && repo != null) {
90 repos.remove(repo.coordinates());
91 }
92 }
93
94 /**
95 * RtReleaseAsset can fetch as JSON object.
96 * @throws Exception if some problem inside
97 */
98 @Test
99 public void fetchAsJSON() throws Exception {
100 final String name = RandomStringUtils.randomAlphanumeric(Tv.TEN);
101 final Release release = repo.releases().create(name);
102 try {
103 MatcherAssert.assertThat(
104 release.json().getInt("id"),
105 Matchers.equalTo(release.number())
106 );
107 } finally {
108 release.delete();
109 }
110 }
111
112 /**
113 * RtReleaseAsset can execute patch request.
114 * @throws Exception if some problem inside
115 */
116 @Test
117 public void executePatchRequest() throws Exception {
118 final Release release = repo.releases().create(
119 String.format("v%s", RandomStringUtils.randomAlphanumeric(Tv.TEN))
120 );
121 final String desc = "Description of the release";
122 try {
123 release.patch(Json.createObjectBuilder().add("body", desc).build());
124 MatcherAssert.assertThat(
125 new Release.Smart(release).body(),
126 Matchers.startsWith(desc)
127 );
128 } finally {
129 release.delete();
130 }
131 }
132
133 /**
134 * RtReleaseAsset can do delete operation.
135 * @throws Exception If something goes wrong
136 */
137 @Test
138 public void removesReleaseAsset() throws Exception {
139 final Releases releases = repo.releases();
140 final String rname = RandomStringUtils.randomAlphanumeric(Tv.TEN);
141 final Release release = releases.create(rname);
142 try {
143 MatcherAssert.assertThat(
144 releases.get(release.number()),
145 Matchers.notNullValue()
146 );
147 } finally {
148 release.delete();
149 }
150 MatcherAssert.assertThat(
151 releases.iterate(),
152 Matchers.not(Matchers.contains(release))
153 );
154 }
155
156 }