added more tests for both lazy loading entities and collections
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / test / java / eu / etaxonomy / taxeditor / remoting / RemotePersistentCollectionTest.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.remoting;
10
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.apache.log4j.Level;
17 import org.apache.log4j.Logger;
18 import org.hibernate.collection.internal.AbstractPersistentCollection;
19 import org.hibernate.collection.spi.PersistentCollection;
20 import org.junit.Assert;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.unitils.UnitilsJUnit4;
24
25 import eu.etaxonomy.cdm.api.service.IClassificationService;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.common.Language;
28 import eu.etaxonomy.cdm.model.common.LanguageString;
29 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30 import eu.etaxonomy.cdm.model.description.Feature;
31 import eu.etaxonomy.cdm.model.description.TaxonDescription;
32 import eu.etaxonomy.cdm.model.description.TextData;
33 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
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.strategy.match.MatchException;
38
39 /**
40 * Test class which tests remoting for persistent collections.
41 *
42 * @author c.mathew
43 * @created 13.03.2014
44 */
45 public class RemotePersistentCollectionTest extends RemoteApplicationConfigurationTest {
46 private static final Logger logger = Logger.getLogger(RemotePersistentCollectionTest.class);
47
48 private static IClassificationService classificationService;
49
50 @BeforeClass
51 public static void initializeServices() {
52 Logger.getRootLogger().setLevel(Level.INFO);
53 classificationService= applicationController.getClassificationService();
54 }
55
56 /**
57 * Test method which checks remoting for persistent lists.
58 *
59 */
60 @Test
61 public void persistentListTest() {
62
63 List<TaxonNode> taxonNodes = classificationService.getAllNodes();
64 int size = taxonNodes.size();
65 logger.debug("classificationService.getAllNodes() size : " + size);
66 TaxonNode taxonNode = null;
67 if(size > 0) {
68 Assert.assertFalse(taxonNodes.isEmpty());
69
70 taxonNode = taxonNodes.get(0);
71 Assert.assertNotNull(taxonNode);
72 Assert.assertTrue(taxonNodes.contains(taxonNode));
73
74 // get the list of child nodes, which will give a
75 // proxy list which is not yet initialised
76 List<TaxonNode> childNodes = taxonNode.getChildNodes();
77 // this size call will first initialise the list locally by internally
78 // calling the ICommonService.initializeCollection method and then
79 // call size on the initialised list
80 int childCount = childNodes.size();
81
82 // this size call will initialise the list remotely and only return the
83 // size of the list
84 int remoteChildCount = applicationController.getCommonService().size((PersistentCollection)childNodes);
85 Assert.assertEquals(childCount, remoteChildCount);
86
87 String firstNodeTaxonTitle = taxonNode.getTaxon().getTitleCache();
88 Assert.assertNotNull(firstNodeTaxonTitle);
89
90 if(childCount > 0) {
91 Assert.assertFalse(childNodes.isEmpty());
92 // this get call will use the already initialised list to get the
93 // 0th element
94 TaxonNode localTaxonNode = childNodes.get(0);
95
96 // this get call will initialise the list remotely and only return the
97 // 0th element from the list
98 TaxonNode remoteTaxonNode = (TaxonNode)applicationController.getCommonService().get((PersistentCollection)childNodes,0);
99
100 // the locally and remotely retrieved taxon node should exist in the
101 // (local and remote) child nodes list, should be not-null and should be equal to each other
102 Assert.assertTrue(taxonNode.getChildNodes().contains(localTaxonNode));
103 Assert.assertTrue(taxonNode.getChildNodes().contains(remoteTaxonNode));
104 Assert.assertTrue(applicationController.getCommonService().contains((PersistentCollection)childNodes, localTaxonNode));
105 Assert.assertTrue(applicationController.getCommonService().contains((PersistentCollection)childNodes, remoteTaxonNode));
106 Assert.assertNotNull(remoteTaxonNode);
107 Assert.assertNotNull(localTaxonNode);
108 Assert.assertEquals(remoteTaxonNode,localTaxonNode);
109 }
110 }
111 }
112
113 @Test
114 public void persistentSetTest() {
115 List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
116 int size = classifications.size();
117 if(size > 0) {
118 Assert.assertFalse(classifications.isEmpty());
119
120 Classification classification = classifications.get(0);
121 Assert.assertNotNull(classification);
122 Assert.assertTrue(classifications.contains(classification));
123
124 TaxonNode rootTaxonNode = classification.getRootNode();
125 // get the list of child nodes, which will give a
126 // proxy list which is not yet initialised
127 List<TaxonNode> childNodes = rootTaxonNode.getChildNodes();
128
129 // this size call will initialise the list locally by internally
130 // calling the ICommonService.initializeCollection method
131 int childCount = childNodes.size();
132
133 if(childCount > 0) {
134 Assert.assertFalse(childNodes.isEmpty());
135
136 // this get call will use the already initialised list to get the
137 // 0th element
138 Taxon localTaxon = childNodes.get(0).getTaxon();
139 Assert.assertNotNull(localTaxon);
140
141 TaxonNameBase taxonName = localTaxon.getName();
142 Assert.assertNotNull(taxonName);
143
144 // get the list of taxa, which will give a
145 // proxy set which is not yet initialised
146 Set<Taxon> taxa = taxonName.getTaxonBases();
147
148 // this size call will initialise the list locally by internally
149 // calling the ICommonService.initializeCollection method
150 int taxaCount = taxa.size();
151 Assert.assertNotEquals(taxaCount,-1);
152
153 if(taxaCount > 0) {
154 Assert.assertFalse(taxa.isEmpty());
155 // the locally retrieved taxon should exist in the
156 // (local and remote) taxon list and should be not-null
157 Assert.assertTrue(taxa.contains(localTaxon));
158 Assert.assertNotNull(localTaxon);
159 Assert.assertTrue(applicationController.getCommonService().contains((PersistentCollection)taxa, localTaxon));
160 }
161 }
162 }
163 }
164
165 @Test
166 public void persistentMapTest() {
167 List<TaxonNode> taxonNodes = classificationService.getAllNodes();
168 // calling iterator will initialise the collection
169 Iterator<TaxonNode> taxonNodesItr = taxonNodes.iterator();
170 while(taxonNodesItr.hasNext()) {
171 TaxonNode taxonNode = taxonNodesItr.next();
172 Taxon taxon = taxonNode.getTaxon();
173
174 if(taxon != null) {
175 Set<TaxonDescription> descriptions = taxon.getDescriptions();
176 Iterator<TaxonDescription> descriptionsItr = descriptions.iterator();
177 while(descriptionsItr.hasNext()) {
178 TaxonDescription desc = descriptionsItr.next();
179 if(desc != null) {
180 for (DescriptionElementBase element : desc.getElements()){
181 if (element.isInstanceOf(TextData.class)){
182 // usually a call to 'get' collections should not initialise the collection,
183 // but the 'getMultilanguageText' call internally calls readSize on the collection
184 // which triggers the initialisation
185 Map<Language, LanguageString> multilanguagetextMap = ((TextData)element).getMultilanguageText();
186 boolean init = AbstractPersistentCollection.isInitialized(multilanguagetextMap);
187 Assert.assertTrue(init);
188
189 if(!multilanguagetextMap.isEmpty()) {
190 // found a map whcih we can test!
191 logger.info("Found Non-empty multilanguagetextMap");
192 boolean empty = applicationController.getCommonService().isEmpty((PersistentCollection)multilanguagetextMap);
193 Assert.assertFalse(empty);
194 // test retrieval of key set, which should already by initialised
195 Set<Language> langKeySet = multilanguagetextMap.keySet();
196 Iterator<Language> langKeySetItr = langKeySet.iterator();
197 while(langKeySetItr.hasNext()) {
198 Language key = langKeySetItr.next();
199 // testing 'containsKey' on locally initialised collection
200 boolean localContainsKey = multilanguagetextMap.containsKey(key);
201 Assert.assertTrue(localContainsKey);
202 // testing 'containsKey' on remotely initialised collection
203 boolean remoteContainsKey =
204 applicationController.getCommonService().containsKey((PersistentCollection)multilanguagetextMap, key);
205 Assert.assertTrue(remoteContainsKey);
206
207 LanguageString value = multilanguagetextMap.get(key);
208 // testing 'containsValue' on locally initialised collection
209 boolean localContainsValue = multilanguagetextMap.containsValue(value);
210 Assert.assertTrue(localContainsValue);
211 // testing 'containsValue' on remotely initialised collection
212 boolean remoteContainsValue =
213 applicationController.getCommonService().containsValue((PersistentCollection)multilanguagetextMap, value);
214 Assert.assertTrue(remoteContainsValue);
215
216 }
217 return;
218 }
219 }
220 }
221 }
222 }
223 }
224 }
225 }
226 }