View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github.mock;
6   
7   import com.jcabi.github.Gist;
8   import com.jcabi.github.Gists;
9   import java.io.IOException;
10  import java.util.Collections;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link MkGists}.
17   * @since 0.1
18   * @checkstyle MultipleStringLiterals (500 lines)
19   */
20  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
21  final class MkGistsTest {
22  
23      @Test
24      void worksWithMockedGists() throws IOException {
25          final Gist gist = new MkGitHub().gists().create(
26              Collections.singletonMap("test-file-name.txt", "none"), false
27          );
28          final String file = "t.txt";
29          gist.write(file, "hello, everybody!");
30          MatcherAssert.assertThat(
31              "String does not start with expected value",
32              gist.read(file),
33              Matchers.startsWith("hello, ")
34          );
35      }
36  
37      /**
38       * This tests that the remove() method in MkGists is working fine.
39       */
40      @Test
41      void removesGistByIdentifier() throws IOException {
42          final Gists gists = new MkGitHub().gists();
43          final Gist gist = gists.create(
44              Collections.singletonMap("fileName.txt", "content"), false
45          );
46          MatcherAssert.assertThat(
47              "Collection does not contain expected item",
48              gists.iterate(),
49              Matchers.hasItem(gist)
50          );
51          gists.remove(gist.identifier());
52          MatcherAssert.assertThat(
53              "Collection does not contain expected item",
54              gists.iterate(),
55              Matchers.not(Matchers.hasItem(gist))
56          );
57      }
58  
59      /**
60       * MkGists can work several gists.
61       * Test to check issue #128
62       */
63      @Test
64      void worksWithSeveralGists() throws IOException {
65          final Gists gists = new MkGitHub().gists();
66          final Gist gist = gists.create(
67              Collections.singletonMap("test-file-name.txt", "none"), false
68          );
69          final Gist othergist = gists.create(
70              Collections.singletonMap("test-file-name2.txt", ""), false
71          );
72          final String file = "t.txt";
73          gist.write(file, "hello, everybody!");
74          othergist.write(file, "bye, everybody!");
75          MatcherAssert.assertThat(
76              "String does not start with expected value",
77              gist.read(file),
78              Matchers.startsWith("hello, ")
79          );
80          MatcherAssert.assertThat(
81              "String does not start with expected value",
82              othergist.read(file),
83              Matchers.startsWith("bye, ")
84          );
85      }
86  
87      /**
88       * Test starring and star-checking of a gist.
89       */
90      @Test
91      void testStar() throws IOException {
92          final Gist gist = new MkGitHub().gists().create(
93              Collections.singletonMap("file-name.txt", ""), false
94          );
95          MatcherAssert.assertThat(
96              "Values are not equal",
97              gist.starred(),
98              Matchers.equalTo(false)
99          );
100         gist.star();
101         MatcherAssert.assertThat(
102             "Values are not equal",
103             gist.starred(),
104             Matchers.equalTo(true)
105         );
106     }
107 
108     /**
109      * Test unstarring and star-checking of a gist.
110      */
111     @Test
112     void testUnstar() throws IOException {
113         final Gist gist = new MkGitHub().gists().create(
114             Collections.singletonMap("file-name.txt", ""), false
115         );
116         MatcherAssert.assertThat(
117             "Values are not equal",
118             gist.starred(),
119             Matchers.equalTo(false)
120         );
121         gist.star();
122         MatcherAssert.assertThat(
123             "Values are not equal",
124             gist.starred(),
125             Matchers.equalTo(true)
126         );
127         gist.unstar();
128         MatcherAssert.assertThat(
129             "Values are not equal",
130             gist.starred(),
131             Matchers.equalTo(false)
132         );
133     }
134 
135     /**
136      * MkGists can create gists with empty files.
137      * @throws IOException If some problem inside
138      */
139     @Test
140     void createGistWithEmptyFile() throws IOException {
141         final String filename = "file.txt";
142         final Gist gist = new MkGitHub().gists().create(
143             Collections.singletonMap(filename, ""), false
144         );
145         MatcherAssert.assertThat(
146             "Values are not equal",
147             gist.read(filename),
148             Matchers.is(Matchers.emptyString())
149         );
150     }
151 
152 }