Merge branch 'develop' into remoting-4.0
[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.BotanicalName;
31 import eu.etaxonomy.cdm.model.name.NonViralName;
32 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33 import eu.etaxonomy.cdm.model.taxon.Classification;
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 /**
43 * Class to test the {@link CdmApplicationRemoteController}
44 *
45 */
46 // FIXME:Remoting need to create a proper dataset for this test
47 // Could be combined with RemotePersistentCollectionTest
48 @Ignore
49 @DataSet
50 public class AbstractLazyInitializerTest extends BaseRemotingTest {
51 private static final Logger logger = Logger.getLogger(AbstractLazyInitializerTest.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 taxonService = getRemoteApplicationController().getTaxonService();
66 classificationService= getRemoteApplicationController().getClassificationService();
67 List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
68 Assert.assertFalse(classifications.isEmpty());
69
70 Classification classification = classifications.get(0);
71 Assert.assertNotNull(classification);
72 taxonNodes = classificationService.getAllNodes();
73 Assert.assertFalse(taxonNodes.isEmpty());
74
75 }
76
77
78
79 @Test
80 public void testCDMEntityGet() {
81 //ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
82 Iterator<TaxonNode> taxonNodeItr = taxonNodes.iterator();
83 int maxcount = 30;
84 int count = 0;
85 while(taxonNodeItr.hasNext() && count <= maxcount) {
86 TaxonNode taxonNode = taxonNodeItr.next();
87 Assert.assertNotNull(taxonNode);
88
89 Taxon taxon = taxonNode.getTaxon();
90 Assert.assertNotNull(taxon);
91
92 String taxonTitle = taxon.getTitleCache();
93 logger.info("Taxon : " + taxonTitle);
94
95 TaxonNameBase name = taxon.getName();
96 Assert.assertNotNull(name);
97
98 String nameTitle = name.getTitleCache();
99 logger.info("Taxon Name : " + nameTitle);
100
101 count++;
102 }
103 }
104
105 @Test
106 public void taxonReadTest() {
107 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
108
109 }
110
111
112 @Test
113 public void testCDMEntitySave() {
114 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
115 String oldTitleCache = taxon.getTitleCache();
116
117 System.out.println("Taxon title : " + oldTitleCache);
118
119 taxon.setTitleCache(oldTitleCache + ":updated", true);
120 taxonService.merge(taxon);
121
122 Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
123 System.out.println("New Taxon Title : " + taxonNew.getTitleCache());
124
125 Assert.assertTrue("Title caches should not be equal",oldTitleCache.equals(taxonNew.getTitleCache()));
126
127
128 taxonNew.setTitleCache(oldTitleCache, true);
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 testCDMEntitySaveLazyNew() {
141 Team combAuthor = Team.NewInstance();
142 combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
143 BotanicalName name = BotanicalName.NewInstance(null, "Test1", null, null, null, null, null, null, null);
144 name.setCombinationAuthorship(combAuthor);
145 Taxon tax1 = Taxon.NewInstance(name, null);
146 UUID taxonUuid1 = taxonService.save(tax1).getUuid();
147
148 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
149
150 NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
151 String oldTitleCache = nvn.getTitleCache();
152 logger.info("Taxon Name Title : " + oldTitleCache);
153 nvn.setTitleCache(oldTitleCache + ":updated",false);
154 taxonService.update(taxon);
155
156 Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
157 NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
158 logger.info("New Taxon Name Title : " + nvnNew.getTitleCache());
159
160 Assert.assertTrue("Title caches should not be equal",oldTitleCache.equals(nvnNew.getTitleCache()));
161
162 nvnNew.setTitleCache(oldTitleCache, true);
163 taxonService.update(taxon);
164
165 Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
166 NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
167 logger.info("Old Taxon Name Title : " + nvnNew.getTitleCache());
168
169 Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
170 }
171
172 @Ignore
173 @Test
174 public void testCDMEntitySaveCollection() {
175 Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
176
177 Set<SynonymRelationship> synRelations = taxon.getSynonymRelations();
178 Set<String> relToTitles = new HashSet<String>();
179 Iterator<SynonymRelationship> srItr = synRelations.iterator();
180 while(srItr.hasNext()) {
181 SynonymRelationship sr = srItr.next();
182 System.out.println("Synonym Title Cache : " + sr.getSynonym().getTitleCache());
183 relToTitles.add(sr.getSynonym().getTitleCache());
184 sr.getSynonym().setTitleCache(sr.getSynonym().getTitleCache() + ":updated");
185
186 }
187 taxonService.merge(taxon);
188
189 Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
190 Set<SynonymRelationship> synRelationsNew = taxonNew.getSynonymRelations();
191
192 Iterator<SynonymRelationship> srItrNew = synRelationsNew.iterator();
193 Iterator<String> relToTitlesItr = relToTitles.iterator();
194 while(srItrNew.hasNext() && relToTitlesItr.hasNext()) {
195 SynonymRelationship srNew = srItrNew.next();
196 String relToTitle = relToTitlesItr.next();
197 System.out.println("New Synonym Title Cache: " + srNew.getSynonym().getTitleCache());
198 Assert.assertTrue("Synonym Title caches should not be equal", srNew.getSynonym().getTitleCache().equals(relToTitle));
199 srNew.getSynonym().setTitleCache(relToTitle);
200 }
201
202 Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
203
204 Set<SynonymRelationship> synRelationsOld = taxonNew.getSynonymRelations();
205 Iterator<SynonymRelationship> srItrOld = synRelationsOld.iterator();
206 relToTitlesItr = relToTitles.iterator();
207 while(srItrOld.hasNext() && relToTitlesItr.hasNext()) {
208 SynonymRelationship srOld = srItrOld.next();
209 String relToTitle = relToTitlesItr.next();
210 System.out.println("New Synonym Title Cache: " + srOld.getSynonym().getTitleCache());
211 Assert.assertEquals("Synonym Title caches should be equal", srOld.getSynonym().getTitleCache(), relToTitle);
212
213 }
214 }
215 }