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