1
2
3
4
5 package com.jcabi.github;
6
7 import com.jcabi.github.mock.MkGitHub;
8 import com.jcabi.http.mock.MkAnswer;
9 import com.jcabi.http.mock.MkContainer;
10 import com.jcabi.http.mock.MkGrizzlyContainer;
11 import com.jcabi.http.request.FakeRequest;
12 import com.jcabi.http.request.JdkRequest;
13 import jakarta.json.Json;
14 import jakarta.json.JsonObject;
15 import java.io.IOException;
16 import java.net.HttpURLConnection;
17 import java.util.Iterator;
18 import org.hamcrest.MatcherAssert;
19 import org.hamcrest.Matchers;
20 import org.hamcrest.core.IsEqual;
21 import org.junit.jupiter.api.Test;
22 import org.junit.jupiter.api.extension.ExtendWith;
23
24
25
26
27
28
29 @ExtendWith(RandomPort.class)
30 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
31 final class RtBranchesTest {
32
33
34
35
36
37 @Test
38 void iteratesOverBranches() throws IOException {
39 final String firstname = "first";
40 final String firstsha = "a971b1aca044105897297b87b0b0983a54dd5817";
41 final String secondname = "second";
42 final String secondsha = "5d8dc2acf9c95d0d4e8881eebe04c2f0cbb249ff";
43 final MkAnswer answer = new MkAnswer.Simple(
44 HttpURLConnection.HTTP_OK,
45 Json.createArrayBuilder()
46 .add(RtBranchesTest.branch(firstname, firstsha))
47 .add(RtBranchesTest.branch(secondname, secondsha))
48 .build().toString()
49 );
50 try (
51 MkContainer container = new MkGrizzlyContainer()
52 .next(answer)
53 .next(answer)
54 .start(RandomPort.port())
55 ) {
56 final RtBranches branches = new RtBranches(
57 new JdkRequest(container.home()),
58 new MkGitHub().randomRepo()
59 );
60 MatcherAssert.assertThat(
61 "Collection size is incorrect",
62 branches.iterate(),
63 Matchers.iterableWithSize(2)
64 );
65 final Iterator<Branch> iter = branches.iterate().iterator();
66 final Branch first = iter.next();
67 MatcherAssert.assertThat(
68 "Values are not equal", first.name(), Matchers.equalTo(firstname)
69 );
70 MatcherAssert.assertThat(
71 "Values are not equal",
72 first.commit().sha(),
73 Matchers.equalTo(firstsha)
74 );
75 final Branch second = iter.next();
76 MatcherAssert.assertThat(
77 "Values are not equal",
78 second.name(),
79 Matchers.equalTo(secondname)
80 );
81 MatcherAssert.assertThat(
82 "Values are not equal",
83 second.commit().sha(),
84 Matchers.equalTo(secondsha)
85 );
86 container.stop();
87 }
88 }
89
90 @Test
91 void findBranch() throws IOException {
92 final String thirdname = "third";
93 final String thirdsha = "297b87b0b0983a54dd5817a971b1aca044105897";
94 final String fourthname = "fourth";
95 final String fourthsha = "d0d4e8881eebe04c5d8dc2acf9c952f0cbb249ff";
96 final MkAnswer answer = new MkAnswer.Simple(
97 HttpURLConnection.HTTP_OK,
98 Json.createArrayBuilder()
99 .add(RtBranchesTest.branch(thirdname, thirdsha))
100 .add(RtBranchesTest.branch(fourthname, fourthsha))
101 .build().toString()
102 );
103 try (
104 MkContainer container = new MkGrizzlyContainer()
105 .next(answer)
106 .next(answer)
107 .start(RandomPort.port())
108 ) {
109 final RtBranches branches = new RtBranches(
110 new JdkRequest(container.home()),
111 new MkGitHub().randomRepo()
112 );
113 MatcherAssert.assertThat(
114 "could not find branch correctly",
115 branches.find(fourthname).commit().sha(),
116 new IsEqual<>(
117 fourthsha
118 )
119 );
120 container.stop();
121 }
122 }
123
124
125
126
127
128 @Test
129 void fetchesRepo() throws IOException {
130 final Repo repo = new MkGitHub().randomRepo();
131 final RtBranches branch = new RtBranches(new FakeRequest(), repo);
132 final Coordinates coords = branch.repo().coordinates();
133 MatcherAssert.assertThat(
134 "Values are not equal",
135 coords.user(),
136 Matchers.equalTo(repo.coordinates().user())
137 );
138 MatcherAssert.assertThat(
139 "Values are not equal",
140 coords.repo(),
141 Matchers.equalTo(repo.coordinates().repo())
142 );
143 }
144
145
146
147
148
149
150
151 private static JsonObject branch(final String name, final String sha) {
152 return Json.createObjectBuilder()
153 .add("name", name)
154 .add(
155 "commit",
156 Json.createObjectBuilder()
157 .add("sha", sha)
158 .add(
159 "url",
160 String.format(
161 "https://api.jcabi-github.invalid/repos/user/repo/commits/%s",
162 sha
163 )
164 )
165 )
166 .build();
167 }
168 }