Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
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.Logger;
20
import org.junit.Assert;
21
import org.junit.BeforeClass;
22
import org.junit.Ignore;
23
import org.junit.Test;
24

    
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.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
36
import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
37

    
38

    
39

    
40
/**
41
 * Test class which tests remoting for persistent cdm entities.
42
 *
43
 * FIXME:Remoting saving tests are ignored until the merge development is underway
44
 * @author c.mathew
45
 *
46
 */
47
//FIXME:Remoting need to create a proper dataset for this test
48
//Could be combined with AbstractLazyInitializerTest
49
@Ignore
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
        taxonService = getRemoteApplicationController().getTaxonService();
66

    
67
        classificationService= getRemoteApplicationController().getClassificationService();
68
        //List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
69
//        Assert.assertFalse(classifications.isEmpty());
70
//
71
//        Classification classification = classifications.get(0);
72
//        Assert.assertNotNull(classification);
73
        taxonNodes = classificationService.getAllNodes();
74
        Assert.assertFalse(taxonNodes.isEmpty());
75

    
76
    }
77

    
78

    
79

    
80
    @Test
81
    public void testCDMEntityGet() {
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
    }
106

    
107
    @Test
108
    public void test() {
109

    
110
    }
111

    
112
    @Test
113
    public void testCDMEntitySaveEager() {
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");
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
        taxonNew.setTitleCache(oldTitleCache);
128
        taxonService.merge(taxonNew);
129

    
130
        Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
131
        System.out.println("Old Taxon Title : " + taxonOld.getTitleCache());
132

    
133
        Assert.assertEquals("Title caches should be equal",oldTitleCache,taxonOld.getTitleCache());
134

    
135
    }
136

    
137

    
138
    @Test
139
    public void testCDMEntityUpdate() {
140

    
141
        Team combAuthor = Team.NewInstance();
142
        combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
143
        BotanicalName name = TaxonNameBase.NewBotanicalInstance(null, "Test1", null, null, null, null, null, null, null);
144
        name.setCombinationAuthorship(combAuthor);
145
        Taxon taxon = Taxon.NewInstance(name, null);
146
        UUID taxonUuid = taxonService.save(taxon).getUuid();
147

    
148
        //        Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
149
        //        NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
150
        //        String oldTitleCache = nvn.getTitleCache();
151
        //        System.out.println("Taxon Name Title : " + oldTitleCache);
152
        //        nvn.setTitleCache(oldTitleCache + ":updated", true);
153
        //
154
        //        taxon.setTitleCache(oldTitleCache + ":updated",true);
155
        //        try {
156
        //            taxonService.update(taxon);
157
        //        } catch (LazyInitializationException lie) {
158
        //            lie.printStackTrace();
159
        //        }
160

    
161
        List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String[] {
162
                "name"
163
        });
164
        Taxon taxonNew = (Taxon)taxonService.findTaxonByUuid(taxonUuid,TAXON_INIT_STRATEGY);
165
        NonViralName nvn = CdmBase.deproxy(taxonNew.getName(),NonViralName.class);
166
        Team team = CdmBase.deproxy(nvn.getCombinationAuthorship(),Team.class);
167
        String oldTitleCache = nvn.getTitleCache();
168
        System.out.println("Taxon Name Title : " + oldTitleCache);
169
        nvn.setTitleCache(oldTitleCache + ":updated", true);
170
        taxonService.update(taxonNew);
171

    
172
    }
173

    
174

    
175
    @Test
176
    public void testCDMEntitySaveLazy() {
177
        Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
178

    
179
        NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
180
        String oldTitleCache = nvn.getTitleCache();
181
        System.out.println("Taxon Name Title : " + oldTitleCache);
182
        nvn.setTitleCache(oldTitleCache + ":updated", true);
183
        taxonService.update(taxon);
184

    
185
        //		Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
186
        //		NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
187
        //		System.out.println("New Taxon Name Title : " + nvnNew.getTitleCache());
188
        //
189
        //		Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,nvnNew.getTitleCache());
190
        //
191
        //		nvnNew.setTitleCache(oldTitleCache, true);
192
        //		taxonService.update(taxon);
193
        //
194
        //		Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
195
        //		NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
196
        //		System.out.println("Old Taxon Name Title : " + nvnNew.getTitleCache());
197
        //
198
        //		Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
199
    }
200

    
201
    @Test
202
    public void testCDMEntitySaveLazyNew() {
203
        Team combAuthor = Team.NewInstance();
204
        combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
205
        BotanicalName name = TaxonNameBase.NewBotanicalInstance(null, "Test1", null, null, null, null, null, null, null);
206
        name.setCombinationAuthorship(combAuthor);
207
        Taxon tax1 = Taxon.NewInstance(name, null);
208
        UUID taxonUuid1 = taxonService.save(tax1).getUuid();
209

    
210
        Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
211

    
212
        NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
213
        String oldTitleCache = nvn.getTitleCache();
214
        logger.info("Taxon Name Title : " + oldTitleCache);
215
        nvn.setTitleCache(oldTitleCache + ":updated",false);
216
        taxonService.update(taxon);
217

    
218
        Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
219
        NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
220
        logger.info("New Taxon Name Title : " + nvnNew.getTitleCache());
221

    
222
        Assert.assertTrue("Title caches should not be equal",oldTitleCache.equals(nvnNew.getTitleCache()));
223

    
224
        nvnNew.setTitleCache(oldTitleCache, true);
225
        taxonService.update(taxon);
226

    
227
        Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
228
        NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
229
        logger.info("Old Taxon Name Title : " + nvnNew.getTitleCache());
230

    
231
        Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
232
    }
233
    @Ignore
234
    @Test
235
    public void testCDMEntitySaveCollection() {
236
        Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
237

    
238
        Set<TaxonRelationship> taxRelations = taxon.getTaxonRelations();
239
        Set<String> relToTitles = new HashSet<String>();
240
        Iterator<TaxonRelationship> trItr = taxRelations.iterator();
241
        while(trItr.hasNext()) {
242
            TaxonRelationship tr = trItr.next();
243
            System.out.println("Synonym Title Cache : " + tr.getFromTaxon().getTitleCache());
244
            relToTitles.add(tr.getFromTaxon().getTitleCache());
245
            tr.getFromTaxon().setTitleCache(tr.getFromTaxon().getTitleCache() + ":updated");
246

    
247
        }
248
        taxonService.merge(taxon);
249

    
250
        Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
251
        Set<TaxonRelationship> taxRelationsNew = taxonNew.getTaxonRelations();
252

    
253
        Iterator<TaxonRelationship> trItrNew = taxRelationsNew.iterator();
254
        Iterator<String> relToTitlesItr = relToTitles.iterator();
255
        while(trItrNew.hasNext() && relToTitlesItr.hasNext()) {
256
            TaxonRelationship trNew = trItrNew.next();
257
            String relToTitle = relToTitlesItr.next();
258
            System.out.println("New Synonym Title Cache: " + trNew.getFromTaxon().getTitleCache());
259
            Assert.assertTrue("Synonym Title caches should not be equal", trNew.getFromTaxon().getTitleCache().equals(relToTitle));
260
            trNew.getFromTaxon().setTitleCache(relToTitle);
261
        }
262

    
263
        Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
264

    
265
        Set<TaxonRelationship> synRelationsOld = taxonNew.getTaxonRelations();
266
        Iterator<TaxonRelationship> srItrOld = synRelationsOld.iterator();
267
        relToTitlesItr = relToTitles.iterator();
268
        while(srItrOld.hasNext() && relToTitlesItr.hasNext()) {
269
            TaxonRelationship srOld = srItrOld.next();
270
            String relToTitle = relToTitlesItr.next();
271
            System.out.println("New Synonym Title Cache: " + srOld.getFromTaxon().getTitleCache());
272
            Assert.assertEquals("Synonym Title caches should be equal", srOld.getFromTaxon().getTitleCache(), relToTitle);
273

    
274
        }
275
    }
276
}
(4-4/5)