Project

General

Profile

« Previous | Next » 

Revision 1207266e

Added by Andreas Müller over 1 year ago

ref #9772 , ref #9842 cleanup rowwrapper test

View differences:

cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/description/DescriptiveDataSetServiceTest.java
15 15
import java.math.BigDecimal;
16 16
import java.util.ArrayList;
17 17
import java.util.HashSet;
18
import java.util.Iterator;
19 18
import java.util.List;
20 19
import java.util.Set;
21 20
import java.util.UUID;
......
40 39
import eu.etaxonomy.cdm.api.service.ITermService;
41 40
import eu.etaxonomy.cdm.api.service.ITermTreeService;
42 41
import eu.etaxonomy.cdm.api.service.IVocabularyService;
43
import eu.etaxonomy.cdm.api.service.UpdateResult;
44 42
import eu.etaxonomy.cdm.api.service.dto.CategoricalDataDto;
45 43
import eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto;
46 44
import eu.etaxonomy.cdm.api.service.dto.DescriptionElementDto;
......
49 47
import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
50 48
import eu.etaxonomy.cdm.api.service.dto.StateDataDto;
51 49
import eu.etaxonomy.cdm.api.service.dto.StatisticalMeasurementValueDto;
50
import eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO;
52 51
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
53 52
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
54
import eu.etaxonomy.cdm.model.common.CdmBase;
55 53
import eu.etaxonomy.cdm.model.description.CategoricalData;
56 54
import eu.etaxonomy.cdm.model.description.DescriptionBase;
57
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
58 55
import eu.etaxonomy.cdm.model.description.DescriptionType;
59 56
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
60 57
import eu.etaxonomy.cdm.model.description.Feature;
......
62 59
import eu.etaxonomy.cdm.model.description.QuantitativeData;
63 60
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
64 61
import eu.etaxonomy.cdm.model.description.State;
65
import eu.etaxonomy.cdm.model.description.StateData;
66 62
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
67 63
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
68 64
import eu.etaxonomy.cdm.model.description.TaxonDescription;
......
167 163
        DescriptiveDataSet dataSet = createTestDataset();
168 164
        commitAndStartNewTransaction();
169 165

  
170
        List<RowWrapperDTO<?>> result =  datasetService.getRowWrapper(dataSet.getUuid(), monitor);
166
        List<RowWrapperDTO<?>> rowWrappers =  datasetService.getRowWrapper(dataSet.getUuid(), monitor);
171 167

  
172 168
        //There are 4 specimen descriptions and one literature description (taxon association)
173
        assertTrue(result.size() == 5);
169
        assertTrue(rowWrappers.size() == 5);
174 170
        //check rowWrapper
175 171
        //specimen_1 2 categorical and 1 quantitative
176
        for (RowWrapperDTO<?> row: result){
177
            if (row instanceof SpecimenRowWrapperDTO){
178
                SpecimenRowWrapperDTO specimen = (SpecimenRowWrapperDTO)row;
179
                if (specimen.getSpecimenDto().getLabel().equals("alpina specimen1")){
180
                    Set<DescriptionElementDto> elements = specimen.getDataValueForFeature(uuidFeatureLeafColor);
181
                    if (elements != null && !elements.isEmpty() ){
182
                        Iterator<DescriptionElementDto> it = elements.iterator();
183
                        DescriptionElementDto dto = null;
184
                        if (it.hasNext()){
185
                            dto = it.next();
186
                        }else{
187
                            Assert.fail("There is no element for feature leaf color, but should.");
188
                        }
189
                        if (dto instanceof CategoricalDataDto){
190
                            assertTrue("The states should contain one element", ((CategoricalDataDto)dto).getStates().size() == 1);
191
                            StateDataDto stateData = ((CategoricalDataDto)dto).getStates().iterator().next();
192
                            assertEquals(uuidLeafColorBlue, stateData.getState().getUuid());
193
                        }else{
194
                            Assert.fail("The element is not of type categorical data");
195
                        }
196
                    }else{
197
                        Assert.fail();
198
                    }
199

  
200

  
201
                    elements = specimen.getDataValueForFeature(uuidFeatureLeafLength);
202
                    if (elements != null && !elements.isEmpty() ){
203
                        Iterator<DescriptionElementDto> it = elements.iterator();
204
                        DescriptionElementDto dto = null;
205
                        if (it.hasNext()){
206
                            dto = it.next();
207
                        }else{
208
                            Assert.fail("There is no element for feature leaf length, but should.");
209
                        }
210
                        if (dto instanceof QuantitativeDataDto){
211
                            assertTrue("The statistical values should contain one element", ((QuantitativeDataDto)dto).getValues().size() == 1);
212
                            StatisticalMeasurementValueDto statValue = ((QuantitativeDataDto)dto).getValues().iterator().next();
213
                            assertEquals(new BigDecimal("5.0"), statValue.getValue());
214
                        }else{
215
                            Assert.fail("The element is not of type quantitative data");
216
                        }
217
                    }else{
218
                        Assert.fail();
219
                    }
220

  
221

  
222
                    elements = specimen.getDataValueForFeature(uuidFeatureLeafPA);
223
//                    assertTrue("The element should be categorical data ", element instanceof CategoricalDataDto);
224
//                    assertTrue("The statistical values should contain one element", ((CategoricalDataDto)element).getStates().size() == 1);
225
//                    StateDataDto stateData2 = ((CategoricalDataDto)element).getStates().iterator().next();
226
//                    assertEquals(State.uuidPresent, stateData2.getState().getUuid());
227

  
228
                }
229
            }
230
//            if (row instanceof TaxonRowWrapperDTO){
231
//                TaxonRowWrapperDTO taxonRow = (TaxonRowWrapperDTO)row;
232
//                Set<DescriptionElementDto> element = taxonRow.getDataValueForFeature(uuidFeatureLeafLength);
233
//                assertTrue(element instanceof QuantitativeDataDto);
234
//                assertTrue(((QuantitativeDataDto)element).getValues().size() == 2);
235
//            }
236
        }
237

  
238

  
239
        commitAndStartNewTransaction();
172
        List<SpecimenRowWrapperDTO> alpinaSpec1List = rowWrappers.stream().filter(r->r instanceof SpecimenRowWrapperDTO)
173
            .map(r->(SpecimenRowWrapperDTO)r)
174
            .filter(s->s.getSpecimenDto().getLabel().equals("alpina specimen1")).collect(Collectors.toList());
175
        Assert.assertEquals(1, alpinaSpec1List.size());
176
        SpecimenRowWrapperDTO alpinaSpec1Dto = alpinaSpec1List.get(0);
177

  
178
        //leafColor
179
        Set<DescriptionElementDto> leafColorElements = alpinaSpec1Dto.getDataValueForFeature(uuidFeatureLeafColor);
180
        Assert.assertNotNull(leafColorElements);
181
        Assert.assertTrue(leafColorElements.size() == 1);
182
        DescriptionElementDto dto = leafColorElements.iterator().next();
183
        Assert.assertTrue(dto instanceof CategoricalDataDto);
184
        CategoricalDataDto cDto = (CategoricalDataDto)dto;
185
        assertTrue("The states should contain one element", cDto.getStates().size() == 1);
186
        StateDataDto stateData = cDto.getStates().iterator().next();
187
        assertEquals(uuidLeafColorBlue, stateData.getState().getUuid());
188

  
189
        //leaf length
190
        Set<DescriptionElementDto> leafLengthElements = alpinaSpec1Dto.getDataValueForFeature(uuidFeatureLeafLength);
191
        Assert.assertNotNull(leafLengthElements);
192
        Assert.assertTrue(leafLengthElements.size() == 1);
193
        dto = leafLengthElements.iterator().next();
194
        Assert.assertTrue(dto instanceof QuantitativeDataDto);
195
        QuantitativeDataDto qDto = (QuantitativeDataDto)dto;
196
        assertTrue("The statistical values should contain one element", qDto.getValues().size() == 1);
197
        StatisticalMeasurementValueDto statValue = qDto.getValues().iterator().next();
198
        assertEquals(new BigDecimal("5.0"), statValue.getValue());
199

  
200
        Set<DescriptionElementDto> leafPAElements = alpinaSpec1Dto.getDataValueForFeature(uuidFeatureLeafPA);
201
        Assert.assertNotNull(leafPAElements);
202
        Assert.assertTrue(leafPAElements.size() == 1);
203
        dto = leafPAElements.iterator().next();
204
        Assert.assertTrue(dto instanceof CategoricalDataDto);
205
        cDto = (CategoricalDataDto)dto;
206
        assertTrue("The statistical values should contain one element", cDto.getStates().size() == 1);
207
        stateData = cDto.getStates().iterator().next();
208
        assertEquals(State.uuidPresent, stateData.getState().getUuid());
209

  
210

  
211
        //taxon descriptions
212
        List<TaxonRowWrapperDTO> taxonDescList = rowWrappers.stream().filter(r->r instanceof TaxonRowWrapperDTO)
213
                .map(r->(TaxonRowWrapperDTO)r).collect(Collectors.toList());
214
        Assert.assertEquals(1, taxonDescList.size());
215
//        .filter(s->s.getTaxonDto().getLabel().equals("alpina specimen1")).collect(Collectors.toList());
216
        TaxonRowWrapperDTO taxonDto = taxonDescList.get(0);
217
        leafLengthElements = taxonDto.getDataValueForFeature(uuidFeatureLeafLength);
218
        Assert.assertNotNull(leafLengthElements);
219
        Assert.assertTrue(leafLengthElements.size() == 1);
220
        dto = leafLengthElements.iterator().next();
221
        Assert.assertTrue(dto instanceof QuantitativeDataDto);
222
        qDto = (QuantitativeDataDto)dto;
223
        assertTrue("The statistical values should contain one element", qDto.getValues().size() == 2);
224
        List<StatisticalMeasurementValueDto> minList = qDto.getValues().stream()
225
                .filter(vs->vs.getValue().equals(new BigDecimal("4.5"))).collect(Collectors.toList());
226
        Assert.assertEquals(1, minList.size());
227
        TermDto minDtoType = minList.get(0).getType();
228
        Assert.assertTrue(minDtoType.getUuid().equals(StatisticalMeasure.MIN().getUuid()));
229
        Assert.assertTrue(minDtoType.getTermType().equals(TermType.StatisticalMeasure));
240 230

  
241 231
    }
242 232

  
243
    @Ignore
233
    @Ignore   //the test currently does not run in suite due to exception during descriptionService.mergeDescriptions(descToUpdate, dataSet.getUuid());
244 234
    @Test
245 235
    @DataSets({
246 236
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
......
274 264
                updatedDescription = descDto.getDescriptionUuid();
275 265
            }
276 266
        }
267

  
268
        commitAndStartNewTransaction();
277 269
        descriptionService.mergeDescriptions(descToUpdate, dataSet.getUuid());
278 270

  
279 271
        commitAndStartNewTransaction();
......
289 281
        }
290 282
    }
291 283

  
292
    @Test
293
    @DataSets({
294
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
295
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
296
        @DataSet(value="StructuredDescriptionAggregationTest.xml"),
297
    })
298
    @Ignore
299
    public void incompleteCategoricalDataTest(){
300
        createDefaultFeatureTree();
301
        DescriptiveDataSet dataSet = DescriptiveDataSet.NewInstance();
302
        datasetService.save(dataSet);
303

  
304
        SpecimenDescription specDescAlpina1 = createSpecimenDescription(dataSet, T_LAPSANA_COMMUNIS_ALPINA_UUID, "alpina specimen1");
305
        addCategoricalData(specDescAlpina1, uuidFeatureLeafColor, null);
306

  
307
        TaxonNode tnLapsana = taxonNodeService.find(TN_LAPSANA_UUID);
308
        Assert.assertNotNull(tnLapsana);
309
        dataSet.addTaxonSubtree(tnLapsana);
310

  
311
        @SuppressWarnings("unchecked")
312
        TermTree<Feature> descriptiveSystem = termTreeService.find(uuidFeatureTree);
313
        dataSet.setDescriptiveSystem(descriptiveSystem);
314
        commitAndStartNewTransaction();
315

  
316

  
317

  
318
        Taxon taxLapsanaCommunisAlpina = (Taxon)taxonService.find(T_LAPSANA_COMMUNIS_ALPINA_UUID);
319
        TaxonDescription aggrDescLapsanaCommunisAlpina = testTaxonDescriptions(taxLapsanaCommunisAlpina, 1);
320
        List<StateData> sdAlpinaLeafColor = testCategoricalData(uuidFeatureLeafColor, 1, aggrDescLapsanaCommunisAlpina, false);
321
        testState(sdAlpinaLeafColor, uuidLeafColorBlue, 0);
322
        testState(sdAlpinaLeafColor, uuidLeafColorYellow, 0);
323
    }
324

  
325

  
326
    private void testStatusOk(UpdateResult result) {
327
        if (result.getStatus() != UpdateResult.Status.OK){
328
            Assert.fail("Aggregation should have status OK but was " + result.toString());
329
            for (Exception ex : result.getExceptions()){
330
                ex.printStackTrace();
331
            }
332
        }
333
    }
334 284

  
335 285
    private void addLiterature(DescriptiveDataSet dataSet) {
336 286

  
......
379 329
        return dataSet;
380 330
    }
381 331

  
382
    private TaxonDescription testTaxonDescriptions(Taxon taxon, int elementSize){
383
        List<TaxonDescription> taxonDescriptions = taxon.getDescriptions().stream()
384
                .filter(desc->desc.getTypes().contains(DescriptionType.AGGREGATED_STRUC_DESC))
385
                .collect(Collectors.toList());
386

  
387
        Assert.assertEquals(1, taxonDescriptions.size());
388
        TaxonDescription aggrDesc = taxonDescriptions.iterator().next();
389
        Set<DescriptionElementBase> elements = aggrDesc.getElements();
390
        Assert.assertEquals(elementSize, elements.size());
391
        return aggrDesc;
392
    }
393

  
394
    private void testQuantitativeData(UUID featureUuid, BigDecimal sampleSize, BigDecimal min,
395
            BigDecimal max, BigDecimal avg, TaxonDescription aggrDesc) {
396
        List<QuantitativeData> quantitativeDatas = aggrDesc.getElements().stream()
397
                .filter(element->element.getFeature().getUuid().equals(featureUuid))
398
                .map(catData->CdmBase.deproxy(catData, QuantitativeData.class))
399
                .collect(Collectors.toList());
400
        Assert.assertEquals(1, quantitativeDatas.size());
401
        QuantitativeData leafLength = quantitativeDatas.iterator().next();
402
        Assert.assertEquals(sampleSize, leafLength.getSampleSize());
403
        Assert.assertEquals(min, leafLength.getMin());
404
        Assert.assertEquals(max, leafLength.getMax());
405
        Assert.assertEquals(avg, leafLength.getAverage());
406
    }
407

  
408

  
409
    private List<StateData> testCategoricalData(UUID featureUuid, int stateDataCount, TaxonDescription taxonDescription, boolean withAddedData) {
410
        List<CategoricalData> categoricalDatas = taxonDescription.getElements().stream()
411
                .filter(element->element.getFeature().getUuid().equals(featureUuid))
412
                .map(catData->CdmBase.deproxy(catData, CategoricalData.class))
413
                .collect(Collectors.toList());
414
        int nCD = withAddedData ? 2 : 1;
415
        Assert.assertEquals(nCD, categoricalDatas.size());
416
        CategoricalData categoricalData;
417
        if (withAddedData){
418
            categoricalData = categoricalDatas.stream().filter(cd->cd.getStateData().get(0).getCount() != null ).findFirst().get();
419
        }else{
420
            categoricalData = categoricalDatas.iterator().next(); // categoricalDatas.stream().filter(cd->cd.getStateData().size() != 1).collect(Collectors.toList());
421
        }
422
        List<StateData> stateDatas = categoricalData.getStateData();
423
        Assert.assertEquals(stateDataCount, stateDatas.size());
424
        return stateDatas;
425
    }
426 332

  
427
    private void testState(List<StateData> stateDatas, UUID stateUuid, Integer stateDataCount){
428
        List<StateData> filteredStateDatas = stateDatas.stream()
429
                .filter(stateData->stateData.getState()!=null && stateData.getState().getUuid().equals(stateUuid))
430
                .collect(Collectors.toList());
431
        if(stateDataCount==0){
432
            // non-existence test
433
            Assert.assertEquals(0, filteredStateDatas.size());
434
            return;
435
        }
436
        Assert.assertEquals(1, filteredStateDatas.size());
437
        StateData stateData = filteredStateDatas.iterator().next();
438
        Assert.assertEquals(stateDataCount, stateData.getCount());
439
        Assert.assertEquals(stateUuid, stateData.getState().getUuid());
440
    }
441 333

  
442 334
    private void addQuantitativeData(DescriptionBase<?> desc, UUID uuidFeature, StatisticalMeasure type, BigDecimal value) {
443 335
        Feature feature = (Feature)termService.find(uuidFeature);

Also available in: Unified diff