2 * Copyright (C) 2014 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
.taxeditor
.lazyloading
;
12 import java
.util
.Arrays
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.junit
.Assert
;
21 import org
.junit
.BeforeClass
;
22 import org
.junit
.Ignore
;
23 import org
.junit
.Test
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
31 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
33 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameFactory
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
37 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
42 * Test class which tests remoting for persistent cdm entities.
44 * FIXME:Remoting saving tests are ignored until the merge development is underway
48 //FIXME:Remoting need to create a proper dataset for this test
49 //Could be combined with AbstractLazyInitializerTest
51 public class RemoteLazyLoadingTest
extends BaseRemotingTest
{
53 private static final Logger logger
= Logger
.getLogger(RemoteLazyLoadingTest
.class);
55 private static IClassificationService classificationService
;
56 private static ITaxonService taxonService
;
58 private static List
<TaxonNode
> taxonNodes
;
60 private final UUID taxonUuid1
= UUID
.fromString("8217ef77-2ab1-4318-bd67-ccd0cdef07c4");
61 private final UUID taxonUuid2
= UUID
.fromString("ef96fafa-7750-4141-b31b-1ad1daab3e76");
65 public static void initializeRemoteLazyLoading() {
66 taxonService
= getRemoteApplicationController().getTaxonService();
68 classificationService
= getRemoteApplicationController().getClassificationService();
69 //List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
70 // Assert.assertFalse(classifications.isEmpty());
72 // Classification classification = classifications.get(0);
73 // Assert.assertNotNull(classification);
74 taxonNodes
= classificationService
.getAllNodes();
75 Assert
.assertFalse(taxonNodes
.isEmpty());
82 public void testCDMEntityGet() {
83 Iterator
<TaxonNode
> taxonNodeItr
= taxonNodes
.iterator();
86 while(taxonNodeItr
.hasNext() && count
<= maxcount
) {
87 TaxonNode taxonNode
= taxonNodeItr
.next();
88 Assert
.assertNotNull(taxonNode
);
90 Taxon taxon
= taxonNode
.getTaxon();
91 Assert
.assertNotNull(taxon
);
93 String taxonTitle
= taxon
.getTitleCache();
94 logger
.info("Taxon : " + taxonTitle
);
96 TaxonNameBase name
= taxon
.getName();
97 Assert
.assertNotNull(name
);
99 String nameTitle
= name
.getTitleCache();
100 logger
.info("Taxon Name : " + nameTitle
);
114 public void testCDMEntitySaveEager() {
115 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
116 String oldTitleCache
= taxon
.getTitleCache();
118 System
.out
.println("Taxon title : " + oldTitleCache
);
120 taxon
.setTitleCache(oldTitleCache
+ ":updated");
121 taxonService
.merge(taxon
);
123 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
124 System
.out
.println("New Taxon Title : " + taxonNew
.getTitleCache());
126 Assert
.assertTrue("Title caches should not be equal",oldTitleCache
.equals(taxonNew
.getTitleCache()));
128 taxonNew
.setTitleCache(oldTitleCache
);
129 taxonService
.merge(taxonNew
);
131 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
132 System
.out
.println("Old Taxon Title : " + taxonOld
.getTitleCache());
134 Assert
.assertEquals("Title caches should be equal",oldTitleCache
,taxonOld
.getTitleCache());
140 public void testCDMEntityUpdate() {
142 Team combAuthor
= Team
.NewInstance();
143 combAuthor
.addTeamMember(Person
.NewTitledInstance("test member"));
144 BotanicalName name
= TaxonNameFactory
.NewBotanicalInstance(null, "Test1", null, null, null, null, null, null, null);
145 name
.setCombinationAuthorship(combAuthor
);
146 Taxon taxon
= Taxon
.NewInstance(name
, null);
147 UUID taxonUuid
= taxonService
.save(taxon
).getUuid();
149 // Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
150 // NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
151 // String oldTitleCache = nvn.getTitleCache();
152 // System.out.println("Taxon Name Title : " + oldTitleCache);
153 // nvn.setTitleCache(oldTitleCache + ":updated", true);
155 // taxon.setTitleCache(oldTitleCache + ":updated",true);
157 // taxonService.update(taxon);
158 // } catch (LazyInitializationException lie) {
159 // lie.printStackTrace();
162 List
<String
> TAXON_INIT_STRATEGY
= Arrays
.asList(new String
[] {
165 Taxon taxonNew
= (Taxon
)taxonService
.findTaxonByUuid(taxonUuid
,TAXON_INIT_STRATEGY
);
166 NonViralName nvn
= CdmBase
.deproxy(taxonNew
.getName(),NonViralName
.class);
167 Team team
= CdmBase
.deproxy(nvn
.getCombinationAuthorship(),Team
.class);
168 String oldTitleCache
= nvn
.getTitleCache();
169 System
.out
.println("Taxon Name Title : " + oldTitleCache
);
170 nvn
.setTitleCache(oldTitleCache
+ ":updated", true);
171 taxonService
.update(taxonNew
);
177 public void testCDMEntitySaveLazy() {
178 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
180 NonViralName nvn
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
181 String oldTitleCache
= nvn
.getTitleCache();
182 System
.out
.println("Taxon Name Title : " + oldTitleCache
);
183 nvn
.setTitleCache(oldTitleCache
+ ":updated", true);
184 taxonService
.update(taxon
);
186 // Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
187 // NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
188 // System.out.println("New Taxon Name Title : " + nvnNew.getTitleCache());
190 // Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,nvnNew.getTitleCache());
192 // nvnNew.setTitleCache(oldTitleCache, true);
193 // taxonService.update(taxon);
195 // Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
196 // NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
197 // System.out.println("Old Taxon Name Title : " + nvnNew.getTitleCache());
199 // Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
203 public void testCDMEntitySaveLazyNew() {
204 Team combAuthor
= Team
.NewInstance();
205 combAuthor
.addTeamMember(Person
.NewTitledInstance("test member"));
206 BotanicalName name
= TaxonNameFactory
.NewBotanicalInstance(null, "Test1", null, null, null, null, null, null, null);
207 name
.setCombinationAuthorship(combAuthor
);
208 Taxon tax1
= Taxon
.NewInstance(name
, null);
209 UUID taxonUuid1
= taxonService
.save(tax1
).getUuid();
211 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
213 NonViralName nvn
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
214 String oldTitleCache
= nvn
.getTitleCache();
215 logger
.info("Taxon Name Title : " + oldTitleCache
);
216 nvn
.setTitleCache(oldTitleCache
+ ":updated",false);
217 taxonService
.update(taxon
);
219 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
220 NonViralName nvnNew
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
221 logger
.info("New Taxon Name Title : " + nvnNew
.getTitleCache());
223 Assert
.assertTrue("Title caches should not be equal",oldTitleCache
.equals(nvnNew
.getTitleCache()));
225 nvnNew
.setTitleCache(oldTitleCache
, true);
226 taxonService
.update(taxon
);
228 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
229 NonViralName nvnOld
= CdmBase
.deproxy(taxon
.getName(),NonViralName
.class);
230 logger
.info("Old Taxon Name Title : " + nvnNew
.getTitleCache());
232 Assert
.assertEquals("Title caches should be equal",oldTitleCache
,nvnOld
.getTitleCache());
236 public void testCDMEntitySaveCollection() {
237 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid1
);
239 Set
<TaxonRelationship
> taxRelations
= taxon
.getTaxonRelations();
240 Set
<String
> relToTitles
= new HashSet
<String
>();
241 Iterator
<TaxonRelationship
> trItr
= taxRelations
.iterator();
242 while(trItr
.hasNext()) {
243 TaxonRelationship tr
= trItr
.next();
244 System
.out
.println("Synonym Title Cache : " + tr
.getFromTaxon().getTitleCache());
245 relToTitles
.add(tr
.getFromTaxon().getTitleCache());
246 tr
.getFromTaxon().setTitleCache(tr
.getFromTaxon().getTitleCache() + ":updated");
249 taxonService
.merge(taxon
);
251 Taxon taxonNew
= (Taxon
)taxonService
.find(taxonUuid1
);
252 Set
<TaxonRelationship
> taxRelationsNew
= taxonNew
.getTaxonRelations();
254 Iterator
<TaxonRelationship
> trItrNew
= taxRelationsNew
.iterator();
255 Iterator
<String
> relToTitlesItr
= relToTitles
.iterator();
256 while(trItrNew
.hasNext() && relToTitlesItr
.hasNext()) {
257 TaxonRelationship trNew
= trItrNew
.next();
258 String relToTitle
= relToTitlesItr
.next();
259 System
.out
.println("New Synonym Title Cache: " + trNew
.getFromTaxon().getTitleCache());
260 Assert
.assertTrue("Synonym Title caches should not be equal", trNew
.getFromTaxon().getTitleCache().equals(relToTitle
));
261 trNew
.getFromTaxon().setTitleCache(relToTitle
);
264 Taxon taxonOld
= (Taxon
)taxonService
.find(taxonUuid1
);
266 Set
<TaxonRelationship
> synRelationsOld
= taxonNew
.getTaxonRelations();
267 Iterator
<TaxonRelationship
> srItrOld
= synRelationsOld
.iterator();
268 relToTitlesItr
= relToTitles
.iterator();
269 while(srItrOld
.hasNext() && relToTitlesItr
.hasNext()) {
270 TaxonRelationship srOld
= srItrOld
.next();
271 String relToTitle
= relToTitlesItr
.next();
272 System
.out
.println("New Synonym Title Cache: " + srOld
.getFromTaxon().getTitleCache());
273 Assert
.assertEquals("Synonym Title caches should be equal", srOld
.getFromTaxon().getTitleCache(), relToTitle
);