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