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
/
StatisticsServiceImplTest.java
526 lines (428 loc) · 20.6 KB
/
StatisticsServiceImplTest.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* Copyright 2010, 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 org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import net.sf.ehcache.CacheManager;
import org.dbunit.operation.DatabaseOperation;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.ZanataDbunitJpaTest;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.dao.PersonDAO;
import org.zanata.dao.TextFlowTargetDAO;
import org.zanata.exception.InvalidDateParamException;
import org.zanata.model.HPerson;
import org.zanata.model.HTextFlowTarget;
import org.zanata.rest.NoSuchEntityException;
import org.zanata.rest.dto.stats.ContainerTranslationStatistics;
import org.zanata.rest.dto.stats.TranslationStatistics;
import org.zanata.rest.dto.stats.contribution.BaseContributionStatistic;
import org.zanata.rest.dto.stats.contribution.ContributionStatistics;
import org.zanata.rest.dto.stats.contribution.LocaleStatistics;
import org.zanata.rest.service.StatisticsResource;
import org.zanata.seam.SeamAutowire;
import org.zanata.service.ValidationService;
/**
* @author Carlos Munoz <a
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Test(groups = { "business-tests" })
public class StatisticsServiceImplTest extends ZanataDbunitJpaTest {
private SeamAutowire seam = SeamAutowire.instance();
@Mock
private ValidationService validationServiceImpl;
private CacheManager cacheManager;
private StatisticsServiceImpl statisticsService;
private TextFlowTargetDAO textFlowTargetDAO;
private final SimpleDateFormat formatter =
new SimpleDateFormat(StatisticsResource.DATE_FORMAT);
private Date today = new Date();
@Override
protected void prepareDBUnitOperations() {
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/ClearAllTables.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/ProjectsData.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/LocalesData.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/AccountData.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/TextFlowTestData.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
}
@BeforeMethod
public void initializeSeam() {
MockitoAnnotations.initMocks(this);
// @formatter:off
seam.reset()
.use("entityManager", getEm())
.use("session", getSession())
.use("validationServiceImpl", validationServiceImpl)
.useImpl(TranslationStateCacheImpl.class)
.ignoreNonResolvable();
// @formatter:on
cacheManager = CacheManager.create();
cacheManager.removalAll();
statisticsService = seam.autowire(StatisticsServiceImpl.class);
textFlowTargetDAO = seam.autowire(TextFlowTargetDAO.class);
}
@AfterMethod
public void after() {
cacheManager.shutdown();
}
@Test
public void getSimpleIterationStatisticsForAllLocales() {
ContainerTranslationStatistics stats =
statisticsService.getStatistics("sample-project", "1.0", false,
false, new String[] {});
// Make sure the id matches
assertThat(stats.getId()).isEqualTo("1.0");
// Make sure there are links
assertThat(stats.getRefs().size()).isGreaterThan(0);
// No detailed stats
assertThat(stats.getDetailedStats()).isNull();
assertThat(stats.getStats().get(0).getUnit()).isEqualTo(
TranslationStatistics.StatUnit.MESSAGE);
for (TranslationStatistics transStat : stats.getStats()) {
// Check that there are no word level stats
assertThat(transStat.getUnit()).isNotEqualTo(
TranslationStatistics.StatUnit.WORD);
// make sure counts are sane
assertThat(
transStat.getDraft() + transStat.getApproved()
+ transStat.getTranslatedOnly()
+ transStat.getUntranslated()).isEqualTo(
transStat.getTotal());
}
}
@Test
public void getWordIterationStatisticsForAllLocales() {
ContainerTranslationStatistics stats =
statisticsService.getStatistics("sample-project", "1.0", false,
true, new String[] {});
// Make sure the id matches
assertThat(stats.getId()).isEqualTo("1.0");
// Make sure there are links
assertThat(stats.getRefs().size()).isGreaterThan(0);
// No detailed stats
assertThat(stats.getDetailedStats()).isNull();
// Word level AND message level stats
int wordLevel = 0;
int mssgLevel = 0;
for (TranslationStatistics transStat : stats.getStats()) {
if (transStat.getUnit() == TranslationStatistics.StatUnit.MESSAGE) {
mssgLevel++;
} else if (transStat.getUnit() == TranslationStatistics.StatUnit.WORD) {
wordLevel++;
}
// make sure counts are sane
assertThat(
transStat.getDraft() + transStat.getApproved()
+ transStat.getTranslatedOnly()
+ transStat.getUntranslated()).isEqualTo(
transStat.getTotal());
}
// make sure word and message level counts are the same and > 0
assertThat(wordLevel).isGreaterThan(0);
assertThat(wordLevel).isEqualTo(mssgLevel);
}
@Test
public void getDetailedIterationStatisticsForSpecificLocales() {
String[] locales = new String[] { "en-US", "es", "as" };
ContainerTranslationStatistics stats =
statisticsService.getStatistics("sample-project", "1.0", true,
true, locales);
// Make sure the id matches
assertThat(stats.getId()).isEqualTo("1.0");
// Make sure there are links
assertThat(stats.getRefs().size()).isGreaterThan(0);
// Detailed Stats
assertThat(stats.getDetailedStats().size()).isGreaterThan(0);
// Results returned only for specified locales
for (TranslationStatistics transStat : stats.getStats()) {
assertThat(Arrays.asList(locales)).contains(transStat.getLocale());
// make sure counts are sane
assertThat(
transStat.getDraft() + transStat.getApproved()
+ transStat.getTranslatedOnly()
+ transStat.getUntranslated()).isEqualTo(
transStat.getTotal());
}
}
@Test
public void getSimpleDocumentStatisticsForAllLocales() {
ContainerTranslationStatistics stats =
statisticsService.getStatistics("sample-project", "1.0",
"my/path/document.txt", false, new String[] {});
// Make sure the id matches
assertThat(stats.getId()).isEqualTo("my/path/document.txt");
// Make sure there are links
assertThat(stats.getRefs().size()).isGreaterThan(0);
// No detailed stats
assertThat(stats.getDetailedStats()).isNull();
assertThat(stats.getStats().get(0).getUnit()).isEqualTo(
TranslationStatistics.StatUnit.MESSAGE);
for (TranslationStatistics transStat : stats.getStats()) {
// Check that there are no word level stats
assertThat(transStat.getUnit()).isNotEqualTo(
TranslationStatistics.StatUnit.WORD);
// make sure counts are sane
assertThat(
transStat.getDraft() + transStat.getApproved()
+ transStat.getTranslatedOnly()
+ transStat.getUntranslated()).isEqualTo(
transStat.getTotal());
}
}
@Test
public void getDetailedDocumentStatisticsForSpecificLocales() {
String[] locales = new String[] { "en-US", "es", "as" };
ContainerTranslationStatistics stats =
statisticsService.getStatistics("sample-project", "1.0",
"my/path/document.txt", true, locales);
// Make sure the id matches
assertThat(stats.getId()).isEqualTo("my/path/document.txt");
// Make sure there are links
assertThat(stats.getRefs().size()).isGreaterThan(0);
// No Detailed Stats
assertThat(stats.getDetailedStats()).isNull();
// Results returned only for specified locales
for (TranslationStatistics transStat : stats.getStats()) {
assertThat(Arrays.asList(locales)).contains(transStat.getLocale());
// make sure counts are sane
assertThat(
transStat.getDraft() + transStat.getApproved()
+ transStat.getTranslatedOnly()
+ transStat.getUntranslated()).isEqualTo(
transStat.getTotal());
}
}
@Test(expectedExceptions = NoSuchEntityException.class)
public void contributionStatsInvalidVersion() {
statisticsService.getContributionStatistics("non-exist-project",
"non-exist-version", "admin", "2013-01-01..2014-01-01");
}
@Test(expectedExceptions = NoSuchEntityException.class)
public void contributionStatsInvalidPerson() {
statisticsService.getContributionStatistics("sample-project",
"1.0", "non-exist-user", "2013-01-01..2014-01-01");
}
@Test(expectedExceptions = InvalidDateParamException.class,
description = "invalid date range separator")
public void contributionStatsInvalidDateRange1() {
statisticsService.getContributionStatistics("sample-project",
"1.0", "admin", "2013-01-012014-01-01");
}
@Test(expectedExceptions = InvalidDateParamException.class,
description = "invalid date format")
public void contributionStatsInvalidDateRange2() {
statisticsService.getContributionStatistics("sample-project",
"1.0", "admin", "203-01-01..201-01-01");
}
@Test(expectedExceptions = InvalidDateParamException.class,
description = "toDate is before fromDate")
public void contributionStatsInvalidDateRange3() {
statisticsService.getContributionStatistics("sample-project",
"1.0", "admin", "2014-01-01..2013-01-01");
}
@Test(expectedExceptions = InvalidDateParamException.class,
description = "date range is more than max-range(365 days)")
public void contributionStatsInvalidDateRange4() {
statisticsService.getContributionStatistics("sample-project",
"1.0", "admin", "2013-01-01..2014-06-01");
}
@Test
public void getContribStatsSingleTarget() {
PersonDAO personDAO = seam.autowire(PersonDAO.class);
// Initial state = needReview
HTextFlowTarget target = textFlowTargetDAO.findById(2L);
int wordCount = target.getTextFlow().getWordCount().intValue();
String todayDate = formatter.format(today);
String username = "demo";
HPerson demoPerson = personDAO.findByUsername(username);
ContributionStatistics initialStats =
statisticsService.getContributionStatistics(
"sample-project", "1.0", username, todayDate + ".."
+ todayDate);
BaseContributionStatistic stats =
initialStats.get(username).get(target.getLocaleId());
// Should have no stats for user on today
assertNull(stats);
// needReview -> approved
ContributionStatistics expectedStats = new ContributionStatistics();
expectedStats.put(username, buildStats(target.getLocaleId(), 0, 0,
wordCount, 0));
target = executeStateChangeTest(target, "test1", ContentState.Approved,
demoPerson, expectedStats);
// approved -> approved
target = executeStateChangeTest(target, "test2", ContentState.Approved,
demoPerson, expectedStats);
// approved -> approved
target = executeStateChangeTest(target, "test3", ContentState.Approved,
demoPerson, expectedStats);
// approved -> needReview
expectedStats.put(username, buildStats(target.getLocaleId(), wordCount,
0, 0, 0));
target = executeStateChangeTest(target, "test4",
ContentState.NeedReview, demoPerson, expectedStats);
}
@Test
public void getContribStatsSameLocaleMultiTargets() {
PersonDAO personDAO = seam.autowire(PersonDAO.class);
String username = "demo";
HPerson demoPerson = personDAO.findByUsername(username);
// Initial state = new (en-us)
HTextFlowTarget target1 = textFlowTargetDAO.findById(5L);
// Initial state = new (en-us)
HTextFlowTarget target2 = textFlowTargetDAO.findById(6L);
LocaleId localeId = target1.getLocaleId(); // same as target2
int wordCount1 = target1.getTextFlow().getWordCount().intValue();
int wordCount2 = target2.getTextFlow().getWordCount().intValue();
// new -> approved
ContentState newState = ContentState.Approved;
ContributionStatistics expectedStats = new ContributionStatistics();
expectedStats.put(username, buildStats(localeId, 0, 0,
wordCount1, 0));
target1 = executeStateChangeTest(target1, "test1",
newState, demoPerson, expectedStats);
expectedStats.put(username, buildStats(localeId, 0, 0,
wordCount1 + wordCount2, 0));
target2 = executeStateChangeTest(target2, "test1",
newState, demoPerson, expectedStats);
// approved -> needReview
newState = ContentState.NeedReview;
expectedStats.put(username, buildStats(localeId, wordCount1, 0,
wordCount1, 0));
target1 = executeStateChangeTest(target1, "test2",
newState, demoPerson, expectedStats);
expectedStats.put(username, buildStats(localeId, wordCount1 + wordCount2,
0, 0, 0));
target2 = executeStateChangeTest(target2, "test2",
newState, demoPerson, expectedStats);
}
@Test
public void getContributionStatisticsMultiLocale() {
PersonDAO personDAO = seam.autowire(PersonDAO.class);
String username = "demo";
HPerson demoPerson = personDAO.findByUsername(username);
// Initial state = needReview (AS)
HTextFlowTarget target1 = textFlowTargetDAO.findById(1L);
// Initial state = needReview (DE)
HTextFlowTarget target2 = textFlowTargetDAO.findById(2L);
int wordCount1 = target1.getTextFlow().getWordCount().intValue();
int wordCount2 = target2.getTextFlow().getWordCount().intValue();
// needReview -> approved
ContentState newState = ContentState.Approved;
ContributionStatistics expectedStats = new ContributionStatistics();
expectedStats.put(username, buildStats(target1.getLocaleId(), 0, 0,
wordCount1, 0));
target1 = executeStateChangeTest(target1, "test1",
newState, demoPerson, expectedStats);
expectedStats.get(username).putAll(buildStats(target2.getLocaleId(), 0,
0, wordCount2, 0));
target2 = executeStateChangeTest(target2, "test1",
newState, demoPerson, expectedStats);
// approved -> needReview
newState = ContentState.NeedReview;
BaseContributionStatistic localeStat = expectedStats.get(username)
.get(target1.getLocaleId());
localeStat.set(newState, localeStat.get(newState) + wordCount1);
localeStat.set(ContentState.Approved, 0);
target1 = executeStateChangeTest(target1, "test2",
newState, demoPerson, expectedStats);
localeStat = expectedStats.get(username).get(target2.getLocaleId());
localeStat.set(newState, localeStat.get(newState) + wordCount2);
localeStat.set(ContentState.Approved, 0);
target2 = executeStateChangeTest(target2, "test2",
newState, demoPerson, expectedStats);
}
@Test
public void getContribStatsDiffUser() {
PersonDAO personDAO = seam.autowire(PersonDAO.class);
String username1 = "demo";
String username2 = "admin";
HPerson person1 = personDAO.findByUsername(username1);
HPerson person2 = personDAO.findByUsername(username2);
HTextFlowTarget target = textFlowTargetDAO.findById(1L);
int wordCount = target.getTextFlow().getWordCount().intValue();
ContentState newState = ContentState.Approved;
ContributionStatistics expectedStats = new ContributionStatistics();
expectedStats.put(username1, buildStats(target.getLocaleId(), 0, 0,
wordCount, 0));
target = executeStateChangeTest(target, "test1",
newState, person1, expectedStats);
ContributionStatistics expectedStats2 = new ContributionStatistics();
expectedStats2.put(username2, buildStats(target.getLocaleId(), 0, 0,
wordCount, 0));
target = executeStateChangeTest(target, "test2",
newState, person2, expectedStats2);
//Test person1 statistic has not changed
String todayDate = formatter.format(today);
ContributionStatistics newStats =
statisticsService.getContributionStatistics(
"sample-project", "1.0", person1.getAccount()
.getUsername(), todayDate + ".." + todayDate);
assertThat(newStats).isEqualTo(expectedStats);
}
private LocaleStatistics buildStats(LocaleId localeId, int needReview,
int translated, int approved, int rejected) {
LocaleStatistics localeStatistics = new LocaleStatistics();
localeStatistics.put(localeId, new BaseContributionStatistic(approved,
needReview, translated, rejected));
return localeStatistics;
}
private HTextFlowTarget executeStateChangeTest(HTextFlowTarget target,
String newContent, ContentState newState, HPerson translator,
ContributionStatistics expectedStats) {
target = changeState(target, newContent, newState, translator);
String todayDate = formatter.format(today);
ContributionStatistics newStats =
statisticsService.getContributionStatistics(
"sample-project", "1.0", translator.getAccount()
.getUsername(), todayDate + ".." + todayDate);
assertNotNull(newStats);
assertThat(newStats).isEqualTo(expectedStats);
return target;
}
private HTextFlowTarget changeState(HTextFlowTarget target,
String contents, ContentState newState, HPerson translator) {
target.setContents(contents);
target.setState(newState);
target.setTranslator(translator);
textFlowTargetDAO.makePersistent(target);
textFlowTargetDAO.flush();
return textFlowTargetDAO.findById(target.getId());
}
}