Project

General

Profile

Download (18.4 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

    
10
package eu.etaxonomy.cdm.strategy.merge;
11

    
12
import java.net.URI;
13
import java.time.ZoneId;
14
import java.time.ZonedDateTime;
15
import java.util.HashSet;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Logger;
20
import org.junit.After;
21
import org.junit.AfterClass;
22
import org.junit.Assert;
23
import org.junit.Before;
24
import org.junit.BeforeClass;
25
import org.junit.Test;
26

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

    
54
/**
55
 * @author a.mueller
56
 * @created 03.08.2009
57
 */
58
public class DefaultMergeStrategyTest {
59
	@SuppressWarnings("unused")
60
	private static final Logger logger = Logger.getLogger(DefaultMergeStrategyTest.class);
61

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

    
74
	private Reference book2;
75
	private String editionString2 ="Ed.2";
76
	private String volumeString2 ="Vol.2";
77
	private Team team2;
78
	private Reference printSeries2;
79
	private Annotation annotation2;
80
	private String annotationString2;
81
	private String title2 = "Title2";
82
	private ZonedDateTime created2 = ZonedDateTime.of(1999, 3, 1, 0, 0, 0, 0, ZoneId.systemDefault());
83
	private TimePeriod datePublished2 = TimePeriod.NewInstance(2002);
84
	private int hasProblem2 = 1;
85
	private LSID lsid2;
86

    
87

    
88
	private Reference book3;
89

    
90
	/**
91
	 * @throws java.lang.Exception
92
	 */
93
	@BeforeClass
94
	public static void setUpBeforeClass() throws Exception {
95
		DefaultTermInitializer termInitializer = new DefaultTermInitializer();
96
		termInitializer.initialize();
97
	}
98

    
99
	/**
100
	 * @throws java.lang.Exception
101
	 */
102
	@AfterClass
103
	public static void tearDownAfterClass() throws Exception {
104
	}
105

    
106
	/**
107
	 * @throws java.lang.Exception
108
	 */
109
	@Before
110
	public void setUp() throws Exception {
111
		bookMergeStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
112
		team1 = Team.NewInstance();
113
		team1.setTitleCache("Team1", true);
114
		team2 = Team.NewInstance();
115
		team2.setTitleCache("Team2", true);
116
		printSeries1 = ReferenceFactory.newPrintSeries("Series1");
117
		printSeries2 = ReferenceFactory.newPrintSeries("Series2");
118
		annotation1 = Annotation.NewInstance("Annotation1", null);
119
		annotationString2 = "Annotation2";
120
		annotation2 = Annotation.NewInstance(annotationString2, null);
121

    
122
		book1 = ReferenceFactory.newBook();
123
		book1.setAuthorship(team1);
124
		book1.setTitle(title1);
125
		book1.setEdition(editionString1);
126
		book1.setVolume(volumeString1);
127
		book1.setInReference(printSeries1);
128
		book1.addAnnotation(annotation1);
129
		book1.setDatePublished(datePublished1);
130
		book1.setParsingProblem(hasProblem1);
131
		lsid1 = new LSID("authority1", "namespace1", "object1", "revision1");
132
		book1.setLsid(lsid1);
133
		book1.setNomenclaturallyRelevant(false);
134

    
135
		book2 = ReferenceFactory.newBook();
136
		book2.setAuthorship(team2);
137
		book2.setTitle(title2);
138
		book2.setEdition(editionString2);
139
		book2.setVolume(volumeString2);
140
		book2.setInReference(printSeries2);
141
		book2.addAnnotation(annotation2);
142
		book2.setCreated(created2);
143
		book2.setDatePublished(datePublished2);
144
		book2.setParsingProblem(hasProblem2);
145
		lsid2 = new LSID("authority2", "namespace2", "object2", "revision2");
146
		book2.setLsid(lsid2);
147
		book2.setNomenclaturallyRelevant(true);
148

    
149
	}
150

    
151
	/**
152
	 * @throws java.lang.Exception
153
	 */
154
	@After
155
	public void tearDown() throws Exception {
156
	}
157

    
158
//********************* TEST *********************************************/
159

    
160
	/**
161
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#NewInstance(java.lang.Class)}.
162
	 */
163
	@Test
164
	public void testNewInstance() {
165
		Assert.assertNotNull(bookMergeStrategy);
166
		Assert.assertEquals(Reference.class, bookMergeStrategy.getMergeClass());
167
	}
168

    
169
	/**
170
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#getMergeMode(java.lang.String)}.
171
	 */
172
	@Test
173
	public void testGetMergeMode() {
174
		Assert.assertEquals("Merge mode for title should be MergeMode.FIRST", MergeMode.FIRST, bookMergeStrategy.getMergeMode("title"));
175
	}
176

    
177
	/**
178
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#getMergeMode(java.lang.String)}.
179
	 */
180
	@Test
181
	public void testGetSetMergeMode() {
182
		//legal value
183
		try {
184
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
185
			Assert.assertEquals("Merge mode for edition should be", MergeMode.SECOND, bookMergeStrategy.getMergeMode("edition"));
186
		} catch (MergeException e1) {
187
			Assert.fail();
188
		}
189
		//illegalValue
190
		try {
191
			bookMergeStrategy.setMergeMode("xxx", MergeMode.SECOND);
192
			Assert.fail("A property name must exist, otherwise an exception must be thrown");
193
		} catch (Exception e) {
194
			//ok
195
		}
196
		//illegalValue
197
		try {
198
			bookMergeStrategy.setMergeMode("cacheStrategy", MergeMode.SECOND);
199
			Assert.fail("CacheStrategy is transient and therefore not a legal merge parameter");
200
		} catch (Exception e) {
201
			//ok
202
		}
203
		//illegalValue
204
		try {
205
			bookMergeStrategy.setMergeMode("id", MergeMode.SECOND);
206
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
207
		} catch (Exception e) {
208
			//ok
209
		}
210
		//illegalValue
211
		try {
212
			bookMergeStrategy.setMergeMode("uuid", MergeMode.SECOND);
213
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
214
		} catch (Exception e) {
215
			//ok
216
		}
217

    
218

    
219
	}
220

    
221

    
222
	/**
223
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
224
	 * @throws MergeException
225
	 */
226
	@Test
227
	public void testInvokeReferences() throws MergeException {
228
		INomenclaturalReferenceCacheStrategy cacheStrategy1 = book1.getCacheStrategy();
229
		int id = book1.getId();
230
		UUID uuid = book1.getUuid();
231
		try {
232
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
233
			bookMergeStrategy.setMergeMode("volume", MergeMode.NULL);
234
			bookMergeStrategy.setMergeMode("authorship", MergeMode.SECOND);
235
			bookMergeStrategy.setMergeMode("created", MergeMode.SECOND);
236
			bookMergeStrategy.setMergeMode("updated",MergeMode.NULL);
237
			bookMergeStrategy.setMergeMode("datePublished", MergeMode.SECOND);
238
			bookMergeStrategy.setMergeMode("parsingProblem", MergeMode.SECOND);
239
			bookMergeStrategy.setMergeMode("inReference", MergeMode.SECOND);
240
			bookMergeStrategy.setMergeMode("lsid", MergeMode.SECOND);
241

    
242
			bookMergeStrategy.invoke(book1, book2);
243
		} catch (MergeException e) {
244
			throw e;
245
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
246
		}
247
		Assert.assertEquals("Title should stay the same", title1, book1.getTitle());
248
		Assert.assertEquals("Edition should become edition 2", editionString2, book1.getEdition());
249
		Assert.assertNull("Volume should be null", book1.getVolume());
250

    
251
		//Boolean
252
		Assert.assertEquals("Has problem must be hasProblem2", hasProblem2, book1.getParsingProblem());
253
		Assert.assertEquals("nomenclaturally relevant must have value true (AND semantics)", true, book1.isNomenclaturallyRelevant() );
254

    
255

    
256
		//CdmBase
257
		Assert.assertSame("Authorship must be the one of book2", team2, book1.getAuthorship());
258
		Assert.assertSame("In Series must be the one of book2", printSeries2, book1.getInReference());
259

    
260
		//Transient
261
		Assert.assertSame("Cache strategy is transient and shouldn't change therefore", cacheStrategy1, book1.getCacheStrategy());
262

    
263

    
264
		//UserType
265
		System.out.println(created2.toString());
266
		System.out.println(book1.getCreated().toString());
267
		//TODO: Why does assertSame does not work??
268
		Assert.assertEquals("Created must be created2", created2, book1.getCreated());
269
		//TODO updated should have the actual date if any value has changed
270
		Assert.assertSame("Created must be created2", null, book1.getUpdated());
271
		Assert.assertSame("Created must be datePublsihed2", datePublished2, book1.getDatePublished());
272
		//TODO this may not be correct
273
		Assert.assertSame("LSID must be LSID2", lsid2, book1.getLsid());
274

    
275

    
276
		//TODO
277
		//	book1.setProblemEnds(end);
278
		//	book1.setProtectedTitleCache(protectedTitleCache);
279

    
280
		//annotations -> ADD_CLONE
281
		Assert.assertEquals("Annotations should contain annotations of both books", 2, book1.getAnnotations().size());
282
		boolean cloneExists = false;
283
		for (Annotation annotation : book1.getAnnotations()){
284
			if (annotation == this.annotation2){
285
				//Hibernate will not persist the exact same object. Probably this is a bug (the according row in the
286
				//M:M table is not deleted and a unique constraints does not allow adding 2 rows with the same annotation_id
287
				//This test can be changed once this bug does not exist anymore
288
				Assert.fail("Book1 should contain a clone of annotation2 but contains annotation2 itself");
289
			}else if (annotationString2.equals(annotation.getText())){
290
				cloneExists = true;
291
			}
292
		}
293
		Assert.assertTrue("Book1 should contain a clone of annotation2", cloneExists);
294
	//	Assert.assertEquals("Annotations from book2 should be deleted", 0, book2.getAnnotations().size());
295

    
296
		//identifier
297
		Assert.assertSame("Identifier must never be changed", id, book1.getId());
298
		Assert.assertSame("Identifier must never be changed", uuid, book1.getUuid());
299

    
300
		//Test Thesis
301
		Institution school1 = Institution.NewInstance();
302
		Institution school2 = Institution.NewInstance();
303

    
304
		Reference thesis1 = ReferenceFactory.newThesis();
305
		thesis1.setSchool(school1);
306
		//Thesis thesis1 = Thesis.NewInstance(school1);
307
		Reference thesis2 = ReferenceFactory.newThesis();
308
		thesis2.setSchool(school2);
309
		DefaultMergeStrategy thesisStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
310

    
311
		thesisStrategy.setMergeMode("school", MergeMode.SECOND);
312
		thesisStrategy.invoke(thesis1, thesis2);
313
		Assert.assertSame("school must be school2", school2, thesis1.getSchool());
314
	}
315

    
316

    
317
	/**
318
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
319
	 * @throws MergeException
320
	 */
321
	@Test
322
	//@Ignore
323
	public void testInvokeTxonNames() throws MergeException {
324
		IMergeStrategy botNameMergeStrategy = DefaultMergeStrategy.NewInstance(TaxonName.class);
325
		TaxonName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
326
		TaxonName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
327
		TaxonName botName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
328

    
329
		botName1.setGenusOrUninomial("Genus1");
330
		botName1.setSpecificEpithet("species1");
331
		botName1.setAnamorphic(true);
332

    
333
		botName2.setGenusOrUninomial("Genus2");
334
		botName2.setSpecificEpithet("species2");
335
		botName2.setAnamorphic(false);
336

    
337
		//name relations
338
		botName2.addBasionym(botName3, book1, "p.22", null);
339
		DerivedUnit specimen1 = DerivedUnit.NewPreservedSpecimenInstance();
340
		botName2.addSpecimenTypeDesignation(specimen1, SpecimenTypeDesignationStatus.HOLOTYPE(), book2, "p.56", "originalNameString", false, true);
341

    
342
		//descriptions
343
		TaxonNameDescription description1 = TaxonNameDescription.NewInstance();
344
		botName1.addDescription(description1);
345
		TaxonNameDescription description2 = TaxonNameDescription.NewInstance();
346
		botName1.addDescription(description2);
347

    
348
		//authors
349
		Team team1 = Team.NewInstance();
350
		Team team2 = Team.NewInstance();
351
		Person person1 = Person.NewInstance();
352
		botName1.setCombinationAuthorship(team1);
353
		botName2.setCombinationAuthorship(team2);
354

    
355
		//taxa
356
		TaxonBase<?> taxon1= Taxon.NewInstance(botName1, book1);
357
		TaxonBase<?> taxon2= Taxon.NewInstance(botName2, book2);
358

    
359
		try {
360
			botNameMergeStrategy.setMergeMode("combinationAuthorship", MergeMode.SECOND);
361
			botNameMergeStrategy.setMergeMode("anamorphic", MergeMode.AND);
362

    
363
//			botNameMergeStrategy.invoke(botName1, botName2);
364
		} catch (MergeException e) {
365
			throw e;
366
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
367
		}
368

    
369
		//Boolean
370
		Assert.assertEquals("Is anamorphic must be false", true && false, botName2.isAnamorphic());
371

    
372
		//NameRelations
373
		Set<NameRelationship> toRelations = botName2.getRelationsToThisName();
374
		Set<NameRelationship> basionymRelations = new HashSet<NameRelationship>();
375
		for (NameRelationship toRelation : toRelations){
376
			if (toRelation.getType().equals(NameRelationshipType.BASIONYM())){
377
				basionymRelations.add(toRelation);
378
			}
379
		}
380
		Assert.assertEquals("Number of basionyms must be 1", 1, basionymRelations.size());
381
		Assert.assertEquals("Basionym must have same reference", book1, basionymRelations.iterator().next().getCitation());
382
		//TODO merge relation if matches() = true
383

    
384
		//Types
385
		Assert.assertEquals("Number of specimen type designations must be 1", 1, botName2.getSpecimenTypeDesignations().size());
386
		//TODO add to all names etc.
387

    
388
		//Description
389
		Assert.assertEquals("Number of descriptions must be 2", 2, botName1.getDescriptions().size());
390

    
391
		//Authorships
392
		Assert.assertEquals("Combination author must be combination author 1", team1, botName1.getCombinationAuthorship());
393

    
394
		//Taxa
395
		Assert.assertEquals("TaxonName of taxon1 must be name1", botName1, taxon1.getName());
396
		Assert.assertEquals("TaxonName of taxon2 must be name2", botName2, taxon2.getName());
397

    
398
	}
399

    
400
	/**
401
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
402
	 * @throws MergeException
403
	 */
404
	@Test
405
	public void testInvokeAgents() throws MergeException {
406
		IMergeStrategy teamMergeStrategy = DefaultMergeStrategy.NewInstance(Team.class);
407

    
408
		Team team1 = Team.NewInstance();
409
		Team team2 = Team.NewInstance();
410
		Team team3 = Team.NewInstance();
411

    
412
		Person person1 = Person.NewTitledInstance("person1");
413
		Person person2 = Person.NewTitledInstance("person2");
414
		Person person3 = Person.NewTitledInstance("person3");
415

    
416
		team1.setTitleCache("Team1", true);
417
		team1.setNomenclaturalTitle("T.1");
418
		String street1 = "Strasse1";
419
		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)));
420
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
421
		String street3 = "Street3";
422
		team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
423
		String emailAddress1 = "Email1";
424
		team1.addEmailAddress(emailAddress1);
425

    
426
		team2.addTeamMember(person1);
427
		team2.addTeamMember(person2);
428
		String emailAddress2 = "Email2";
429
		team2.addEmailAddress(emailAddress2);
430

    
431
		team3.addTeamMember(person3);
432
		team3.addEmailAddress("emailAddress3");
433

    
434
		teamMergeStrategy.invoke(team2, team3);
435

    
436
		Assert.assertEquals("Team2 must have 3 persons as members",3, team2.getTeamMembers().size());
437
		Assert.assertTrue("Team2 must have person3 as new member", team2.getTeamMembers().contains(person3));
438
		Assert.assertSame("Team2 must have person3 as third member",person3, team2.getTeamMembers().get(2));
439

    
440
		//Contact
441
		teamMergeStrategy.invoke(team2, team1);
442
		Contact team2Contact = team2.getContact();
443
		Assert.assertNotNull("team2Contact must not be null", team2Contact);
444
		Assert.assertNotNull("Addresses must not be null", team2Contact.getAddresses());
445
		Assert.assertEquals("Number of addresses must be 3", 3, team2Contact.getAddresses().size());
446
		Assert.assertEquals("Number of email addresses must be 4", 4, team2Contact.getEmailAddresses().size());
447

    
448
		boolean street1Exists = false;
449
		boolean street3Exists = false;
450
		boolean country1Exists = false;
451
		for  (Address address : team2Contact.getAddresses()){
452
			if (street1.equals(address.getStreet())){
453
				street1Exists = true;
454
			}
455
			if (street3.equals(address.getStreet())){
456
				street3Exists = true;
457
			}
458
			if (Country.ARGENTINAARGENTINEREPUBLIC() == address.getCountry()){
459
				country1Exists = true;
460
			}
461
		}
462
		Assert.assertTrue("Street1 must be one of the streets in team2's addresses", street1Exists);
463
		Assert.assertTrue("Street3 must be one of the streets in team2's addressesss", street3Exists);
464
		Assert.assertTrue("Argentina must be one of the countries in team2's addresses", country1Exists);
465

    
466
		//Person
467
		Institution institution1 = Institution.NewInstance();
468
		institution1.setTitleCache("inst1", true);
469
		Institution institution2 = Institution.NewInstance();
470
		institution2.setTitleCache("inst2", true);
471

    
472
		TimePeriod period1 = TimePeriod.NewInstance(2002, 2004);
473
		TimePeriod period2 = TimePeriod.NewInstance(2004, 2006);
474

    
475
		person1.addInstitutionalMembership(institution1, period1, "departement1", "role1");
476
		person2.addInstitutionalMembership(institution2, period2, "departement2", "role2");
477

    
478
		IMergeStrategy personMergeStrategy = DefaultMergeStrategy.NewInstance(Person.class);
479
		personMergeStrategy.invoke(person1, person2);
480

    
481
		Assert.assertEquals("Number of institutional memberships must be 2", 2, person1.getInstitutionalMemberships().size());
482
		for (InstitutionalMembership institutionalMembership : person1.getInstitutionalMemberships()){
483
			Assert.assertSame("Person of institutional memebership must be person1", person1, institutionalMembership.getPerson());
484
		}
485

    
486
	}
487

    
488
}
    (1-1/1)