Merge branch 'release/5.18.0'
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / DescriptionServiceImplTest.java
1 /**
2 * Copyright (C) 2009 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.api.service;
11
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14
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;
20 import java.util.Set;
21 import java.util.UUID;
22
23 import org.apache.log4j.Logger;
24 import org.junit.Assert;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.unitils.dbunit.annotation.DataSet;
28 import org.unitils.spring.annotation.SpringBeanByType;
29
30 import eu.etaxonomy.cdm.api.service.pager.Pager;
31 import eu.etaxonomy.cdm.model.common.Language;
32 import eu.etaxonomy.cdm.model.common.LanguageString;
33 import eu.etaxonomy.cdm.model.description.DescriptionBase;
34 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
35 import eu.etaxonomy.cdm.model.description.Feature;
36 import eu.etaxonomy.cdm.model.description.TaxonDescription;
37 import eu.etaxonomy.cdm.model.description.TextData;
38 import eu.etaxonomy.cdm.model.taxon.Taxon;
39 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
40
41 /**
42 * @author a.babadshanjan
43 * @since 09.02.2009
44 */
45 public class DescriptionServiceImplTest extends CdmTransactionalIntegrationTest {
46
47 @SuppressWarnings("unused")
48 private static Logger logger = Logger.getLogger(DescriptionServiceImplTest.class);
49
50 @SpringBeanByType
51 private IDescriptionService service;
52
53 @SpringBeanByType
54 private ITermService termService;
55
56 @Test
57 public void testGetDefaultFeatureVocabulary() {
58 service.getDefaultFeatureVocabulary();
59 }
60
61 @Test
62 @DataSet("CommonServiceImplTest.xml")
63 public void testChangeDescriptionElement(){
64 DescriptionBase<?> descBase = service.find(UUID.fromString("eb17b80a-9be6-4642-a6a8-b19a318925e6"));
65 Set<DescriptionElementBase> elements = descBase.getElements();
66 Iterator<?> iterator = elements.iterator();
67 while (iterator.hasNext()){
68 DescriptionElementBase base = (DescriptionElementBase) iterator.next();
69 if (base instanceof TextData){
70 TextData textdata = (TextData) base;
71 Set <Entry<Language,LanguageString>> entries = textdata.getMultilanguageText().entrySet();
72 Iterator<?> entryIterator = entries.iterator();
73 while (entryIterator.hasNext()){
74 Entry <Language, LanguageString> entry = (Entry<Language, LanguageString>) entryIterator.next();
75 LanguageString langString = entry.getValue();
76 // System.out.println(langString);
77 langString.setText("blablubber");
78 }
79 }
80 }
81 service.saveOrUpdate(descBase);
82 Pager<DescriptionElementBase> allElements = service.getDescriptionElements(null, null, null, null, null, null);
83 Assert.assertEquals(1, allElements.getCount().intValue());
84 DescriptionElementBase test = allElements.getRecords().get(0);
85 if (test instanceof TextData){
86
87 Set <Entry<Language,LanguageString>> entries = ((TextData) test).getMultilanguageText().entrySet();
88 Iterator<Entry<Language,LanguageString>> entryIterator = entries.iterator();
89 while (entryIterator.hasNext()){
90 Entry <Language, LanguageString> entry = entryIterator.next();
91 LanguageString langString = entry.getValue();
92 // System.out.println(langString);
93 }
94 }
95 }
96
97 @Test
98 @DataSet("../../database/ClearDBDataSet.xml")
99 public void testMoveDescriptionElement(){
100 //Create data
101 Taxon taxon = Taxon.NewInstance(null, null);
102 TaxonDescription desc1 = TaxonDescription.NewInstance(taxon);
103 TextData textData1 = TextData.NewInstance(Feature.HABITAT(), "My habitat", Language.GERMAN(), null);
104 desc1.addElement(textData1);
105 service.saveOrUpdate(desc1);
106
107 TaxonDescription desc2 = TaxonDescription.NewInstance(taxon);
108 TextData textData2 = TextData.NewInstance(Feature.HABITAT(), "My habitat2", Language.GERMAN(), null);
109 desc2.addElement(textData2);
110 service.saveOrUpdate(desc2);
111 commitAndStartNewTransaction(null);
112
113 DescriptionBase<?> descLoaded1 = service.find(desc1.getUuid());
114 DescriptionBase<?> descLoaded2 = service.find(desc2.getUuid());
115
116 DescriptionElementBase textDataLoaded = descLoaded1.getElements().iterator().next();
117 Set<DescriptionElementBase> tmpSet = new HashSet<DescriptionElementBase>(descLoaded1.getElements());
118
119 //test for #4806
120 service.moveDescriptionElementsToDescription(tmpSet, descLoaded2, false, true);
121 try {
122 commitAndStartNewTransaction(null);
123 } catch (Exception e) {
124 Assert.fail("Moving description element should not throw an exception. Exception is " + e.getMessage());
125 }
126 }
127
128 @Test
129 @Ignore
130 public void testMoveDescriptionElementsToTaxon(){
131 //Create data
132 UUID commonNameFeatureUuid = Feature.COMMON_NAME().getUuid();
133 Feature commonNameFeatureData = (Feature)termService.find(commonNameFeatureUuid);
134
135 TaxonDescription sourceDescriptionData = TaxonDescription.NewInstance();
136 TextData elementData = TextData.NewInstance();
137 elementData.setFeature(commonNameFeatureData);
138 sourceDescriptionData.addElement(elementData);
139
140 TextData element2 = TextData.NewInstance();
141 element2.setFeature(commonNameFeatureData);
142 sourceDescriptionData.addElement(element2);
143
144 TextData element3Data = TextData.NewInstance();
145 element3Data.setFeature(commonNameFeatureData);
146 sourceDescriptionData.addElement(element3Data);
147 Assert.assertEquals(3, sourceDescriptionData.getElements().size());
148 TaxonDescription targetDescriptionData = TaxonDescription.NewInstance();
149 this.service.save(sourceDescriptionData);
150 this.service.save(targetDescriptionData);
151
152 commitAndStartNewTransaction(null);
153
154 TaxonDescription sourceDescription = (TaxonDescription)this.service.find(sourceDescriptionData.getId());
155 Assert.assertEquals(3, sourceDescription.getElements().size());
156
157 TaxonDescription targetDescription = (TaxonDescription)this.service.find(targetDescriptionData.getId());
158
159
160 Collection<DescriptionElementBase> sourceCollection = new HashSet<DescriptionElementBase>();
161 sourceCollection.addAll(sourceDescription.getElements());
162 sourceCollection.remove(element3Data); //should work as it works on equal
163 Assert.assertEquals(2, sourceCollection.size());
164
165 service.moveDescriptionElementsToDescription(sourceCollection, targetDescription, false, true);
166
167 Assert.assertEquals("Source description should have 1 element left", 1, sourceDescription.getElements().size());
168 Assert.assertEquals("Target description should have 2 new elements", 2, targetDescription.getElements().size());
169 //the following tests are not valid anymore as elements are cloned now even if isCopy is false
170 // Assert.assertTrue("The moved element should be in the new description", targetDescription.getElements().contains(element));
171 // Assert.assertTrue("The moved element2 should be in the new description", targetDescription.getElements().contains(element2));
172 // Assert.assertFalse("Element3 should not be in the new description", targetDescription.getElements().contains(element3));
173
174 Assert.assertTrue("Element3 should remain in the old description", sourceDescription.getElements().contains(element3Data));
175 sourceDescription = (TaxonDescription) this.service.find(sourceDescription.getUuid());
176 targetDescription = (TaxonDescription) this.service.find(targetDescription.getUuid());
177 assertNotNull(sourceDescription);
178 assertNotNull(targetDescription);
179 try {
180 service.moveDescriptionElementsToDescription(targetDescription.getElements(), sourceDescription, false, true);
181 } catch (Exception e) {
182 //asserting that no ConcurrentModificationException is thrown when the elements collection is passed as a parameter
183 e.printStackTrace();
184 Assert.fail();
185 }
186
187 Assert.assertEquals("Source description should have 3 elements again", 3, sourceDescription.getElements().size());
188 Assert.assertEquals("Destination description should have no elements again", 0, targetDescription.getElements().size());
189 sourceDescription = (TaxonDescription) this.service.find(sourceDescription.getUuid());
190 targetDescription = (TaxonDescription) this.service.find(targetDescription.getUuid());
191 assertNotNull(sourceDescription);
192 assertNull(targetDescription);
193
194
195
196
197 }
198
199 @Test
200 // @Ignore
201 public void testMoveDescriptionElementsToTaxonAndResaveDeletedDescription(){
202
203 //Create data
204 UUID commonNameFeatureUuid = Feature.COMMON_NAME().getUuid();
205 Feature commonNameFeatureData = (Feature)termService.find(commonNameFeatureUuid);
206
207 TaxonDescription sourceDescriptionData = TaxonDescription.NewInstance();
208 TextData elementData = TextData.NewInstance();
209 elementData.setFeature(commonNameFeatureData);
210 sourceDescriptionData.addElement(elementData);
211
212 TextData element2 = TextData.NewInstance();
213 element2.setFeature(commonNameFeatureData);
214 sourceDescriptionData.addElement(element2);
215
216 TextData element3Data = TextData.NewInstance();
217 element3Data.setFeature(commonNameFeatureData);
218 sourceDescriptionData.addElement(element3Data);
219 Assert.assertEquals(3, sourceDescriptionData.getElements().size());
220 TaxonDescription targetDescriptionData = TaxonDescription.NewInstance();
221 this.service.save(sourceDescriptionData);
222 this.service.save(targetDescriptionData);
223
224 commitAndStartNewTransaction(null);
225
226 TaxonDescription sourceDescription = (TaxonDescription)this.service.find(sourceDescriptionData.getId());
227 Assert.assertEquals(3, sourceDescription.getElements().size());
228
229 TaxonDescription targetDescription = (TaxonDescription)this.service.find(targetDescriptionData.getId());
230 service.moveDescriptionElementsToDescription(sourceDescription.getElements(), targetDescription, false, true);
231 //the description is not removed anymore to avoid deletion of not persisted description elements
232
233 // TaxonDescription removedDescription = (TaxonDescription) this.service.find(sourceDescription.getUuid());
234 // assertNull(removedDescription);
235 this.service.save(targetDescription);
236
237 targetDescription = (TaxonDescription) this.service.find(targetDescription.getUuid());
238 assertNotNull(targetDescription);
239 }
240
241 @Override
242 public void createTestDataSet() throws FileNotFoundException {};
243 }