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