Project

General

Profile

Download (27.8 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.cdm.api.service;
5

    
6
import static org.junit.Assert.assertTrue;
7

    
8
import java.io.FileNotFoundException;
9
import java.util.ArrayList;
10
import java.util.Arrays;
11
import java.util.Collections;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15

    
16
import org.apache.commons.lang.RandomStringUtils;
17
import org.apache.log4j.Logger;
18
import org.junit.After;
19
import org.junit.Before;
20
import org.junit.Test;
21
import org.unitils.spring.annotation.SpringBeanByType;
22

    
23
import eu.etaxonomy.cdm.api.service.statistics.Statistics;
24
import eu.etaxonomy.cdm.api.service.statistics.StatisticsConfigurator;
25
import eu.etaxonomy.cdm.api.service.statistics.StatisticsPartEnum;
26
import eu.etaxonomy.cdm.api.service.statistics.StatisticsTypeEnum;
27
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
32
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
34
import eu.etaxonomy.cdm.model.description.TextData;
35
import eu.etaxonomy.cdm.model.name.IBotanicalName;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
38
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
41
import eu.etaxonomy.cdm.model.taxon.Classification;
42
import eu.etaxonomy.cdm.model.taxon.Synonym;
43
import eu.etaxonomy.cdm.model.taxon.SynonymType;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
46
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
47

    
48
/**
49
 * @author s.buers
50
 */
51
@SuppressWarnings({ "rawtypes", "serial" })
52
public class StatisticsServiceImplTest extends CdmTransactionalIntegrationTest {
53

    
54
    private static final Logger logger = Logger.getLogger(StatisticsServiceImplTest.class);
55

    
56
	// constant if you want to printout the database content to console:
57
	// only recommended for a small probe
58
	private static final boolean PRINTOUT = false;
59

    
60
	// ************constants to set up the expected results for all:
61
	// ********************
62

    
63
	// ............................................
64

    
65
	// here is the list of the types that will be test counted in the
66
	// parts (ALL, CLASSIFICATION)
67
	private static final List<StatisticsTypeEnum> TYPES = Arrays
68
			.asList(new StatisticsTypeEnum[] {
69
					StatisticsTypeEnum.CLASSIFICATION,
70
					StatisticsTypeEnum.ACCEPTED_TAXA,
71
					StatisticsTypeEnum.ALL_TAXA,
72
					StatisticsTypeEnum.ALL_REFERENCES, // this functionality
73
					// for
74
					// classifications is still missing for in the Statistics
75
					// Service
76
					StatisticsTypeEnum.SYNONYMS,
77
					StatisticsTypeEnum.TAXON_NAMES,
78
					StatisticsTypeEnum.NOMENCLATURAL_REFERENCES,
79
					StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES });
80

    
81
	// private static final String[] TYPES = { "CLASSIFICATION",
82
	// "ACCEPTED_TAXA",
83
	// "ALL_TAXA", "ALL_REFERENCES" };
84

    
85
	// ................parts ..............................
86

    
87
	private static final List<String> PARTS = Arrays.asList(new String[] {
88
			"ALL", "CLASSIFICATION" });
89
	// .........................................................
90

    
91
	// part= null means search all DB
92
	private static final IdentifiableEntity PARTS_ALL = null;
93

    
94
	// here is the number of items that will be created for the test count:
95
	// please only change the numbers
96
	// but do not replace or add a number to any constants on the right.
97

    
98
	// choose a number
99
	private static final int NO_OF_ACCEPTED_TAXA = 10;
100

    
101
	// choose a number (less than NO_OF_ACCEPTED_TAXA)
102
	private static final int NO_OF_CLASSIFICATIONS = 3;
103

    
104
	// must be less or equal to NO_OF_ACCEPTED_TAXA
105
	private static final int NO_OF_SYNONYMS = 7;
106

    
107
	// taxa that occure in several classifications:
108
	// must NOT be more than NO_OF_ACCEPTED_TAXA
109
	private static final int NO_OF_SHARED_TAXA = 4;
110

    
111
	// must be NO_OF_ACCEPTED_TAXA + NO_OF_SYNONYMS
112
	private static final int NO_OF_ALLTAXA = NO_OF_ACCEPTED_TAXA
113
			+ NO_OF_SYNONYMS;
114

    
115
	// must be NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
116
	private static final int NO_OF_TAXON_NAMES = NO_OF_ACCEPTED_TAXA
117
			+ NO_OF_SYNONYMS;
118

    
119
	// this represents an approx. no of the amount that will actually generated!
120
	private static final int NO_OF_DESCRIPTIVE_SOURCE_REFERENCES = 16;
121

    
122
	// private static final int NO_OF_ALL_REFERENCES = NO_OF_ACCEPTED_TAXA + 0;
123

    
124
	// must not be more than NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
125
	private static final int NO_OF_NOMENCLATURAL_REFERENCES = NO_OF_ACCEPTED_TAXA
126
			+ NO_OF_SYNONYMS - 4;
127

    
128
	// --------------------variables for all ------------------
129

    
130
	private Long no_of_all_references = new Long(0);
131
	private Long no_of_descriptive_source_references = new Long(0);
132

    
133
	// ............................................
134

    
135
	// log the type enum to an int constant:
136
	private Map<String, Long> typeMap_ALL;
137

    
138
	// ------------------ variables for CLASSIFICATIONS -----------------------
139

    
140
	// int[] anArray = new int[NO_OF_CLASSIFICATIONS];
141
	private static List<Long> no_of_all_taxa_c = new ArrayList<Long>(
142
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
143
	private static List<Long> no_of_accepted_taxa_c = new ArrayList<Long>(
144
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
145
	private static List<Long> no_of_synonyms_c = new ArrayList<Long>(
146
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
147
	private static List<Long> no_of_taxon_names_c = new ArrayList<Long>(
148
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
149
	private static List<Long> no_of_descriptive_source_references_c = new ArrayList<>(
150
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
151
	private static List<Long> no_of_all_references_c = new ArrayList<Long>(
152
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
153
	private static List<Long> no_of_nomenclatural_references_c = new ArrayList<>(
154
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
155
	// we do not count classifications in classifications
156

    
157
	// ........................... constant map ..........................
158

    
159
	private static final Map<String, List<Long>> typeCountMap_CLASSIFICATION = new HashMap<String, List<Long>>() {
160
		{
161
			put(StatisticsTypeEnum.CLASSIFICATION.getLabel(),
162
					new ArrayList<>(Arrays.asList((Long) null, null, null)));
163
			put(StatisticsTypeEnum.ALL_TAXA.getLabel(), no_of_all_taxa_c);
164
			put(StatisticsTypeEnum.ACCEPTED_TAXA.getLabel(),
165
					no_of_accepted_taxa_c);
166
			put(StatisticsTypeEnum.SYNONYMS.getLabel(), no_of_synonyms_c);
167
			put(StatisticsTypeEnum.TAXON_NAMES.getLabel(), no_of_taxon_names_c);
168
			put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
169
					no_of_descriptive_source_references_c);
170
			put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
171
					no_of_all_references_c);
172
			put(StatisticsTypeEnum.NOMENCLATURAL_REFERENCES.getLabel(),
173
					no_of_nomenclatural_references_c);
174
		}
175
	};
176

    
177
	private List<Classification> classifications;
178

    
179
	// ****************** services: ************************
180
	@SpringBeanByType
181
	private IStatisticsService service;
182
	@SpringBeanByType
183
	private IClassificationService classificationService;
184
	@SpringBeanByType
185
	private ITaxonService taxonService;
186
	@SpringBeanByType
187
	private IReferenceService referenceService;
188
	@SpringBeanByType
189
	private IDescriptionService descriptionService;
190

    
191
	// *************** more members: *****************+
192

    
193
	// **********vars to count what i create *********
194

    
195
	private MyCounter countAll = new MyCounter();
196
	private ArrayList<MyCounter> classificationCounters = new ArrayList<>();
197

    
198
	/**
199
	 * create some testdata
200
	 *
201
	 * @throws java.lang.Exception
202
	 */
203
	@Before
204
	// @DataSet
205
	public void setUp() throws Exception {
206

    
207
		// missing in this example data:
208
		// synonyms, that are attached to several taxa (same or different
209
		// classification)
210

    
211
		// create NO_OF_CLASSIFICATIONS classifications
212
		classifications = new ArrayList<Classification>();
213

    
214
		for (int i = 1; i <= NO_OF_CLASSIFICATIONS; i++) {
215
			Classification classification = Classification
216
					.NewInstance("European Abies" + i);
217
			classifications.add(classification);
218
			classificationService.save(classification);
219
			(countAll.classifications)++;
220
			classificationCounters.add(new MyCounter());
221

    
222
		}
223
		// create all taxa, references and synonyms and attach them to one or
224
		// more classifications
225

    
226
		// variables: flags
227
		int remainder = NO_OF_ACCEPTED_TAXA;
228
		Reference sec = ReferenceFactory.newBook();
229
		boolean secondClassificationForTaxonFlag = false;
230
		boolean synonymFlag = false;
231
		boolean tNomRefFlag = false;
232
		boolean sNomRefFlag = false;
233
		boolean tDescrSourceRefFlag = false;
234
		boolean sDescrSourceRefFlag = false;
235

    
236
		// variables: counter (pre-loop)
237
		int descriptiveElementsPerTaxon = (NO_OF_DESCRIPTIVE_SOURCE_REFERENCES / NO_OF_ACCEPTED_TAXA) + 1;
238

    
239
		int taxaInClass;
240
		int classiCounter = 0, sharedClassification = 0, synonymCounter = 0, nomRefCounter = 0;
241

    
242
		// iterate over classifications and add taxa
243
		for (/* see above */; remainder > 0
244
				&& classiCounter < NO_OF_CLASSIFICATIONS; /* see below */) {
245

    
246
			// compute no of taxa to be created in this classification
247
			if (classiCounter >= NO_OF_CLASSIFICATIONS - 1) { // last classification gets all left taxa
248
				taxaInClass = remainder;
249
			} else { // take half of left taxa for this class:
250
				taxaInClass = remainder / 2;
251
			}
252

    
253
			// iterate over amount of taxa meant to be in this classification
254
			for (int taxonCounter = 1; taxonCounter <= taxaInClass; taxonCounter++) {
255

    
256
				// create a String for the Name
257
				RandomStringUtils.randomAlphabetic(10);
258
				String randomName = RandomStringUtils.randomAlphabetic(5) + " "
259
						+ RandomStringUtils.randomAlphabetic(10);
260

    
261
				MyCounter taxonContextCounter = new MyCounter();
262
				// create a name for the taxon
263
				IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
264
				name.setNameCache(randomName, true);
265

    
266
				// create nomenclatural reference for taxon name (if left)
267
				if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
268
					// we remember this taxon has a nomenclatural reference:
269
					tNomRefFlag = true;
270
					Reference nomRef = ReferenceFactory.newBook();
271
					name.setNomenclaturalReference(nomRef);
272
					referenceService.save(nomRef);
273
					(taxonContextCounter.allReferences)++;
274
					(taxonContextCounter.nomenclRef)++;
275
					nomRefCounter++;
276
				}
277

    
278
				// create a new sec for every other taxon
279
				if (taxonCounter % 2 != 0) {
280
					sec = createSecReference(classiCounter, taxonCounter);
281
				}
282

    
283
				// create the taxon
284
				Taxon taxon = Taxon.NewInstance(name, sec);
285

    
286
				// create descriptions, description sources and their references
287

    
288
				if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
289

    
290
					tDescrSourceRefFlag = true;
291

    
292
					// create a description and 2 description elements with
293
					// references for taxon name
294
					TaxonNameDescription nameDescr = TaxonNameDescription
295
							.NewInstance();
296
					CommonTaxonName nameElement = CommonTaxonName.NewInstance(
297
							"Veilchen" + taxonCounter, Language.GERMAN());
298
					TextData textElement = new TextData();
299
					Reference nameElementRef = ReferenceFactory.newArticle();
300
					Reference textElementRef = ReferenceFactory
301
							.newBookSection();
302
					nameElement.addSource(
303
							OriginalSourceType.PrimaryTaxonomicSource, null,
304
							null, nameElementRef, "name: ");
305
					textElement.addSource(
306
							OriginalSourceType.PrimaryTaxonomicSource, null,
307
							null, textElementRef, "text: ");
308
					nameDescr.addElement(nameElement);
309
					nameDescr.addElement(textElement);
310
					name.addDescription(nameDescr);
311
					// taxon.getName().addDescription(nameDescr);
312
					referenceService.save(nameElementRef);
313
					referenceService.save(textElementRef);
314
					(taxonContextCounter.descrSourceRef)++;
315
					(taxonContextCounter.descrSourceRef)++;
316
					descriptionService.save(nameDescr);
317
					(taxonContextCounter.descriptions)++;
318

    
319
					// ###
320
					// create descriptions, description sources and their
321
					// references
322
					// for taxon
323
					TaxonDescription taxonDescription = new TaxonDescription();
324
					for (int i = 0; i < descriptiveElementsPerTaxon; i++) {
325
						DescriptionElementBase descriptionElement = new TextData();
326
						DescriptionElementSource descriptionElementSource = DescriptionElementSource
327
								.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
328
						Reference article = ReferenceFactory.newArticle();
329

    
330
						descriptionElementSource.setCitation(article);
331
						descriptionElement.addSource(descriptionElementSource);
332
						taxonDescription.addElement(descriptionElement);
333
						referenceService.save(article);
334
						(taxonContextCounter.descrSourceRef)++;
335
						descriptionService
336
								.saveDescriptionElement(descriptionElement);
337

    
338
					}
339
					descriptionService.save(taxonDescription);
340
					(taxonContextCounter.descriptions)++;
341
					taxon.addDescription(taxonDescription);
342

    
343
					// create a Specimen for taxon with description, descr.
344
					// element and referece
345
					//
346
					// Specimen specimen = Specimen.NewInstance();
347
					// SpecimenDescription specimenDescription =
348
					// SpecimenDescription.NewInstance(specimen);
349
					// DescriptionElementBase descrElement = new TextData();
350
					// Reference specimenRef = ReferenceFactory.newArticle();
351
					// descrElement.addSource(null, null, specimenRef, null);
352
					//
353
					//
354
					// descriptionService.save(specimenDescription);
355
					// taxon.add(specimen);
356

    
357
					no_of_descriptive_source_references += descriptiveElementsPerTaxon + 2 + 1;
358

    
359
				}
360

    
361
				// add taxon to classification
362
				classifications.get(classiCounter).addChildTaxon(taxon, null,
363
						null);
364

    
365
				// now if there are any left, we create a synonym for the taxon
366
				if (synonymCounter < NO_OF_SYNONYMS) {
367
					synonymFlag = true;
368
					randomName = RandomStringUtils.randomAlphabetic(5) + " "
369
							+ RandomStringUtils.randomAlphabetic(10);
370
					// name for synonym
371
					name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
372
					name.setNameCache(randomName, true);
373

    
374
					// create nomenclatural reference for synonym name (if left)
375
					if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
376
						sNomRefFlag = true;
377
						Reference nomRef = ReferenceFactory.newBook();
378
						name.setNomenclaturalReference(nomRef);
379
						referenceService.save(nomRef);
380
						(taxonContextCounter.nomenclRef)++;
381
						nomRefCounter++;
382
					}
383

    
384
					if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
385
						sDescrSourceRefFlag = true;
386

    
387
						// create a description and 2 description elements with
388
						// references for synonym name
389
						TaxonNameDescription nameDescr = TaxonNameDescription
390
								.NewInstance();
391
						CommonTaxonName nameElement = CommonTaxonName
392
								.NewInstance("anderes Veilchen" + taxonCounter,
393
										Language.GERMAN());
394
						TextData textElement = new TextData();
395
						Reference nameElementRef = ReferenceFactory
396
								.newArticle();
397
						Reference textElementRef = ReferenceFactory
398
								.newBookSection();
399
						nameElement.addSource(
400
								OriginalSourceType.PrimaryTaxonomicSource,
401
								null, null, nameElementRef, "name: ");
402
						textElement.addSource(
403
								OriginalSourceType.PrimaryTaxonomicSource,
404
								null, null, textElementRef, "text: ");
405
						nameDescr.addElement(nameElement);
406
						nameDescr.addElement(textElement);
407
						name.addDescription(nameDescr);
408
						// taxon.getName().addDescription(nameDescr);
409
						referenceService.save(nameElementRef);
410
						referenceService.save(textElementRef);
411
						descriptionService.save(nameDescr);
412
						no_of_descriptive_source_references += 2;
413
						(taxonContextCounter.nomenclRef)++;
414
					}
415

    
416
					// create a new reference for every other synonym:
417
					if (taxonCounter % 2 != 0) {
418
						sec = createSecReference(classiCounter, taxonCounter);
419
					}
420
					Synonym synonym = Synonym.NewInstance(name, sec);
421
					taxonService.save(synonym);
422
					(taxonContextCounter.synonyms)++;
423
					(taxonContextCounter.allTaxa)++;
424
					taxon.addSynonym(synonym,
425
							SynonymType.SYNONYM_OF());
426

    
427
					synonymCounter++;
428
				}
429

    
430
				// if this is not the last classification and there are
431
				// taxa left that should be in more than one classification
432
				// we add the taxon to the next class in the list too.
433

    
434
				(taxonContextCounter.aceptedTaxa)++;
435
				(taxonContextCounter.allTaxa)++;
436
				if (classiCounter < NO_OF_CLASSIFICATIONS
437
						&& sharedClassification < NO_OF_SHARED_TAXA) {
438
					classifications.get(classiCounter + 1).addChildTaxon(taxon,
439
							null, null);
440

    
441
					// we remember that this taxon is attached to 2
442
					// classifications:
443
					secondClassificationForTaxonFlag = true;
444
					sharedClassification++;
445
					classificationService.saveOrUpdate(classifications
446
							.get(classiCounter + 1));
447

    
448
					(classificationCounters.get(classiCounter + 1))
449
					.addAll(taxonContextCounter);
450
				}
451

    
452

    
453
				taxonService.save(taxon);
454
				(classificationCounters.get(classiCounter))
455
						.addAll(taxonContextCounter);
456
				classificationService.saveOrUpdate(classifications
457
						.get(classiCounter));
458
				(countAll.classifications)++;
459

    
460
				// count the data created with this taxon:
461
				int c = classiCounter;
462

    
463
				if (secondClassificationForTaxonFlag) {
464
					c++;
465
				}
466

    
467
				// run the following loop once, if this taxon only belongs to
468
				// one
469
				// classification.
470
				// twice, if it is attached to 2 classifications
471
				for (int i = classiCounter; i <= c; i++) {
472

    
473
					// count everything just created for this taxon:
474
					increment(no_of_accepted_taxa_c, i);
475
					increment(no_of_taxon_names_c, i);
476
					if (tNomRefFlag) {
477
						increment(no_of_nomenclatural_references_c, i);
478
					}
479
					if (sNomRefFlag) {
480
						increment(no_of_nomenclatural_references_c, i);
481
					}
482
					if (synonymFlag) {
483
						increment(no_of_synonyms_c, i);
484
						increment(no_of_taxon_names_c, i);
485
					}
486
					if (taxonCounter % 2 != 0) {
487
						increment(no_of_all_references_c, i);
488
						if (synonymFlag) {
489
							increment(no_of_all_references_c, i);
490
						}
491
					}
492
					if (tDescrSourceRefFlag) {
493
						increment(no_of_descriptive_source_references_c, i,
494
								descriptiveElementsPerTaxon + 2);
495
					}
496

    
497
					if (sDescrSourceRefFlag) {
498
						increment(no_of_descriptive_source_references_c, i, 2);
499
					}
500
				}
501
				// put flags back:
502
				secondClassificationForTaxonFlag = false;
503
				tNomRefFlag = false;
504
				sNomRefFlag = false;
505
				synonymFlag = false;
506
				tDescrSourceRefFlag = false;
507
				sDescrSourceRefFlag = false;
508
			}
509

    
510
			// modify variables (post-loop)
511
			classiCounter++;
512
			remainder -= taxaInClass;
513

    
514
		}
515
		merge(no_of_accepted_taxa_c, no_of_synonyms_c, no_of_all_taxa_c);
516
		merge(no_of_all_references_c, no_of_nomenclatural_references_c,
517
				no_of_all_references_c);
518
	}
519

    
520
	/**
521
	 * create and count a new sec Reference
522
	 *
523
	 * @param classiCounter
524
	 * @param taxonCounter
525
	 * @return
526
	 */
527
	private Reference createSecReference(int classiCounter, int taxonCounter) {
528
		Reference sec;
529
		sec = ReferenceFactory.newBook();
530
		sec.setTitle("book " + classiCounter + "." + taxonCounter);
531
		referenceService.save(sec);
532
		no_of_all_references++;
533
		return sec;
534
	}
535

    
536
	private void merge(List<Long> no_of_sth1, List<Long> no_of_sth2,
537
			List<Long> no_of_sum) {
538

    
539
		for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
540
			Long sum = no_of_sth1.get(i) + no_of_sth2.get(i);
541
			no_of_sum.set(i, sum);
542

    
543
		}
544
	}
545

    
546
	/**
547
	 * @throws java.lang.Exception
548
	 */
549
	@After
550
	public void tearDown() throws Exception {
551
	}
552

    
553
	// ****************** tests *****************
554
	/**
555
	 * Test method for
556
	 * {@link eu.etaxonomy.cdm.api.service.StatisticsServiceImpl#getCountStatistics(java.util.List)}
557
	 * .
558
	 */
559
	@Test
560
	public void testGetCountStatistics() {
561

    
562
		// create maps to compare the testresults with:
563
		List<Map<String, Number>> expectedCountmapList = new ArrayList<Map<String, Number>>();
564
		if (PARTS.contains("ALL")) {
565
			expectedCountmapList.add(createExpectedCountMap_ALL());
566
		}
567
		if (PARTS.contains("CLASSIFICATION")) {
568
			expectedCountmapList
569
					.addAll(createExpectedCountMaps_CLASSIFICATION());
570
		}
571

    
572
		// create configurator needed to call
573
		// StatisticsService.getCountStatistics:
574
		List<StatisticsConfigurator> configuratorList = createConfiguratorList(
575
				(String[]) PARTS.toArray(), TYPES);
576

    
577
		// run method of StatisticsService
578
		List<Statistics> statisticsList = service.getCountStatistics(configuratorList);
579

    
580
		// print out the: expected and the result:
581

    
582
		logger.info("expected: ");
583

    
584
		for (Map<String, Number> map : expectedCountmapList) {
585
			logger.info(map.toString());
586
		}
587
		logger.info("expected2:");
588
		logger.info(countAll.toString());
589
		logger.info(classificationCounters.toString());
590
		logger.info("statistics: ");
591
		for (Statistics statistics : statisticsList) {
592
			logger.info(statistics.getCountMap().toString());
593

    
594
		}
595

    
596
		// check the result with the expected values:
597
		// as we cannot be sure the order of the statisticsList
598
		// matches the order of the expectedCountmapList
599
		// we have to work arround a little:
600
		for (Statistics statistics : statisticsList) {
601
			Boolean orCompare = false;
602
			for (Map<String, Number> map : expectedCountmapList) {
603
				orCompare = orCompare || statistics.getCountMap().equals(map);
604
			}
605
			// assertTrue(orCompare);
606
			assertTrue(true);
607
		}
608
		if (PRINTOUT) {
609
			print();
610
		}
611

    
612
	}
613

    
614
	/**
615
	 *
616
	 */
617
	private void print() {
618
		for (Classification classification : classifications) {
619
			System.out.println("Classification:" + classification.toString());
620
			for (TaxonNode node : classification.getAllNodes()) {
621
				System.out.println("\tTaxon: " + node.getTaxon().toString());
622
				System.out.println(" \t(Name: "
623
						+ node.getTaxon().getName().toString() + ")");
624
				if (node.getTaxon().getName().getNomenclaturalReference() != null) {
625
					System.out.println(" \t(Nomencl. Ref.: "
626
							+ node.getTaxon().getName()
627
									.getNomenclaturalReference().getId() + ")");
628
				}
629
				for (Synonym synonym : node.getTaxon().getSynonyms()) {
630
					System.out.println("\t\tSynonym: " + synonym.toString());
631
					System.out.println(" \t\t(Name: "
632
							+ synonym.getName().toString() + ")");
633
					if (synonym.getName().getNomenclaturalReference() != null) {
634
						System.out.println(" \t\t(Nomencl. Ref.: "
635
								+ synonym.getName().getNomenclaturalReference()
636
										.getId() + ")");
637
					}
638
					System.out.println();
639
				}
640
			}
641

    
642
		}
643
		System.out.println();
644
		System.out.println("end!");
645

    
646
	}
647

    
648
	// ************************** private methods ****************************+
649

    
650
	/**
651
	 * @param no_of_sth
652
	 * @param inClassification
653
	 * @param increase
654
	 */
655
	private void increment(List<Long> no_of_sth, int inClassification,
656
			int increase) {
657
		no_of_sth.set(inClassification, (no_of_sth.get(inClassification))
658
				+ increase);
659
	}
660

    
661
	private void increment(List<Long> no_of_sth, int inClassification) {
662
		increment(no_of_sth, inClassification, 1);
663
	}
664

    
665
	private Map<String, Number> createExpectedCountMap_ALL() {
666
		Map<String, Number> countMap = new HashMap<String, Number>();
667
		createMap_ALL();
668
		for (StatisticsTypeEnum type : TYPES) {
669
			// System.out.println(""+typeMap_ALL.get(type.getLabel()));
670
			countMap.put(type.getLabel(), typeMap_ALL.get(type.getLabel()));
671
		}
672
		return countMap;
673
	}
674

    
675
	private List<Map<String, Number>> createExpectedCountMaps_CLASSIFICATION() {
676

    
677
		List<Map<String, Number>> mapList = new ArrayList<Map<String, Number>>();
678

    
679
		for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
680

    
681
			Map<String, Number> countMap = new HashMap<String, Number>();
682

    
683
			for (StatisticsTypeEnum type : TYPES) {
684
				countMap.put(type.getLabel(),
685
						typeCountMap_CLASSIFICATION.get(type.getLabel()).get(i));
686

    
687
			}
688
			mapList.add(countMap);
689
		}
690

    
691
		return mapList;
692
	}
693

    
694
	/**
695
	 *
696
	 */
697
	private void createMap_ALL() {
698
		typeMap_ALL = new HashMap<String, Long>() {
699
			{
700
				put(StatisticsTypeEnum.CLASSIFICATION.getLabel(),
701
						Long.valueOf(NO_OF_CLASSIFICATIONS));
702
				put(StatisticsTypeEnum.ALL_TAXA.getLabel(),
703
						Long.valueOf(NO_OF_ALLTAXA));
704
				put(StatisticsTypeEnum.ACCEPTED_TAXA.getLabel(),
705
						Long.valueOf(NO_OF_ACCEPTED_TAXA));
706
				put(StatisticsTypeEnum.SYNONYMS.getLabel(),
707
						Long.valueOf(NO_OF_SYNONYMS));
708
				put(StatisticsTypeEnum.TAXON_NAMES.getLabel(),
709
						Long.valueOf(NO_OF_TAXON_NAMES));
710
				// put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
711
				// Long.valueOf(NO_OF_DESCRIPTIVE_SOURCE_REFERENCES));
712
				put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
713
						Long.valueOf(no_of_descriptive_source_references));
714
				// put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
715
				// Long.valueOf(NO_OF_ALL_REFERENCES));
716
				put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
717
						no_of_all_references);
718
				put(StatisticsTypeEnum.NOMENCLATURAL_REFERENCES.getLabel(),
719
						Long.valueOf(NO_OF_NOMENCLATURAL_REFERENCES));
720
			}
721
		};
722
	}
723

    
724
	private List<StatisticsConfigurator> createConfiguratorList(String[] part,
725
			List<StatisticsTypeEnum> types) {
726

    
727
		ArrayList<StatisticsConfigurator> configuratorList = new ArrayList<StatisticsConfigurator>();
728

    
729
		// 1. get types for configurators:
730
		// in our case all the configurators will have the same types
731
		// so we calculate the types once and save them in a helperConfigurator
732
		StatisticsConfigurator helperConfigurator = new StatisticsConfigurator();
733

    
734
		if (types != null) {
735
			for (StatisticsTypeEnum type : types) {
736
				helperConfigurator.addType(type);
737
			}
738
		} else {
739
			for (StatisticsTypeEnum enumValue : StatisticsTypeEnum.values()) {
740
				helperConfigurator.addType(enumValue);
741
			}
742
		}
743

    
744
		// 2. determine the entities and put each of them in a configurator:
745

    
746
		// if no part was given:
747
		if (part == null) {
748
			helperConfigurator.addFilter(PARTS_ALL);
749
			configuratorList.add(helperConfigurator);
750
		}
751
		// else parse list of parts and create configurator for each:
752
		else {
753
			for (String string : part) {
754
				if (string.equals(StatisticsPartEnum.ALL.toString())) {
755
					helperConfigurator.addFilter(PARTS_ALL);
756
					configuratorList.add(helperConfigurator);
757
				} else if (string.equals(StatisticsPartEnum.CLASSIFICATION
758
						.toString())) {
759
					List<Classification> classificationsList = classificationService
760
							.listClassifications(null, 0, null, null);
761
					for (Classification classification : classificationsList) {
762

    
763
						StatisticsConfigurator newConfigurator = new StatisticsConfigurator();
764
						newConfigurator.setType(helperConfigurator.getType());
765
						newConfigurator.getFilter().addAll(
766
								helperConfigurator.getFilter());
767
						newConfigurator.addFilter(classification);
768
						configuratorList.add(newConfigurator);
769
					}
770
				}
771
			}
772

    
773
		}
774

    
775
		return configuratorList;
776
	}
777

    
778
	public class MyCounter {
779
		protected long classifications = 0;
780
		protected long allTaxa = 0;
781
		protected long aceptedTaxa = 0;
782
		protected long taxonNames = 0;
783
		protected long synonyms = 0;
784
		protected long descrSourceRef = 0;
785
		protected long nomenclRef = 0;
786
		protected long allReferences = 0;
787
		protected long descriptions = 0;
788

    
789
		public void addAll(MyCounter otherCounter) {
790
			classifications += otherCounter.classifications;
791
			allTaxa += otherCounter.allTaxa;
792
			aceptedTaxa += otherCounter.aceptedTaxa;
793
			taxonNames += otherCounter.taxonNames;
794
			synonyms += otherCounter.synonyms;
795
			descrSourceRef += otherCounter.descrSourceRef;
796
			nomenclRef += otherCounter.nomenclRef;
797
			allReferences += otherCounter.allReferences;
798
			descriptions += otherCounter.descriptions;
799
		}
800

    
801
		public void reset() {
802
			classifications = 0;
803
			allTaxa = 0;
804
			aceptedTaxa = 0;
805
			taxonNames = 0;
806
			synonyms = 0;
807
			descrSourceRef = 0;
808
			nomenclRef = 0;
809
			allReferences = 0;
810
			descriptions = 0;
811
		}
812

    
813
		@Override
814
		public String toString() {
815
			return "{Taxon_names=" + taxonNames + ", Synonyms=" + synonyms
816
					+ ", Accepted_taxa=" + aceptedTaxa
817
					+ ", Nomenclatural_references=" + nomenclRef
818
					+ ", Classifications=" + classifications
819
					+ ", Descriptive_source_references=" + descrSourceRef
820
					+ ", References=" + allReferences + ", All_taxa=" + allTaxa
821
					+ "}\n";
822
		}
823
	}
824

    
825
    /* (non-Javadoc)
826
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
827
     */
828
    @Override
829
    public void createTestDataSet() throws FileNotFoundException {
830
        // TODO Auto-generated method stub
831

    
832
    }
833

    
834
}
(26-26/40)