Project

General

Profile

Download (13.3 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.assertFalse;
13
import static org.junit.Assert.assertNull;
14
import static org.junit.Assert.assertTrue;
15

    
16
import java.io.FileNotFoundException;
17
import java.lang.reflect.Field;
18
import java.util.UUID;
19

    
20
import org.apache.log4j.Logger;
21
import org.junit.Assert;
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.common.URI;
28
import eu.etaxonomy.cdm.model.agent.Contact;
29
import eu.etaxonomy.cdm.model.agent.Person;
30
import eu.etaxonomy.cdm.model.agent.Team;
31
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
32
import eu.etaxonomy.cdm.model.common.Annotation;
33
import eu.etaxonomy.cdm.model.location.Point;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
37
import eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy;
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
    @SuppressWarnings("unused")
49
    private static final Logger logger = Logger.getLogger(AgentServiceImplTest.class);
50

    
51
    private static final UUID UUID_EHRENBERG = UUID.fromString("6363ae88-ec57-4b23-8235-6c86fbe59446");
52

    
53
    @SpringBeanByType
54
    private IAgentService service;
55

    
56
    @SpringBeanByType
57
    private INameService nameSerivce;
58

    
59
    @Test
60
    @DataSets({
61
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
62
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")
63
    })
64
    public void testConvertPerson2Team(){
65

    
66
        //create data
67
        String fullAuthor = "Original author";
68
    	String nomTitle = "Abrev. aut.";
69
    	Person person = Person.NewTitledInstance(fullAuthor);
70
    	person.setNomenclaturalTitle(nomTitle);
71
    	Annotation annotation = Annotation.NewDefaultLanguageInstance("Meine annotation");
72
    	person.setContact(getContact());
73
    	TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
74
    	name.setCombinationAuthorship(person);
75
    	person.addAnnotation(annotation);
76

    
77
    	nameSerivce.save(name);
78

    
79
    	Team team = null;
80
	    try {
81
	        UpdateResult result = service.convertPerson2Team(person);
82
	        team = (Team) result.getCdmEntity();
83
	    } catch (MergeException e) {
84
	        Assert.fail("No Merge exception should be thrown");
85
	    }
86
	    Assert.assertNotNull(team);
87
	    Assert.assertEquals("Title cache must be equal", fullAuthor, team.getTitleCache());
88
	    Assert.assertEquals("Nom. title must be equal", nomTitle, team.getNomenclaturalTitle());
89
	    Assert.assertTrue("Members must be empty", team.getTeamMembers().isEmpty());
90
	    Assert.assertEquals("Annotations should be moved", 1, team.getAnnotations().size());
91
	    Assert.assertNotNull("Contact must be copied too", team.getContact());
92
	    Assert.assertEquals("Team must be combination author now", team, name.getCombinationAuthorship());
93

    
94
	    //test un-protected titleCache
95
	    Person person2 = person.clone();
96
	    person2.setProtectedTitleCache(false);
97
	    Assert.assertEquals("Title cache must be equal", nomTitle, person2.getTitleCache());
98
	    service.save(person2);
99

    
100
	    try{
101
	        UpdateResult result = service.convertPerson2Team(person2);
102
	        team = (Team) result.getCdmEntity();
103
        } catch (MergeException e) {
104
            Assert.fail("No Merge exception should be thrown, but was: " + e.getMessage());
105
        }
106
	    Assert.assertEquals("Title cache must be equal", person2.getTitleCache(), team.getTitleCache());
107
        Assert.assertEquals("Nom. title must be equal", nomTitle, team.getNomenclaturalTitle());
108
        Assert.assertTrue("Nom. title must be protected", team.isProtectedNomenclaturalTitleCache());
109

    
110
        //test fully empty
111
        person2 = person2.clone();
112
        person2.setNomenclaturalTitle(null);  //now it is fully empty
113
        Assert.assertEquals("Title cache must be equal", "Person#0<"+person2.getUuid()+">", person2.getTitleCache());  //expected value may change when toString() implementation changes for Person class
114

    
115
        service.save(person2);
116
        try{
117
            UpdateResult result = service.convertPerson2Team(person2.getUuid());
118
            team = (Team) result.getCdmEntity();
119
        } catch (MergeException e) {
120
            Assert.fail("No Merge exception should be thrown, but was: " + e.getMessage());
121
        }
122
        Assert.assertEquals("If person was completely empty we don't expect the title cache to be taken from person.nomenclaturalTitle", TeamDefaultCacheStrategy.EMPTY_TEAM, team.getTitleCache());
123
        Assert.assertFalse("If person was completely empty we don't expect the title cache to be protected", team.isProtectedTitleCache());
124
        Assert.assertEquals("Nom. title must be equal", person2.getNomenclaturalTitle(), team.getNomenclaturalTitle());
125

    
126
        try{
127
            service.convertPerson2Team(person2.getUuid());
128
            Assert.fail("Non-existing person should throw an exception");
129
        } catch (MergeException e) {
130
            Assert.fail("No Merge exception should be thrown, but was: " + e.getMessage());
131
        }catch (IllegalArgumentException e) {
132
            //nothing to do
133
        }
134
    }
135

    
136
    private Contact getContact(){
137
    	URI uri = URI.create("a");
138
    	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));
139
    	return contact;
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 testConvertTeam2Person(){
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
    	team.setContact(getContact());
155
    	TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
156
    	name.setCombinationAuthorship(team);
157

    
158
    	service.save(team);
159
    	nameSerivce.save(name);
160

    
161
    	UpdateResult result = null;
162
    	Person person = null;
163
		try {
164
			result = service.convertTeam2Person(team);
165
			person = (Person)result.getCdmEntity();
166
		} catch (IllegalArgumentException e) {
167
			Assert.fail("No IllegalArgumentException should be thrown");
168
		} catch (MergeException e) {
169
			Assert.fail("No Merge exception should be thrown");
170
		}
171
    	Assert.assertNotNull(person);
172
    	Assert.assertEquals("Title cache must be equal", fullAuthor, person.getTitleCache());
173
    	Assert.assertEquals("Nom. title must be equal", nomTitle, person.getNomenclaturalTitle());
174
    	Assert.assertEquals("Annotations should be moved", 1, person.getAnnotations().size());
175
    	Assert.assertNotNull("Contact must be copied too", person.getContact());
176
    	Assert.assertEquals("person must be combination author now", person, name.getCombinationAuthorship());
177
    }
178

    
179

    
180
    @Test
181
    @DataSets({
182
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
183
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")
184
    })
185
    public void testConvertTeam2PersonWithMember(){
186
    	String fullAuthor = "Original author";
187
    	String nomTitle = "Abrev. aut.";
188
    	Team team = Team.NewTitledInstance(fullAuthor, nomTitle);
189
    	Annotation annotation = Annotation.NewDefaultLanguageInstance("Meine annotation");
190
    	team.addAnnotation(annotation);
191
    	Annotation annotation2 = Annotation.NewDefaultLanguageInstance("Meine annotation2");
192
    	team.addAnnotation(annotation2);
193
    	team.setContact(getContact());
194
    	TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
195
    	name.setCombinationAuthorship(team);
196
    	Person member = Person.NewTitledInstance("Member person");
197
    	member.setNomenclaturalTitle("Memb. pers.");
198
    	Annotation annotation3 = Annotation.NewDefaultLanguageInstance("Meine annotation3");
199
    	member.addAnnotation(annotation3);
200
    	team.addTeamMember(member);
201

    
202
    	service.save(team);
203

    
204
    	nameSerivce.save(name);
205

    
206
    	Person person = null;
207
    	UpdateResult result = null;
208
		try {
209
		    result = service.convertTeam2Person(team);
210
		    person = (Person) result.getCdmEntity();
211
		} catch (IllegalArgumentException e) {
212
			Assert.fail("No IllegalArgumentException should be thrown");
213
		} catch (MergeException e) {
214
			Assert.fail("No Merge exception should be thrown");
215
		}
216
    	Assert.assertNotNull(person);
217
    	Assert.assertEquals("Convert result and 'member' must be equal'", member, person);
218
    	Assert.assertEquals("Title cache must be equal", "Member person", person.getTitleCache());
219
    	Assert.assertEquals("Nom. title must be equal", "Memb. pers.", person.getNomenclaturalTitle());
220
    	//FIXME should annotations be taken only from member ??
221
//    	Assert.assertEquals("Annotations should be moved", 1, person.getAnnotations().size());
222
    	String annotationText = person.getAnnotations().iterator().next().getText();
223
//    	Assert.assertEquals("The only annotation should be annotation 3", annotation3.getText(), annotationText);
224
    	//FIXME currently merge mode is still MERGE for user defined fields
225
//    	Assert.assertNull("Contact must not be copied", person.getContact());
226
    	Assert.assertEquals("person must be combination author now", person, name.getCombinationAuthorship());
227
    }
228

    
229

    
230
    @Test  //7874 //8030
231
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="AgentServiceImplTest.testUpdateTitleCache.xml")
232
    public final void testUpdateNomTitle() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
233

    
234
        Field nomenclaturalTitleField = TeamOrPersonBase.class.getDeclaredField("nomenclaturalTitle");
235
        nomenclaturalTitleField.setAccessible(true);
236

    
237
        Person turland = (Person) service.load(UUID.fromString("a598ab3f-b33b-4b4b-b237-d616fcb6b5b1"));
238
        Person monro = (Person) service.load(UUID.fromString("e7206bc5-61ab-468e-a9f5-dec118b46b7f"));
239
        // TODO Add Assertion Person "Ehrenberg" must not be member of a team.
240
        Person ehrenberg = (Person) service.load(UUID_EHRENBERG);
241

    
242

    
243
        Team turland_monro_protected = (Team) service.load(UUID.fromString("5bff55de-f7cc-44d9-baac-908f52ad0cb8"));
244
        Team turland_monro = (Team) service.load(UUID.fromString("30ca93d6-b543-4bb9-b6ff-e9ededa65af7"));
245
        Team turland_monro_null = (Team) service.load(UUID.fromString("a4ca0d37-d78b-4bcc-875e-d4ea5a031089"));
246

    
247
        // Person has no flag for protecting the nomenclaturalTitle
248
        assertNull(nomenclaturalTitleField.get(turland));
249
        assertNull(nomenclaturalTitleField.get(ehrenberg));
250
        assertTrue(ehrenberg.isProtectedTitleCache());
251
        assertEquals("A.M. Monro", nomenclaturalTitleField.get(monro).toString());
252

    
253
        // Team has a flag for protectedNomenclaturalTitle flag
254
        assertEquals("Turland, Monro", nomenclaturalTitleField.get(turland_monro_protected));
255
        assertTrue(turland_monro_protected.isProtectedNomenclaturalTitleCache());
256
        assertEquals("--to be updated--", nomenclaturalTitleField.get(turland_monro).toString());
257
        assertFalse(turland_monro.isProtectedNomenclaturalTitleCache());
258
        assertNull(nomenclaturalTitleField.get(turland_monro_null));
259
        assertFalse(turland_monro_null.isProtectedNomenclaturalTitleCache());
260

    
261
        service.updateCaches();
262

    
263
        turland_monro_protected = (Team) service.load(UUID.fromString("5bff55de-f7cc-44d9-baac-908f52ad0cb8"));
264
        turland_monro = (Team) service.load(UUID.fromString("30ca93d6-b543-4bb9-b6ff-e9ededa65af7"));
265
        ehrenberg = (Person)service.load(UUID_EHRENBERG);
266

    
267
        assertEquals("Expecting nomenclaturalTitle to be set since it was NULL", "Turland, N.J.", nomenclaturalTitleField.get(turland));
268
        assertEquals("Expecting nomenclaturalTitle to be set since it was NULL", "Ehrenb.", nomenclaturalTitleField.get(ehrenberg));
269
        assertEquals("Expecting titleChache to be unchaged since it was protecetd", "Ehrenb.", ehrenberg.getTitleCache());
270
        assertEquals("Expecting nomenclaturalTitle to be unchanged", "A.M. Monro", nomenclaturalTitleField.get(monro).toString());
271

    
272
        assertEquals("Turland, Monro", nomenclaturalTitleField.get(turland_monro_protected));
273
        assertEquals("Turland, N.J. & A.M. Monro", nomenclaturalTitleField.get(turland_monro).toString());
274
        assertEquals("Expecting nomenclaturalTitle to be set since it was NULL", "Turland, N.J. & A.M. Monro", nomenclaturalTitleField.get(turland_monro_null).toString());
275

    
276
    }
277

    
278

    
279
    @Override
280
    public void createTestDataSet() throws FileNotFoundException {}
281
}
(2-2/40)