1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.linecorp.centraldogma.testing.junit4;
18
19 import java.io.IOException;
20 import java.net.InetSocketAddress;
21 import java.util.concurrent.CompletableFuture;
22
23 import org.junit.Rule;
24 import org.junit.rules.TemporaryFolder;
25
26 import com.spotify.futures.CompletableFutures;
27
28 import com.linecorp.armeria.client.WebClient;
29 import com.linecorp.armeria.client.WebClientBuilder;
30 import com.linecorp.centraldogma.client.CentralDogma;
31 import com.linecorp.centraldogma.client.armeria.ArmeriaCentralDogmaBuilder;
32 import com.linecorp.centraldogma.client.armeria.legacy.LegacyCentralDogmaBuilder;
33 import com.linecorp.centraldogma.server.CentralDogmaBuilder;
34 import com.linecorp.centraldogma.server.MirroringService;
35 import com.linecorp.centraldogma.server.storage.project.ProjectManager;
36 import com.linecorp.centraldogma.testing.internal.CentralDogmaRuleDelegate;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class CentralDogmaRule extends TemporaryFolder {
56
57 private final CentralDogmaRuleDelegate delegate;
58
59
60
61
62 public CentralDogmaRule() {
63 this(false);
64 }
65
66
67
68
69 public CentralDogmaRule(boolean useTls) {
70 delegate = new CentralDogmaRuleDelegate(useTls) {
71 @Override
72 protected void configure(CentralDogmaBuilder builder) {
73 CentralDogmaRule.this.configure(builder);
74 }
75
76 @Override
77 protected void configureClient(ArmeriaCentralDogmaBuilder builder) {
78 CentralDogmaRule.this.configureClient(builder);
79 }
80
81 @Override
82 protected void configureClient(LegacyCentralDogmaBuilder builder) {
83 CentralDogmaRule.this.configureClient(builder);
84 }
85
86 @Override
87 protected void configureHttpClient(WebClientBuilder builder) {
88 CentralDogmaRule.this.configureHttpClient(builder);
89 }
90
91 @Override
92 protected void scaffold(CentralDogma client) {
93 CentralDogmaRule.this.scaffold(client);
94 }
95 };
96 }
97
98
99
100
101 @Override
102 protected void before() throws Throwable {
103 super.before();
104 delegate.setUp(getRoot());
105 }
106
107
108
109
110 @Override
111 protected void after() {
112 stopAsync().whenComplete((unused1, unused2) -> delete());
113 }
114
115
116
117
118
119
120 public final void start() {
121 startAsync().join();
122 }
123
124
125
126
127
128 public final CompletableFuture<Void> startAsync() {
129
130 try {
131 getRoot();
132 } catch (IllegalStateException unused) {
133 try {
134 create();
135 } catch (IOException e) {
136 return CompletableFutures.exceptionallyCompletedFuture(e);
137 }
138 }
139
140 return delegate.startAsync(getRoot());
141 }
142
143
144
145
146
147 public final void stop() {
148 stopAsync().join();
149 }
150
151
152
153
154
155 public final CompletableFuture<Void> stopAsync() {
156 return delegate.stopAsync();
157 }
158
159
160
161
162 public boolean useTls() {
163 return delegate.useTls();
164 }
165
166
167
168
169
170
171 public final com.linecorp.centraldogma.server.CentralDogma dogma() {
172 return delegate.dogma();
173 }
174
175
176
177
178
179
180 public ProjectManager projectManager() {
181 return delegate.projectManager();
182 }
183
184
185
186
187
188
189 public final MirroringService mirroringService() {
190 return delegate.mirroringService();
191 }
192
193
194
195
196
197
198 public final CentralDogma client() {
199 return delegate.client();
200 }
201
202
203
204
205
206
207 public final CentralDogma legacyClient() {
208 return delegate.legacyClient();
209 }
210
211
212
213
214
215
216 public final WebClient httpClient() {
217 return delegate.httpClient();
218 }
219
220
221
222
223
224
225 public final InetSocketAddress serverAddress() {
226 return delegate.serverAddress();
227 }
228
229
230
231
232 protected void configure(CentralDogmaBuilder builder) {}
233
234
235
236
237 protected void configureClient(ArmeriaCentralDogmaBuilder builder) {}
238
239
240
241
242 protected void configureClient(LegacyCentralDogmaBuilder builder) {}
243
244
245
246
247 protected void configureHttpClient(WebClientBuilder builder) {}
248
249
250
251
252
253 protected void scaffold(CentralDogma client) {}
254 }