ref #6682 further fixes for taxon relationship service
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / dto / TaxonRelationshipsDTOTest.java
1 /**
2 * Copyright (C) 2018 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.dto;
10
11 import java.util.List;
12
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17
18 import eu.etaxonomy.cdm.api.service.dto.TaxonRelationshipsDTO.TaxonRelation;
19 import eu.etaxonomy.cdm.format.taxon.TaxonRelationshipFormatter;
20 import eu.etaxonomy.cdm.model.agent.Person;
21 import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
22 import eu.etaxonomy.cdm.model.common.Language;
23 import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
24 import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
25 import eu.etaxonomy.cdm.model.name.Rank;
26 import eu.etaxonomy.cdm.model.name.TaxonName;
27 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
28 import eu.etaxonomy.cdm.model.reference.Reference;
29 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
30 import eu.etaxonomy.cdm.model.taxon.Taxon;
31 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
32 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
33 import eu.etaxonomy.cdm.strategy.cache.TaggedText;
34 import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
35
36 /**
37 * @author a.mueller
38 * @since 15.08.2018
39 *
40 */
41 public class TaxonRelationshipsDTOTest {
42 private TaxonRelationship taxonRel;
43 private Reference relSec;
44
45 private Taxon fromTaxon;
46 private TaxonName fromName;
47 private Reference fromSec;
48
49 private Taxon toTaxon;
50 private TaxonName toName;
51 private Reference toSec;
52
53 private TaxonRelationshipFormatter formatter;
54 private boolean reverse;
55
56 Person toNameAuthor;
57 private List<Language> languages;
58
59 /**
60 * @throws java.lang.Exception
61 */
62 @BeforeClass
63 public static void setUpBeforeClass() throws Exception {
64 if (Language.DEFAULT() == null){
65 new DefaultTermInitializer().initialize();
66 }
67 }
68
69 /**
70 * @throws java.lang.Exception
71 */
72 @Before
73 public void setUp() throws Exception {
74 fromName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
75 fromName.setGenusOrUninomial("Abies");
76 fromName.setSpecificEpithet("alba");
77
78 toName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
79 toName.setGenusOrUninomial("Pinus");
80 toName.setSpecificEpithet("pinova");
81 toNameAuthor = Person.NewInstance("Mill.", "Miller", "A.", "Andrew");
82 toName.setCombinationAuthorship(toNameAuthor);
83
84 fromSec = ReferenceFactory.newGeneric();
85 fromSec.setTitle("From Sec");
86 String initials = "J.M.";
87 fromSec.setAuthorship(Person.NewInstance(null, "Macfarlane", initials, null));
88 fromSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1918));
89
90 relSec = ReferenceFactory.newGeneric();
91 relSec.setTitle("From rel reference");
92 initials = null; //"M.R.";
93 relSec.setAuthorship(Person.NewInstance(null, "Cheek", initials, null));
94 relSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1919));
95
96 toSec = ReferenceFactory.newGeneric();
97 toSec.setTitle("To Sec");
98 toSec.setAuthorship(Person.NewTitledInstance("ToSecAuthor"));
99 toSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1928));
100
101 fromTaxon = Taxon.NewInstance(fromName, fromSec);
102 toTaxon = Taxon.NewInstance(toName, toSec);
103
104 TaxonRelationshipType type = TaxonRelationshipType.MISAPPLIED_NAME_FOR();
105 taxonRel = fromTaxon.addTaxonRelation(toTaxon, type, relSec, "123");
106 reverse = false;
107
108 formatter = new TaxonRelationshipFormatter();
109
110 languages = null;
111 }
112
113 @Test
114 public void test() {
115 Reference sec2 = ReferenceFactory.newGeneric();
116 sec2.setAuthorship(Person.NewInstance("Mue.", "Mueller", "I.", "Inger"));
117 sec2.setDatePublished(TimePeriodParser.parseStringVerbatim("1987"));
118 Taxon from2 = Taxon.NewInstance(fromName, sec2);
119 TaxonRelationship rel2 = toTaxon.addMisappliedName(from2, sec2, "333");
120 //same as rel1 except for sec
121 TaxonRelationship rel3 = toTaxon.addMisappliedName(from2, taxonRel.getCitation(), taxonRel.getCitationMicroReference());
122
123 TaxonRelationshipsDTO dto = new TaxonRelationshipsDTO();
124
125 dto.addRelation(taxonRel, Direction.relatedFrom, languages);
126 dto.addRelation(rel2, Direction.relatedFrom, languages);
127 TaxonRelation relToDuplicate = dto.addRelation(rel3, Direction.relatedFrom, languages);
128 dto.createMisapplicationString();
129 List<List<TaggedText>> misapplications = dto.getMisapplications();
130 Assert.assertEquals(2, misapplications.size()); //1 deduplicated
131 List<TaggedText> deduplicated = misapplications.get(0);
132 Assert.assertEquals(14, deduplicated.size());
133 Assert.assertSame(relToDuplicate.getTaggedText().get(7), deduplicated.get(9));
134 Assert.assertEquals(12, misapplications.get(1).size());
135
136 }
137
138 }