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 57
/
FilterConstraintToQueryJpaTest.java
492 lines (430 loc) · 20.8 KB
/
FilterConstraintToQueryJpaTest.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package org.zanata.search;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.hamcrest.Matchers;
import org.hibernate.transform.ResultTransformer;
import org.joda.time.DateTime;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.ZanataJpaTest;
import org.zanata.common.ContentState;
import org.zanata.model.Activity;
import org.zanata.model.HAccount;
import org.zanata.model.HAccountActivationKey;
import org.zanata.model.HDocument;
import org.zanata.model.HDocumentHistory;
import org.zanata.model.HGlossaryEntry;
import org.zanata.model.HGlossaryTerm;
import org.zanata.model.HIterationGroup;
import org.zanata.model.HLocale;
import org.zanata.model.HLocaleMember;
import org.zanata.model.HPerson;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HTermComment;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowBuilder;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.HTextFlowTargetHistory;
import org.zanata.model.HTextFlowTargetReviewComment;
import org.zanata.model.po.HPoTargetHeader;
import org.zanata.model.security.HCredentials;
import org.zanata.model.tm.TransMemory;
import org.zanata.model.tm.TransMemoryUnit;
import org.zanata.model.tm.TransMemoryUnitVariant;
import org.zanata.webtrans.server.rpc.GetTransUnitsNavigationService;
import org.zanata.webtrans.shared.model.ContentStateGroup;
import org.zanata.webtrans.shared.model.DocumentId;
import com.github.huangp.entityunit.entity.EntityCleaner;
import com.github.huangp.entityunit.entity.EntityMakerBuilder;
import com.github.huangp.entityunit.maker.FixedValueMaker;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Test(groups = "jpa-tests")
@Slf4j
public class FilterConstraintToQueryJpaTest extends ZanataJpaTest {
private ContentStateGroup untranslatedOnly = ContentStateGroup.builder()
.removeAll().includeNew(true).build();
private ContentStateGroup allContentStates = ContentStateGroup.builder()
.addAll().build();
private HLocale hLocale;
private DocumentId documentId;
private DateTime today = new DateTime();
private DateTime yesterday = new DateTime().minusDays(1);
private ResultTransformer transformer;
private HPerson admin;
private HPerson translator;
@BeforeMethod
public void setUpData() {
deleteData();
hLocale =
EntityMakerBuilder.builder().build()
.makeAndPersist(getEm(), HLocale.class);
transformer =
new GetTransUnitsNavigationService.TextFlowResultTransformer(
hLocale);
admin = makePerson("admin123");
translator = makePerson("translator123");
HDocument hDocument =
EntityMakerBuilder
.builder()
.addFieldOrPropertyMaker(HProject.class,
"sourceViewURL",
FixedValueMaker.EMPTY_STRING_MAKER)
.reuseEntity(hLocale).build()
.makeAndPersist(getEm(), HDocument.class);
documentId = new DocumentId(hDocument.getId(), hDocument.getDocId());
HTextFlowBuilder baseBuilder =
new HTextFlowBuilder().withDocument(hDocument)
.withTargetLocale(hLocale);
HTextFlowBuilder adminModifiedToday =
baseBuilder.withLastModifiedDate(today).withLastModifiedBy(
admin);
// make 10 text flows and some text flow targets
// 1. null target
baseBuilder.withResId("res1").withSourceContent("source 1").build();
// target translated by admin on today
// 2. translated target
adminModifiedToday.withResId("res2").withSourceContent("source 2")
.withTargetContent("target 2")
.withTargetState(ContentState.Translated).build();
// 3. fuzzy target
adminModifiedToday.withResId("res3").withSourceContent("source 3")
.withTargetContent("target 3")
.withTargetState(ContentState.NeedReview).build();
// 4. untranslated target but is not null
adminModifiedToday.withResId("res4").withSourceContent("source 4")
.withTargetContent("").withTargetState(ContentState.New)
.build();
// 5. target with comment
adminModifiedToday.withResId("res5").withSourceContent("source 5")
.withTargetContent("target 5")
.withTargetState(ContentState.Translated)
.withTargetComment("target comment").build();
// 6. source with msgContext
adminModifiedToday.withResId("res6").withSourceContent("source 6")
.withMsgContext(",gettext ,fuzzy").build();
// 7. source with comment
adminModifiedToday.withResId("res7").withSourceContent("source 7")
.withSourceComment("source comment").build();
// 8. target translated by admin on yesterday
baseBuilder.withLastModifiedDate(yesterday).withLastModifiedBy(admin)
.withResId("res8").withSourceContent("source 8")
.withTargetContent("target 8")
.withTargetState(ContentState.Translated).build();
// 9. target translated by translator on today
baseBuilder.withLastModifiedBy(translator).withResId("res9")
.withSourceContent("source 9").withTargetContent("target 9")
.withTargetState(ContentState.Translated).build();
// 10. target translated by translator on yesterday
baseBuilder.withLastModifiedBy(translator)
.withLastModifiedDate(yesterday).withResId("res10")
.withSourceContent("source 10").withTargetContent("target_10")
.withTargetState(ContentState.Translated).build();
getEm().flush();
}
private void deleteData() {
EntityCleaner.deleteAll(getEm(), Lists.<Class> newArrayList(
TransMemoryUnitVariant.class, TransMemoryUnit.class,
TransMemory.class,
Activity.class,
// glossary
HTermComment.class, HGlossaryTerm.class,
HGlossaryEntry.class,
// tex flows and targets
HPoTargetHeader.class, HTextFlowTargetHistory.class,
HTextFlowTargetReviewComment.class,
HTextFlowTarget.class, HTextFlow.class,
// documents
HDocumentHistory.class, HDocument.class,
// locales
HLocaleMember.class, HLocale.class,
// version group
HIterationGroup.class,
// project
HProjectIteration.class, HProject.class,
// account
HAccountActivationKey.class, HCredentials.class, HPerson.class,
HAccount.class));
}
private HPerson makePerson(String username) {
return EntityMakerBuilder
.builder()
.addFieldOrPropertyMaker(HAccount.class, "username",
FixedValueMaker.fix(username))
.includeOptionalOneToOne().build()
.makeAndPersist(getEm(), HPerson.class);
}
@Test
public void getAll() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> textFlows = getResultList(hql, constraintToQuery);
assertThat(textFlows, Matchers.hasSize(10));
String navigationQuery = constraintToQuery.toModalNavigationQuery();
List<HTextFlow> navigationResult =
getNavigationResult(navigationQuery, constraintToQuery);
assertThat(navigationResult, Matchers.hasSize(10));
}
@SuppressWarnings("unchecked")
private List<HTextFlow> getNavigationResult(String navigationQuery,
FilterConstraintToQuery constraintToQuery) {
org.hibernate.Query query =
getSession().createQuery(navigationQuery).setResultTransformer(
transformer);
return constraintToQuery.setQueryParameters(query, hLocale).list();
}
@Test
public void filterBySourceContent() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.checkInSource(true)
.includeStates(allContentStates)
.filterBy("source 3").build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res3"));
verifyModalNavigationQuery(constraintToQuery, result);
}
private void verifyModalNavigationQuery(
FilterConstraintToQuery constraintToQuery, List<HTextFlow> result) {
String navigationQuery = constraintToQuery.toModalNavigationQuery();
List<HTextFlow> navigationResult =
getNavigationResult(navigationQuery, constraintToQuery);
verifyIdAndContentStateMatches(result, navigationResult);
}
private void verifyIdAndContentStateMatches(List<HTextFlow> one,
List<HTextFlow> two) {
assertThat(one.size(), Matchers.equalTo(two.size()));
for (int i = 0; i < one.size(); i++) {
HTextFlow textFlow1 = one.get(i);
HTextFlow textFlow2 = two.get(i);
assertThat(textFlow1.getId(), Matchers.equalTo(textFlow2.getId()));
assertThat(getContentState(textFlow1, hLocale),
Matchers.equalTo(getContentState(textFlow2, hLocale)));
}
}
private static ContentState getContentState(HTextFlow hTextFlow,
HLocale hLocale) {
HTextFlowTarget target = hTextFlow.getTargets().get(hLocale.getId());
return target == null ? ContentState.New : target.getState();
}
@Test
public void filterByContentInSourceAndTarget() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.checkInSource(true).checkInTarget(true)
.includeStates(allContentStates).filterBy("2")
.build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res2"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByUntranslated() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.includeStates(untranslatedOnly).build(),
documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res1", "res4", "res6", "res7"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByUntranslatedAndSourceContent() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.checkInSource(true)
.includeStates(untranslatedOnly)
.filterBy("source 4").build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res4"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByResId() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.includeStates(allContentStates)
.resourceIdIs("res2").build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res2"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByMessageContext() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.includeStates(allContentStates)
.msgContext(",FuzZy").build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res6"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterBySourceComment() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.includeStates(allContentStates)
.sourceCommentContains("Comment").build(),
documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res7"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByTargetComment() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.includeStates(allContentStates)
.targetCommentContains("COMMENT").build(),
documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res5"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByTargetModifiedUser() {
String username = translator.getAccount().getUsername();
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.includeStates(allContentStates)
.lastModifiedBy(username).build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res9", "res10"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByTargetChangedDateAfter() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.includeStates(allContentStates)
.targetChangedAfter(yesterday).build(),
documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids,
Matchers.contains("res2", "res3", "res4", "res5", "res9"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByTargetChangedDateBefore() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery
.filterInSingleDocument(FilterConstraints.builder()
.keepNone().includeStates(allContentStates)
.targetChangedBefore(today).build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res8", "res10"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByUntranslatedAndModifiedPerson() {
String username = admin.getAccount().getUsername();
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.includeStates(untranslatedOnly)
.lastModifiedBy(username).build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res4"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void filterByContentAndModifiedPersonAndState() {
String username = admin.getAccount().getUsername();
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.checkInSource(true).checkInTarget(true)
.filterBy("source")
.includeStates(untranslatedOnly)
.lastModifiedBy(username).build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res4"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@Test
public void testEscapeCharacter() {
FilterConstraintToQuery constraintToQuery =
FilterConstraintToQuery.filterInSingleDocument(
FilterConstraints.builder().keepNone()
.checkInSource(true).checkInTarget(true)
.filterBy("_").includeStates(allContentStates)
.build(), documentId);
String hql = constraintToQuery.toEntityQuery();
List<HTextFlow> result = getResultList(hql, constraintToQuery);
List<String> ids = transformToResIds(result);
log.debug("result: {}", ids);
assertThat(ids, Matchers.contains("res10"));
verifyModalNavigationQuery(constraintToQuery, result);
}
@SuppressWarnings("unchecked")
private List<HTextFlow> getResultList(String hql,
FilterConstraintToQuery constraintToQuery) {
org.hibernate.Query query = getSession().createQuery(hql);
return constraintToQuery.setQueryParameters(query, hLocale).list();
}
private static List<String> transformToResIds(List<HTextFlow> textFlows) {
return Lists.transform(textFlows, new Function<HTextFlow, String>() {
@Override
public String apply(HTextFlow input) {
return input.getResId();
}
});
}
}