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;
31
32 import com.jcabi.http.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.mock.MkQuery;
37 import com.jcabi.http.request.ApacheRequest;
38 import java.net.HttpURLConnection;
39 import javax.json.Json;
40 import javax.json.JsonObject;
41 import org.hamcrest.MatcherAssert;
42 import org.hamcrest.Matchers;
43 import org.hamcrest.core.IsEqual;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47
48
49
50
51
52
53
54
55
56 public final class RtReposTest {
57
58
59
60
61
62 @Rule
63 public final transient RandomPort resource = new RandomPort();
64
65
66
67
68 @Rule
69 public final transient RepoRule rule = new RepoRule();
70
71
72
73
74
75 @Test
76 public void createRepo() throws Exception {
77 final String owner = "test-owner";
78 final String name = "test-repo";
79 final String response = response(owner, name).toString();
80 try (
81 final MkContainer container = new MkGrizzlyContainer().next(
82 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, response)
83 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, response))
84 .start(this.resource.port())
85 ) {
86 final RtRepos repos = new RtRepos(
87 Mockito.mock(Github.class),
88 new ApacheRequest(container.home())
89 );
90 final Repo repo = this.rule.repo(repos);
91 MatcherAssert.assertThat(
92 container.take().method(),
93 Matchers.equalTo(Request.POST)
94 );
95 MatcherAssert.assertThat(
96 repo.coordinates(),
97 new IsEqual<>(new Coordinates.Simple(owner, name))
98 );
99 container.stop();
100 }
101 }
102
103
104
105
106
107 @Test
108 public void iterateRepos() throws Exception {
109 final String identifier = "1";
110 try (
111 final MkContainer container = new MkGrizzlyContainer().next(
112 new MkAnswer.Simple(
113 HttpURLConnection.HTTP_OK,
114 Json.createArrayBuilder()
115 .add(response("octocat", identifier))
116 .add(response("dummy", "2"))
117 .build().toString()
118 )
119 ).start(this.resource.port())
120 ) {
121 final RtRepos repos = new RtRepos(
122 Mockito.mock(Github.class),
123 new ApacheRequest(container.home())
124 );
125 MatcherAssert.assertThat(
126 repos.iterate(identifier),
127 Matchers.<Repo>iterableWithSize(2)
128 );
129 container.stop();
130 }
131 }
132
133
134
135
136
137 @Test
138 public void removeRepo() throws Exception {
139 try (
140 final MkContainer container = new MkGrizzlyContainer().next(
141 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
142 ).start(this.resource.port())
143 ) {
144 final Repos repos = new RtRepos(
145 Mockito.mock(Github.class),
146 new ApacheRequest(container.home())
147 );
148 repos.remove(new Coordinates.Simple("", ""));
149 final MkQuery query = container.take();
150 MatcherAssert.assertThat(
151 query.method(),
152 Matchers.equalTo(Request.DELETE)
153 );
154 MatcherAssert.assertThat(
155 query.body(),
156 Matchers.is(Matchers.emptyString())
157 );
158 container.stop();
159 }
160 }
161
162
163
164
165
166
167
168
169 private static JsonObject response(
170 final String owner, final String name) {
171 return Json.createObjectBuilder()
172 .add("name", name)
173 .add("full_name", String.format("%s/%s", owner, name))
174 .add(
175 "owner",
176 Json.createObjectBuilder().add("login", owner).build()
177 )
178 .build();
179 }
180
181 }