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.Immutable;
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.ApacheRequest;
37 import com.jcabi.http.request.FakeRequest;
38 import java.io.IOException;
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 @Immutable
55 public final class RtDeployKeysTest {
56
57
58
59
60
61 @Rule
62 public final transient RandomPort resource = new RandomPort();
63
64
65
66
67 @Test
68 public void canFetchEmptyListOfDeployKeys() {
69 final DeployKeys deployKeys = new RtDeployKeys(
70 new FakeRequest().withBody("[]"),
71 RtDeployKeysTest.repo()
72 );
73 MatcherAssert.assertThat(
74 deployKeys.iterate(),
75 Matchers.emptyIterable()
76 );
77 }
78
79
80
81
82
83
84 @Test
85 public void canFetchNonEmptyListOfDeployKeys() throws IOException {
86 try (final MkContainer container = new MkGrizzlyContainer().next(
87 new MkAnswer.Simple(
88 HttpURLConnection.HTTP_OK,
89 Json.createArrayBuilder()
90 .add(key(1))
91 .add(key(2))
92 .build().toString()
93 )
94 )) {
95 container.start(this.resource.port());
96 MatcherAssert.assertThat(
97 new RtDeployKeys(
98 new ApacheRequest(container.home()),
99 RtDeployKeysTest.repo()
100 ).iterate(),
101 Matchers.<DeployKey>iterableWithSize(2)
102 );
103 }
104 }
105
106
107
108
109
110 @Test
111 public void canFetchSingleDeployKey() throws IOException {
112 final int number = 1;
113 final DeployKeys keys = new RtDeployKeys(
114
115 new FakeRequest().withBody(String.format("{\"id\":%d}", number)),
116 RtDeployKeysTest.repo()
117 );
118 MatcherAssert.assertThat(
119
120 keys.get(number).json().getInt("id"),
121 Matchers.equalTo(number)
122 );
123 }
124
125
126
127
128
129 @Test
130 public void canCreateDeployKey() throws IOException {
131 final int number = 2;
132 try (final MkContainer container = new MkGrizzlyContainer().next(
133 new MkAnswer.Simple(
134 HttpURLConnection.HTTP_CREATED,
135 String.format("{\"id\":%d}", number)
136 )
137 )) {
138 container.start(this.resource.port());
139 final DeployKeys keys = new RtDeployKeys(
140 new ApacheRequest(container.home()), RtDeployKeysTest.repo()
141 );
142 MatcherAssert.assertThat(
143 keys.create("Title", "Key").number(),
144 Matchers.equalTo(number)
145 );
146 }
147 }
148
149
150
151
152
153 private static Repo repo() {
154 final Repo repo = Mockito.mock(Repo.class);
155 Mockito.doReturn(new Coordinates.Simple("test", "keys"))
156 .when(repo).coordinates();
157 return repo;
158 }
159
160
161
162
163
164
165 private static JsonObject key(final int number) {
166 return Json.createObjectBuilder()
167 .add("id", number)
168 .add("key", "ssh-rsa AAA")
169 .build();
170 }
171 }