View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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.mock;
31  
32  import com.jcabi.github.Gist;
33  import com.jcabi.github.Gists;
34  import java.io.IOException;
35  import java.util.Collections;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  
40  /**
41   * Test case for {@link MkGists}.
42   * @author Yegor Bugayenko (yegor256@gmail.com)
43   * @version $Id: af98cecaad64683ac88fc70240bcb50901b978b8 $
44   * @checkstyle MultipleStringLiterals (500 lines)
45   */
46  public final class MkGistsTest {
47  
48      /**
49       * MkGists can work with gists.
50       * @throws Exception If some problem inside
51       */
52      @Test
53      public void worksWithMockedGists() throws Exception {
54          final Gist gist = new MkGithub().gists().create(
55              Collections.singletonMap("test-file-name.txt", "none"), false
56          );
57          final String file = "t.txt";
58          gist.write(file, "hello, everybody!");
59          MatcherAssert.assertThat(
60              gist.read(file),
61              Matchers.startsWith("hello, ")
62          );
63      }
64  
65      /**
66       * This tests that the remove() method in MkGists is working fine.
67       * @throws Exception - if anything goes wrong.
68       */
69      @Test
70      public void removesGistByIdentifier() throws Exception {
71          final Gists gists = new MkGithub().gists();
72          final Gist gist = gists.create(
73              Collections.singletonMap("fileName.txt", "content"), false
74          );
75          MatcherAssert.assertThat(
76              gists.iterate(),
77              Matchers.hasItem(gist)
78          );
79          gists.remove(gist.identifier());
80          MatcherAssert.assertThat(
81              gists.iterate(),
82              Matchers.not(Matchers.hasItem(gist))
83          );
84      }
85      /**
86       * MkGists can work several gists.
87       * Test to check issue #128
88       * @throws Exception If some problem inside
89       */
90      @Test
91      public void worksWithSeveralGists() throws Exception {
92          final Gists gists = new MkGithub().gists();
93          final Gist gist = gists.create(
94              Collections.singletonMap("test-file-name.txt", "none"), false
95          );
96          final Gist othergist = gists.create(
97              Collections.singletonMap("test-file-name2.txt", ""), false
98          );
99          final String file = "t.txt";
100         gist.write(file, "hello, everybody!");
101         othergist.write(file, "bye, everybody!");
102         MatcherAssert.assertThat(
103             gist.read(file),
104             Matchers.startsWith("hello, ")
105         );
106         MatcherAssert.assertThat(
107             othergist.read(file),
108             Matchers.startsWith("bye, ")
109         );
110     }
111 
112     /**
113      * Test starring and star-checking of a gist.
114      * @throws Exception If some problem inside
115      */
116     @Test
117     public void testStar() throws Exception {
118         final Gist gist = new MkGithub().gists().create(
119             Collections.singletonMap("file-name.txt", ""), false
120         );
121         MatcherAssert.assertThat(
122             gist.starred(),
123             Matchers.equalTo(false)
124         );
125         gist.star();
126         MatcherAssert.assertThat(
127             gist.starred(),
128             Matchers.equalTo(true)
129         );
130     }
131 
132     /**
133      * Test unstarring and star-checking of a gist.
134      * @throws Exception If some problem inside
135      */
136     @Test
137     public void testUnstar() throws Exception {
138         final Gist gist = new MkGithub().gists().create(
139             Collections.singletonMap("file-name.txt", ""), false
140         );
141         MatcherAssert.assertThat(
142             gist.starred(),
143             Matchers.equalTo(false)
144         );
145         gist.star();
146         MatcherAssert.assertThat(
147             gist.starred(),
148             Matchers.equalTo(true)
149         );
150         gist.unstar();
151         MatcherAssert.assertThat(
152             gist.starred(),
153             Matchers.equalTo(false)
154         );
155     }
156 
157     /**
158      * MkGists can create gists with empty files.
159      * @throws IOException If some problem inside
160      */
161     @Test
162     public void createGistWithEmptyFile() throws IOException {
163         final String filename = "file.txt";
164         final Gist gist = new MkGithub().gists().create(
165             Collections.singletonMap(filename, ""), false
166         );
167         MatcherAssert.assertThat(
168             gist.read(filename),
169             Matchers.isEmptyString()
170         );
171     }
172 
173 }