View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import java.io.IOException;
8   import java.util.Collections;
9   import org.hamcrest.MatcherAssert;
10  import org.hamcrest.Matchers;
11  import org.junit.jupiter.api.Assumptions;
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * Integration case for {@link Gist}.
16   * @since 0.1
17   */
18  @OAuthScope(OAuthScope.Scope.GIST)
19  final class RtGistITCase {
20  
21      @Test
22      void readsAndWritesGists() throws IOException {
23          final Gists gists = RtGistITCase.github().gists();
24          Gist.Smart smart = null;
25          try {
26              final String content = "content of file";
27              final String filename = "filename.txt";
28              final Gist gist = gists.create(
29                  Collections.singletonMap(filename, content), false
30              );
31              smart = new Gist.Smart(gist);
32              final String file = smart.files().iterator().next();
33              gist.write(file, "hey, works for you this way?");
34              MatcherAssert.assertThat(
35                  "String does not start with expected value",
36                  gist.read(file),
37                  Matchers.startsWith("hey, works for ")
38              );
39          } finally {
40              if (smart != null) {
41                  gists.remove(smart.identifier());
42              }
43          }
44      }
45  
46      /**
47       * RtGist can fork a gist.
48       * @checkstyle MultipleStringLiterals (7 lines)
49       * @checkstyle LocalFinalVariableName (11 lines)
50       */
51      @Test
52      void forksGist() throws IOException {
53          final String filename = "filename1.txt";
54          final String content = "content of file1";
55          final Gists gists1 = RtGistITCase.github("failsafe.github.key").gists();
56          final Gists gists2 = RtGistITCase.github("failsafe.github.key.second")
57              .gists();
58          final Gist gist = gists1.get(
59              gists2.create(Collections.singletonMap(filename, content), false)
60                  .identifier()
61          );
62          final Gist forked = gist.fork();
63          try {
64              MatcherAssert.assertThat(
65                  "Values are not equal",
66                  forked.read(filename),
67                  Matchers.equalTo(content)
68              );
69          } finally {
70              gists1.remove(forked.identifier());
71              gists2.remove(gist.identifier());
72          }
73      }
74  
75      /**
76       * Return github to test. Property "failsafe.github.key" is used
77       * for authentication.
78       * @return GitHub
79       */
80      private static GitHub github() {
81          return RtGistITCase.github("failsafe.github.key");
82      }
83  
84      /**
85       * Return github to test.
86       * @param property Name of a property with github key
87       * @return GitHub
88       */
89      private static GitHub github(final String property) {
90          final String key = System.getProperty(property);
91          Assumptions.assumeTrue(
92              key != null && !key.isBlank(),
93              "GitHub key is required for this test"
94          );
95          return new RtGitHub(key);
96      }
97  
98  }