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
31 package com.jcabi.github;
32
33 import com.jcabi.github.mock.MkGithub;
34 import com.jcabi.http.Request;
35 import com.jcabi.http.mock.MkAnswer;
36 import com.jcabi.http.mock.MkContainer;
37 import com.jcabi.http.mock.MkGrizzlyContainer;
38 import com.jcabi.http.request.ApacheRequest;
39 import java.net.HttpURLConnection;
40 import javax.json.Json;
41 import org.hamcrest.MatcherAssert;
42 import org.hamcrest.Matchers;
43 import org.junit.Rule;
44 import org.junit.Test;
45
46
47
48
49
50
51
52
53
54 public final class RtReferenceTest {
55
56
57
58
59
60 @Rule
61 public final transient RandomPort resource = new RandomPort();
62
63
64
65
66
67 @Test
68 public void patchesContent() throws Exception {
69 try (
70 final MkContainer container = new MkGrizzlyContainer().next(
71 new MkAnswer.Simple(
72 HttpURLConnection.HTTP_OK,
73 "{\"ref\":\"refs/heads/featureA\"}"
74 )
75 ).start(this.resource.port())
76 ) {
77 final Reference reference = new RtReference(
78 new ApacheRequest(container.home()),
79 new MkGithub().randomRepo(),
80 "refs/heads/featureA"
81 );
82 reference.patch(
83 Json.createObjectBuilder().add("sha", "abcdef12345")
84 .add("force", "false").build()
85 );
86 MatcherAssert.assertThat(
87 container.take().method(),
88 Matchers.equalTo(Request.PATCH)
89 );
90 container.stop();
91 }
92 }
93
94
95
96
97
98 @Test
99 public void fetchesContent() throws Exception {
100 try (
101 final MkContainer container = new MkGrizzlyContainer().next(
102 new MkAnswer.Simple(
103 HttpURLConnection.HTTP_OK,
104 "{\"ref\":\"refs/heads/featureB\"}"
105 )
106 ).start(this.resource.port())
107 ) {
108 final Reference reference = new RtReference(
109 new ApacheRequest(container.home()),
110 new MkGithub().randomRepo(),
111 "refs/heads/featureB"
112 );
113 MatcherAssert.assertThat(
114 reference.json().getString("ref"),
115 Matchers.is("refs/heads/featureB")
116 );
117 container.stop();
118 }
119 }
120
121
122
123
124
125 @Test
126 public void returnsRef() throws Exception {
127 try (
128 final MkContainer container = new MkGrizzlyContainer().next(
129 new MkAnswer.Simple(
130 HttpURLConnection.HTTP_OK,
131 "{\"ref\":\"refs/heads/featureC\"}"
132 )
133 ).start(this.resource.port())
134 ) {
135 final Reference reference = new RtReference(
136 new ApacheRequest(container.home()),
137 new MkGithub().randomRepo(),
138 "refs/heads/featureC"
139 );
140 MatcherAssert.assertThat(
141 reference.ref(),
142 Matchers.is("refs/heads/featureC")
143 );
144 container.stop();
145 }
146 }
147
148
149
150
151
152 @Test
153 public void returnsOwner() throws Exception {
154 final Repo owner = new MkGithub().randomRepo();
155 try (
156 final MkContainer container = new MkGrizzlyContainer().next(
157 new MkAnswer.Simple(
158 HttpURLConnection.HTTP_OK,
159 "{\"ref\":\"refs/heads/featureD\"}"
160 )
161 ).start(this.resource.port());
162 ) {
163 final Reference reference = new RtReference(
164 new ApacheRequest(container.home()),
165 owner,
166 "refs/heads/featureD"
167 );
168 MatcherAssert.assertThat(
169 reference.repo(),
170 Matchers.is(owner)
171 );
172 container.stop();
173 }
174 }
175 }