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.Request;
34 import com.jcabi.http.mock.MkAnswer;
35 import com.jcabi.http.mock.MkContainer;
36 import com.jcabi.http.mock.MkGrizzlyContainer;
37 import com.jcabi.http.request.ApacheRequest;
38 import java.net.HttpURLConnection;
39 import org.hamcrest.MatcherAssert;
40 import org.hamcrest.Matchers;
41 import org.junit.Rule;
42 import org.junit.Test;
43
44
45
46
47
48
49
50
51 public final class RtReferencesTest {
52
53
54
55
56
57 @Rule
58 public final transient RandomPort resource = new RandomPort();
59
60
61
62
63
64 @Test
65 public void createsReference() throws Exception {
66 try (
67 final MkContainer container = new MkGrizzlyContainer().next(
68 new MkAnswer.Simple(
69 HttpURLConnection.HTTP_CREATED,
70 "{\"ref\":\"refs/heads/feature-a\"}"
71 )
72 ).start(this.resource.port())
73 ) {
74 final References refs = new RtReferences(
75 new ApacheRequest(container.home()),
76 new MkGithub().randomRepo()
77 );
78 MatcherAssert.assertThat(
79 refs.create("abceefgh3456", "refs/heads/feature-a"),
80 Matchers.instanceOf(Reference.class)
81 );
82 MatcherAssert.assertThat(
83 container.take().method(),
84 Matchers.equalTo(Request.POST)
85 );
86 container.stop();
87 }
88 }
89
90
91
92
93
94 @Test
95 public void iteratesReferences() throws Exception {
96 try (
97 final MkContainer container = new MkGrizzlyContainer().next(
98 new MkAnswer.Simple(
99 HttpURLConnection.HTTP_OK,
100 "{\"ref\":\"refs/heads/feature-a\"}"
101 )
102 ).start(this.resource.port())
103 ) {
104 final References refs = new RtReferences(
105 new ApacheRequest(container.home()),
106 new MkGithub().randomRepo()
107 );
108 MatcherAssert.assertThat(
109 refs.iterate(),
110 Matchers.notNullValue()
111 );
112 container.stop();
113 }
114 }
115
116
117
118
119
120 @Test
121 public void removesReference() throws Exception {
122 try (
123 final MkContainer container = new MkGrizzlyContainer().next(
124 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
125 ).start(this.resource.port())
126 ) {
127 final References refs = new RtReferences(
128 new ApacheRequest(container.home()),
129 new MkGithub().randomRepo()
130 );
131 refs.remove("heads/feature-a");
132 MatcherAssert.assertThat(
133 container.take().method(),
134 Matchers.equalTo(Request.DELETE)
135 );
136 container.stop();
137 }
138 }
139
140
141
142
143
144 @Test
145 public void iteratesTags() throws Exception {
146 try (
147 final MkContainer container = new MkGrizzlyContainer().next(
148 new MkAnswer.Simple(
149 HttpURLConnection.HTTP_OK,
150 "[{\"ref\":\"refs/tags/feature-b\"}]"
151 )
152 ).start(this.resource.port())
153 ) {
154 final References refs = new RtReferences(
155 new ApacheRequest(container.home()),
156 new MkGithub().randomRepo()
157 );
158 MatcherAssert.assertThat(
159 refs.tags(),
160 Matchers.<Reference>iterableWithSize(1)
161 );
162 MatcherAssert.assertThat(
163 container.take().uri().toString(),
164 Matchers.endsWith("/git/refs/tags")
165 );
166 container.stop();
167 }
168 }
169
170
171
172
173
174 @Test
175 public void iteratesHeads() throws Exception {
176 try (
177 final MkContainer container = new MkGrizzlyContainer().next(
178 new MkAnswer.Simple(
179 HttpURLConnection.HTTP_OK,
180 "[{\"ref\":\"refs/heads/feature-c\"}]"
181 )
182 ).start(this.resource.port())
183 ) {
184 final References refs = new RtReferences(
185 new ApacheRequest(container.home()),
186 new MkGithub().randomRepo()
187 );
188 MatcherAssert.assertThat(
189 refs.heads(),
190 Matchers.<Reference>iterableWithSize(1)
191 );
192 MatcherAssert.assertThat(
193 container.take().uri().toString(),
194 Matchers.endsWith("/git/refs/heads")
195 );
196 container.stop();
197 }
198 }
199 }