Project

General

Profile

Download (16.8 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.strategy.merge;
10

    
11
import java.util.HashSet;
12
import java.util.Set;
13
import java.util.UUID;
14

    
15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
17
import org.joda.time.DateTime;
18
import org.junit.Assert;
19
import org.junit.Before;
20
import org.junit.Test;
21

    
22
import eu.etaxonomy.cdm.common.URI;
23
import eu.etaxonomy.cdm.model.agent.Address;
24
import eu.etaxonomy.cdm.model.agent.Contact;
25
import eu.etaxonomy.cdm.model.agent.Institution;
26
import eu.etaxonomy.cdm.model.agent.InstitutionalMembership;
27
import eu.etaxonomy.cdm.model.agent.Person;
28
import eu.etaxonomy.cdm.model.agent.Team;
29
import eu.etaxonomy.cdm.model.common.Annotation;
30
import eu.etaxonomy.cdm.model.common.LSID;
31
import eu.etaxonomy.cdm.model.common.TimePeriod;
32
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
33
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
34
import eu.etaxonomy.cdm.model.location.Country;
35
import eu.etaxonomy.cdm.model.location.Point;
36
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
37
import eu.etaxonomy.cdm.model.name.NameRelationship;
38
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
39
import eu.etaxonomy.cdm.model.name.Rank;
40
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
41
import eu.etaxonomy.cdm.model.name.TaxonName;
42
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
43
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
44
import eu.etaxonomy.cdm.model.reference.Reference;
45
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
48
import eu.etaxonomy.cdm.strategy.cache.reference.IReferenceCacheStrategy;
49
import eu.etaxonomy.cdm.test.TermTestBase;
50

    
51
/**
52
 * @author a.mueller
53
 * @since 03.08.2009
54
 */
55
public class DefaultMergeStrategyTest extends TermTestBase {
56

    
57
	@SuppressWarnings("unused")
58
	private static final Logger logger = LogManager.getLogger(DefaultMergeStrategyTest.class);
59

    
60
	private DefaultMergeStrategy bookMergeStrategy;
61
	private Reference book1;
62
	private String editionString1 ="Ed.1";
63
	private String volumeString1 ="Vol.1";
64
	private Team team1;
65
	private Reference printSeries1;
66
	private Annotation annotation1;
67
	private String title1 = "Title1";
68
	private VerbatimTimePeriod datePublished1 = VerbatimTimePeriod.NewVerbatimInstance(2000);
69
	private int hasProblem1 = 1;
70
	private LSID lsid1;
71

    
72
	private Reference book2;
73
	private String editionString2 ="Ed.2";
74
	private String volumeString2 ="Vol.2";
75
	private Team team2;
76
	private Reference printSeries2;
77
	private Annotation annotation2;
78
	private String annotationString2;
79
	private String title2 = "Title2";
80
	private DateTime created2 = new DateTime(1999, 3, 1, 0, 0, 0, 0);
81
	private VerbatimTimePeriod datePublished2 = VerbatimTimePeriod.NewVerbatimInstance(2002);
82
	private int hasProblem2 = 1;
83
	private LSID lsid2;
84

    
85
	@Before
86
	public void setUp() throws Exception {
87
		bookMergeStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
88
		team1 = Team.NewInstance();
89
		team1.setTitleCache("Team1", true);
90
		team2 = Team.NewInstance();
91
		team2.setTitleCache("Team2", true);
92
		printSeries1 = ReferenceFactory.newPrintSeries("Series1");
93
		printSeries2 = ReferenceFactory.newPrintSeries("Series2");
94
		annotation1 = Annotation.NewInstance("Annotation1", null);
95
		annotationString2 = "Annotation2";
96
		annotation2 = Annotation.NewInstance(annotationString2, null);
97

    
98
		book1 = ReferenceFactory.newBook();
99
		book1.setAuthorship(team1);
100
		book1.setTitle(title1);
101
		book1.setEdition(editionString1);
102
		book1.setVolume(volumeString1);
103
		book1.setInReference(printSeries1);
104
		book1.addAnnotation(annotation1);
105
		book1.setDatePublished(datePublished1);
106
		book1.setParsingProblem(hasProblem1);
107
		lsid1 = new LSID("authority1", "namespace1", "object1", "revision1");
108
		book1.setLsid(lsid1);
109
		book1.setNomenclaturallyRelevant(false);
110

    
111
		book2 = ReferenceFactory.newBook();
112
		book2.setAuthorship(team2);
113
		book2.setTitle(title2);
114
		book2.setEdition(editionString2);
115
		book2.setVolume(volumeString2);
116
		book2.setInReference(printSeries2);
117
		book2.addAnnotation(annotation2);
118
		book2.setCreated(created2);
119
		book2.setDatePublished(datePublished2);
120
		book2.setParsingProblem(hasProblem2);
121
		lsid2 = new LSID("authority2", "namespace2", "object2", "revision2");
122
		book2.setLsid(lsid2);
123
		book2.setNomenclaturallyRelevant(true);
124
	}
125

    
126
//********************* TEST *********************************************/
127

    
128
	@Test
129
	public void testNewInstance() {
130
		Assert.assertNotNull(bookMergeStrategy);
131
		Assert.assertEquals(Reference.class, bookMergeStrategy.getMergeClass());
132
	}
133

    
134
	@Test
135
	public void testGetMergeMode() {
136
		Assert.assertEquals("Merge mode for title should be MergeMode.FIRST", MergeMode.FIRST, bookMergeStrategy.getMergeMode("title"));
137
	}
138

    
139
	@Test
140
	public void testGetSetMergeMode() {
141
		//legal value
142
		try {
143
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
144
			Assert.assertEquals("Merge mode for edition should be", MergeMode.SECOND, bookMergeStrategy.getMergeMode("edition"));
145
		} catch (MergeException e1) {
146
			Assert.fail();
147
		}
148
		//illegalValue
149
		try {
150
			bookMergeStrategy.setMergeMode("xxx", MergeMode.SECOND);
151
			Assert.fail("A property name must exist, otherwise an exception must be thrown");
152
		} catch (Exception e) {
153
			//ok
154
		}
155
		//illegalValue
156
		try {
157
			bookMergeStrategy.setMergeMode("cacheStrategy", MergeMode.SECOND);
158
			Assert.fail("CacheStrategy is transient and therefore not a legal merge parameter");
159
		} catch (Exception e) {
160
			//ok
161
		}
162
		//illegalValue
163
		try {
164
			bookMergeStrategy.setMergeMode("id", MergeMode.SECOND);
165
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
166
		} catch (Exception e) {
167
			//ok
168
		}
169
		//illegalValue
170
		try {
171
			bookMergeStrategy.setMergeMode("uuid", MergeMode.SECOND);
172
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
173
		} catch (Exception e) {
174
			//ok
175
		}
176
	}
177

    
178
	@Test
179
	public void testInvokeReferences() throws MergeException {
180
		IReferenceCacheStrategy cacheStrategy1 = book1.cacheStrategy();
181
		int id = book1.getId();
182
		UUID uuid = book1.getUuid();
183
		try {
184
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
185
			bookMergeStrategy.setMergeMode("volume", MergeMode.NULL);
186
			bookMergeStrategy.setMergeMode("authorship", MergeMode.SECOND);
187
			bookMergeStrategy.setMergeMode("created", MergeMode.SECOND);
188
			bookMergeStrategy.setMergeMode("updated",MergeMode.NULL);
189
			bookMergeStrategy.setMergeMode("datePublished", MergeMode.SECOND);
190
			bookMergeStrategy.setMergeMode("parsingProblem", MergeMode.SECOND);
191
			bookMergeStrategy.setMergeMode("inReference", MergeMode.SECOND);
192
			bookMergeStrategy.setMergeMode("lsid", MergeMode.SECOND);
193

    
194
			bookMergeStrategy.invoke(book1, book2);
195
		} catch (MergeException e) {
196
			throw e;
197
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
198
		}
199
		Assert.assertEquals("Title should stay the same", title1, book1.getTitle());
200
		Assert.assertEquals("Edition should become edition 2", editionString2, book1.getEdition());
201
		Assert.assertNull("Volume should be null", book1.getVolume());
202

    
203
		//Boolean
204
		Assert.assertEquals("Has problem must be hasProblem2", hasProblem2, book1.getParsingProblem());
205
		Assert.assertEquals("nomenclaturally relevant must have value true (AND semantics)", true, book1.isNomenclaturallyRelevant() );
206

    
207
		//CdmBase
208
		Assert.assertSame("Authorship must be the one of book2", team2, book1.getAuthorship());
209
		Assert.assertSame("In Series must be the one of book2", printSeries2, book1.getInReference());
210

    
211
		//Transient
212
		Assert.assertSame("Cache strategy is transient and shouldn't change therefore", cacheStrategy1, book1.cacheStrategy());
213

    
214
		//UserType
215
		Assert.assertSame("Created must be created2", created2, book1.getCreated());
216
		//TODO updated should have the actual date if any value has changed
217
		Assert.assertSame("Created must be created2", null, book1.getUpdated());
218
		Assert.assertEquals("Created must be datePublsihed2", datePublished2, book1.getDatePublished());
219
		//TODO this may not be correct
220
		Assert.assertSame("LSID must be LSID2", lsid2, book1.getLsid());
221

    
222
		//TODO
223
		//	book1.setProblemEnds(end);
224
		//	book1.setProtectedTitleCache(protectedTitleCache);
225

    
226
		//annotations -> ADD_CLONE
227
		Assert.assertEquals("Annotations should contain annotations of both books", 2, book1.getAnnotations().size());
228
		boolean cloneExists = false;
229
		for (Annotation annotation : book1.getAnnotations()){
230
			if (annotation == this.annotation2){
231
				//Hibernate will not persist the exact same object. Probably this is a bug (the according row in the
232
				//M:M table is not deleted and a unique constraints does not allow adding 2 rows with the same annotation_id
233
				//This test can be changed once this bug does not exist anymore
234
				Assert.fail("Book1 should contain a clone of annotation2 but contains annotation2 itself");
235
			}else if (annotationString2.equals(annotation.getText())){
236
				cloneExists = true;
237
			}
238
		}
239
		Assert.assertTrue("Book1 should contain a clone of annotation2", cloneExists);
240
	//	Assert.assertEquals("Annotations from book2 should be deleted", 0, book2.getAnnotations().size());
241

    
242
		//identifier
243
		Assert.assertSame("Identifier must never be changed", id, book1.getId());
244
		Assert.assertSame("Identifier must never be changed", uuid, book1.getUuid());
245

    
246
		//Test Thesis
247
		Institution school1 = Institution.NewInstance();
248
		Institution school2 = Institution.NewInstance();
249

    
250
		Reference thesis1 = ReferenceFactory.newThesis();
251
		thesis1.setSchool(school1);
252
		//Thesis thesis1 = Thesis.NewInstance(school1);
253
		Reference thesis2 = ReferenceFactory.newThesis();
254
		thesis2.setSchool(school2);
255
		DefaultMergeStrategy thesisStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
256

    
257
		thesisStrategy.setMergeMode("school", MergeMode.SECOND);
258
		thesisStrategy.invoke(thesis1, thesis2);
259
		Assert.assertSame("school must be school2", school2, thesis1.getSchool());
260
	}
261

    
262
	@Test
263
	public void testInvokeTxonNames() throws MergeException {
264
		IMergeStrategy botNameMergeStrategy = DefaultMergeStrategy.NewInstance(TaxonName.class);
265
		TaxonName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
266
		TaxonName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
267
		TaxonName botName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
268

    
269
		botName1.setGenusOrUninomial("Genus1");
270
		botName1.setSpecificEpithet("species1");
271
		botName1.setAnamorphic(true);
272

    
273
		botName2.setGenusOrUninomial("Genus2");
274
		botName2.setSpecificEpithet("species2");
275
		botName2.setAnamorphic(false);
276

    
277
		//name relations
278
		botName2.addBasionym(botName3, book1, "p.22", null, null);
279
		DerivedUnit specimen1 = DerivedUnit.NewPreservedSpecimenInstance();
280
		botName2.addSpecimenTypeDesignation(specimen1, SpecimenTypeDesignationStatus.HOLOTYPE(), book2, "p.56", "originalNameString", false, true);
281

    
282
		//descriptions
283
		TaxonNameDescription description1 = TaxonNameDescription.NewInstance();
284
		botName1.addDescription(description1);
285
		TaxonNameDescription description2 = TaxonNameDescription.NewInstance();
286
		botName1.addDescription(description2);
287

    
288
		//authors
289
		Team team1 = Team.NewInstance();
290
		Team team2 = Team.NewInstance();
291
		Person person1 = Person.NewInstance();
292
		botName1.setCombinationAuthorship(team1);
293
		botName2.setCombinationAuthorship(team2);
294

    
295
		//taxa
296
		TaxonBase<?> taxon1= Taxon.NewInstance(botName1, book1);
297
		TaxonBase<?> taxon2= Taxon.NewInstance(botName2, book2);
298

    
299
		try {
300
			botNameMergeStrategy.setMergeMode("combinationAuthorship", MergeMode.SECOND);
301
			botNameMergeStrategy.setMergeMode("anamorphic", MergeMode.AND);
302

    
303
//			botNameMergeStrategy.invoke(botName1, botName2);
304
		} catch (MergeException e) {
305
			throw e;
306
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
307
		}
308

    
309
		//Boolean
310
		Assert.assertEquals("Is anamorphic must be false", true && false, botName2.isAnamorphic());
311

    
312
		//NameRelations
313
		Set<NameRelationship> toRelations = botName2.getRelationsToThisName();
314
		Set<NameRelationship> basionymRelations = new HashSet<NameRelationship>();
315
		for (NameRelationship toRelation : toRelations){
316
			if (toRelation.getType().equals(NameRelationshipType.BASIONYM())){
317
				basionymRelations.add(toRelation);
318
			}
319
		}
320
		Assert.assertEquals("Number of basionyms must be 1", 1, basionymRelations.size());
321
		Assert.assertEquals("Basionym must have same reference", book1, basionymRelations.iterator().next().getCitation());
322
		//TODO merge relation if matches() = true
323

    
324
		//Types
325
		Assert.assertEquals("Number of specimen type designations must be 1", 1, botName2.getSpecimenTypeDesignations().size());
326
		//TODO add to all names etc.
327

    
328
		//Description
329
		Assert.assertEquals("Number of descriptions must be 2", 2, botName1.getDescriptions().size());
330

    
331
		//Authorships
332
		Assert.assertEquals("Combination author must be combination author 1", team1, botName1.getCombinationAuthorship());
333

    
334
		//Taxa
335
		Assert.assertEquals("TaxonName of taxon1 must be name1", botName1, taxon1.getName());
336
		Assert.assertEquals("TaxonName of taxon2 must be name2", botName2, taxon2.getName());
337
	}
338

    
339
	@Test
340
	public void testInvokeAgents() throws MergeException {
341
		IMergeStrategy teamMergeStrategy = DefaultMergeStrategy.NewInstance(Team.class);
342

    
343
		Team team1 = Team.NewInstance();
344
		Team team2 = Team.NewInstance();
345
		Team team3 = Team.NewInstance();
346

    
347
		Person person1 = Person.NewTitledInstance("person1");
348
		Person person2 = Person.NewTitledInstance("person2");
349
		Person person3 = Person.NewTitledInstance("person3");
350

    
351
		team1.setTitleCache("Team1", true);
352
		team1.setNomenclaturalTitleCache("T.1", true);
353
		String street1 = "Strasse1";
354
		team1.setContact(Contact.NewInstance(street1, "12345", "Berlin", Country.ARGENTINAARGENTINEREPUBLIC(),"pobox" , "Region", "a@b.de", "f12345", "+49-30-123456", URI.create("www.abc.de"), Point.NewInstance(2.4, 3.2, ReferenceSystem.WGS84(), 3)));
355
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
356
		String street3 = "Street3";
357
		team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
358
		String emailAddress1 = "Email1";
359
		team1.addEmailAddress(emailAddress1);
360

    
361
		team2.addTeamMember(person1);
362
		team2.addTeamMember(person2);
363
		String emailAddress2 = "Email2";
364
		team2.addEmailAddress(emailAddress2);
365

    
366
		team3.addTeamMember(person3);
367
		team3.addEmailAddress("emailAddress3");
368

    
369
		teamMergeStrategy.invoke(team2, team3);
370

    
371
		Assert.assertEquals("Team2 must have 3 persons as members",3, team2.getTeamMembers().size());
372
		Assert.assertTrue("Team2 must have person3 as new member", team2.getTeamMembers().contains(person3));
373
		Assert.assertSame("Team2 must have person3 as third member",person3, team2.getTeamMembers().get(2));
374

    
375
		//Contact
376
		teamMergeStrategy.invoke(team2, team1);
377
		Contact team2Contact = team2.getContact();
378
		Assert.assertNotNull("team2Contact must not be null", team2Contact);
379
		Assert.assertNotNull("Addresses must not be null", team2Contact.getAddresses());
380
		Assert.assertEquals("Number of addresses must be 3", 3, team2Contact.getAddresses().size());
381
		Assert.assertEquals("Number of email addresses must be 4", 4, team2Contact.getEmailAddresses().size());
382

    
383
		boolean street1Exists = false;
384
		boolean street3Exists = false;
385
		boolean country1Exists = false;
386
		for  (Address address : team2Contact.getAddresses()){
387
			if (street1.equals(address.getStreet())){
388
				street1Exists = true;
389
			}
390
			if (street3.equals(address.getStreet())){
391
				street3Exists = true;
392
			}
393
			if (Country.ARGENTINAARGENTINEREPUBLIC() == address.getCountry()){
394
				country1Exists = true;
395
			}
396
		}
397
		Assert.assertTrue("Street1 must be one of the streets in team2's addresses", street1Exists);
398
		Assert.assertTrue("Street3 must be one of the streets in team2's addressesss", street3Exists);
399
		Assert.assertTrue("Argentina must be one of the countries in team2's addresses", country1Exists);
400

    
401
		//Person
402
		Institution institution1 = Institution.NewInstance();
403
		institution1.setTitleCache("inst1", true);
404
		Institution institution2 = Institution.NewInstance();
405
		institution2.setTitleCache("inst2", true);
406

    
407
		TimePeriod period1 = TimePeriod.NewInstance(2002, 2004);
408
		TimePeriod period2 = TimePeriod.NewInstance(2004, 2006);
409

    
410
		person1.addInstitutionalMembership(institution1, period1, "departement1", "role1");
411
		person2.addInstitutionalMembership(institution2, period2, "departement2", "role2");
412

    
413
		IMergeStrategy personMergeStrategy = DefaultMergeStrategy.NewInstance(Person.class);
414
		personMergeStrategy.invoke(person1, person2);
415

    
416
		Assert.assertEquals("Number of institutional memberships must be 2", 2, person1.getInstitutionalMemberships().size());
417
		for (InstitutionalMembership institutionalMembership : person1.getInstitutionalMemberships()){
418
			Assert.assertSame("Person of institutional memebership must be person1", person1, institutionalMembership.getPerson());
419
		}
420
	}
421
}
    (1-1/1)