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.github.OAuthScope.Scope;
33 import java.util.Collections;
34 import javax.json.Json;
35 import org.hamcrest.MatcherAssert;
36 import org.hamcrest.Matchers;
37 import org.junit.Test;
38
39 /**
40 * Integration test for {@link RtGistComment}.
41 * @author Giang Le (giang@vn-smartsolutions.com)
42 * @version $Id: 9ea977a82834ba7478ae221098a70be8b044ef75 $
43 * @see <a href="https://developer.github.com/v3/gists/comments/">Gist Comments API</a>
44 * @since 0.8
45 */
46 @OAuthScope(Scope.GIST)
47 public final class RtGistCommentITCase {
48
49 /**
50 * RtGistComment can remove itself.
51 * @throws Exception if some problem inside
52 */
53 @Test
54 public void removeItself() throws Exception {
55 final Gist gist = RtGistCommentITCase.gist();
56 final String body = "comment body";
57 final GistComments comments = gist.comments();
58 final GistComment comment = comments.post(body);
59 MatcherAssert.assertThat(
60 comments.iterate(),
61 Matchers.hasItem(comment)
62 );
63 comment.remove();
64 MatcherAssert.assertThat(
65 comments.iterate(),
66 Matchers.not(Matchers.hasItem(comment))
67 );
68 gist.github().gists().remove(gist.identifier());
69 }
70
71 /**
72 * RtGistComment can fetch as JSON object.
73 * @throws Exception if some problem inside
74 */
75 @Test
76 public void fetchAsJSON() throws Exception {
77 final Gist gist = RtGistCommentITCase.gist();
78 final GistComments comments = gist.comments();
79 final GistComment comment = comments.post("comment");
80 MatcherAssert.assertThat(
81 comment.json().getInt("id"),
82 Matchers.equalTo(comment.number())
83 );
84 comment.remove();
85 gist.github().gists().remove(gist.identifier());
86 }
87
88 /**
89 * RtGistComment can execute patch request.
90 * @throws Exception if some problem inside
91 */
92 @Test
93 public void executePatchRequest() throws Exception {
94 final Gist gist = RtGistCommentITCase.gist();
95 final GistComments comments = gist.comments();
96 final GistComment comment = comments.post("test comment");
97 MatcherAssert.assertThat(
98 new GistComment.Smart(comment).body(),
99 Matchers.startsWith("test")
100 );
101 comment.patch(Json.createObjectBuilder().add("body", "hi!").build());
102 MatcherAssert.assertThat(
103 new GistComment.Smart(comment).body(),
104 Matchers.startsWith("hi")
105 );
106 comment.remove();
107 gist.github().gists().remove(gist.identifier());
108 }
109
110 /**
111 * RtGistComment can change comment body.
112 * @throws Exception if some problem inside
113 */
114 @Test
115 public void changeCommentBody() throws Exception {
116 final Gist gist = RtGistCommentITCase.gist();
117 final GistComments comments = gist.comments();
118 final GistComment comment = comments.post("hi there");
119 MatcherAssert.assertThat(
120 new GistComment.Smart(comment).body(),
121 Matchers.endsWith("there")
122 );
123 new GistComment.Smart(comment).body("hello there");
124 MatcherAssert.assertThat(
125 new GistComment.Smart(comment).body(),
126 Matchers.startsWith("hello")
127 );
128 comment.remove();
129 gist.github().gists().remove(gist.identifier());
130 }
131
132 /**
133 * Return gist to test.
134 * @return Gist
135 * @throws Exception If some problem inside
136 */
137 private static Gist gist() throws Exception {
138 return new GithubIT()
139 .connect()
140 .gists()
141 .create(
142 Collections.singletonMap("file.txt", "file content"), false
143 );
144 }
145 }