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.Language;
31
import eu.etaxonomy.cdm.model.description.State;
32
import eu.etaxonomy.cdm.model.location.NamedArea;
33
import eu.etaxonomy.cdm.model.name.IBotanicalName;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
36
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.term.DefinedTerm;
39
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
40
import eu.etaxonomy.cdm.model.term.Representation;
41
import eu.etaxonomy.cdm.model.term.TermType;
42
import eu.etaxonomy.cdm.model.term.TermVocabulary;
43
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
44
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
45

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

    
54
    @SpringBeanByType
55
    private ITermService termService;
56

    
57
    @SpringBeanByType
58
    private IVocabularyService vocabularyService;
59

    
60
    @SpringBeanByType
61
    private ITaxonService taxonService;
62

    
63
/* ************************* TESTS *************************************************/
64

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

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

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

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

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

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

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

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

    
173
    	commitAndStartNewTransaction(null);
174

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

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

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

    
187

    
188
    	commitAndStartNewTransaction(null);
189

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

    
194

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

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

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

    
228

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

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

    
247

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

    
251
}
(36-36/40)