Project

General

Profile

Download (8.26 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
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.Level;
18
import org.apache.log4j.Logger;
19
import org.junit.Assert;
20
import org.junit.BeforeClass;
21
import org.junit.Ignore;
22
import org.junit.Test;
23
import org.unitils.dbunit.annotation.DataSet;
24

    
25
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
26
import eu.etaxonomy.cdm.api.service.IClassificationService;
27
import eu.etaxonomy.cdm.api.service.ITaxonService;
28
import eu.etaxonomy.cdm.model.agent.Person;
29
import eu.etaxonomy.cdm.model.agent.Team;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.name.BotanicalName;
32
import eu.etaxonomy.cdm.model.name.NonViralName;
33
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34
import eu.etaxonomy.cdm.model.taxon.Classification;
35
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38
import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
39

    
40

    
41

    
42

    
43
/**
44
 * Class to test the {@link CdmApplicationRemoteController}
45
 *
46
 */
47
@DataSet
48
public class AbstractLazyInitializerTest extends BaseRemotingTest {
49
    private static final Logger logger = Logger.getLogger(AbstractLazyInitializerTest.class);
50

    
51
    private static IClassificationService classificationService;
52
    private static ITaxonService taxonService;
53

    
54
    private static List<TaxonNode> taxonNodes;
55

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

    
59

    
60
    @BeforeClass
61
    public void initializeRemoteLazyLoading() {
62

    
63
        Logger.getRootLogger().setLevel(Level.DEBUG);
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.assertNotEquals("Title caches should not be equal",oldTitleCache,taxonNew.getTitleCache());
126

    
127
        taxonNew.setTitleCache(oldTitleCache, true);
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 testCDMEntitySaveLazyNew() {
140
            Team combAuthor = Team.NewInstance();
141
            combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
142
            BotanicalName name = BotanicalName.NewInstance(null, "Test1", null, null, null, null, null, null, null);
143
            name.setCombinationAuthorTeam(combAuthor);
144
            Taxon tax1 = Taxon.NewInstance(name, null);
145
            UUID taxonUuid1 = taxonService.save(tax1);
146

    
147
            Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
148

    
149
            NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
150
            String oldTitleCache = nvn.getTitleCache();
151
            logger.info("Taxon Name Title : " + oldTitleCache);
152
            nvn.setTitleCache(oldTitleCache + ":updated",false);
153
            taxonService.update(taxon);
154

    
155
            Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
156
            NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
157
            logger.info("New Taxon Name Title : " + nvnNew.getTitleCache());
158

    
159
            Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,nvnNew.getTitleCache());
160

    
161
            nvnNew.setTitleCache(oldTitleCache, true);
162
            taxonService.update(taxon);
163

    
164
            Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
165
            NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
166
            logger.info("Old Taxon Name Title : " + nvnNew.getTitleCache());
167

    
168
            Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
169
        }
170

    
171
        @Ignore
172
        @Test
173
        public void testCDMEntitySaveCollection() {
174
            Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
175

    
176
            Set<SynonymRelationship> synRelations = taxon.getSynonymRelations();
177
            Set<String> relToTitles = new HashSet<String>();
178
            Iterator<SynonymRelationship> srItr = synRelations.iterator();
179
            while(srItr.hasNext()) {
180
                SynonymRelationship sr = srItr.next();
181
                System.out.println("Synonym Title Cache : " + sr.getSynonym().getTitleCache());
182
                relToTitles.add(sr.getSynonym().getTitleCache());
183
                sr.getSynonym().setTitleCache(sr.getSynonym().getTitleCache() + ":updated");
184

    
185
            }
186
            taxonService.merge(taxon);
187

    
188
            Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
189
            Set<SynonymRelationship> synRelationsNew = taxonNew.getSynonymRelations();
190

    
191
            Iterator<SynonymRelationship> srItrNew = synRelationsNew.iterator();
192
            Iterator<String> relToTitlesItr = relToTitles.iterator();
193
            while(srItrNew.hasNext() && relToTitlesItr.hasNext()) {
194
                SynonymRelationship srNew = srItrNew.next();
195
                String relToTitle = relToTitlesItr.next();
196
                System.out.println("New Synonym Title Cache: " + srNew.getSynonym().getTitleCache());
197
                Assert.assertNotEquals("Synonym Title caches should not be equal", srNew.getSynonym().getTitleCache(), relToTitle);
198
                srNew.getSynonym().setTitleCache(relToTitle);
199
            }
200

    
201
            Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
202

    
203
            Set<SynonymRelationship> synRelationsOld = taxonNew.getSynonymRelations();
204
            Iterator<SynonymRelationship> srItrOld = synRelationsOld.iterator();
205
            relToTitlesItr = relToTitles.iterator();
206
            while(srItrOld.hasNext() && relToTitlesItr.hasNext()) {
207
                SynonymRelationship srOld = srItrOld.next();
208
                String relToTitle = relToTitlesItr.next();
209
                System.out.println("New Synonym Title Cache: " + srOld.getSynonym().getTitleCache());
210
                Assert.assertEquals("Synonym Title caches should be equal", srOld.getSynonym().getTitleCache(), relToTitle);
211

    
212
            }
213
        }
214
}
(1-1/5)