Revision 6163ee20
Added by Andreas Müller over 2 years ago
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/description/StructuredDescriptionAggregationTest.java | ||
---|---|---|
172 | 172 |
UpdateResult result = engine.invoke(config, repository); |
173 | 173 |
testStatusOk(result); |
174 | 174 |
commitAndStartNewTransaction(); |
175 |
testAggregatedDescription(false, false, false);
|
|
175 |
testAggregatedDescription(new TestConfig());
|
|
176 | 176 |
|
177 | 177 |
addSomeDataToFirstAggregation(); |
178 | 178 |
commitAndStartNewTransaction(); |
179 |
testAggregatedDescription(true, false, false);
|
|
179 |
testAggregatedDescription(new TestConfig().setWithAddedData());
|
|
180 | 180 |
|
181 | 181 |
// 2nd aggregation |
182 | 182 |
result = engine.invoke(config, repository); |
183 | 183 |
testStatusOk(result); |
184 | 184 |
commitAndStartNewTransaction(); |
185 |
testAggregatedDescription(false, false, false);
|
|
185 |
testAggregatedDescription(new TestConfig());
|
|
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
private void addSomeDataToFirstAggregation() { |
... | ... | |
212 | 212 |
UpdateResult result = engine.invoke(config, repository); |
213 | 213 |
testStatusOk(result); |
214 | 214 |
commitAndStartNewTransaction(); |
215 |
testAggregatedDescription(false, false, false);
|
|
215 |
testAggregatedDescription(new TestConfig());
|
|
216 | 216 |
|
217 | 217 |
removeSomeDataFromFirstAggregation(); |
218 | 218 |
commitAndStartNewTransaction(); |
... | ... | |
224 | 224 |
result = engine.invoke(config, repository); |
225 | 225 |
testStatusOk(result); |
226 | 226 |
commitAndStartNewTransaction(); |
227 |
testAggregatedDescription(false, false, true);
|
|
227 |
testAggregatedDescription(new TestConfig().setWithRemoved());
|
|
228 | 228 |
} |
229 | 229 |
|
230 | 230 |
private void removeSomeDataFromFirstAggregation() { |
... | ... | |
308 | 308 |
testState(sdAlpinaLeafColor, uuidLeafColorYellow, 0); |
309 | 309 |
} |
310 | 310 |
|
311 |
@Test |
|
312 |
@DataSets({ |
|
313 |
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"), |
|
314 |
@DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"), |
|
315 |
@DataSet(value="StructuredDescriptionAggregationTest.xml"), |
|
316 |
}) |
|
317 |
public void noneSourceTest() { |
|
318 |
createDefaultFeatureTree(); |
|
319 |
DescriptiveDataSet dataSet = createTestDataset(); |
|
320 |
commitAndStartNewTransaction(); |
|
321 |
|
|
322 |
StructuredDescriptionAggregationConfiguration config = createConfig(dataSet); |
|
323 |
config.setWithinTaxonSourceMode(AggregationSourceMode.NONE); |
|
324 |
config.setToParentSourceMode(AggregationSourceMode.NONE); |
|
325 |
|
|
326 |
// 1st aggregation |
|
327 |
UpdateResult result = engine.invoke(config, repository); |
|
328 |
testStatusOk(result); |
|
329 |
commitAndStartNewTransaction(); |
|
330 |
testAggregatedDescription(new TestConfig().setAggConfig(config)); |
|
331 |
|
|
332 |
config.setWithinTaxonSourceMode(AggregationSourceMode.DESCRIPTION); |
|
333 |
config.setToParentSourceMode(AggregationSourceMode.NONE); |
|
334 |
result = engine.invoke(config, repository); |
|
335 |
testStatusOk(result); |
|
336 |
commitAndStartNewTransaction(); |
|
337 |
testAggregatedDescription(new TestConfig().setAggConfig(config)); |
|
338 |
|
|
339 |
config.setWithinTaxonSourceMode(AggregationSourceMode.NONE); |
|
340 |
config.setToParentSourceMode(AggregationSourceMode.DESCRIPTION); |
|
341 |
result = engine.invoke(config, repository); |
|
342 |
testStatusOk(result); |
|
343 |
commitAndStartNewTransaction(); |
|
344 |
testAggregatedDescription(new TestConfig().setAggConfig(config)); |
|
345 |
|
|
346 |
config.setWithinTaxonSourceMode(AggregationSourceMode.DESCRIPTION); |
|
347 |
config.setToParentSourceMode(AggregationSourceMode.TAXON); |
|
348 |
result = engine.invoke(config, repository); |
|
349 |
testStatusOk(result); |
|
350 |
commitAndStartNewTransaction(); |
|
351 |
testAggregatedDescription(new TestConfig().setAggConfig(config)); |
|
352 |
|
|
353 |
config.setWithinTaxonSourceMode(AggregationSourceMode.NONE); |
|
354 |
config.setToParentSourceMode(AggregationSourceMode.TAXON); |
|
355 |
result = engine.invoke(config, repository); |
|
356 |
testStatusOk(result); |
|
357 |
commitAndStartNewTransaction(); |
|
358 |
testAggregatedDescription(new TestConfig().setAggConfig(config)); |
|
359 |
|
|
360 |
config.setWithinTaxonSourceMode(AggregationSourceMode.ALL); |
|
361 |
config.setToParentSourceMode(AggregationSourceMode.DESCRIPTION); |
|
362 |
result = engine.invoke(config, repository); |
|
363 |
testStatusNotOk(result); |
|
364 |
commitAndStartNewTransaction(); |
|
365 |
|
|
366 |
config.setWithinTaxonSourceMode(AggregationSourceMode.ALL); |
|
367 |
config.setToParentSourceMode(AggregationSourceMode.DESCRIPTION); |
|
368 |
result = engine.invoke(config, repository); |
|
369 |
testStatusNotOk(result); |
|
370 |
commitAndStartNewTransaction(); |
|
371 |
|
|
372 |
// removeSomeDataFromFirstAggregation(); |
|
373 |
// commitAndStartNewTransaction(); |
|
374 |
// Assert.assertEquals("Should have 3 specimen desc, 1 literature desc, 2 individual association holder, " |
|
375 |
// + "4 aggregated descriptions, 4 cloned specimen descriptions (still not deleted), (3 cloned aggregated descriptions?) = 17", |
|
376 |
// 17, descriptionService.count(null)); |
|
377 |
// |
|
378 |
// // 2nd aggregation |
|
379 |
// result = engine.invoke(config, repository); |
|
380 |
// testStatusOk(result); |
|
381 |
// commitAndStartNewTransaction(); |
|
382 |
// testAggregatedDescription(false, false, true); |
|
383 |
} |
|
384 |
|
|
311 | 385 |
@Test |
312 | 386 |
@DataSets({ |
313 | 387 |
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"), |
... | ... | |
324 | 398 |
UpdateResult result = engine.invoke(config, repository); |
325 | 399 |
commitAndStartNewTransaction(); |
326 | 400 |
testStatusOk(result); |
327 |
testAggregatedDescription(false, false, false);
|
|
401 |
testAggregatedDescription(new TestConfig());
|
|
328 | 402 |
|
329 | 403 |
config.setIncludeLiterature(true); |
330 | 404 |
|
331 | 405 |
result = engine.invoke(config, repository); |
332 | 406 |
commitAndStartNewTransaction(); |
333 | 407 |
testStatusOk(result); |
334 |
testAggregatedDescription(false, true, false); //with literature
|
|
408 |
testAggregatedDescription(new TestConfig().setWithLiterature());
|
|
335 | 409 |
} |
336 | 410 |
|
337 | 411 |
private void testStatusOk(UpdateResult result) { |
... | ... | |
343 | 417 |
} |
344 | 418 |
} |
345 | 419 |
|
420 |
private void testStatusNotOk(UpdateResult result) { |
|
421 |
if (result.getStatus() == UpdateResult.Status.OK){ |
|
422 |
Assert.fail("Aggregation should fail but did not " + result.toString()); |
|
423 |
//TODO test for expected exception |
|
424 |
} |
|
425 |
} |
|
426 |
|
|
346 | 427 |
private void addLiterature(DescriptiveDataSet dataSet) { |
347 | 428 |
|
348 | 429 |
//literature description |
... | ... | |
354 | 435 |
dataSet.addDescription(literatureDescription); |
355 | 436 |
} |
356 | 437 |
|
357 |
private void testAggregatedDescription(boolean withAddedData, boolean withLiterature, boolean withRemovedData) { |
|
438 |
private class TestConfig{ |
|
439 |
boolean withAddedData; |
|
440 |
boolean withLiterature; |
|
441 |
boolean withRemovedData; |
|
442 |
AggregationSourceMode withinTaxonSourceMode = AggregationSourceMode.DESCRIPTION; |
|
443 |
AggregationSourceMode toParentSourceMode = AggregationSourceMode.DESCRIPTION; |
|
444 |
|
|
445 |
private TestConfig setWithAddedData() {withAddedData = true; return this;} |
|
446 |
private TestConfig setWithLiterature() {withLiterature = true; return this;} |
|
447 |
private TestConfig setWithRemoved() {withRemovedData = true; return this;} |
|
448 |
|
|
449 |
public TestConfig setAggConfig(StructuredDescriptionAggregationConfiguration config) { |
|
450 |
withinTaxonSourceMode = config.getWithinTaxonSourceMode(); |
|
451 |
toParentSourceMode = config.getToParentSourceMode(); |
|
452 |
return this; |
|
453 |
} |
|
454 |
} |
|
455 |
|
|
456 |
private void testAggregatedDescription(TestConfig config) { |
|
358 | 457 |
|
458 |
boolean withRemovedData = config.withRemovedData; |
|
459 |
boolean withLiterature = config.withLiterature; |
|
460 |
boolean withAddedData = config.withAddedData; |
|
461 |
boolean isWithinNone = config.withinTaxonSourceMode.isNone(); |
|
462 |
boolean isToParentNone = config.toParentSourceMode.isNone(); |
|
463 |
boolean isToParentTaxon = config.toParentSourceMode.isTaxon(); |
|
359 | 464 |
int intDel = withRemovedData? -1 : 0; |
360 | 465 |
int intLit = withLiterature? 1 : 0; |
361 | 466 |
|
... | ... | |
376 | 481 |
BigDecimal max = withRemovedData ? new BigDecimal("7.0") : new BigDecimal("8.0"); |
377 | 482 |
testQuantitativeData(uuidFeatureLeafLength, count, min, max, avg, aggrDescLapsanaCommunisAlpina); |
378 | 483 |
//... sources |
379 |
Assert.assertEquals(3+intLit+intDel, aggrDescLapsanaCommunisAlpina.getSources().size()); |
|
484 |
int nWithinSources = isWithinNone ? 0 : 3+intLit+intDel; |
|
485 |
Assert.assertEquals(nWithinSources, aggrDescLapsanaCommunisAlpina.getSources().size()); |
|
380 | 486 |
SpecimenOrObservationBase<?> specLcommunisAlpina1 = CdmBase.deproxy(occurrenceService.find(T_LAPSANA_COMMUNIS_ALPINA_SPEC1_UUID)); |
381 |
Assert.assertEquals("Spec1 must have 2 descriptions now. The primary one and the cloned.", 2, specLcommunisAlpina1.getSpecimenDescriptions().size()); |
|
382 |
Assert.assertEquals(1, specLcommunisAlpina1.getSpecimenDescriptions().stream().filter(d->d.isCloneForSource()).count()); |
|
383 |
DescriptionBase<?> clonedDesc = specLcommunisAlpina1.getDescriptions().stream().filter(d->d.isCloneForSource()).findFirst().get(); |
|
384 |
DescriptionBase<?> sourceDescription = getSingleSpecimenDescriptionSource(aggrDescLapsanaCommunisAlpina, T_LAPSANA_COMMUNIS_ALPINA_SPEC1_UUID); |
|
385 |
Assert.assertEquals(clonedDesc, sourceDescription); |
|
487 |
int nCloned = isWithinNone ? 0 : 1; |
|
488 |
Assert.assertEquals("Spec1 must have 2 descriptions now. The primary one and the cloned.", 1+nCloned, specLcommunisAlpina1.getSpecimenDescriptions().size()); |
|
489 |
Assert.assertEquals(nCloned, specLcommunisAlpina1.getSpecimenDescriptions().stream().filter(d->d.isCloneForSource()).count()); |
|
490 |
if (nCloned > 0){ |
|
491 |
DescriptionBase<?> clonedDesc = specLcommunisAlpina1.getDescriptions().stream().filter(d->d.isCloneForSource()).findFirst().get(); |
|
492 |
DescriptionBase<?> sourceDescription = getSingleSpecimenDescriptionSource(aggrDescLapsanaCommunisAlpina, T_LAPSANA_COMMUNIS_ALPINA_SPEC1_UUID); |
|
493 |
Assert.assertEquals(clonedDesc, sourceDescription); |
|
494 |
} |
|
386 | 495 |
|
387 | 496 |
//L. communis adenophora |
388 | 497 |
Taxon taxLapsanaCommunisAdenophora = (Taxon)taxonService.find(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID); |
... | ... | |
406 | 515 |
testQuantitativeData(uuidFeatureLeafLength, count, min, |
407 | 516 |
new BigDecimal("10.0"), avg, aggrDescLapsanaCommunis); |
408 | 517 |
//... sources |
409 |
Assert.assertEquals(2, aggrDescLapsanaCommunis.getSources().size()); |
|
518 |
int nToParent = isToParentNone ? 0 : 2; |
|
519 |
Assert.assertEquals(nToParent, aggrDescLapsanaCommunis.getSources().size()); |
|
410 | 520 |
Map<UUID, List<TaxonDescription>> taxonDescriptionMap = getSourceTaxonDescriptionMap(aggrDescLapsanaCommunis); |
411 |
Assert.assertEquals(2, taxonDescriptionMap.size()); |
|
412 |
Assert.assertEquals(1, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_ALPINA_UUID).size()); |
|
413 |
Assert.assertEquals(1, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID).size()); |
|
414 |
Assert.assertNotEquals(aggrDescLapsanaCommunisAlpina, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_ALPINA_UUID).get(0)); |
|
521 |
int nToParentDescs = isToParentTaxon? 0 : nToParent; |
|
522 |
Assert.assertEquals(nToParentDescs, taxonDescriptionMap.size()); |
|
523 |
if (nToParentDescs > 0){ |
|
524 |
Assert.assertEquals(1, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_ALPINA_UUID).size()); |
|
525 |
Assert.assertEquals(1, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID).size()); |
|
526 |
Assert.assertNotEquals(aggrDescLapsanaCommunisAlpina, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_ALPINA_UUID).get(0)); |
|
527 |
}else if (isToParentTaxon){ |
|
528 |
Map<UUID, List<Taxon>> taxonToTaxonSourceMap = getSourceTaxonMap(aggrDescLapsanaCommunis); |
|
529 |
Assert.assertEquals(1, taxonToTaxonSourceMap.get(T_LAPSANA_COMMUNIS_ALPINA_UUID).size()); |
|
530 |
Assert.assertEquals(1, taxonToTaxonSourceMap.get(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID).size()); |
|
531 |
Assert.assertEquals(taxLapsanaCommunisAlpina, taxonToTaxonSourceMap.get(T_LAPSANA_COMMUNIS_ALPINA_UUID).get(0)); |
|
532 |
} |
|
415 | 533 |
|
416 | 534 |
//Lapsana |
417 | 535 |
Taxon taxLapsana = (Taxon)taxonService.find(T_LAPSANA_UUID); |
... | ... | |
423 | 541 |
testQuantitativeData(uuidFeatureLeafLength, count, min, |
424 | 542 |
new BigDecimal("10.0"), avg, aggrDescLapsana); |
425 | 543 |
//... sources |
426 |
Assert.assertEquals(1, aggrDescLapsana.getSources().size()); |
|
544 |
nToParent = isToParentNone ? 0 : 1; |
|
545 |
Assert.assertEquals(nToParent, aggrDescLapsana.getSources().size()); |
|
546 |
nToParentDescs = isToParentTaxon? 0 : nToParent; |
|
427 | 547 |
taxonDescriptionMap = getSourceTaxonDescriptionMap(aggrDescLapsana); |
428 |
Assert.assertEquals(1, taxonDescriptionMap.size()); |
|
429 |
Assert.assertEquals(1, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_UUID).size()); |
|
430 |
Assert.assertNotEquals(aggrDescLapsanaCommunis, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_UUID).get(0)); |
|
548 |
Assert.assertEquals(nToParentDescs, taxonDescriptionMap.size()); |
|
549 |
if (nToParentDescs > 0){ |
|
550 |
Assert.assertEquals(1, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_UUID).size()); |
|
551 |
Assert.assertNotEquals(aggrDescLapsanaCommunis, taxonDescriptionMap.get(T_LAPSANA_COMMUNIS_UUID).get(0)); |
|
552 |
}else if (isToParentTaxon){ |
|
553 |
Map<UUID, List<Taxon>> taxonToTaxonSourceMap = getSourceTaxonMap(aggrDescLapsana); |
|
554 |
Assert.assertEquals(1, taxonToTaxonSourceMap.get(T_LAPSANA_COMMUNIS_UUID).size()); |
|
555 |
Assert.assertEquals(taxLapsanaCommunis, taxonToTaxonSourceMap.get(T_LAPSANA_COMMUNIS_UUID).get(0)); |
|
556 |
} |
|
431 | 557 |
|
432 | 558 |
//total description count |
559 |
nCloned = (isToParentNone || isToParentTaxon ? 0 : 3) + (isWithinNone ? 0 : 4); |
|
433 | 560 |
Assert.assertEquals("Should have 4 specimen desc, 1 literature desc, 2 individual association holder, " |
434 |
+ "4 aggregated descriptions, 4 cloned specimen descriptions, (3/4 cloned aggregated descriptions?) = 18/19",
|
|
435 |
18+intLit+(intDel*2), descriptionService.count(null));
|
|
561 |
+ "4 aggregated descriptions, 4/0 cloned specimen descriptions, (3/4/0 cloned aggregated descriptions?) = 18/19",
|
|
562 |
11+nCloned+intLit+(intDel*2), descriptionService.count(null));
|
|
436 | 563 |
|
437 | 564 |
} |
438 | 565 |
|
566 |
//a map of the taxon uuid to the attached source descriptions |
|
439 | 567 |
private Map<UUID, List<TaxonDescription>> getSourceTaxonDescriptionMap(TaxonDescription desc) { |
440 | 568 |
return desc.getSources().stream().filter(s->(s.getCdmSource() instanceof TaxonDescription)) |
441 | 569 |
.map(s->CdmBase.deproxy(s.getCdmSource(), TaxonDescription.class)) |
... | ... | |
446 | 574 |
((Function<DescriptionBase<?>, IDescribable<?>>)(d->d.isInstanceOf(SpecimenDescription.class)? d.getDescribedSpecimenOrObservation(): CdmBase.deproxy(d, TaxonDescription.class).getTaxon())) |
447 | 575 |
.andThen(IDescribable::getUuid); |
448 | 576 |
|
577 |
//a map of the taxon to the attached taxon (source link) |
|
578 |
private Map<UUID, List<Taxon>> getSourceTaxonMap(TaxonDescription desc) { |
|
579 |
return desc.getSources().stream().filter(s->(s.getCdmSource() instanceof Taxon)) |
|
580 |
.map(s->CdmBase.deproxy(s.getCdmSource(), Taxon.class)) |
|
581 |
.collect(Collectors.groupingBy(t->t.getUuid())); |
|
582 |
} |
|
583 |
|
|
584 |
|
|
449 | 585 |
private DescriptionBase<?> getSingleSpecimenDescriptionSource( |
450 | 586 |
TaxonDescription aggrDescLapsanaCommunisAlpina, UUID specimenUuid) { |
451 | 587 |
|
Also available in: Unified diff
ref #8871, ref #7980 handle aggregation source mode TAXON for toParent