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.github.mock.MkGithub;
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.request.FakeRequest;
37 import com.jcabi.http.request.JdkRequest;
38 import java.io.IOException;
39 import java.net.HttpURLConnection;
40 import java.util.Iterator;
41 import javax.json.Json;
42 import javax.json.JsonObject;
43 import org.hamcrest.MatcherAssert;
44 import org.hamcrest.Matchers;
45 import org.hamcrest.core.IsEqual;
46 import org.junit.Rule;
47 import org.junit.Test;
48
49
50
51
52
53
54
55
56 public final class RtBranchesTest {
57
58
59
60
61
62 @Rule
63 public final transient RandomPort resource = new RandomPort();
64
65
66
67
68
69 @Test
70 public void iteratesOverBranches() throws Exception {
71 final String firstname = "first";
72 final String firstsha = "a971b1aca044105897297b87b0b0983a54dd5817";
73 final String secondname = "second";
74 final String secondsha = "5d8dc2acf9c95d0d4e8881eebe04c2f0cbb249ff";
75 final MkAnswer answer = new MkAnswer.Simple(
76 HttpURLConnection.HTTP_OK,
77 Json.createArrayBuilder()
78 .add(branch(firstname, firstsha))
79 .add(branch(secondname, secondsha))
80 .build().toString()
81 );
82 try (
83 final MkContainer container = new MkGrizzlyContainer()
84 .next(answer)
85 .next(answer)
86 .start(this.resource.port())
87 ) {
88 final RtBranches branches = new RtBranches(
89 new JdkRequest(container.home()),
90 new MkGithub().randomRepo()
91 );
92 MatcherAssert.assertThat(
93 branches.iterate(),
94 Matchers.<Branch>iterableWithSize(2)
95 );
96 final Iterator<Branch> iter = branches.iterate().iterator();
97 final Branch first = iter.next();
98 MatcherAssert.assertThat(first.name(), Matchers.equalTo(firstname));
99 MatcherAssert.assertThat(
100 first.commit().sha(),
101 Matchers.equalTo(firstsha)
102 );
103 final Branch second = iter.next();
104 MatcherAssert.assertThat(
105 second.name(),
106 Matchers.equalTo(secondname)
107 );
108 MatcherAssert.assertThat(
109 second.commit().sha(),
110 Matchers.equalTo(secondsha)
111 );
112 container.stop();
113 }
114 }
115
116
117
118
119
120 @Test
121 public void findBranch() throws Exception {
122 final String thirdname = "third";
123 final String thirdsha = "297b87b0b0983a54dd5817a971b1aca044105897";
124 final String fourthname = "fourth";
125 final String fourthsha = "d0d4e8881eebe04c5d8dc2acf9c952f0cbb249ff";
126 final MkAnswer answer = new MkAnswer.Simple(
127 HttpURLConnection.HTTP_OK,
128 Json.createArrayBuilder()
129 .add(branch(thirdname, thirdsha))
130 .add(branch(fourthname, fourthsha))
131 .build().toString()
132 );
133 try (
134 final MkContainer container = new MkGrizzlyContainer()
135 .next(answer)
136 .next(answer)
137 .start(this.resource.port());
138 ) {
139 final RtBranches branches = new RtBranches(
140 new JdkRequest(container.home()),
141 new MkGithub().randomRepo()
142 );
143 MatcherAssert.assertThat(
144 "could not find branch correctly",
145 branches.find(fourthname).commit().sha(),
146 new IsEqual<>(
147 fourthsha
148 )
149 );
150 container.stop();
151 }
152 }
153
154
155
156
157
158 @Test
159 public void fetchesRepo() throws IOException {
160 final Repo repo = new MkGithub().randomRepo();
161 final RtBranches branch = new RtBranches(new FakeRequest(), repo);
162 final Coordinates coords = branch.repo().coordinates();
163 MatcherAssert.assertThat(
164 coords.user(),
165 Matchers.equalTo(repo.coordinates().user())
166 );
167 MatcherAssert.assertThat(
168 coords.repo(),
169 Matchers.equalTo(repo.coordinates().repo())
170 );
171 }
172
173
174
175
176
177
178
179 private static JsonObject branch(final String name, final String sha) {
180 return Json.createObjectBuilder()
181 .add("name", name)
182 .add(
183 "commit",
184 Json.createObjectBuilder()
185 .add("sha", sha)
186
187 .add("url", String.format("https://api.jcabi-github.invalid/repos/user/repo/commits/%s", sha))
188 )
189 .build();
190 }
191 }