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.logging.log4j.LogManager;import org.apache.logging.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.IBotanicalName;
31
import eu.etaxonomy.cdm.model.name.INonViralName;
32
import eu.etaxonomy.cdm.model.name.TaxonName;
33
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
37
import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
38

    
39

    
40

    
41
/**
42
 * Test class which tests remoting for persistent cdm entities.
43
 *
44
 * FIXME:Remoting saving tests are ignored until the merge development is underway
45
 * @author c.mathew
46
 *
47
 */
48
//FIXME:Remoting need to create a proper dataset for this test
49
//Could be combined with AbstractLazyInitializerTest
50
@Ignore
51
public class RemoteLazyLoadingTest extends BaseRemotingTest {
52

    
53
    private static final Logger logger = LogManager.getLogger(RemoteLazyLoadingTest.class);
54

    
55
    private static IClassificationService classificationService;
56
    private static ITaxonService taxonService;
57

    
58
    private static List<TaxonNode> taxonNodes;
59

    
60
    private final UUID taxonUuid1 = UUID.fromString("8217ef77-2ab1-4318-bd67-ccd0cdef07c4");
61
    private final UUID taxonUuid2 = UUID.fromString("ef96fafa-7750-4141-b31b-1ad1daab3e76");
62

    
63

    
64
    @BeforeClass
65
    public static void initializeRemoteLazyLoading() {
66
        taxonService = getRemoteApplicationController().getTaxonService();
67

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

    
77
    }
78

    
79

    
80

    
81
    @Test
82
    public void testCDMEntityGet() {
83
        Iterator<TaxonNode> taxonNodeItr = taxonNodes.iterator();
84
        int maxcount = 30;
85
        int count = 0;
86
        while(taxonNodeItr.hasNext() && count <= maxcount) {
87
            TaxonNode taxonNode = taxonNodeItr.next();
88
            Assert.assertNotNull(taxonNode);
89

    
90
            Taxon taxon = taxonNode.getTaxon();
91
            Assert.assertNotNull(taxon);
92

    
93
            String taxonTitle = taxon.getTitleCache();
94
            logger.info("Taxon : " + taxonTitle);
95

    
96
            TaxonName name = taxon.getName();
97
            Assert.assertNotNull(name);
98

    
99
            String nameTitle = name.getTitleCache();
100
            logger.info("Taxon Name : " + nameTitle);
101

    
102
            count++;
103
        }
104

    
105

    
106
    }
107

    
108
    @Test
109
    public void test() {
110

    
111
    }
112

    
113
    @Test
114
    public void testCDMEntitySaveEager() {
115
        Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
116
        String oldTitleCache = taxon.getTitleCache();
117

    
118
        System.out.println("Taxon title : " + oldTitleCache);
119

    
120
        taxon.setTitleCache(oldTitleCache + ":updated");
121
        taxonService.merge(taxon);
122

    
123
        Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
124
        System.out.println("New Taxon Title : " + taxonNew.getTitleCache());
125

    
126
        Assert.assertTrue("Title caches should not be equal",oldTitleCache.equals(taxonNew.getTitleCache()));
127

    
128
        taxonNew.setTitleCache(oldTitleCache);
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 testCDMEntityUpdate() {
141

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

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

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

    
173
    }
174

    
175

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

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

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

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

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

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

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

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

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

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

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

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

    
248
        }
249
        taxonService.merge(taxon);
250

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

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

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

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

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