Merge branch 'release/5.45.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.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;
30
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;
41
42 /**
43 * @author a.babadshanjan
44 * @since 09.02.2009
45 */
46 public class DescriptionServiceImplTest extends CdmTransactionalIntegrationTest {
47
48 @SuppressWarnings("unused")
49 private static final Logger logger = LogManager.getLogger();
50
51 @SpringBeanByType
52 private IDescriptionService service;
53
54 @SpringBeanByType
55 private ITermService termService;
56
57 @Test
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");
74 }
75 }
76 }
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){
82
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);
89 }
90 }
91 }
92
93 @Test
94 @DataSet("../../database/ClearDBDataSet.xml")
95 public void testMoveDescriptionElement(){
96 //Create data
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);
102
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);
108
109 DescriptionBase<?> descLoaded1 = service.find(desc1.getUuid());
110 DescriptionBase<?> descLoaded2 = service.find(desc2.getUuid());
111
112 DescriptionElementBase textDataLoaded = descLoaded1.getElements().iterator().next();
113 Set<DescriptionElementBase> tmpSet = new HashSet<DescriptionElementBase>(descLoaded1.getElements());
114
115 //test for #4806
116 service.moveDescriptionElementsToDescription(tmpSet, descLoaded2, false, true);
117 try {
118 commitAndStartNewTransaction(null);
119 } catch (Exception e) {
120 Assert.fail("Moving description element should not throw an exception. Exception is " + e.getMessage());
121 }
122 }
123
124 @Test
125 @Ignore
126 public void testMoveDescriptionElementsToTaxon(){
127 //Create data
128 UUID commonNameFeatureUuid = Feature.COMMON_NAME().getUuid();
129 Feature commonNameFeatureData = (Feature)termService.find(commonNameFeatureUuid);
130
131 TaxonDescription sourceDescriptionData = TaxonDescription.NewInstance();
132 TextData elementData = TextData.NewInstance();
133 elementData.setFeature(commonNameFeatureData);
134 sourceDescriptionData.addElement(elementData);
135
136 TextData element2 = TextData.NewInstance();
137 element2.setFeature(commonNameFeatureData);
138 sourceDescriptionData.addElement(element2);
139
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);
147
148 commitAndStartNewTransaction(null);
149
150 TaxonDescription sourceDescription = (TaxonDescription)this.service.find(sourceDescriptionData.getId());
151 Assert.assertEquals(3, sourceDescription.getElements().size());
152
153 TaxonDescription targetDescription = (TaxonDescription)this.service.find(targetDescriptionData.getId());
154
155
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());
160
161 service.moveDescriptionElementsToDescription(sourceCollection, targetDescription, false, true);
162
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));
169
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);
175 try {
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
179 e.printStackTrace();
180 Assert.fail();
181 }
182
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);
189
190
191
192
193 }
194
195 @Test
196 // @Ignore
197 public void testMoveDescriptionElementsToTaxonAndResaveDeletedDescription(){
198
199 //Create data
200 UUID commonNameFeatureUuid = Feature.COMMON_NAME().getUuid();
201 Feature commonNameFeatureData = (Feature)termService.find(commonNameFeatureUuid);
202
203 TaxonDescription sourceDescriptionData = TaxonDescription.NewInstance();
204 TextData elementData = TextData.NewInstance();
205 elementData.setFeature(commonNameFeatureData);
206 sourceDescriptionData.addElement(elementData);
207
208 TextData element2 = TextData.NewInstance();
209 element2.setFeature(commonNameFeatureData);
210 sourceDescriptionData.addElement(element2);
211
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);
219
220 commitAndStartNewTransaction(null);
221
222 TaxonDescription sourceDescription = (TaxonDescription)this.service.find(sourceDescriptionData.getId());
223 Assert.assertEquals(3, sourceDescription.getElements().size());
224
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
228
229 // TaxonDescription removedDescription = (TaxonDescription) this.service.find(sourceDescription.getUuid());
230 // assertNull(removedDescription);
231 this.service.save(targetDescription);
232
233 targetDescription = (TaxonDescription) this.service.find(targetDescription.getUuid());
234 assertNotNull(targetDescription);
235 }
236
237 @Override
238 public void createTestDataSet() throws FileNotFoundException {};
239 }