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.github.OAuthScope.Scope;
33  import java.util.Collections;
34  import org.hamcrest.MatcherAssert;
35  import org.hamcrest.Matchers;
36  import org.junit.Assume;
37  import org.junit.Test;
38  
39  /**
40   * Integration case for {@link Gist}.
41   * @author Yegor Bugayenko (yegor256@gmail.com)
42   * @version $Id: b8d9431da54a1eddd04db62cb18fe2fc1d3636a3 $
43   */
44  @OAuthScope(Scope.GIST)
45  public final class RtGistITCase {
46  
47      /**
48       * RtGist can text and write files.
49       * @throws Exception If some problem inside
50       */
51      @Test
52      public void readsAndWritesGists() throws Exception {
53          final String filename = "filename.txt";
54          final String content = "content of file";
55          final Gists gists = RtGistITCase.github().gists();
56          Gist.Smart smart = null;
57          try {
58              final Gist gist = gists.create(
59                  Collections.singletonMap(filename, content), false
60              );
61              smart = new Gist.Smart(gist);
62              final String file = smart.files().iterator().next();
63              gist.write(file, "hey, works for you this way?");
64              MatcherAssert.assertThat(
65                  gist.read(file),
66                  Matchers.startsWith("hey, works for ")
67              );
68          } finally {
69              if (smart != null) {
70                  gists.remove(smart.identifier());
71              }
72          }
73      }
74  
75      /**
76       * RtGist can fork a gist.
77       * @throws Exception If some problem inside
78       * @checkstyle MultipleStringLiterals (7 lines)
79       * @checkstyle LocalFinalVariableName (11 lines)
80       */
81      @Test
82      public void forksGist() throws Exception {
83          final String filename = "filename1.txt";
84          final String content = "content of file1";
85          final Gists gists1 = RtGistITCase.github("failsafe.github.key").gists();
86          final Gists gists2 = RtGistITCase.github("failsafe.github.key.second")
87              .gists();
88          final Gist gist = gists1.get(
89              gists2.create(Collections.singletonMap(filename, content), false)
90                  .identifier()
91          );
92          final Gist forked = gist.fork();
93          try {
94              MatcherAssert.assertThat(
95                  forked.read(filename),
96                  Matchers.equalTo(content)
97              );
98          } finally {
99              gists1.remove(forked.identifier());
100             gists2.remove(gist.identifier());
101         }
102     }
103 
104     /**
105      * Return github to test. Property "failsafe.github.key" is used
106      * for authentication.
107      * @return Github
108      */
109     private static Github github() {
110         return RtGistITCase.github("failsafe.github.key");
111     }
112 
113     /**
114      * Return github to test.
115      * @param property Name of a property with github key
116      * @return Github
117      */
118     private static Github github(final String property) {
119         final String key = System.getProperty(property);
120         Assume.assumeThat(
121             key,
122             Matchers.not(Matchers.is(Matchers.emptyOrNullString()))
123         );
124         return new RtGithub(key);
125     }
126 
127 }