cleanup
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / AgentServiceImplTest.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.assertFalse;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16
17 import java.io.FileNotFoundException;
18 import java.lang.reflect.Field;
19 import java.net.URI;
20 import java.util.UUID;
21
22 import org.apache.log4j.Logger;
23 import org.junit.Assert;
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.model.agent.Contact;
30 import eu.etaxonomy.cdm.model.agent.Person;
31 import eu.etaxonomy.cdm.model.agent.Team;
32 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33 import eu.etaxonomy.cdm.model.common.Annotation;
34 import eu.etaxonomy.cdm.model.location.Point;
35 import eu.etaxonomy.cdm.model.name.Rank;
36 import eu.etaxonomy.cdm.model.name.TaxonName;
37 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
38 import eu.etaxonomy.cdm.strategy.merge.MergeException;
39 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
40 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
41
42 /**
43 * @author a.mueller
44 * @since 2015-04-01
45 */
46 public class AgentServiceImplTest extends CdmTransactionalIntegrationTest{
47
48 /**
49 *
50 */
51 private static final UUID UUID_EHRENBERG = UUID.fromString("6363ae88-ec57-4b23-8235-6c86fbe59446");
52
53 @SuppressWarnings("unused")
54 private static final Logger logger = Logger.getLogger(AgentServiceImplTest.class);
55
56 @SpringBeanByType
57 private IAgentService service;
58
59 @SpringBeanByType
60 private INameService nameSerivce;
61
62
63 @Test
64 @DataSets({
65 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
66 @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")
67 })
68 public void testConvertPerson2Team(){
69 String fullAuthor = "Original author";
70 String nomTitle = "Abrev. aut.";
71 Person person = Person.NewTitledInstance(fullAuthor);
72 person.setNomenclaturalTitle(nomTitle);
73 Annotation annotation = Annotation.NewDefaultLanguageInstance("Meine annotation");
74 person.setContact(getContact());
75 TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
76 name.setCombinationAuthorship(person);
77 person.addAnnotation(annotation);
78
79 service.save(person);
80 nameSerivce.save(name);
81
82 Team team = null;
83 UpdateResult result = null;
84 try {
85 result = service.convertPerson2Team(person);
86 team = (Team) result.getCdmEntity();
87 } catch (MergeException e) {
88 Assert.fail("No Merge exception should be thrown");
89 }
90 Assert.assertNotNull(team);
91 //Assert.assertEquals("Title cache must be equal", fullAuthor, team.getTitleCache());
92 //Assert.assertEquals("Nom. title must be equal", nomTitle, team.getNomenclaturalTitle());
93 Assert.assertEquals("Annotations should be moved", 1, team.getAnnotations().size());
94 Assert.assertNotNull("Contact must be copied too", team.getContact());
95 Assert.assertEquals("Team must be combination author now", team, name.getCombinationAuthorship());
96
97 }
98
99 private Contact getContact(){
100 URI uri = URI.create("a");
101 Contact contact = Contact.NewInstance("My street", "12345", null, null, null, "region", "a@bc.de", "030-445566", "030-12345", uri, Point.NewInstance(2d, 5d, null, null));
102 return contact;
103 }
104
105
106 @Test
107 @DataSets({
108 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
109 @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")
110 })
111 public void testConvertTeam2Person(){
112 String fullAuthor = "Original author";
113 String nomTitle = "Abrev. aut.";
114 Team team = Team.NewTitledInstance(fullAuthor, nomTitle);
115 Annotation annotation = Annotation.NewDefaultLanguageInstance("Meine annotation");
116 team.addAnnotation(annotation);
117 team.setContact(getContact());
118 TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
119 name.setCombinationAuthorship(team);
120
121 service.save(team);
122 nameSerivce.save(name);
123
124 UpdateResult result = null;
125 Person person = null;
126 try {
127 result = service.convertTeam2Person(team);
128 person = (Person)result.getCdmEntity();
129 } catch (IllegalArgumentException e) {
130 Assert.fail("No IllegalArgumentException should be thrown");
131 } catch (MergeException e) {
132 Assert.fail("No Merge exception should be thrown");
133 }
134 Assert.assertNotNull(person);
135 Assert.assertEquals("Title cache must be equal", fullAuthor, person.getTitleCache());
136 Assert.assertEquals("Nom. title must be equal", nomTitle, person.getNomenclaturalTitle());
137 Assert.assertEquals("Annotations should be moved", 1, person.getAnnotations().size());
138 Assert.assertNotNull("Contact must be copied too", person.getContact());
139 Assert.assertEquals("person must be combination author now", person, name.getCombinationAuthorship());
140 }
141
142
143 @Test
144 @DataSets({
145 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
146 @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")
147 })
148 public void testConvertTeam2PersonWithMember(){
149 String fullAuthor = "Original author";
150 String nomTitle = "Abrev. aut.";
151 Team team = Team.NewTitledInstance(fullAuthor, nomTitle);
152 Annotation annotation = Annotation.NewDefaultLanguageInstance("Meine annotation");
153 team.addAnnotation(annotation);
154 Annotation annotation2 = Annotation.NewDefaultLanguageInstance("Meine annotation2");
155 team.addAnnotation(annotation2);
156 team.setContact(getContact());
157 TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
158 name.setCombinationAuthorship(team);
159 Person member = Person.NewTitledInstance("Member person");
160 member.setNomenclaturalTitle("Memb. pers.");
161 Annotation annotation3 = Annotation.NewDefaultLanguageInstance("Meine annotation3");
162 member.addAnnotation(annotation3);
163 team.addTeamMember(member);
164
165 service.save(team);
166
167 nameSerivce.save(name);
168
169 Person person = null;
170 UpdateResult result = null;
171 try {
172 result = service.convertTeam2Person(team);
173 person = (Person) result.getCdmEntity();
174 } catch (IllegalArgumentException e) {
175 Assert.fail("No IllegalArgumentException should be thrown");
176 } catch (MergeException e) {
177 Assert.fail("No Merge exception should be thrown");
178 }
179 Assert.assertNotNull(person);
180 Assert.assertEquals("Convert result and 'member' must be equal'", member, person);
181 Assert.assertEquals("Title cache must be equal", "Member person", person.getTitleCache());
182 Assert.assertEquals("Nom. title must be equal", "Memb. pers.", person.getNomenclaturalTitle());
183 //FIXME should annotations be taken only from member ??
184 // Assert.assertEquals("Annotations should be moved", 1, person.getAnnotations().size());
185 String annotationText = person.getAnnotations().iterator().next().getText();
186 // Assert.assertEquals("The only annotation should be annotation 3", annotation3.getText(), annotationText);
187 //FIXME currently merge mode is still MERGE for user defined fields
188 // Assert.assertNull("Contact must not be copied", person.getContact());
189 Assert.assertEquals("person must be combination author now", person, name.getCombinationAuthorship());
190 }
191
192
193 @Test //7874
194 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="AgentServiceImplTest.testUpdateTitleCache.xml")
195 public final void testUpdateNomTitle() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
196
197 Field nomenclaturalTitleField = TeamOrPersonBase.class.getDeclaredField("nomenclaturalTitle");
198 nomenclaturalTitleField.setAccessible(true);
199
200 Person turland = (Person) service.load(UUID.fromString("a598ab3f-b33b-4b4b-b237-d616fcb6b5b1"));
201 Person monro = (Person) service.load(UUID.fromString("e7206bc5-61ab-468e-a9f5-dec118b46b7f"));
202 // TODO Add Assertion Person "Ehrenberg" must not be member of a team.
203 Person ehrenberg = (Person) service.load(UUID_EHRENBERG);
204
205
206 Team turland_monro_protected = (Team) service.load(UUID.fromString("5bff55de-f7cc-44d9-baac-908f52ad0cb8"));
207 Team turland_monro = (Team) service.load(UUID.fromString("30ca93d6-b543-4bb9-b6ff-e9ededa65af7"));
208 Team turland_monro_null = (Team) service.load(UUID.fromString("a4ca0d37-d78b-4bcc-875e-d4ea5a031089"));
209
210 // Person has no flag for protecting the nomenclaturalTitle
211 assertNull(nomenclaturalTitleField.get(turland));
212 assertNull(nomenclaturalTitleField.get(ehrenberg));
213 assertTrue(ehrenberg.isProtectedTitleCache());
214 assertEquals("A.M. Monro", nomenclaturalTitleField.get(monro).toString());
215
216 // Team has a flag for protectedNomenclaturalTitle flag
217 assertEquals("Turland, Monro", nomenclaturalTitleField.get(turland_monro_protected));
218 assertTrue(turland_monro_protected.isProtectedNomenclaturalTitleCache());
219 assertEquals("--to be updated--", nomenclaturalTitleField.get(turland_monro).toString());
220 assertFalse(turland_monro.isProtectedNomenclaturalTitleCache());
221 assertNull(nomenclaturalTitleField.get(turland_monro_null));
222 assertFalse(turland_monro_null.isProtectedNomenclaturalTitleCache());
223
224 service.updateTitleCache();
225
226 turland_monro_protected = (Team) service.load(UUID.fromString("5bff55de-f7cc-44d9-baac-908f52ad0cb8"));
227 turland_monro = (Team) service.load(UUID.fromString("30ca93d6-b543-4bb9-b6ff-e9ededa65af7"));
228 ehrenberg = (Person)service.load(UUID_EHRENBERG);
229
230 assertEquals("Expecting nomenclaturalTitle to be set since it was NULL", "Turland, N.J.", nomenclaturalTitleField.get(turland));
231 assertEquals("Expecting nomenclaturalTitle to be set since it was NULL", "Ehrenberg, C.G.", nomenclaturalTitleField.get(ehrenberg));
232 assertEquals("Expecting titleChache to be unchaged since it was protecetd", "Ehrenb.", ehrenberg.getTitleCache());
233 assertEquals("Expecting nomenclaturalTitle to be unchanged", "A.M. Monro", nomenclaturalTitleField.get(monro).toString());
234
235 assertEquals("Turland, Monro", nomenclaturalTitleField.get(turland_monro_protected));
236 assertEquals("Turland, N.J. & A.M. Monro", nomenclaturalTitleField.get(turland_monro).toString());
237 assertEquals("Expecting nomenclaturalTitle to be set since it was NULL", "Turland, N.J. & A.M. Monro", nomenclaturalTitleField.get(turland_monro_null).toString());
238
239 }
240
241
242 @Override
243 public void createTestDataSet() throws FileNotFoundException {}
244 }