2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import static org
.junit
.Assert
.assertNotNull
;
13 import static org
.junit
.Assert
.assertNull
;
15 import java
.io
.FileNotFoundException
;
16 import java
.util
.Collection
;
17 import java
.util
.HashSet
;
18 import java
.util
.Iterator
;
19 import java
.util
.Map
.Entry
;
21 import java
.util
.UUID
;
23 import org
.apache
.logging
.log4j
.LogManager
;
24 import org
.apache
.logging
.log4j
.Logger
;
25 import org
.junit
.Assert
;
26 import org
.junit
.Ignore
;
27 import org
.junit
.Test
;
28 import org
.unitils
.dbunit
.annotation
.DataSet
;
29 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
31 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
32 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
33 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
36 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
37 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
38 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
40 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
43 * @author a.babadshanjan
46 public class DescriptionServiceImplTest
extends CdmTransactionalIntegrationTest
{
48 @SuppressWarnings("unused")
49 private static final Logger logger
= LogManager
.getLogger();
52 private IDescriptionService service
;
55 private ITermService termService
;
58 @DataSet("CommonServiceImplTest.xml")
59 public void testChangeDescriptionElement(){
60 DescriptionBase
<?
> descBase
= service
.find(UUID
.fromString("eb17b80a-9be6-4642-a6a8-b19a318925e6"));
61 Set
<DescriptionElementBase
> elements
= descBase
.getElements();
62 Iterator
<?
> iterator
= elements
.iterator();
63 while (iterator
.hasNext()){
64 DescriptionElementBase base
= (DescriptionElementBase
) iterator
.next();
65 if (base
instanceof TextData
){
66 TextData textdata
= (TextData
) base
;
67 Set
<Entry
<Language
,LanguageString
>> entries
= textdata
.getMultilanguageText().entrySet();
68 Iterator
<?
> entryIterator
= entries
.iterator();
69 while (entryIterator
.hasNext()){
70 Entry
<Language
, LanguageString
> entry
= (Entry
<Language
, LanguageString
>) entryIterator
.next();
71 LanguageString langString
= entry
.getValue();
72 // System.out.println(langString);
73 langString
.setText("blablubber");
77 service
.saveOrUpdate(descBase
);
78 Pager
<DescriptionElementBase
> allElements
= service
.pageDescriptionElements(null, null, null, null, false, null, null, null);
79 Assert
.assertEquals(1, allElements
.getCount().intValue());
80 DescriptionElementBase test
= allElements
.getRecords().get(0);
81 if (test
instanceof TextData
){
83 Set
<Entry
<Language
,LanguageString
>> entries
= ((TextData
) test
).getMultilanguageText().entrySet();
84 Iterator
<Entry
<Language
,LanguageString
>> entryIterator
= entries
.iterator();
85 while (entryIterator
.hasNext()){
86 Entry
<Language
, LanguageString
> entry
= entryIterator
.next();
87 LanguageString langString
= entry
.getValue();
88 // System.out.println(langString);
94 @DataSet("../../database/ClearDBDataSet.xml")
95 public void testMoveDescriptionElement(){
97 Taxon taxon
= Taxon
.NewInstance(null, null);
98 TaxonDescription desc1
= TaxonDescription
.NewInstance(taxon
);
99 TextData textData1
= TextData
.NewInstance(Feature
.HABITAT(), "My habitat", Language
.GERMAN(), null);
100 desc1
.addElement(textData1
);
101 service
.saveOrUpdate(desc1
);
103 TaxonDescription desc2
= TaxonDescription
.NewInstance(taxon
);
104 TextData textData2
= TextData
.NewInstance(Feature
.HABITAT(), "My habitat2", Language
.GERMAN(), null);
105 desc2
.addElement(textData2
);
106 service
.saveOrUpdate(desc2
);
107 commitAndStartNewTransaction(null);
109 DescriptionBase
<?
> descLoaded1
= service
.find(desc1
.getUuid());
110 DescriptionBase
<?
> descLoaded2
= service
.find(desc2
.getUuid());
112 DescriptionElementBase textDataLoaded
= descLoaded1
.getElements().iterator().next();
113 Set
<DescriptionElementBase
> tmpSet
= new HashSet
<DescriptionElementBase
>(descLoaded1
.getElements());
116 service
.moveDescriptionElementsToDescription(tmpSet
, descLoaded2
, false, true);
118 commitAndStartNewTransaction(null);
119 } catch (Exception e
) {
120 Assert
.fail("Moving description element should not throw an exception. Exception is " + e
.getMessage());
126 public void testMoveDescriptionElementsToTaxon(){
128 UUID commonNameFeatureUuid
= Feature
.COMMON_NAME().getUuid();
129 Feature commonNameFeatureData
= (Feature
)termService
.find(commonNameFeatureUuid
);
131 TaxonDescription sourceDescriptionData
= TaxonDescription
.NewInstance();
132 TextData elementData
= TextData
.NewInstance();
133 elementData
.setFeature(commonNameFeatureData
);
134 sourceDescriptionData
.addElement(elementData
);
136 TextData element2
= TextData
.NewInstance();
137 element2
.setFeature(commonNameFeatureData
);
138 sourceDescriptionData
.addElement(element2
);
140 TextData element3Data
= TextData
.NewInstance();
141 element3Data
.setFeature(commonNameFeatureData
);
142 sourceDescriptionData
.addElement(element3Data
);
143 Assert
.assertEquals(3, sourceDescriptionData
.getElements().size());
144 TaxonDescription targetDescriptionData
= TaxonDescription
.NewInstance();
145 this.service
.save(sourceDescriptionData
);
146 this.service
.save(targetDescriptionData
);
148 commitAndStartNewTransaction(null);
150 TaxonDescription sourceDescription
= (TaxonDescription
)this.service
.find(sourceDescriptionData
.getId());
151 Assert
.assertEquals(3, sourceDescription
.getElements().size());
153 TaxonDescription targetDescription
= (TaxonDescription
)this.service
.find(targetDescriptionData
.getId());
156 Collection
<DescriptionElementBase
> sourceCollection
= new HashSet
<DescriptionElementBase
>();
157 sourceCollection
.addAll(sourceDescription
.getElements());
158 sourceCollection
.remove(element3Data
); //should work as it works on equal
159 Assert
.assertEquals(2, sourceCollection
.size());
161 service
.moveDescriptionElementsToDescription(sourceCollection
, targetDescription
, false, true);
163 Assert
.assertEquals("Source description should have 1 element left", 1, sourceDescription
.getElements().size());
164 Assert
.assertEquals("Target description should have 2 new elements", 2, targetDescription
.getElements().size());
165 //the following tests are not valid anymore as elements are cloned now even if isCopy is false
166 // Assert.assertTrue("The moved element should be in the new description", targetDescription.getElements().contains(element));
167 // Assert.assertTrue("The moved element2 should be in the new description", targetDescription.getElements().contains(element2));
168 // Assert.assertFalse("Element3 should not be in the new description", targetDescription.getElements().contains(element3));
170 Assert
.assertTrue("Element3 should remain in the old description", sourceDescription
.getElements().contains(element3Data
));
171 sourceDescription
= (TaxonDescription
) this.service
.find(sourceDescription
.getUuid());
172 targetDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
173 assertNotNull(sourceDescription
);
174 assertNotNull(targetDescription
);
176 service
.moveDescriptionElementsToDescription(targetDescription
.getElements(), sourceDescription
, false, true);
177 } catch (Exception e
) {
178 //asserting that no ConcurrentModificationException is thrown when the elements collection is passed as a parameter
183 Assert
.assertEquals("Source description should have 3 elements again", 3, sourceDescription
.getElements().size());
184 Assert
.assertEquals("Destination description should have no elements again", 0, targetDescription
.getElements().size());
185 sourceDescription
= (TaxonDescription
) this.service
.find(sourceDescription
.getUuid());
186 targetDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
187 assertNotNull(sourceDescription
);
188 assertNull(targetDescription
);
197 public void testMoveDescriptionElementsToTaxonAndResaveDeletedDescription(){
200 UUID commonNameFeatureUuid
= Feature
.COMMON_NAME().getUuid();
201 Feature commonNameFeatureData
= (Feature
)termService
.find(commonNameFeatureUuid
);
203 TaxonDescription sourceDescriptionData
= TaxonDescription
.NewInstance();
204 TextData elementData
= TextData
.NewInstance();
205 elementData
.setFeature(commonNameFeatureData
);
206 sourceDescriptionData
.addElement(elementData
);
208 TextData element2
= TextData
.NewInstance();
209 element2
.setFeature(commonNameFeatureData
);
210 sourceDescriptionData
.addElement(element2
);
212 TextData element3Data
= TextData
.NewInstance();
213 element3Data
.setFeature(commonNameFeatureData
);
214 sourceDescriptionData
.addElement(element3Data
);
215 Assert
.assertEquals(3, sourceDescriptionData
.getElements().size());
216 TaxonDescription targetDescriptionData
= TaxonDescription
.NewInstance();
217 this.service
.save(sourceDescriptionData
);
218 this.service
.save(targetDescriptionData
);
220 commitAndStartNewTransaction(null);
222 TaxonDescription sourceDescription
= (TaxonDescription
)this.service
.find(sourceDescriptionData
.getId());
223 Assert
.assertEquals(3, sourceDescription
.getElements().size());
225 TaxonDescription targetDescription
= (TaxonDescription
)this.service
.find(targetDescriptionData
.getId());
226 service
.moveDescriptionElementsToDescription(sourceDescription
.getElements(), targetDescription
, false, true);
227 //the description is not removed anymore to avoid deletion of not persisted description elements
229 // TaxonDescription removedDescription = (TaxonDescription) this.service.find(sourceDescription.getUuid());
230 // assertNull(removedDescription);
231 this.service
.save(targetDescription
);
233 targetDescription
= (TaxonDescription
) this.service
.find(targetDescription
.getUuid());
234 assertNotNull(targetDescription
);
238 public void createTestDataSet() throws FileNotFoundException
{};