1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
54
55
56
57
58 public final class MkGithubTest {
59
60
61
62 private static final Repos.RepoCreate NEW_REPO_SETTINGS =
63 new Repos.RepoCreate(
64 "test",
65 false
66 );
67
68
69
70
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
95
96
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
128
129
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
142
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
156
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
183
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 }