Project

General

Profile

Download (10.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.cdm.api.service;
11

    
12
import static org.junit.Assert.assertEquals;
13
import static org.junit.Assert.assertNotNull;
14
import static org.junit.Assert.assertNull;
15
import static org.junit.Assert.assertTrue;
16

    
17
import java.io.FileNotFoundException;
18
import java.net.URI;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import org.apache.log4j.Logger;
23
import org.junit.Ignore;
24
import org.junit.Test;
25
import org.unitils.dbunit.annotation.DataSet;
26
import org.unitils.dbunit.annotation.DataSets;
27
import org.unitils.spring.annotation.SpringBeanByType;
28

    
29
import eu.etaxonomy.cdm.api.service.pager.Pager;
30
import eu.etaxonomy.cdm.model.common.DefinedTerm;
31
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
32
import eu.etaxonomy.cdm.model.common.Language;
33
import eu.etaxonomy.cdm.model.common.Representation;
34
import eu.etaxonomy.cdm.model.common.TermType;
35
import eu.etaxonomy.cdm.model.common.TermVocabulary;
36
import eu.etaxonomy.cdm.model.location.NamedArea;
37
import eu.etaxonomy.cdm.model.name.BotanicalName;
38
import eu.etaxonomy.cdm.model.name.Rank;
39
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
42
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
43

    
44
/**
45
 * @author a.mueller
46
 * @created 27.05.2008
47
 * @version 1.0
48
 */
49
public class TermServiceImplTest extends CdmTransactionalIntegrationTest{
50
    @SuppressWarnings("unused")
51
    private static final Logger logger = Logger.getLogger(TermServiceImplTest.class);
52

    
53
    @SpringBeanByType
54
    private ITermService termService;
55

    
56
    @SpringBeanByType
57
    private IVocabularyService vocabularyService;
58

    
59
    @SpringBeanByType
60
    private ITaxonService taxonService;
61

    
62
/* ************************* TESTS *************************************************/
63

    
64
    /**
65
     * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#getTermByUri(java.lang.String)}.
66
     */
67
    @Ignore //second part of test throws unexpected exception & also first part fails since language(406)
68
    //is also not found here, for an explanation see comment below
69
    @Test
70
    /* @DataSet
71
     * WARNING:
72
     *    the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
73
     *    and thus will cause unitils to empty the according tables, thus all terms etc will be deleted for the
74
     *    following tests, thus it might be a good idea moving this test to the end
75
     */
76
    public void testGetTermByUri() {
77
        String uriStr = "http://any.uri.com";
78
        URI uri = URI.create(uriStr);
79
        DefinedTermBase<?> term = termService.getByUri(uri);
80
        assertNotNull(term);
81
        //for testing only
82
//		TermVocabulary<?> voc = term.getVocabulary();
83
//		service.saveOrUpdate(term);
84
//		List<MarkerType> list = service.listByTermClass(MarkerType.class, null, null, null, null);
85

    
86
        //NULL
87
        //FIXME throws object not found exception. Wants to load term.voc(11).representation(496).language(124) which does not exist
88
        //I do not understand where the vocabulary data comes from (checked persistence TermsDataSet-with_auditing_info.xml) but somehow this does not apply
89
        String uriNotExistStr = "http://www.notExisting.com";
90
        URI uriNotExist = URI.create(uriNotExistStr);
91
        DefinedTermBase<?> termNotExist = termService.getByUri(uriNotExist);
92
        assertNull(termNotExist);
93
    }
94

    
95
    /**
96
     * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#getContinentByUuid(java.util.UUID)}.
97
     */
98
    @Test
99
    /* @DataSet
100
     * WARNING:
101
     *    the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
102
     *    and thus will cause unitils empty the according tables
103
     */
104
    public void testGetTermByUuid() {
105
        // Rank.Domain
106
        String strUUID = "ffca6ec8-8b88-417b-a6a0-f7c992aac19b";
107
        UUID uuid = UUID.fromString(strUUID);
108
        DefinedTermBase<?> term = termService.find(uuid);
109
        assertNotNull(term);
110
        assertEquals(Rank.DOMAIN(), term);
111
        //NULL
112
        String strUUIDNotExist = "00000000-8b88-417b-a6a0-f7c992aac19c";
113
        UUID uuidNotExist = UUID.fromString(strUUIDNotExist);
114
        DefinedTermBase<?> termNotExist = termService.find(uuidNotExist);
115
        assertNull(termNotExist);
116
    }
117

    
118

    
119
    /**
120
     * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#listTerms(java.util.UUID)}.
121
     */
122
    @Test
123
    /* @DataSet
124
     * WARNING:
125
     *    the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
126
     *    and thus will cause unitils empty the according tables
127
     */
128
    public void testGetVocabularyUUID() {
129
        //Rank
130
        String rankVocabularyUuid = "ef0d1ce1-26e3-4e83-b47b-ca74eed40b1b";
131
        UUID rankUuid = UUID.fromString(rankVocabularyUuid);
132
        TermVocabulary<Rank> voc = vocabularyService.find(rankUuid);
133
        assertNotNull(voc);
134
        assertEquals(62, voc.getTerms().size());
135
        //Null
136
        String nullVocabularyUuid = "00000000-26e3-4e83-b47b-ca74eed40b1b";
137
        UUID nullUuid = UUID.fromString(nullVocabularyUuid);
138
        TermVocabulary<Rank> nullVoc = vocabularyService.find(nullUuid);
139
        assertNull(nullVoc);
140
    }
141

    
142

    
143
    @Test
144
    /* @DataSet
145
     * WARNING:
146
     *    the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
147
     *    and thus will cause unitils empty the according tables
148
     */
149
    public void testGetAreaByTdwgAbbreviation(){
150
        String tdwgAbbreviation = "GER-OO";
151
        NamedArea germany = termService.getAreaByTdwgAbbreviation(tdwgAbbreviation);
152
        assertEquals(tdwgAbbreviation, germany.getRepresentation(Language.DEFAULT()).getAbbreviatedLabel());
153
    }
154

    
155
    @Test
156
    /* @DataSet
157
     * WARNING:
158
     *    the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
159
     *    and thus will cause unitils empty the according tables
160
     */
161
    public void testListTerms() {
162
        Pager<SpecimenTypeDesignationStatus> results = termService.page(SpecimenTypeDesignationStatus.class, null,null,null,null);
163
        assertNotNull("Results should not be null",results);
164
    }
165

    
166
    @Ignore
167
    @Test
168
    public void testTitleCacheUpdate(){
169
    	String uuid = "ae787603-3070-4298-9ca6-4cbe73378122";
170
    	UUID fromString = UUID.fromString(uuid);
171
    	DefinedTermBase<?> termBase = termService.find(fromString);
172

    
173
    	// change label
174
    	String expectedTitleCache = termBase.getLabel() + "append";
175
    	termBase.setLabel(expectedTitleCache);
176

    
177
    	commitAndStartNewTransaction(null);
178

    
179
    	termBase = termService.find(fromString);
180
    	assertEquals("Title cache did not update after setting the label and saving the term", expectedTitleCache, termBase.getTitleCache());
181

    
182
    	// add new representation for default language
183
    	String expecteTitleCacheAfterRepresentationChange = "new label";
184
    	Representation representation = termBase.getRepresentation(Language.DEFAULT());
185
    	representation.setLabel(expecteTitleCacheAfterRepresentationChange);
186
    	termBase.addRepresentation(representation);
187

    
188
    	//this will create another termBase in the DB which has the same UUID -> test failure
189
//    	termBase.addRepresentation(Representation.NewInstance(expecteTitleCacheAfterRepresentationChange, "", "", Language.DEFAULT()));////new Representation(expecteTitleCacheAfterRepresentationChange, "", "", Language.DEFAULT()));
190

    
191

    
192
    	commitAndStartNewTransaction(null);
193

    
194
    	termBase = termService.find(fromString);
195
    	assertEquals("Title cache did not update after adding a new representation for default language and saving the term", expecteTitleCacheAfterRepresentationChange, termBase.getTitleCache());
196
    }
197

    
198

    
199
    @Test
200
    @DataSets({
201
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="ClearDB_with_Terms_DataSet.xml"),
202
        @DataSet(value="TermsDataSet-with_auditing_info.xml")
203
    })
204
    public void testDeleteTerms(){
205
    	final String[] tableNames = new String[]{
206
                "DefinedTermBase","Representation"};
207

    
208
    	commitAndStartNewTransaction(tableNames);
209
    	TermVocabulary<DefinedTerm> vocs = TermVocabulary.NewInstance(TermType.Feature, "TestFeatures", null, null, null);
210
    	vocs.addTerm(DefinedTerm.NewInstance(TermType.State, "green", "green", "gn"));
211
    	vocs= vocabularyService.save(vocs);
212
    	Pager<DefinedTermBase> term = termService.findByRepresentationText("green", DefinedTermBase.class, null, null);
213
    	if (term.getCount() != 0){
214

    
215
    		DeleteResult result = termService.delete(term.getRecords().get(0));
216
    		assertTrue(result.isOk());
217
    		commitAndStartNewTransaction(tableNames);
218
       	}
219
    	TermVocabulary<DefinedTerm> voc = TermVocabulary.NewInstance(TermType.Feature, "TestFeatures", null, null, null);
220
    	voc.addTerm(DefinedTerm.NewDnaMarkerInstance("test", "marker", "t"));
221
    	UUID vocUUID = vocabularyService.save(voc).getUuid();
222

    
223
    	voc = vocabularyService.find(vocUUID);
224
    	Set<DefinedTerm> terms = voc.getTerms();
225
    	DefinedTermBase termBase =terms.iterator().next();
226
    	UUID termUUID = termBase.getUuid();
227
    	termService.delete(termBase, null);
228
    	//commitAndStartNewTransaction(tableNames);
229
    	termBase =  termService.load(termUUID);
230
    	assertNull(termBase);
231

    
232

    
233
    	//TermVocabulary<DefinedTerm> voc = TermVocabulary.NewInstance(TermType.Feature, "TestFeatures", null, null, null);
234
        voc.addTerm(DefinedTerm.NewDnaMarkerInstance("test", "marker", "t"));
235
        vocUUID = vocabularyService.save(voc).getUuid();
236

    
237
        voc = vocabularyService.find(vocUUID);
238
        terms = voc.getTerms();
239
        termBase =terms.iterator().next();
240
        termUUID = termBase.getUuid();
241
        termBase = termService.load(termUUID);
242
        BotanicalName testName = BotanicalName.NewInstance(Rank.SPECIES());
243
        Taxon testTaxon = Taxon.NewInstance(testName,null);
244
        testTaxon.addIdentifier("Test", (DefinedTerm) termBase);
245
        taxonService.save(testTaxon);
246
        termService.delete(termBase, null);
247
        //commitAndStartNewTransaction(tableNames);
248
        termBase =  termService.load(termUUID);
249
        assertNotNull(termBase);
250
    }
251

    
252
    @Override
253
    public void createTestDataSet() throws FileNotFoundException {}
254

    
255
}
(28-28/31)