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.mock;
31  
32  import com.jcabi.aspects.Tv;
33  import com.jcabi.github.Comment;
34  import com.jcabi.github.Github;
35  import com.jcabi.github.Issue;
36  import com.jcabi.github.Repo;
37  import com.jcabi.github.Repos;
38  import com.jcabi.github.User;
39  import com.jcabi.immutable.ArrayMap;
40  import com.jcabi.log.VerboseCallable;
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.Date;
44  import java.util.concurrent.Callable;
45  import java.util.concurrent.ExecutorService;
46  import java.util.concurrent.Executors;
47  import org.hamcrest.MatcherAssert;
48  import org.hamcrest.Matchers;
49  import org.hamcrest.core.IsEqual;
50  import org.junit.Test;
51  
52  /**
53   * Test case for {@link MkGithub}.
54   * @author Yegor Bugayenko (yegor256@gmail.com)
55   * @version $Id: 1f161a1188dc2c6fbbe0e710834b4e907c51848f $
56   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
57   */
58  public final class MkGithubTest {
59      /**
60       * Settings to use when creating temporary repos.
61       */
62      private static final Repos.RepoCreate NEW_REPO_SETTINGS =
63          new Repos.RepoCreate(
64              "test",
65              false
66          );
67  
68      /**
69       * MkGithub can work.
70       * @throws Exception If some problem inside
71       */
72      @Test
73      public void worksWithMockedData() throws Exception {
74          final Repo repo = new MkGithub().repos().create(NEW_REPO_SETTINGS);
75          final Issue issue = repo.issues().create("hey", "how are you?");
76          final Comment comment = issue.comments().post("hey, works?");
77          MatcherAssert.assertThat(
78              new Comment.Smart(comment).body(),
79              Matchers.startsWith("hey, ")
80          );
81          MatcherAssert.assertThat(
82              repo.issues().get(issue.number()).comments().iterate(new Date(0L)),
83              Matchers.<Comment>iterableWithSize(1)
84          );
85          MatcherAssert.assertThat(
86              new User.Smart(new Comment.Smart(comment).author()).login(),
87              Matchers.equalTo(
88                  new User.Smart(repo.github().users().self()).login()
89              )
90          );
91      }
92  
93      /**
94       * MkGithub can relogin.
95       *
96       * @throws Exception if some problem inside
97       */
98      @Test
99      public void canRelogin() throws Exception {
100         final String login = "mark";
101         final MkGithub github = new MkGithub();
102         final Repo repo = github.repos().create(NEW_REPO_SETTINGS);
103         final Issue issue = repo.issues().create("title", "Found a bug");
104         final Comment comment = github
105             .relogin(login)
106             .repos()
107             .get(repo.coordinates())
108             .issues()
109             .get(issue.number())
110             .comments()
111             .post("Nice change");
112         MatcherAssert.assertThat(
113             new User.Smart(new Comment.Smart(comment).author()).login(),
114             Matchers.not(
115                 Matchers.equalTo(
116                     new User.Smart(repo.github().users().self()).login()
117                 )
118             )
119         );
120         MatcherAssert.assertThat(
121             new User.Smart(new Comment.Smart(comment).author()).login(),
122             Matchers.equalTo(login)
123         );
124     }
125 
126     /**
127      * MkGithub can retrieve the markdown.
128      *
129      * @throws Exception if a problem occurs.
130      */
131     @Test
132     public void retrievesMarkdown() throws Exception {
133         final Github github = new MkGithub();
134         MatcherAssert.assertThat(
135             github.markdown(),
136             Matchers.notNullValue()
137         );
138     }
139 
140     /**
141      * MkGithub can create random repo.
142      * @throws Exception if some problem inside
143      */
144     @Test
145     public void canCreateRandomRepo() throws Exception {
146         final MkGithub github = new MkGithub();
147         final Repo repo = github.randomRepo();
148         MatcherAssert.assertThat(
149             github.repos().get(repo.coordinates()).coordinates(),
150             Matchers.equalTo(repo.coordinates())
151         );
152     }
153 
154     /**
155      * MkGithub can handle multiple threads in parallel.
156      * @throws Exception if some problem inside
157      */
158     @Test
159     public void canHandleMultipleThreads() throws Exception {
160         final Repo repo = new MkGithub().randomRepo();
161         final Callable<Void> task = new VerboseCallable<>(
162             () -> {
163                 repo.issues().create("", "");
164                 return null;
165             }
166         );
167         final int threads = Tv.HUNDRED;
168         final Collection<Callable<Void>> tasks =
169             new ArrayList<>(threads);
170         for (int idx = 0; idx < threads; ++idx) {
171             tasks.add(task);
172         }
173         final ExecutorService svc = Executors.newFixedThreadPool(threads);
174         svc.invokeAll(tasks);
175         MatcherAssert.assertThat(
176             repo.issues().iterate(new ArrayMap<>()),
177             Matchers.<Issue>iterableWithSize(threads)
178         );
179     }
180 
181     /**
182      * Can retrieve users.
183      * @throws Exception If something goes wrong
184      */
185     @Test
186     public void canRetrieveUsers() throws Exception {
187         MatcherAssert.assertThat(
188             "Retrieved inexistent user",
189             new User.Smart(
190                 new MkGithub().users().get("other")
191             ).exists(),
192             new IsEqual<>(false)
193         );
194     }
195 }