1
2
3
4
5 package com.jcabi.github.mock;
6
7 import com.jcabi.github.Comment;
8 import com.jcabi.github.GitHub;
9 import com.jcabi.github.Issue;
10 import com.jcabi.github.Repo;
11 import com.jcabi.github.Repos;
12 import com.jcabi.github.User;
13 import com.jcabi.immutable.ArrayMap;
14 import com.jcabi.log.VerboseCallable;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Date;
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
22 import org.hamcrest.MatcherAssert;
23 import org.hamcrest.Matchers;
24 import org.hamcrest.core.IsEqual;
25 import org.junit.jupiter.api.Test;
26
27
28
29
30
31
32 final class MkGitHubTest {
33
34
35
36 private static final Repos.RepoCreate NEW_REPO_SETTINGS =
37 new Repos.RepoCreate(
38 "test",
39 false
40 );
41
42 @Test
43 void worksWithMockedData() throws IOException {
44 final Repo repo = new MkGitHub().repos().create(MkGitHubTest.NEW_REPO_SETTINGS);
45 final Issue issue = repo.issues().create("hey", "how are you?");
46 final Comment comment = issue.comments().post("hey, works?");
47 MatcherAssert.assertThat(
48 "String does not start with expected value",
49 new Comment.Smart(comment).body(),
50 Matchers.startsWith("hey, ")
51 );
52 MatcherAssert.assertThat(
53 "Collection size is incorrect",
54 repo.issues().get(issue.number()).comments().iterate(new Date(0L)),
55 Matchers.iterableWithSize(1)
56 );
57 MatcherAssert.assertThat(
58 "Values are not equal",
59 new User.Smart(new Comment.Smart(comment).author()).login(),
60 Matchers.equalTo(
61 new User.Smart(repo.github().users().self()).login()
62 )
63 );
64 }
65
66 @Test
67 void canRelogin() throws IOException {
68 final String login = "mark";
69 final MkGitHub github = new MkGitHub();
70 final Repo repo = github.repos().create(MkGitHubTest.NEW_REPO_SETTINGS);
71 final Issue issue = repo.issues().create("title", "Found a bug");
72 final Comment comment = github
73 .relogin(login)
74 .repos()
75 .get(repo.coordinates())
76 .issues()
77 .get(issue.number())
78 .comments()
79 .post("Nice change");
80 MatcherAssert.assertThat(
81 "Values are not equal",
82 new User.Smart(new Comment.Smart(comment).author()).login(),
83 Matchers.not(
84 Matchers.equalTo(
85 new User.Smart(repo.github().users().self()).login()
86 )
87 )
88 );
89 MatcherAssert.assertThat(
90 "Values are not equal",
91 new User.Smart(new Comment.Smart(comment).author()).login(),
92 Matchers.equalTo(login)
93 );
94 }
95
96 @Test
97 void retrievesMarkdown() throws IOException {
98 final GitHub github = new MkGitHub();
99 MatcherAssert.assertThat(
100 "Value is null",
101 github.markdown(),
102 Matchers.notNullValue()
103 );
104 }
105
106 @Test
107 void canCreateRandomRepo() throws IOException {
108 final MkGitHub github = new MkGitHub();
109 final Repo repo = github.randomRepo();
110 MatcherAssert.assertThat(
111 "Values are not equal",
112 github.repos().get(repo.coordinates()).coordinates(),
113 Matchers.equalTo(repo.coordinates())
114 );
115 }
116
117 @Test
118 @SuppressWarnings("PMD.CloseResource")
119 void canHandleMultipleThreads() throws IOException, InterruptedException {
120 final Repo repo = new MkGitHub().randomRepo();
121 final Callable<Void> task = new VerboseCallable<>(
122 () -> {
123 repo.issues().create("", "");
124 return null;
125 }
126 );
127 final int threads = 100;
128 final Collection<Callable<Void>> tasks =
129 new ArrayList<>(threads);
130 for (int idx = 0; idx < threads; ++idx) {
131 tasks.add(task);
132 }
133 final ExecutorService svc = Executors.newFixedThreadPool(threads);
134 svc.invokeAll(tasks);
135 MatcherAssert.assertThat(
136 "Collection size is incorrect",
137 repo.issues().iterate(new ArrayMap<>()),
138 Matchers.iterableWithSize(threads)
139 );
140 }
141
142 @Test
143 void canRetrieveUsers() throws IOException {
144 MatcherAssert.assertThat(
145 "Retrieved inexistent user",
146 new User.Smart(
147 new MkGitHub().users().get("other")
148 ).exists(),
149 new IsEqual<>(false)
150 );
151 }
152 }