d9f06cc1f478c56bc8bb029e48c21a9f1d8f1591
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / lazyloading / AbstractLazyInitializerTest.java
1 /**
2 * Copyright (C) 2014 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 package eu.etaxonomy.taxeditor.lazyloading;
10
11 import java.util.HashSet;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.junit.Assert;
19 import org.junit.BeforeClass;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.unitils.dbunit.annotation.DataSet;
23
24 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
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.IBotanicalName;
31 import eu.etaxonomy.cdm.model.name.INonViralName;
32 import eu.etaxonomy.cdm.model.name.TaxonName;
33 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
34 import eu.etaxonomy.cdm.model.taxon.Classification;
35 import eu.etaxonomy.cdm.model.taxon.Taxon;
36 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
38 import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
39
40 /**
41 * Class to test the {@link CdmApplicationRemoteController}
42 */
43 // FIXME:Remoting need to create a proper dataset for this test
44 // Could be combined with RemotePersistentCollectionTest
45 @Ignore
46 @DataSet
47 public class AbstractLazyInitializerTest extends BaseRemotingTest {
48 private static final Logger logger = Logger.getLogger(AbstractLazyInitializerTest.class);
49
50 private static IClassificationService classificationService;
51 private static ITaxonService taxonService;
52
53 private static List<TaxonNode> taxonNodes;
54
55 private final UUID taxonUuid1 = UUID.fromString("8217ef77-2ab1-4318-bd67-ccd0cdef07c4");
56 private final UUID taxonUuid2 = UUID.fromString("ef96fafa-7750-4141-b31b-1ad1daab3e76");
57
58
59 @BeforeClass
60 public static void initializeRemoteLazyLoading() {
61
62 taxonService = getRemoteApplicationController().getTaxonService();
63 classificationService= getRemoteApplicationController().getClassificationService();
64 List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
65 Assert.assertFalse(classifications.isEmpty());
66
67 Classification classification = classifications.get(0);
68 Assert.assertNotNull(classification);
69 taxonNodes = classificationService.getAllNodes();
70 Assert.assertFalse(taxonNodes.isEmpty());
71
72 }
73
74
75
76 @Test
77 public void testCDMEntityGet() {
78 //ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
79 Iterator<TaxonNode> taxonNodeItr = taxonNodes.iterator();
80 int maxcount = 30;
81 int count = 0;
82 while(taxonNodeItr.hasNext() && count <= maxcount) {
83 TaxonNode taxonNode = taxonNodeItr.next();
84 Assert.assertNotNull(taxonNode);
85
86 Taxon taxon = taxonNode.getTaxon();
87 Assert.assertNotNull(taxon);
88
89 String taxonTitle = taxon.getTitleCache();
90 logger.info("Taxon : " + taxonTitle);
91
92 TaxonName name = taxon.getName();
93 Assert.assertNotNull(name);
94
95 String nameTitle = name.getTitleCache();
96 logger.info("Taxon Name : " + nameTitle);
97
98 count++;
99 }
100 }
101
102 @Test
103 public void taxonReadTest() {
104 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
105 }
106
107 @Test
108 public void testCDMEntitySave() {
109 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
110 String oldTitleCache = taxon.getTitleCache();
111
112 System.out.println("Taxon title : " + oldTitleCache);
113
114 taxon.setTitleCache(oldTitleCache + ":updated", true);
115 taxonService.merge(taxon);
116
117 Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
118 System.out.println("New Taxon Title : " + taxonNew.getTitleCache());
119
120 Assert.assertTrue("Title caches should not be equal",oldTitleCache.equals(taxonNew.getTitleCache()));
121
122
123 taxonNew.setTitleCache(oldTitleCache, true);
124 taxonService.merge(taxonNew);
125
126 Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
127 System.out.println("Old Taxon Title : " + taxonOld.getTitleCache());
128
129 Assert.assertEquals("Title caches should be equal",oldTitleCache,taxonOld.getTitleCache());
130
131 }
132
133 @Test
134 public void testCDMEntitySaveLazyNew() {
135 Team combAuthor = Team.NewInstance();
136 combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
137 IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(null, "Test1", null, null, null, null, null, null, null);
138 name.setCombinationAuthorship(combAuthor);
139 Taxon tax1 = Taxon.NewInstance(name, null);
140 UUID taxonUuid1 = taxonService.save(tax1).getUuid();
141
142 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
143
144 INonViralName nvn = CdmBase.deproxy(taxon.getName());
145 String oldTitleCache = nvn.getTitleCache();
146 logger.info("Taxon Name Title : " + oldTitleCache);
147 nvn.setTitleCache(oldTitleCache + ":updated",false);
148 taxonService.update(taxon);
149
150 Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
151 INonViralName nvnNew = CdmBase.deproxy(taxon.getName());
152 logger.info("New Taxon Name Title : " + nvnNew.getTitleCache());
153
154 Assert.assertTrue("Title caches should not be equal",oldTitleCache.equals(nvnNew.getTitleCache()));
155
156 nvnNew.setTitleCache(oldTitleCache, true);
157 taxonService.update(taxon);
158
159 Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
160 INonViralName nvnOld = CdmBase.deproxy(taxon.getName());
161 logger.info("Old Taxon Name Title : " + nvnNew.getTitleCache());
162
163 Assert.assertEquals("Title caches should be equal",oldTitleCache, nvnOld.getTitleCache());
164 }
165
166 @Ignore
167 @Test
168 public void testCdmEntitySaveCollection() {
169 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
170
171 Set<TaxonRelationship> taxRelations = taxon.getTaxonRelations();
172 Set<String> relToTitles = new HashSet<String>();
173 Iterator<TaxonRelationship> trItr = taxRelations.iterator();
174 while(trItr.hasNext()) {
175 TaxonRelationship tr = trItr.next();
176 System.out.println("Synonym Title Cache : " + tr.getFromTaxon().getTitleCache());
177 relToTitles.add(tr.getFromTaxon().getTitleCache());
178 tr.getFromTaxon().setTitleCache(tr.getFromTaxon().getTitleCache() + ":updated");
179
180 }
181 taxonService.merge(taxon);
182
183 Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
184 Set<TaxonRelationship> taxRelationsNew = taxonNew.getTaxonRelations();
185
186 Iterator<TaxonRelationship> trItrNew = taxRelationsNew.iterator();
187 Iterator<String> relToTitlesItr = relToTitles.iterator();
188 while(trItrNew.hasNext() && relToTitlesItr.hasNext()) {
189 TaxonRelationship trNew = trItrNew.next();
190 String relToTitle = relToTitlesItr.next();
191 System.out.println("New Synonym Title Cache: " + trNew.getFromTaxon().getTitleCache());
192 Assert.assertTrue("Synonym Title caches should not be equal", trNew.getFromTaxon().getTitleCache().equals(relToTitle));
193 trNew.getFromTaxon().setTitleCache(relToTitle);
194 }
195
196 Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
197
198 Set<TaxonRelationship> taxRelationsOld = taxonNew.getTaxonRelations();
199 Iterator<TaxonRelationship> trItrOld = taxRelationsOld.iterator();
200 relToTitlesItr = relToTitles.iterator();
201 while(trItrOld.hasNext() && relToTitlesItr.hasNext()) {
202 TaxonRelationship trOld = trItrOld.next();
203 String relToTitle = relToTitlesItr.next();
204 System.out.println("New Synonym Title Cache: " + trOld.getFromTaxon().getTitleCache());
205 Assert.assertEquals("Synonym Title caches should be equal", trOld.getFromTaxon().getTitleCache(), relToTitle);
206
207 }
208 }
209 }