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 org.hamcrest.MatcherAssert;
35 import org.hamcrest.Matchers;
36 import org.junit.Test;
37
38 /**
39 * Integration case for {@link Gists}.
40 * @author Mihai Andronache (amihaiemil@gmail.com)
41 * @version $Id: b6087350cac6eb0a64566863fb0983246161883c $
42 */
43 @OAuthScope(Scope.GIST)
44 public final class RtGistsITCase {
45 /**
46 * RtGists can create a gist.
47 * @throws Exception If some problem inside
48 */
49 @Test
50 public void createGist() throws Exception {
51 final String filename = "filename.txt";
52 final String content = "content of file";
53 final Gists gists = RtGistsITCase.gists();
54 final Gist gist = gists.create(
55 Collections.singletonMap(filename, content), false
56 );
57 final Gist.Smart smart = new Gist.Smart(gist);
58 MatcherAssert.assertThat(
59 smart.read(filename),
60 Matchers.equalTo(content)
61 );
62 gists.remove(smart.identifier());
63 }
64
65 /**
66 * RtGists can iterate all gists.
67 * @throws Exception If some problem inside
68 */
69 @Test
70 public void iterateGists() throws Exception {
71 final Gists gists = RtGistsITCase.gists();
72 final Gist gist = gists.create(
73 Collections.singletonMap("test.txt", "content"), false
74 );
75 MatcherAssert.assertThat(
76 gists.iterate(),
77 Matchers.hasItem(gist)
78 );
79 gists.remove(gist.identifier());
80 }
81 /**
82 * RtGists can get a single gist.
83 * @throws Exception If some problem inside
84 */
85 @Test
86 public void singleGist() throws Exception {
87 final String filename = "single-name.txt";
88 final Gists gists = RtGistsITCase.gists();
89 final Gist gist = gists.create(
90 Collections.singletonMap(filename, "body"), false
91 );
92 MatcherAssert.assertThat(
93 gists.get(gist.identifier()).identifier(),
94 Matchers.equalTo(gist.identifier())
95 );
96 gists.remove(gist.identifier());
97 }
98 /**
99 * This tests that RtGists can remove a gist by name.
100 * @throws Exception - if something goes wrong.
101 */
102 @Test
103 public void removesGistByName() throws Exception {
104 final Gists gists = RtGistsITCase.gists();
105 final Gist gist = gists.create(
106 Collections.singletonMap("fileName.txt", "content of test file"),
107 false
108 );
109 MatcherAssert.assertThat(
110 gists.iterate(),
111 Matchers.notNullValue()
112 );
113 gists.remove(gist.json().getString("id"));
114 MatcherAssert.assertThat(
115 gists.iterate(),
116 Matchers.not(Matchers.hasItem(gist))
117 );
118 }
119 /**
120 * Return gists to test.
121 * @return Gists
122 */
123 private static Gists gists() {
124 return new GithubIT().connect().gists();
125 }
126 }