Project

General

Profile

Download (14 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.getNomenclaturalTitleCache());
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.getNomenclaturalTitleCache());
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("If person was completely empty we expect nom. title to be the empty team constant", TeamDefaultCacheStrategy.EMPTY_TEAM, team.getNomenclaturalTitleCache());
125
        Assert.assertEquals("If person was completely empty we expect collector title to be the empty team constant", TeamDefaultCacheStrategy.EMPTY_TEAM, team.getCollectorTitleCache());
126

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

    
137
    private Contact getContact(){
138
    	URI uri = URI.create("a");
139
    	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));
140
    	return contact;
141
    }
142

    
143

    
144
    @Test
145
    @DataSets({
146
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
147
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")
148
    })
149
    public void testConvertTeam2Person(){
150
    	String fullAuthor = "Original author";
151
    	String nomTitle = "Abrev. aut.";
152
    	Team team = Team.NewTitledInstance(fullAuthor, nomTitle);
153
    	Annotation annotation = Annotation.NewDefaultLanguageInstance("Meine annotation");
154
    	team.addAnnotation(annotation);
155
    	team.setContact(getContact());
156
    	TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
157
    	name.setCombinationAuthorship(team);
158

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

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

    
180

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

    
203
    	service.save(team);
204

    
205
    	nameSerivce.save(name);
206

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

    
230

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

    
235
        Field nomenclaturalTitleField = Person.class.getDeclaredField("nomenclaturalTitle");
236
        nomenclaturalTitleField.setAccessible(true);
237
        Field nomenclaturalTitleCacheField = TeamOrPersonBase.class.getDeclaredField("nomenclaturalTitleCache");
238
        nomenclaturalTitleCacheField.setAccessible(true);
239

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

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

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

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

    
263
        service.updateCaches();
264

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

    
269
        assertNull("Expecting nomenclaturalTitle to be still NULL", nomenclaturalTitleField.get(turland));
270
        assertEquals("Expecting nomenclaturalTitleCache to be set since it was NULL", "Turland, N.J.", nomenclaturalTitleCacheField.get(turland));
271
        assertNull("Expecting nomenclaturalTitle to be still NULL", nomenclaturalTitleField.get(ehrenberg));
272
        assertEquals("Expecting nomenclaturalTitleCache to be set since it was NULL", "Ehrenb.", nomenclaturalTitleCacheField.get(ehrenberg));
273
        assertEquals("Expecting titleChache to be unchaged since it was protecetd", "Ehrenb.", ehrenberg.getTitleCache());
274
        assertEquals("Expecting nomenclaturalTitleCache to be unchanged", "A.M. Monro", nomenclaturalTitleCacheField.get(monro).toString());
275

    
276
        assertEquals("Turland, Monro", nomenclaturalTitleCacheField.get(turland_monro_protected));
277
        assertEquals("Turland, N.J. & A.M. Monro", nomenclaturalTitleCacheField.get(turland_monro).toString());
278
        assertEquals("Expecting nomenclaturalTitle to be set since it was NULL", "Turland, N.J. & A.M. Monro", nomenclaturalTitleCacheField.get(turland_monro_null).toString());
279
    }
280

    
281
    @Override
282
    public void createTestDataSet() throws FileNotFoundException {}
283
}
(2-2/37)