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
package eu.etaxonomy.cdm.api.service;
10

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

    
16
import java.io.FileNotFoundException;
17
import java.util.Set;
18
import java.util.UUID;
19

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

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

    
45
/**
46
 * @author a.mueller
47
 * @since 27.05.2008
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
    @Test
119
    /* @DataSet
120
     * WARNING:
121
     *    the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
122
     *    and thus will cause unitils empty the according tables
123
     */
124
    public void testGetVocabularyUUID() {
125
        //Rank
126
        String rankVocabularyUuid = "ef0d1ce1-26e3-4e83-b47b-ca74eed40b1b";
127
        UUID rankUuid = UUID.fromString(rankVocabularyUuid);
128
        TermVocabulary<Rank> voc = vocabularyService.find(rankUuid);
129
        assertNotNull(voc);
130
        assertEquals(62, voc.getTerms().size());
131
        //Null
132
        String nullVocabularyUuid = "00000000-26e3-4e83-b47b-ca74eed40b1b";
133
        UUID nullUuid = UUID.fromString(nullVocabularyUuid);
134
        TermVocabulary<Rank> nullVoc = vocabularyService.find(nullUuid);
135
        assertNull(nullVoc);
136
    }
137

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

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

    
161
    @Ignore
162
    @Test
163
    public void testTitleCacheUpdate(){
164
    	String uuid = "ae787603-3070-4298-9ca6-4cbe73378122";
165
    	UUID fromString = UUID.fromString(uuid);
166
    	DefinedTermBase<?> termBase = termService.find(fromString);
167

    
168
    	// change label
169
    	String expectedTitleCache = termBase.getLabel() + "append";
170
    	termBase.setLabel(expectedTitleCache);
171

    
172
    	commitAndStartNewTransaction(null);
173

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

    
177
    	// add new representation for default language
178
    	String expecteTitleCacheAfterRepresentationChange = "new label";
179
    	Representation representation = termBase.getRepresentation(Language.DEFAULT());
180
    	representation.setLabel(expecteTitleCacheAfterRepresentationChange);
181
    	termBase.addRepresentation(representation);
182

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

    
186

    
187
    	commitAndStartNewTransaction(null);
188

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

    
193

    
194
    @Test
195
    @DataSets({
196
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
197
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")
198
    })
199
    public void testDeleteTerms(){
200
    	final String[] tableNames = new String[]{
201
                "DefinedTermBase","Representation"};
202

    
203
    	commitAndStartNewTransaction(tableNames);
204
    	TermVocabulary<State> vocStates = TermVocabulary.NewInstance(TermType.State,
205
    	        State.class, "Test States", null, null, null);
206
    	vocStates.addTerm(State.NewInstance("green", "green", "gn"));
207
    	vocabularyService.save(vocStates);
208
    	Pager<DefinedTermBase> term = termService.findByRepresentationText("green", DefinedTermBase.class, null, null);
209
    	if (term.getCount() != 0){
210
    		DeleteResult result = termService.delete(term.getRecords().get(0));
211
    		assertTrue(result.isOk());
212
    		commitAndStartNewTransaction(tableNames);
213
       	}
214
    	TermVocabulary<DefinedTerm> vocDna = TermVocabulary.NewInstance(TermType.DnaMarker,
215
    	        DefinedTerm.class, "Test DNA marker", null, null, null);
216
    	vocDna.addTerm(DefinedTerm.NewDnaMarkerInstance("test", "marker", "t"));
217
    	vocabularyService.save(vocDna);
218

    
219
    	vocDna = vocabularyService.find(vocDna.getUuid());
220
    	Set<DefinedTerm> terms = vocDna.getTerms();
221
    	DefinedTerm termBase =terms.iterator().next();
222
    	termService.delete(termBase, null);
223
    	//commitAndStartNewTransaction(tableNames);
224
    	termBase =  (DefinedTerm)termService.load(termBase.getUuid());
225
    	assertNull(termBase);
226

    
227

    
228
    	//TermVocabulary<DefinedTerm> voc = TermVocabulary.NewInstance(TermType.Feature, "TestFeatures", null, null, null);
229
        vocDna.addTerm(DefinedTerm.NewDnaMarkerInstance("test", "marker", "t"));
230
        vocabularyService.save(vocDna);
231

    
232
        vocDna = vocabularyService.find(vocDna.getUuid());
233
        terms = vocDna.getTerms();
234
        termBase =terms.iterator().next();
235
        termBase = (DefinedTerm)termService.load(termBase.getUuid());
236
        IBotanicalName testName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
237
        Taxon testTaxon = Taxon.NewInstance(testName,null);
238
        testTaxon.addIdentifier("Test", termBase);
239
        taxonService.save(testTaxon);
240
        termService.delete(termBase, null);
241
        //commitAndStartNewTransaction(tableNames);
242
        termBase =  (DefinedTerm)termService.load(termBase.getUuid());
243
        assertNotNull(termBase);
244
    }
245

    
246

    
247
    @Override
248
    public void createTestDataSet() throws FileNotFoundException {}
249

    
250
}
(36-36/38)