merge trunk and bugfix linkbackuri jaxb
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / TermServiceImplTest.java
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
16 import java.net.URI;
17 import java.util.UUID;
18
19 import org.apache.log4j.Logger;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.unitils.spring.annotation.SpringBeanByType;
23
24 import eu.etaxonomy.cdm.api.service.pager.Pager;
25 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
26 import eu.etaxonomy.cdm.model.common.Language;
27 import eu.etaxonomy.cdm.model.common.Representation;
28 import eu.etaxonomy.cdm.model.common.TermVocabulary;
29 import eu.etaxonomy.cdm.model.location.NamedArea;
30 import eu.etaxonomy.cdm.model.name.Rank;
31 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
32 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
33
34 /**
35 * @author a.mueller
36 * @created 27.05.2008
37 * @version 1.0
38 */
39 public class TermServiceImplTest extends CdmTransactionalIntegrationTest{
40 @SuppressWarnings("unused")
41 private static final Logger logger = Logger.getLogger(TermServiceImplTest.class);
42
43 @SpringBeanByType
44 private ITermService termService;
45
46 @SpringBeanByType
47 private IVocabularyService vocabularyService;
48
49 /* ************************* TESTS *************************************************/
50
51 /**
52 * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#getTermByUri(java.lang.String)}.
53 */
54 @Ignore //second part of test throws unexpected exception & also first part fails since language(406)
55 //is also not found here, for an explanation see comment below
56 @Test
57 /* @DataSet
58 * WARNING:
59 * the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
60 * and thus will cause unitils to empty the according tables, thus all terms etc will be deleted for the
61 * following tests, thus it might be a good idea moving this test to the end
62 */
63 public void testGetTermByUri() {
64 String uriStr = "http://any.uri.com";
65 URI uri = URI.create(uriStr);
66 DefinedTermBase<?> term = termService.getByUri(uri);
67 assertNotNull(term);
68 //for testing only
69 // TermVocabulary<?> voc = term.getVocabulary();
70 // service.saveOrUpdate(term);
71 // List<MarkerType> list = service.listByTermClass(MarkerType.class, null, null, null, null);
72
73 //NULL
74 //FIXME throws object not found exception. Wants to load term.voc(11).representation(496).language(124) which does not exist
75 //I do not understand where the vocabulary data comes from (checked persistence TermsDataSet-with_auditing_info.xml) but somehow this does not apply
76 String uriNotExistStr = "http://www.notExisting.com";
77 URI uriNotExist = URI.create(uriNotExistStr);
78 DefinedTermBase<?> termNotExist = termService.getByUri(uriNotExist);
79 assertNull(termNotExist);
80 }
81
82 /**
83 * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#getTermByUuid(java.util.UUID)}.
84 */
85 @Test
86 /* @DataSet
87 * WARNING:
88 * the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
89 * and thus will cause unitils empty the according tables
90 */
91 public void testGetTermByUuid() {
92 // Rank.Domain
93 String strUUID = "ffca6ec8-8b88-417b-a6a0-f7c992aac19b";
94 UUID uuid = UUID.fromString(strUUID);
95 DefinedTermBase<?> term = termService.find(uuid);
96 assertNotNull(term);
97 assertEquals(Rank.DOMAIN(), term);
98 //NULL
99 String strUUIDNotExist = "00000000-8b88-417b-a6a0-f7c992aac19c";
100 UUID uuidNotExist = UUID.fromString(strUUIDNotExist);
101 DefinedTermBase<?> termNotExist = termService.find(uuidNotExist);
102 assertNull(termNotExist);
103 }
104
105
106 /**
107 * Test method for {@link eu.etaxonomy.cdm.api.service.TermServiceImpl#listTerms(java.util.UUID)}.
108 */
109 // @Ignore
110 @Test
111 /* @DataSet
112 * WARNING:
113 * the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
114 * and thus will cause unitils empty the according tables
115 */
116 public void testGetVocabularyUUID() {
117 //Rank
118 String rankVocabularyUuid = "ef0d1ce1-26e3-4e83-b47b-ca74eed40b1b";
119 UUID rankUuid = UUID.fromString(rankVocabularyUuid);
120 TermVocabulary<Rank> voc = vocabularyService.find(rankUuid);
121 assertNotNull(voc);
122 assertEquals(62, voc.getTerms().size());
123 //Null
124 String nullVocabularyUuid = "00000000-26e3-4e83-b47b-ca74eed40b1b";
125 UUID nullUuid = UUID.fromString(nullVocabularyUuid);
126 TermVocabulary<Rank> nullVoc = vocabularyService.find(nullUuid);
127 assertNull(nullVoc);
128 }
129
130
131 @Test
132 /* @DataSet
133 * WARNING:
134 * the dataset contains records for DEFINEDTERMBASE,DEFINEDTERMBASE_REPRESENTATION and REPRESENTAION
135 * and thus will cause unitils empty the according tables
136 */
137 public void testGetAreaByTdwgAbbreviation(){
138 String tdwgAbbreviation = "GER-OO";
139 NamedArea germany = termService.getAreaByTdwgAbbreviation(tdwgAbbreviation);
140 assertEquals(tdwgAbbreviation, germany.getRepresentation(Language.DEFAULT()).getAbbreviatedLabel());
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 testListTerms() {
150 Pager<SpecimenTypeDesignationStatus> results = (Pager)termService.page(SpecimenTypeDesignationStatus.class, null,null,null,null);
151 assertNotNull("Results should not be null",results);
152 }
153
154 @Ignore
155 @Test
156 public void testTitleCacheUpdate(){
157 String uuid = "ae787603-3070-4298-9ca6-4cbe73378122";
158 UUID fromString = UUID.fromString(uuid);
159 DefinedTermBase<?> termBase = termService.find(fromString);
160
161 // change label
162 String expectedTitleCache = termBase.getLabel() + "append";
163 termBase.setLabel(expectedTitleCache);
164
165 commitAndStartNewTransaction(null);
166
167 termBase = termService.find(fromString);
168 assertEquals("Title cache did not update after setting the label and saving the term", expectedTitleCache, termBase.getTitleCache());
169
170 // add new representation for default language
171 String expecteTitleCacheAfterRepresentationChange = "new label";
172 Representation representation = termBase.getRepresentation(Language.DEFAULT());
173 representation.setLabel(expecteTitleCacheAfterRepresentationChange);
174 termBase.addRepresentation(representation);
175
176 //this will create another termBase in the DB which has the same UUID -> test failure
177 // termBase.addRepresentation(Representation.NewInstance(expecteTitleCacheAfterRepresentationChange, "", "", Language.DEFAULT()));////new Representation(expecteTitleCacheAfterRepresentationChange, "", "", Language.DEFAULT()));
178
179
180 commitAndStartNewTransaction(null);
181
182 termBase = termService.find(fromString);
183 assertEquals("Title cache did not update after adding a new representation for default language and saving the term", expecteTitleCacheAfterRepresentationChange, termBase.getTitleCache());
184 }
185
186 }