This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
TransMemoryMergeServiceImplTest.java
479 lines (409 loc) · 20 KB
/
TransMemoryMergeServiceImplTest.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
/*
* Copyright 2012, 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.service.impl;
import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.zanata.service.SecurityService.TranslationAction.MODIFY;
import static org.zanata.webtrans.shared.model.TransMemoryResultItem.MatchType;
import static org.zanata.webtrans.shared.rpc.HasSearchType.SearchType.FUZZY_PLURAL;
import java.util.ArrayList;
import java.util.List;
import net.customware.gwt.dispatch.shared.ActionException;
import org.hamcrest.Matchers;
import org.jglue.cdiunit.InRequestScope;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.dao.TextFlowDAO;
import org.zanata.dao.TransMemoryUnitDAO;
import org.zanata.model.HLocale;
import org.zanata.model.HTextFlow;
import org.zanata.model.TestFixture;
import org.zanata.model.tm.TransMemoryUnit;
import org.zanata.model.type.TranslationSourceType;
import org.zanata.service.LocaleService;
import org.zanata.service.SecurityService;
import org.zanata.service.TranslationMemoryService;
import org.zanata.service.TranslationService;
import org.zanata.test.CdiUnitRunner;
import org.zanata.webtrans.server.TranslationWorkspace;
import org.zanata.webtrans.shared.NoSuchWorkspaceException;
import org.zanata.webtrans.shared.model.ProjectIterationId;
import org.zanata.webtrans.shared.model.TransMemoryDetails;
import org.zanata.webtrans.shared.model.TransMemoryQuery;
import org.zanata.webtrans.shared.model.TransMemoryResultItem;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.model.TransUnitUpdateRequest;
import org.zanata.webtrans.shared.model.WorkspaceId;
import org.zanata.webtrans.shared.rpc.HasSearchType;
import org.zanata.webtrans.shared.rpc.MergeOptions;
import org.zanata.webtrans.shared.rpc.MergeRule;
import org.zanata.webtrans.shared.rpc.TransMemoryMerge;
import com.google.common.base.Optional;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@RunWith(CdiUnitRunner.class)
public class TransMemoryMergeServiceImplTest {
@Inject
TransMemoryMergeServiceImpl transMemoryMergeService;
@Produces @Mock
private SecurityService securityService;
@Produces @Mock
private LocaleService localeService;
@Produces @Mock
private TextFlowDAO textFlowDAO;
@Produces @Mock
private TransMemoryUnitDAO transMemoryUnitDAO;
@Produces @Mock
private TranslationMemoryService translationMemoryService;
@Produces @Mock
private TranslationService translationService;
@Produces @Mock
private TranslationWorkspace workspace;
@Captor
ArgumentCaptor<List<TransUnitUpdateRequest>> updateRequestCaptor;
private String projectSlug = "projectSlug";
private String versionSlug = "versionSlug";
private String docId = "pot/a.po";
private HLocale targetLocale = new HLocale(new LocaleId("de"));
private HLocale sourceLocale = new HLocale(new LocaleId("en-US"));
private static ArrayList<String> tmSource = newArrayList("tm source");
private static ArrayList<String> tmTarget = newArrayList("tm target");
private TransMemoryMerge prepareAction(int threshold,
List<TransUnitUpdateRequest> requests, MergeOptions opts) {
TransMemoryMerge action =
new TransMemoryMerge(threshold, requests, opts);
action.setWorkspaceId(new WorkspaceId(new ProjectIterationId(
projectSlug, versionSlug, ProjectType.File), targetLocale
.getLocaleId()));
return action;
}
private TransMemoryMerge prepareAction(int threshold, long... tranUnitIds)
throws NoSuchWorkspaceException {
return prepareAction(threshold, true, tranUnitIds);
}
private TransMemoryMerge prepareAction(int threshold,
boolean acceptImportedTMResults, long... tranUnitIds)
throws NoSuchWorkspaceException {
List<TransUnitUpdateRequest> requests = newArrayList();
for (long tranUnitId : tranUnitIds) {
requests.add(new TransUnitUpdateRequest(
new TransUnitId(tranUnitId), null, null, 0,
TranslationSourceType.TM_MERGE.getAbbr()));
}
// we have TransMemoryMergeStatusResolverTest to cover various different
// merge options so here we don't test that
MergeRule importedTMOption =
acceptImportedTMResults ? MergeRule.IGNORE_CHECK
: MergeRule.REJECT;
MergeOptions opts = MergeOptions.allIgnore();
opts.setImportedMatch(importedTMOption);
return prepareAction(threshold, requests, opts);
}
private static TransMemoryResultItem tmResult(Long sourceId, int percent) {
TransMemoryResultItem resultItem =
new TransMemoryResultItem(tmSource, tmTarget,
MatchType.ApprovedInternal, 1D, percent);
resultItem.addSourceId(sourceId);
return resultItem;
}
private static TransMemoryResultItem importedTmResult(Long sourceId,
int percent) {
TransMemoryResultItem resultItem =
new TransMemoryResultItem(tmSource, tmTarget,
MatchType.Imported, 1D, percent);
resultItem.addSourceId(sourceId);
return resultItem;
}
private static TransMemoryDetails tmDetail() {
return new TransMemoryDetails("", "", "project a", "master",
"pot/msg.pot", "resId", null, null, null, null, null);
}
private TransMemoryQuery
prepareTMQuery(List<String> contents,
HasSearchType.SearchType searchType, MergeOptions opts,
String resId) {
if (opts == null) {
return new TransMemoryQuery(contents, searchType,
new TransMemoryQuery.Condition(false, projectSlug),
new TransMemoryQuery.Condition(false, docId),
new TransMemoryQuery.Condition(false, resId));
} else {
TransMemoryQuery.Condition projectCondition =
new TransMemoryQuery.Condition(
opts.getDifferentProject() == MergeRule.REJECT,
projectSlug);
TransMemoryQuery.Condition documentCondition =
new TransMemoryQuery.Condition(
opts.getDifferentDocument() == MergeRule.REJECT,
docId);
TransMemoryQuery.Condition resCondition =
new TransMemoryQuery.Condition(
opts.getDifferentResId() == MergeRule.REJECT, resId);
return new TransMemoryQuery(contents, searchType, projectCondition,
documentCondition, resCondition);
}
}
@Test
@InRequestScope
public void willTranslateIfMatches() throws ActionException {
// Given:
// an action with threshold 80% and trans unit id is 1
final long transUnitId = 1L;
TransMemoryMerge action = prepareAction(80, transUnitId);
HTextFlow hTextFlow =
TestFixture.makeHTextFlow(transUnitId, sourceLocale,
targetLocale, ContentState.New, docId, versionSlug,
projectSlug);
when(textFlowDAO.findByIdList(newArrayList(transUnitId))).thenReturn(
newArrayList(hTextFlow));
// Given: there are three TM results returned for text flow id 1, and
// the most matched one is text flow id 11
HTextFlow tmResultSource =
TestFixture.makeApprovedHTextFlow(11L, targetLocale);
TransMemoryResultItem mostSimilarTM =
tmResult(tmResultSource.getId(), 100);
when(textFlowDAO.findById(tmResultSource.getId(), false)).thenReturn(
tmResultSource);
when(localeService.getByLocaleId(action.getWorkspaceId().getLocaleId()))
.thenReturn(targetLocale);
when(
translationMemoryService.getTransMemoryDetail(targetLocale,
tmResultSource)).thenReturn(tmDetail());
Optional<TransMemoryResultItem> matches = Optional.of(mostSimilarTM);
when(
translationMemoryService.searchBestMatchTransMemory(hTextFlow,
targetLocale.getLocaleId(), sourceLocale.getLocaleId(),
false, false, false, action.getThresholdPercent()))
.thenReturn(matches);
// When: execute the action
transMemoryMergeService.executeMerge(action);
// Then:
verify(securityService).checkWorkspaceAction(action.getWorkspaceId(),
MODIFY);
// we should have text flow auto translated by using the most
// similar TM
verify(translationService).translate(same(targetLocale.getLocaleId()),
updateRequestCaptor.capture());
List<TransUnitUpdateRequest> updateRequest =
updateRequestCaptor.getValue();
assertThat(updateRequest, Matchers.hasSize(1));
TransUnitUpdateRequest transUnitUpdateRequest = updateRequest.get(0);
assertThat(transUnitUpdateRequest.getNewContents(),
Matchers.equalTo(mostSimilarTM.getTargetContents()));
assertThat(transUnitUpdateRequest.getSourceType(),
Matchers.equalTo(TranslationSourceType.TM_MERGE.getAbbr()));
assertThat(
transUnitUpdateRequest.getTargetComment(),
Matchers.equalTo("auto translated by TM merge from project: project a, version: master, DocId: pot/msg.pot"));
}
@Test
@InRequestScope
public void willNotTranslateIfNoMatches() throws ActionException {
final long transUnitId = 1L;
TransMemoryMerge action = prepareAction(80, transUnitId);
HTextFlow hTextFlow =
TestFixture.makeHTextFlow(transUnitId, sourceLocale,
targetLocale, ContentState.New, docId, versionSlug,
projectSlug);
when(textFlowDAO.findByIdList(newArrayList(transUnitId))).thenReturn(
newArrayList(hTextFlow));
Optional<TransMemoryResultItem> matches =
Optional.absent();
when(
translationMemoryService.searchBestMatchTransMemory(hTextFlow,
targetLocale.getLocaleId(), sourceLocale.getLocaleId(),
false, false, false, action.getThresholdPercent()))
.thenReturn(matches);
when(localeService.getByLocaleId(action.getWorkspaceId().getLocaleId()))
.thenReturn(targetLocale);
// When: execute the action
transMemoryMergeService.executeMerge(action);
verify(securityService).checkWorkspaceAction(action.getWorkspaceId(),
MODIFY);
// Then: we should have EMPTY trans unit update request
verifyZeroInteractions(translationService);
}
@Test
@InRequestScope
public void canHandleMultipleTextFlows() throws ActionException {
// Given: an action with threshold 90% and trans unit id is 1, 2, 3, 4
final long idWith100MatchTM = 1L;
final long idWithoutTM = 2L;
final long idWith80MatchTM = 3L;
final long idWith90MatchTM = 4L;
TransMemoryMerge action =
prepareAction(90, idWith100MatchTM, idWithoutTM,
idWith80MatchTM, idWith90MatchTM);
HTextFlow textFlow100TM =
TestFixture.makeHTextFlow(idWith100MatchTM, sourceLocale,
targetLocale, ContentState.New, docId, versionSlug,
projectSlug);
HTextFlow textFlowNoTM =
TestFixture.makeHTextFlow(idWithoutTM, sourceLocale,
targetLocale, ContentState.New, docId, versionSlug,
projectSlug);
HTextFlow textFlow80TM =
TestFixture.makeHTextFlow(idWith80MatchTM, sourceLocale,
targetLocale, ContentState.New, docId, versionSlug,
projectSlug);
HTextFlow textFLow90TM =
TestFixture.makeHTextFlow(idWith90MatchTM, sourceLocale,
targetLocale, ContentState.New, docId, versionSlug,
projectSlug);
when(localeService.getByLocaleId(action.getWorkspaceId().getLocaleId()))
.thenReturn(targetLocale);
when(
textFlowDAO.findByIdList(newArrayList(idWith100MatchTM,
idWithoutTM, idWith80MatchTM, idWith90MatchTM)))
.thenReturn(
newArrayList(textFlow100TM, textFlowNoTM, textFlow80TM,
textFLow90TM));
// Given: TM results
HTextFlow tmResultSource =
TestFixture.makeApprovedHTextFlow(11L, targetLocale);
Optional<TransMemoryResultItem> tm100 =
Optional.of(tmResult(tmResultSource.getId(), 100));
Optional<TransMemoryResultItem> tm90 =
Optional.of(tmResult(tmResultSource.getId(), 90));
Optional<TransMemoryResultItem> tm80 =
Optional.of(tmResult(tmResultSource.getId(), 80));
Optional<TransMemoryResultItem> noMatch = Optional.absent();
when(
translationMemoryService.searchBestMatchTransMemory(
textFlow100TM, targetLocale.getLocaleId(),
sourceLocale.getLocaleId(), false, false, false, 90))
.thenReturn(tm100);
when(
translationMemoryService.searchBestMatchTransMemory(
textFLow90TM, targetLocale.getLocaleId(),
sourceLocale.getLocaleId(), false, false, false, 90))
.thenReturn(tm90);
when(
translationMemoryService.searchBestMatchTransMemory(
textFlow80TM, targetLocale.getLocaleId(),
sourceLocale.getLocaleId(), false, false, false, 90))
.thenReturn(tm80);
when(
translationMemoryService.searchBestMatchTransMemory(
textFlowNoTM, targetLocale.getLocaleId(),
sourceLocale.getLocaleId(), false, false, false, 90))
.thenReturn(noMatch);
when(textFlowDAO.findById(tmResultSource.getId(), false)).thenReturn(
tmResultSource);
// Given: tm detail of text flow id 11
when(
translationMemoryService.getTransMemoryDetail(targetLocale,
tmResultSource)).thenReturn(tmDetail());
// When: execute the action
transMemoryMergeService.executeMerge(action);
verify(securityService).checkWorkspaceAction(action.getWorkspaceId(),
MODIFY);
// Then: we should have text flow auto translated by using the most
// similar TM
verify(translationService).translate(
same(action.getWorkspaceId().getLocaleId()),
updateRequestCaptor.capture());
List<TransUnitUpdateRequest> updateRequest =
updateRequestCaptor.getValue();
assertThat(updateRequest, Matchers.hasSize(3));
assertThat(updateRequest.get(0).getNewContents(),
Matchers.equalTo(tm100.get().getTargetContents()));
assertThat(updateRequest.get(1).getNewContents(),
Matchers.equalTo(tm90.get().getTargetContents()));
assertThat(updateRequest.get(2).getNewContents(),
Matchers.equalTo(tm80.get().getTargetContents()));
assertThat(updateRequest.get(0).getSourceType(),
Matchers.equalTo(TranslationSourceType.TM_MERGE.getAbbr()));
assertThat(updateRequest.get(1).getSourceType(),
Matchers.equalTo(TranslationSourceType.TM_MERGE.getAbbr()));
assertThat(updateRequest.get(2).getSourceType(),
Matchers.equalTo(TranslationSourceType.TM_MERGE.getAbbr()));
}
@Test
@InRequestScope
public void canAutoTranslateImportedTMResults() throws Exception {
// Given:
// an action with threshold 80% and trans unit id
final long transUnitId = 1L;
TransMemoryMerge action = prepareAction(80, transUnitId);
// A Text Flow to be translated
HTextFlow hTextFlow =
TestFixture.makeHTextFlow(transUnitId, sourceLocale,
targetLocale, ContentState.New, docId, versionSlug,
projectSlug);
// A matching imported Translation Unit
TransMemoryUnit tuResultSource =
TestFixture.makeTransMemoryUnit(10L, targetLocale);
// and an associated result item
TransMemoryResultItem mostSimilarTM =
importedTmResult(tuResultSource.getId(), 100);
Optional<TransMemoryResultItem> match = Optional.of(mostSimilarTM);
// A Translation memory query
TransMemoryQuery tmQuery =
prepareTMQuery(hTextFlow.getContents(), FUZZY_PLURAL, null,
hTextFlow.getResId());
// Expectations:
when(localeService.getByLocaleId(action.getWorkspaceId().getLocaleId()))
.thenReturn(targetLocale);
when(textFlowDAO.findByIdList(newArrayList(transUnitId))).thenReturn(
newArrayList(hTextFlow));
when(
translationMemoryService.searchBestMatchTransMemory(hTextFlow,
targetLocale.getLocaleId(), sourceLocale.getLocaleId(),
false, false, false, action.getThresholdPercent()))
.thenReturn(match);
when(transMemoryUnitDAO.findById(tuResultSource.getId())).thenReturn(
tuResultSource);
// When: execute the action
transMemoryMergeService.executeMerge(action);
// Then: we should have text flow auto translated by using the most
// similar TM
verify(translationService).translate(
same(action.getWorkspaceId().getLocaleId()),
updateRequestCaptor.capture());
List<TransUnitUpdateRequest> updateRequest =
updateRequestCaptor.getValue();
assertThat(updateRequest, Matchers.hasSize(1));
TransUnitUpdateRequest transUnitUpdateRequest = updateRequest.get(0);
assertThat(transUnitUpdateRequest.getNewContents(),
Matchers.equalTo(mostSimilarTM.getTargetContents()));
assertThat(
transUnitUpdateRequest.getTargetComment(),
Matchers.equalTo("auto translated by TM merge from translation memory: test-tm, unique id: uid10"));
assertThat(transUnitUpdateRequest.getSourceType(),
Matchers.equalTo(TranslationSourceType.TM_MERGE.getAbbr()));
}
}