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.aspects.Tv;
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 com.jcabi.immutable.ArrayMap;
39 import java.net.HttpURLConnection;
40 import javax.json.Json;
41 import javax.json.JsonObject;
42 import org.hamcrest.MatcherAssert;
43 import org.hamcrest.Matchers;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47
48
49
50
51
52
53
54
55 public final class RtPullsTest {
56
57
58
59
60
61 @Rule
62 public final transient RandomPort resource = new RandomPort();
63
64
65
66
67
68
69 @Test
70 public void createPull() throws Exception {
71 final String title = "new feature";
72 final String body = pull(title).toString();
73 try (
74 final MkContainer container = new MkGrizzlyContainer().next(
75 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
76 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
77 .start(this.resource.port())
78 ) {
79 final RtPulls pulls = new RtPulls(
80 new ApacheRequest(container.home()),
81 repo()
82 );
83 final Pull pull = pulls.create(title, "octocat", "master");
84 MatcherAssert.assertThat(
85 container.take().method(),
86 Matchers.equalTo(Request.POST)
87 );
88 MatcherAssert.assertThat(
89 new Pull.Smart(pull).title(),
90 Matchers.equalTo(title)
91 );
92 container.stop();
93 }
94 }
95
96
97
98
99
100 @Test
101 public void getSinglePull() throws Exception {
102 final String title = "new-feature";
103 try (
104 final MkContainer container = new MkGrizzlyContainer().next(
105 new MkAnswer.Simple(
106 HttpURLConnection.HTTP_OK,
107 pull(title).toString()
108 )
109 ).start(this.resource.port())
110 ) {
111 final RtPulls pulls = new RtPulls(
112 new ApacheRequest(container.home()),
113 repo()
114 );
115 final Pull pull = pulls.get(Tv.BILLION);
116 MatcherAssert.assertThat(
117 new Pull.Smart(pull).title(),
118 Matchers.equalTo(title)
119 );
120 container.stop();
121 }
122 }
123
124
125
126
127
128 @Test
129 public void iteratePulls() throws Exception {
130 try (
131 final MkContainer container = new MkGrizzlyContainer().next(
132 new MkAnswer.Simple(
133 HttpURLConnection.HTTP_OK,
134 Json.createArrayBuilder()
135 .add(pull("new-topic"))
136 .add(pull("Amazing new feature"))
137 .build().toString()
138 )
139 ).start(this.resource.port())
140 ) {
141 final RtPulls pulls = new RtPulls(
142 new ApacheRequest(container.home()),
143 repo()
144 );
145 MatcherAssert.assertThat(
146 pulls.iterate(new ArrayMap<>()),
147 Matchers.<Pull>iterableWithSize(2)
148 );
149 container.stop();
150 }
151 }
152
153
154
155
156
157
158 private static JsonObject pull(final String title) {
159 return Json.createObjectBuilder()
160 .add("number", Tv.BILLION)
161 .add("state", Issue.OPEN_STATE)
162 .add("title", title)
163 .build();
164 }
165
166
167
168
169
170 private static Repo repo() {
171 final Repo repo = Mockito.mock(Repo.class);
172 Mockito.doReturn(new Coordinates.Simple("mark", "test"))
173 .when(repo).coordinates();
174 return repo;
175 }
176 }