ref #8248 Adapt test to import
[cdmlib.git] / cdmlib-io / src / test / java / eu / etaxonomy / cdm / io / owl / in / StructureTreeOwlImportTest.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.io.owl.in;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.FileNotFoundException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.unitils.dbunit.annotation.DataSet;
27 import org.unitils.spring.annotation.SpringBeanByType;
28
29 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
30 import eu.etaxonomy.cdm.api.service.ITermService;
31 import eu.etaxonomy.cdm.api.service.IVocabularyService;
32 import eu.etaxonomy.cdm.common.CdmUtils;
33 import eu.etaxonomy.cdm.io.descriptive.owl.in.StructureTreeOwlImport;
34 import eu.etaxonomy.cdm.io.descriptive.owl.in.StructureTreeOwlImportConfigurator;
35 import eu.etaxonomy.cdm.io.descriptive.owl.in.StructureTreeOwlImportState;
36 import eu.etaxonomy.cdm.model.common.Language;
37 import eu.etaxonomy.cdm.model.term.DefinedTerm;
38 import eu.etaxonomy.cdm.model.term.FeatureNode;
39 import eu.etaxonomy.cdm.model.term.FeatureTree;
40 import eu.etaxonomy.cdm.model.term.Representation;
41 import eu.etaxonomy.cdm.model.term.TermType;
42 import eu.etaxonomy.cdm.model.term.TermVocabulary;
43 import eu.etaxonomy.cdm.persistence.query.MatchMode;
44 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
45
46 /**
47 *
48 * @author pplitzner
49 * @since Apr 24, 2019
50 *
51 */
52 public class StructureTreeOwlImportTest extends CdmTransactionalIntegrationTest {
53
54 @SpringBeanByType
55 private StructureTreeOwlImport structureTreeImport;
56
57 @SpringBeanByType
58 private ITermService termService;
59
60 @SpringBeanByType
61 private IFeatureTreeService featureTreeService;
62
63 @SpringBeanByType
64 private IVocabularyService vocabularyService;
65
66 private StructureTreeOwlImportConfigurator configurator;
67
68 private FeatureTree tree;
69
70 @Before
71 public void setUp() throws URISyntaxException {
72 URL url = this.getClass().getResource("/eu/etaxonomy/cdm/io/owl/in/test_structures.owl");
73 URI uri = url.toURI();
74 assertNotNull(url);
75 configurator = StructureTreeOwlImportConfigurator.NewInstance(uri);
76 }
77
78 @Test
79 public void testInit() {
80 assertNotNull("structureTreeImport should not be null", structureTreeImport);
81 }
82
83 @Test
84 @DataSet(value="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
85 public void testDoInvoke() {
86 StructureTreeOwlImportState state = configurator.getNewState();
87 structureTreeImport.doInvoke(state);
88 this.setComplete();
89 this.endTransaction();
90
91 String treeLabel = "test_structures";
92 List<FeatureTree> trees = featureTreeService.listByTitle(FeatureTree.class, treeLabel, MatchMode.EXACT, null, null, null, null, null);
93 List<String> nodeProperties = new ArrayList<>();
94 nodeProperties.add("term");
95 tree = featureTreeService.loadWithNodes(trees.iterator().next().getUuid(), null, nodeProperties);
96 assertNotNull("featureTree should not be null", tree);
97
98 assertEquals("Tree has wrong term type", TermType.Structure, tree.getTermType());
99 assertEquals("Wrong number of distinct features", 4, tree.getDistinctFeatures().size());
100 List rootChildren = tree.getRootChildren();
101 assertEquals("Wrong number of root children", 1, rootChildren.size());
102 Object entirePlant = rootChildren.iterator().next();
103 assertTrue("Root is no feature node", entirePlant instanceof FeatureNode);
104 assertEquals("Root node has wrong term type", TermType.Structure, ((FeatureNode)entirePlant).getTermType());
105 FeatureNode<DefinedTerm> entirePlantNode = (FeatureNode<DefinedTerm>) entirePlant;
106 List<FeatureNode<DefinedTerm>> childNodes = entirePlantNode.getChildNodes();
107 assertEquals("Wrong number of children", 2, childNodes.size());
108
109 String inflorescenceLabel = "inflorescence";
110 String inflorescenceDescription = " the part of the plant that bears the flowers, including all its bracts branches and flowers but excluding unmodified leaves ";
111 List<DefinedTerm> records = termService.findByRepresentationText(inflorescenceDescription, DefinedTerm.class, null, null).getRecords();
112 assertEquals("wrong number of terms found for \"inflorescence\"", 1, records.size());
113 DefinedTerm inflorescence = records.iterator().next();
114 assertEquals(inflorescenceLabel, inflorescence.getLabel(Language.ENGLISH()));
115
116 for (FeatureNode<DefinedTerm> featureNode : childNodes) {
117 assertTrue("Child node not found. Found node with term: "+featureNode.getTerm().getLabel(),
118 featureNode.getTerm().getUuid().equals(inflorescence.getUuid())
119 || featureNode.getTerm().getLabel(Language.ENGLISH()).equals("Flower"));
120 if(featureNode.getTerm().getUuid().equals(inflorescence.getUuid())){
121 assertEquals("Term mismatch", inflorescence, featureNode.getTerm());
122 inflorescence = featureNode.getTerm();
123
124 String englishDescription = inflorescence.getDescription(Language.ENGLISH());
125 assertTrue("Description not found", CdmUtils.isNotBlank(englishDescription));
126 assertEquals("Description wrong", inflorescenceDescription, englishDescription);
127 assertEquals("wrong number of representations", 2, inflorescence.getRepresentations().size());
128 Representation germanRepresentation = inflorescence.getRepresentation(Language.GERMAN());
129 assertNotNull("Representation is null for "+Language.GERMAN(), germanRepresentation);
130 assertEquals("wrong description", "Der Teil der Pflanze, der die Bluete traegt", germanRepresentation.getDescription());
131 assertEquals("wrong label", "Infloreszenz", germanRepresentation.getLabel());
132 }
133 }
134 assertNotNull("term is null", inflorescence);
135 assertEquals("Wrong term type", TermType.Structure, inflorescence.getTermType());
136
137 String vocLabel = "03 Generative Structures";
138 List<TermVocabulary> vocs = vocabularyService.findByTitle(TermVocabulary.class, vocLabel, MatchMode.EXACT, null, null, null, null, Arrays.asList("terms")).getRecords();
139 assertEquals("wrong number of vocabularies", 1, vocs.size());
140 TermVocabulary termVoc = vocs.iterator().next();
141 assertEquals("Wrong vocabulary label", vocLabel, termVoc.getTitleCache());
142 assertEquals(3, termVoc.getTerms().size());
143 assertTrue("Term not included in vocabulary", termVoc.getTerms().contains(inflorescence));
144
145 }
146
147 @Override
148 public void createTestDataSet() throws FileNotFoundException {}
149
150
151 }