This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
MockServerRule.java
369 lines (341 loc) · 14.9 KB
/
MockServerRule.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Copyright 2014, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.client;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anySetOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.InputStream;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.junit.rules.ExternalResource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.zanata.client.commands.ConfigurableProjectOptions;
import org.zanata.client.commands.pull.PullCommand;
import org.zanata.client.commands.pull.PullOptionsImpl;
import org.zanata.client.commands.pull.RawPullCommand;
import org.zanata.client.commands.push.PushCommand;
import org.zanata.client.commands.push.PushOptionsImpl;
import org.zanata.client.commands.push.RawPushCommand;
import org.zanata.client.config.LocaleList;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.rest.DocumentFileUploadForm;
import org.zanata.rest.StringSet;
import org.zanata.rest.client.AsyncProcessClient;
import org.zanata.rest.client.CopyTransClient;
import org.zanata.rest.client.FileResourceClient;
import org.zanata.rest.client.RestClientFactory;
import org.zanata.rest.client.SourceDocResourceClient;
import org.zanata.rest.client.TransDocResourceClient;
import org.zanata.rest.dto.ChunkUploadResponse;
import org.zanata.rest.dto.ProcessStatus;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.ResourceMeta;
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.rest.service.FileResource;
import com.google.common.base.Joiner;
import com.google.common.base.Throwables;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/**
* Test rule to set up push and/or pull command(s) which will interact with a
* mockito mocked REST clients.
*
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class MockServerRule extends ExternalResource {
// async process statuses
private String mockProcessId = "MockServerRuleProcess";
private ProcessStatus finished = new ProcessStatus();
private ProcessStatus running = new ProcessStatus();
private PullOptionsImpl pullOpts;
private PushOptionsImpl pushOpts;
@Captor
private ArgumentCaptor<Resource> resourceCaptor;
@Captor
private ArgumentCaptor<Set<String>> extensionCaptor;
@Captor
private ArgumentCaptor<String> docIdCaptor;
@Captor
private ArgumentCaptor<LocaleId> localeIdCaptor;
@Captor
private ArgumentCaptor<TranslationsResource> transResourceCaptor;
@Mock
private ClientResponse transResourceResponse;
@Captor
private ArgumentCaptor<DocumentFileUploadForm> uploadFormCaptor;
@Mock
private ClientResponse downloadSourceResponse;
@Mock
private ClientResponse downloadTransResponse;
@Mock
private RestClientFactory clientFactory;
@Mock
private CopyTransClient copyTransClient;
@Mock
private AsyncProcessClient asyncClient;
@Mock
private SourceDocResourceClient sourceDocClient;
@Mock
private TransDocResourceClient transDocClient;
@Mock
private FileResourceClient fileResourceClient;
public MockServerRule() {
MockitoAnnotations.initMocks(this);
// async process statuses
running.setUrl(mockProcessId);
running.setStatusCode(ProcessStatus.ProcessStatusCode.Running);
finished.setUrl(mockProcessId);
finished.setStatusCode(ProcessStatus.ProcessStatusCode.Finished);
}
@Override
protected void before() throws Throwable {
super.before();
pushOpts = new PushOptionsImpl();
setCommonOpts(pushOpts);
pushOpts.setCopyTrans(false);
pullOpts = new PullOptionsImpl();
setCommonOpts(pullOpts);
}
private static void setCommonOpts(ConfigurableProjectOptions opts) {
opts.setUsername("admin");
opts.setKey("abcde");
opts.setLocaleMapList(new LocaleList());
opts.setInteractiveMode(false);
setUrl(opts);
opts.setProj("sample-project");
opts.setProjectVersion("master");
}
private static URI setUrl(ConfigurableProjectOptions opts) {
try {
URI uri = new URI("http://localhost:8888/zanata");
opts.setUrl(uri.toURL());
return uri;
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
/**
* Creates a push command that will interact with mock REST proxy factory.
* For async push source and translations the responses are assumed always
* successful. You should verify push afterwards and make assertion on
* all/some of the captor captured values.
*
* @see MockServerRule#verifyPushSource()
* @see MockServerRule#verifyPushTranslation()
* @return push command
*/
public PushCommand createPushCommand() {
when(clientFactory.getSourceDocResourceClient(pushOpts.getProj(),
pullOpts.getProjectVersion())).thenReturn(sourceDocClient);
when(sourceDocClient.getResourceMeta(null)).thenReturn(
Collections.<ResourceMeta>emptyList());
// this assumes async push is always success
when(
asyncClient.startSourceDocCreationOrUpdate(
anyString(),
eq(pushOpts.getProj()), eq(pushOpts.getProjectVersion()),
any(Resource.class), anySetOf(String.class),
eq(false)))
.thenReturn(running);
when(
asyncClient.startTranslatedDocCreationOrUpdate(
docIdCaptor.capture(), eq(pushOpts.getProj()),
eq(pushOpts.getProjectVersion()), localeIdCaptor.capture(),
transResourceCaptor.capture(),
extensionCaptor.capture(), eq(pushOpts.getMergeType())))
.thenReturn(running);
when(asyncClient.getProcessStatus(mockProcessId))
.thenReturn(finished);
return new PushCommand(pushOpts,
copyTransClient,
asyncClient, clientFactory);
}
public PushOptionsImpl getPushOpts() {
return pushOpts;
}
public ArgumentCaptor<Resource> getResourceCaptor() {
return resourceCaptor;
}
public ArgumentCaptor<Set<String>> getExtensionCaptor() {
return extensionCaptor;
}
public ArgumentCaptor<String> getDocIdCaptor() {
return docIdCaptor;
}
public ArgumentCaptor<LocaleId> getLocaleIdCaptor() {
return localeIdCaptor;
}
public ArgumentCaptor<TranslationsResource> getTransResourceCaptor() {
return transResourceCaptor;
}
public void verifyPushSource() {
verify(asyncClient).startSourceDocCreationOrUpdate(
docIdCaptor.capture(), eq(pushOpts.getProj()),
eq(pushOpts.getProjectVersion()), resourceCaptor.capture(),
extensionCaptor.capture(), eq(false));
}
public void verifyPushTranslation() {
verify(asyncClient).startTranslatedDocCreationOrUpdate(
docIdCaptor.capture(), eq(pushOpts.getProj()),
eq(pushOpts.getProjectVersion()), localeIdCaptor.capture(),
transResourceCaptor.capture(), extensionCaptor.capture(),
eq(pushOpts.getMergeType()));
}
public PullOptionsImpl getPullOpts() {
return pullOpts;
}
/**
* Creates a pull command that will interact with mock REST proxy factory
* and get back provided response.
*
* @param remoteDocList
* stubbed server response that represents source doc meta list
* @param resourceOnServer
* stubbed server response that represents source on server
* @param transResourceOnServer
* stubbed server response that represents translation on server
* @return pull command
*/
public PullCommand createPullCommand(List<ResourceMeta> remoteDocList,
Resource resourceOnServer,
TranslationsResource transResourceOnServer) {
when(clientFactory.getSourceDocResourceClient(anyString(), anyString()))
.thenReturn(sourceDocClient);
// return provided remote doc meta list
when(sourceDocClient.getResourceMeta(null)).thenReturn(remoteDocList);
// return provided server resource
when(sourceDocClient.getResource(anyString(), anySetOf(String.class)))
.thenReturn(resourceOnServer);
when(
clientFactory.getTransDocResourceClient(pullOpts.getProj(),
pullOpts.getProjectVersion())).thenReturn(
transDocClient);
// return provided server translation
when(
transDocClient.getTranslations(anyString(),
any(LocaleId.class), anySetOf(String.class),
eq(getPullOpts().getCreateSkeletons()), anyString()))
.thenReturn(transResourceResponse);
when(transResourceResponse.getStatus()).thenReturn(200);
when(transResourceResponse.getHeaders()).thenReturn(
new MultivaluedMapImpl());
when(transResourceResponse.getEntity(TranslationsResource.class))
.thenReturn(transResourceOnServer);
return new PullCommand(pullOpts, clientFactory);
}
/**
* Creates a raw file push command that will interact with mock REST proxy factory.
* File service resource is stubbed to always return successful result.
*
* @see MockServerRule#verifyPushRawFileSource(int)
* @see MockServerRule#verifyPushRawFileTranslation(int)
* @return raw push command
*/
public RawPushCommand createRawPushCommand() {
when(clientFactory.getFileResourceClient()).thenReturn(
fileResourceClient);
List<String> fileTypes =
ProjectType.getSupportedSourceFileTypes(ProjectType.File);
when(fileResourceClient.acceptedFileTypes())
.thenReturn(new StringSet(Joiner.on(";").join(fileTypes)));
ChunkUploadResponse uploadResponse =
new ChunkUploadResponse(1L, 1, false, "Upload successful");
// push source
when(fileResourceClient.uploadSourceFile(eq(pushOpts.getProj()),
eq(pushOpts.getProjectVersion()), anyString(), any(
DocumentFileUploadForm.class))).thenReturn(uploadResponse);
// push translation
when(
fileResourceClient.uploadTranslationFile(eq(pushOpts.getProj()),
eq(pushOpts.getProjectVersion()), anyString(),
anyString(), anyString(),
any(DocumentFileUploadForm.class))).thenReturn(
uploadResponse);
return new RawPushCommand(pushOpts, clientFactory);
}
public void verifyPushRawFileSource(int numOfSource) {
verify(fileResourceClient, times(numOfSource)).uploadSourceFile(
eq(pushOpts.getProj()),
eq(pushOpts.getProjectVersion()), docIdCaptor.capture(),
uploadFormCaptor.capture());
}
public void verifyPushRawFileTranslation(int numOfTrans) {
verify(fileResourceClient, times(numOfTrans)).uploadTranslationFile(
eq(pushOpts.getProj()),
eq(pushOpts.getProjectVersion()),
anyString(), docIdCaptor.capture(),
anyString(), uploadFormCaptor.capture());
}
/**
* Creates a raw pull command that will interact with mock REST proxy factory
* and get back provided response.
*
* @param remoteDocList
* stubbed server response that represents source doc meta list
* @param sourceFileStream
* stubbed server response that represents source doc stream
* @param transFileStream
* stubbed server response that represents translation doc stream
* @return raw pull command
*/
public RawPullCommand createRawPullCommand(
List<ResourceMeta> remoteDocList,
InputStream sourceFileStream, InputStream transFileStream) {
when(
clientFactory.getSourceDocResourceClient(pullOpts.getProj(),
pullOpts.getProjectVersion())).thenReturn(
sourceDocClient);
when(clientFactory.getFileResourceClient()).thenReturn(fileResourceClient);
// return provided remote doc meta list
when(sourceDocClient.getResourceMeta(null)).thenReturn(remoteDocList);
// return provided source stream
when(fileResourceClient.downloadSourceFile(
eq(pullOpts.getProj()), eq(pullOpts.getProjectVersion()),
eq(FileResource.FILETYPE_RAW_SOURCE_DOCUMENT),
anyString())).thenReturn(downloadSourceResponse);
when(downloadSourceResponse.getClientResponseStatus()).thenReturn(
ClientResponse.Status.OK);
when(downloadSourceResponse.getStatus()).thenReturn(200);
when(downloadSourceResponse.getEntity(InputStream.class))
.thenReturn(sourceFileStream);
// return provide translation stream
when(fileResourceClient.downloadTranslationFile(eq(pullOpts.getProj()),
eq(pullOpts.getProjectVersion()), anyString(), anyString(),
anyString())).thenReturn(downloadTransResponse);
when(downloadTransResponse.getStatus()).thenReturn(200);
when(downloadTransResponse.getClientResponseStatus()).thenReturn(
ClientResponse.Status.OK);
when(downloadTransResponse.getEntity(InputStream.class))
.thenReturn(transFileStream);
return new RawPullCommand(pullOpts, fileResourceClient, clientFactory);
}
}