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.common.CdmBase;
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.cache.agent.TeamDefaultCacheStrategy;
39
import eu.etaxonomy.cdm.strategy.merge.MergeException;
40
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
41
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
42

    
43
/**
44
 * @author a.mueller
45
 * @since 2015-04-01
46
 */
47
public class AgentServiceImplTest extends CdmTransactionalIntegrationTest{
48

    
49
    @SuppressWarnings("unused")
50
    private static final Logger logger = Logger.getLogger(AgentServiceImplTest.class);
51

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

    
54
    @SpringBeanByType
55
    private IAgentService service;
56

    
57
    @SpringBeanByType
58
    private INameService nameSerivce;
59

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

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

    
78
    	nameSerivce.save(name);
79

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

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

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

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

    
116
        service.save(person2);
117
        try{
118
            UpdateResult result = service.convertPerson2Team(person2.getUuid());
119
            team = (Team) result.getCdmEntity();
120
        } catch (MergeException e) {
121
            Assert.fail("No Merge exception should be thrown, but was: " + e.getMessage());
122
        }
123
        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());
124
        Assert.assertFalse("If person was completely empty we don't expect the title cache to be protected", team.isProtectedTitleCache());
125
        Assert.assertEquals("If person was completely empty we expect nom. title to be the empty team constant", TeamDefaultCacheStrategy.EMPTY_TEAM, team.getNomenclaturalTitleCache());
126
        Assert.assertEquals("If person was completely empty we expect collector title to be the empty team constant", TeamDefaultCacheStrategy.EMPTY_TEAM, team.getCollectorTitleCache());
127

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

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

    
144

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

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

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

    
181

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

    
204
    	service.save(team);
205

    
206
    	nameSerivce.save(name);
207

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

    
231

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

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

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

    
246
        Team turland_monro_protected = CdmBase.deproxy(service.loadWithoutInitializing(3), Team.class);
247
        Team turland_monro = CdmBase.deproxy(service.loadWithoutInitializing(4), Team.class);
248
        Team turland_monro_null = CdmBase.deproxy(service.loadWithoutInitializing(5), Team.class);
249

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

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

    
264
        service.updateCaches();
265

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

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

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

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