forgot to add in last commit
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / lazyloading / RemoteLazyLoadingTest.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
10 package eu.etaxonomy.taxeditor.lazyloading;
11
12 import java.util.Arrays;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import org.apache.log4j.Level;
20 import org.apache.log4j.Logger;
21 import org.junit.Assert;
22 import org.junit.BeforeClass;
23 import org.junit.Ignore;
24 import org.junit.Test;
25
26 import eu.etaxonomy.cdm.api.service.IClassificationService;
27 import eu.etaxonomy.cdm.api.service.ITaxonService;
28 import eu.etaxonomy.cdm.model.agent.Person;
29 import eu.etaxonomy.cdm.model.agent.Team;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.name.BotanicalName;
32 import eu.etaxonomy.cdm.model.name.NonViralName;
33 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
35 import eu.etaxonomy.cdm.model.taxon.Taxon;
36 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
38
39
40
41 /**
42 * Test class which tests remoting for persistent cdm entities.
43 *
44 * FIXME:Remoting saving tests are ignored until the merge development is underway
45 * @author c.mathew
46 *
47 */
48 //@DataSet
49 public class RemoteLazyLoadingTest extends BaseRemotingTest {
50
51 private static final Logger logger = Logger.getLogger(RemoteLazyLoadingTest.class);
52
53 private static IClassificationService classificationService;
54 private static ITaxonService taxonService;
55
56 private static List<TaxonNode> taxonNodes;
57
58 private final UUID taxonUuid1 = UUID.fromString("8217ef77-2ab1-4318-bd67-ccd0cdef07c4");
59 private final UUID taxonUuid2 = UUID.fromString("ef96fafa-7750-4141-b31b-1ad1daab3e76");
60
61
62 @BeforeClass
63 public static void initializeRemoteLazyLoading() {
64
65 Logger.getRootLogger().setLevel(Level.DEBUG);
66 taxonService = getRemoteApplicationController().getTaxonService();
67
68 classificationService= getRemoteApplicationController().getClassificationService();
69 //List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
70 // Assert.assertFalse(classifications.isEmpty());
71 //
72 // Classification classification = classifications.get(0);
73 // Assert.assertNotNull(classification);
74 taxonNodes = classificationService.getAllNodes();
75 Assert.assertFalse(taxonNodes.isEmpty());
76
77 }
78
79
80
81 @Test
82 public void testCDMEntityGet() {
83 Iterator<TaxonNode> taxonNodeItr = taxonNodes.iterator();
84 int maxcount = 30;
85 int count = 0;
86 while(taxonNodeItr.hasNext() && count <= maxcount) {
87 TaxonNode taxonNode = taxonNodeItr.next();
88 Assert.assertNotNull(taxonNode);
89
90 Taxon taxon = taxonNode.getTaxon();
91 Assert.assertNotNull(taxon);
92
93 String taxonTitle = taxon.getTitleCache();
94 logger.info("Taxon : " + taxonTitle);
95
96 TaxonNameBase name = taxon.getName();
97 Assert.assertNotNull(name);
98
99 String nameTitle = name.getTitleCache();
100 logger.info("Taxon Name : " + nameTitle);
101
102 count++;
103 }
104
105
106 }
107
108 @Test
109 public void test() {
110
111 }
112
113 @Test
114 public void testCDMEntitySaveEager() {
115 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
116 String oldTitleCache = taxon.getTitleCache();
117
118 System.out.println("Taxon title : " + oldTitleCache);
119
120 taxon.setTitleCache(oldTitleCache + ":updated");
121 taxonService.merge(taxon);
122
123 Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
124 System.out.println("New Taxon Title : " + taxonNew.getTitleCache());
125
126 Assert.assertTrue("Title caches should not be equal",oldTitleCache.equals(taxonNew.getTitleCache()));
127
128 taxonNew.setTitleCache(oldTitleCache);
129 taxonService.merge(taxonNew);
130
131 Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
132 System.out.println("Old Taxon Title : " + taxonOld.getTitleCache());
133
134 Assert.assertEquals("Title caches should be equal",oldTitleCache,taxonOld.getTitleCache());
135
136 }
137
138
139 @Test
140 public void testCDMEntityUpdate() {
141
142 Team combAuthor = Team.NewInstance();
143 combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
144 BotanicalName name = BotanicalName.NewInstance(null, "Test1", null, null, null, null, null, null, null);
145 name.setCombinationAuthorTeam(combAuthor);
146 Taxon taxon = Taxon.NewInstance(name, null);
147 UUID taxonUuid = taxonService.save(taxon);
148
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);
154 //
155 // taxon.setTitleCache(oldTitleCache + ":updated",true);
156 // try {
157 // taxonService.update(taxon);
158 // } catch (LazyInitializationException lie) {
159 // lie.printStackTrace();
160 // }
161
162 List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String[] {
163 "name"
164 });
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.getCombinationAuthorTeam(),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);
172
173 }
174
175
176 @Test
177 public void testCDMEntitySaveLazy() {
178 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
179
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);
185
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());
189 //
190 // Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,nvnNew.getTitleCache());
191 //
192 // nvnNew.setTitleCache(oldTitleCache, true);
193 // taxonService.update(taxon);
194 //
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());
198 //
199 // Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
200 }
201
202 @Test
203 public void testCDMEntitySaveLazyNew() {
204 Team combAuthor = Team.NewInstance();
205 combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
206 BotanicalName name = BotanicalName.NewInstance(null, "Test1", null, null, null, null, null, null, null);
207 name.setCombinationAuthorTeam(combAuthor);
208 Taxon tax1 = Taxon.NewInstance(name, null);
209 UUID taxonUuid1 = taxonService.save(tax1);
210
211 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
212
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);
218
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());
222
223 Assert.assertTrue("Title caches should not be equal",oldTitleCache.equals(nvnNew.getTitleCache()));
224
225 nvnNew.setTitleCache(oldTitleCache, true);
226 taxonService.update(taxon);
227
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());
231
232 Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
233 }
234 @Ignore
235 @Test
236 public void testCDMEntitySaveCollection() {
237 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
238
239 Set<SynonymRelationship> synRelations = taxon.getSynonymRelations();
240 Set<String> relToTitles = new HashSet<String>();
241 Iterator<SynonymRelationship> srItr = synRelations.iterator();
242 while(srItr.hasNext()) {
243 SynonymRelationship sr = srItr.next();
244 System.out.println("Synonym Title Cache : " + sr.getSynonym().getTitleCache());
245 relToTitles.add(sr.getSynonym().getTitleCache());
246 sr.getSynonym().setTitleCache(sr.getSynonym().getTitleCache() + ":updated");
247
248 }
249 taxonService.merge(taxon);
250
251 Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
252 Set<SynonymRelationship> synRelationsNew = taxonNew.getSynonymRelations();
253
254 Iterator<SynonymRelationship> srItrNew = synRelationsNew.iterator();
255 Iterator<String> relToTitlesItr = relToTitles.iterator();
256 while(srItrNew.hasNext() && relToTitlesItr.hasNext()) {
257 SynonymRelationship srNew = srItrNew.next();
258 String relToTitle = relToTitlesItr.next();
259 System.out.println("New Synonym Title Cache: " + srNew.getSynonym().getTitleCache());
260 Assert.assertTrue("Synonym Title caches should not be equal", srNew.getSynonym().getTitleCache().equals(relToTitle));
261 srNew.getSynonym().setTitleCache(relToTitle);
262 }
263
264 Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
265
266 Set<SynonymRelationship> synRelationsOld = taxonNew.getSynonymRelations();
267 Iterator<SynonymRelationship> srItrOld = synRelationsOld.iterator();
268 relToTitlesItr = relToTitles.iterator();
269 while(srItrOld.hasNext() && relToTitlesItr.hasNext()) {
270 SynonymRelationship srOld = srItrOld.next();
271 String relToTitle = relToTitlesItr.next();
272 System.out.println("New Synonym Title Cache: " + srOld.getSynonym().getTitleCache());
273 Assert.assertEquals("Synonym Title caches should be equal", srOld.getSynonym().getTitleCache(), relToTitle);
274
275 }
276 }
277 }