Project

General

Profile

Download (13.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.facade;
10

    
11
import java.io.FileNotFoundException;
12
import java.util.UUID;
13

    
14
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
15
import org.hibernate.proxy.HibernateProxy;
16
import org.junit.Assert;
17
import org.junit.Before;
18
import org.junit.Test;
19
import org.unitils.dbunit.annotation.DataSet;
20
import org.unitils.spring.annotation.SpringBeanByType;
21

    
22
import eu.etaxonomy.cdm.api.service.IDescriptionService;
23
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
24
import eu.etaxonomy.cdm.api.service.ITaxonService;
25
import eu.etaxonomy.cdm.model.agent.Person;
26
import eu.etaxonomy.cdm.model.agent.Team;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.common.LanguageString;
30
import eu.etaxonomy.cdm.model.common.TimePeriod;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
33
import eu.etaxonomy.cdm.model.location.Country;
34
import eu.etaxonomy.cdm.model.location.NamedArea;
35
import eu.etaxonomy.cdm.model.location.Point;
36
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
37
import eu.etaxonomy.cdm.model.media.Media;
38
import eu.etaxonomy.cdm.model.name.Rank;
39
import eu.etaxonomy.cdm.model.name.TaxonName;
40
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
41
import eu.etaxonomy.cdm.model.occurrence.Collection;
42
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
43
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
44
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
45
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
46
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
47
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
48
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
49
import eu.etaxonomy.cdm.model.taxon.Taxon;
50
import eu.etaxonomy.cdm.model.term.DefinedTerm;
51
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
52
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
53

    
54
/**
55
 * NOTE: The only reason for having this test is to test if injection of the cache strategy into a
56
 * standard specimen works. Once we use another default cache strategy then the derived unit facade
57
 * this test can be deleted or adapted and moved to cdmlib-model.
58
 *
59
 * @author a.mueller
60
 * @since 2010 Jun 3
61
 */
62
public class DerivedUnitFacadeCacheStrategyInjectionTest
63
        extends CdmTransactionalIntegrationTest {
64

    
65
    @SuppressWarnings("unused")
66
	private static final Logger logger = LogManager.getLogger(DerivedUnitFacadeCacheStrategyInjectionTest.class);
67

    
68
    @SpringBeanByType
69
    private IOccurrenceService occService;
70
    @SpringBeanByType
71
    private ITaxonService taxonService;
72
    @SpringBeanByType
73
    private IDescriptionService descService;
74

    
75
    private static final UUID taxonUuid = UUID.fromString("10cfb372-0b1a-4d82-9707-c5ffd2b93a55");
76

    
77
    private DerivedUnit specimen;
78
    private DerivationEvent derivationEvent;
79
    private FieldUnit fieldUnit;
80
    private GatheringEvent gatheringEvent;
81
    private final Integer absoluteElevation = 40;
82
    private final Integer absoluteElevationError = 2;
83
    private final Team collector = Team.NewInstance();
84
    private final String collectingMethod = "Collection Method";
85
    private final Double distanceToGround = 22.0;
86
    private final Double distanceToSurface = 50.0;
87
	private final ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
88
    private final Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
89
    private final String gatheringEventDescription = "A nice gathering description";
90
    private final TimePeriod gatheringPeriod = TimePeriodParser.parseString("03.05.2005");
91
    private final String ecology = "sand dunes";
92
    private final String plantDescription = "flowers blue";
93

    
94
    private final String fieldNumber = "5678";
95
    private final String fieldNotes = "such a beautiful specimen";
96
    private Person primaryCollector;
97

    
98
    private final String individualCount = "1";
99
	private final DefinedTerm lifeStage = DefinedTerm.NewStageInstance("A wonderful stage", "stage", "st");
100
	private final DefinedTerm sex = DefinedTerm.NewSexInstance("FemaleMale", "FM", "FM");
101
	private final NamedArea country = Country.GERMANY();
102
    private final LanguageString locality = LanguageString.NewInstance("Berlin-Dahlem, E side of Englerallee", Language.DEFAULT());
103

    
104
    private final String exsiccatum = "Greuter, Pl. Dahlem. 456";
105
    private final String accessionNumber = "8909756";
106
    private final String catalogNumber = "UU879873590";
107
    private final TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Abies", null, null, null, null, null, null, null);
108
    private final String collectorsNumber = "234589913A34";
109
    private final Collection collection = Collection.NewInstance();
110

    
111
    private final PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
112

    
113
    private DerivedUnit collectionSpecimen;
114
    private GatheringEvent existingGatheringEvent;
115
    private DerivationEvent firstDerivationEvent;
116
    private FieldUnit firstFieldObject;
117
    private final Media media1 = Media.NewInstance();
118

    
119

    
120
//****************************** SET UP *****************************************/
121

    
122
	@Before
123
	public void setUp() throws Exception {
124
		specimen = DerivedUnit.NewPreservedSpecimenInstance();
125

    
126
		derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
127
		specimen.setDerivedFrom(derivationEvent);
128
		fieldUnit = FieldUnit.NewInstance();
129
		fieldUnit.addDerivationEvent(derivationEvent);
130
		gatheringEvent = GatheringEvent.NewInstance();
131
		fieldUnit.setGatheringEvent(gatheringEvent);
132
		gatheringEvent.setAbsoluteElevation(absoluteElevation);
133
//		gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
134
		gatheringEvent.setActor(collector);
135
		gatheringEvent.setCollectingMethod(collectingMethod);
136
		gatheringEvent.setDistanceToGround(distanceToGround);
137
		gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
138
		gatheringEvent.setExactLocation(exactLocation);
139
		gatheringEvent.setDescription(gatheringEventDescription);
140

    
141
		gatheringEvent.setTimeperiod(gatheringPeriod);
142
		gatheringEvent.setLocality(locality);
143
		gatheringEvent.setCountry(country);
144

    
145
		fieldUnit.setFieldNumber(fieldNumber);
146
		fieldUnit.setFieldNotes(fieldNotes);
147
		fieldUnit.setIndividualCount(individualCount);
148
		fieldUnit.setSex(sex);
149
		fieldUnit.setLifeStage(lifeStage);
150
		primaryCollector = Person.NewTitledInstance("Kilian");
151
		collector.addTeamMember(primaryCollector);
152
		Person secondCollector = Person.NewInstance();
153
		secondCollector.setGivenName("Andreas");
154
		secondCollector.setFamilyName("Muller");
155
		collector.addTeamMember(secondCollector);
156
		Person thirdCollector = Person.NewTitledInstance("Kohlbecker");
157
		collector.addTeamMember(thirdCollector);
158
		fieldUnit.setPrimaryCollector(primaryCollector);
159

    
160
		specimen.setAccessionNumber(accessionNumber);
161
		specimen.setCatalogNumber(catalogNumber);
162
		specimen.setStoredUnder(taxonName);
163
		specimen.setCollection(collection);
164
		specimen.setPreservation(preservationMethod);
165
		specimen.setExsiccatum(exsiccatum);
166

    
167
		//existing specimen with 2 derivation events in line
168
		collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
169
		DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
170
		firstFieldObject = FieldUnit.NewInstance();
171

    
172
		//TODO maybe we should define concrete event types here
173
		DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(null);
174
		DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(null);
175
		firstDerivationEvent = DerivationEvent.NewInstance(null);
176

    
177
		collectionSpecimen.setDerivedFrom(lastDerivationEvent);
178

    
179
		lastDerivationEvent.addOriginal(middleSpecimen);
180
		middleSpecimen.setDerivedFrom(firstDerivationEvent);
181
		firstDerivationEvent.addOriginal(firstFieldObject);
182
		existingGatheringEvent = GatheringEvent.NewInstance();
183
		firstFieldObject.setGatheringEvent(existingGatheringEvent);
184

    
185
	}
186

    
187
	@Test
188
	public void testGetSpecimenTitleCache() {
189
//		String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), sand dunes, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B: 8909756); flowers blue";
190
		String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3 May 2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B: 8909756)";
191

    
192
//		DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
193
//		SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
194
//		fieldUnitDescription.addElement(ecologyItem);
195
////		specimenFacade.setEcology(ecology);
196
//		DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
197
//		fieldUnitDescription.addElement(plantDescItem);
198
////		specimenFacade.setPlantDescription(plantDescription);
199

    
200
		collection.setCode("B");
201
		Assert.assertEquals(correctCache, specimen.getTitleCache());
202
	}
203

    
204
    @Test
205
    public void testGetFieldUnitTitleCache() {
206
//        String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), sand dunes, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; flowers blue";
207
        String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3 May 2005, Kilian 5678, A. Muller & Kohlbecker";
208

    
209
//        DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
210
//        SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
211
//        fieldUnitDescription.addElement(ecologyItem);
212
//        DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
213
//        fieldUnitDescription.addElement(plantDescItem);
214

    
215
        collection.setCode("B");  //no effect
216
        Assert.assertEquals(correctCache, fieldUnit.getTitleCache());
217
    }
218

    
219
    //Test if even a hibernate proxy class correctly loads the DerivedUnitCacheStrategy.
220
    @Test
221
    @DataSet
222
    public void testPersistedDerivedUnit(){
223
        Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
224

    
225
        IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5000);
226
        SpecimenOrObservationBase<?> specimen = indivAssoc.getAssociatedSpecimenOrObservation();
227
        Assert.assertTrue("Specimen should be proxy otherwise the test does not test what it should", specimen instanceof HibernateProxy);
228
        DerivedUnit myUnit = CdmBase.deproxy(specimen, DerivedUnit.class);
229
        myUnit.setTitleCache(null, false);
230
        String expectedDerivedUnitCache = "Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E, 3 May 2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (8909756)";
231
        Assert.assertEquals(expectedDerivedUnitCache, myUnit.getTitleCache());
232
    }
233

    
234
    @Test
235
    @DataSet
236
    public void testPersistedFieldUnit(){
237
        Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
238
        IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5001);
239
        SpecimenOrObservationBase<?> fieldUnit = indivAssoc.getAssociatedSpecimenOrObservation();
240
        Assert.assertTrue("FieldUnit should be proxy otherwise the test does not test what it should", fieldUnit instanceof HibernateProxy);
241
        FieldUnit myFieldUnit = CdmBase.deproxy(fieldUnit, FieldUnit.class);
242
        myFieldUnit.setTitleCache(null, false);
243
        String expectedFieldUnitCache = "Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E, 3 May 2005, Kilian 5678, A. Muller & Kohlbecker";
244
        Assert.assertEquals(expectedFieldUnitCache, myFieldUnit.getTitleCache());
245
    }
246

    
247
    private IndividualsAssociation getDescriptionElement(Taxon taxon, int id) {
248
        for (DescriptionElementBase el : taxon.getDescriptions().iterator().next().getElements()){
249
            if (el.getId() == id) {
250
                return (IndividualsAssociation)el;
251
            }
252
        }
253
        return null;
254
    }
255

    
256
    @Override
257
//  @Test
258
    public void createTestDataSet() throws FileNotFoundException {
259
//        specimen.setUuid(derivedUnitUuid);
260
//        fieldUnit.setUuid(fieldUnitUuid);
261
//        Taxon taxon = Taxon.NewInstance(null, null);
262
//        taxon.setUuid(taxonUuid);
263
//        TaxonDescription desc = TaxonDescription.NewInstance(taxon);
264
//
265
//        IndividualsAssociation indAssoc = IndividualsAssociation.NewInstance(specimen);
266
//        desc.addElement(indAssoc);
267
//
268
//        this.taxonService.saveOrUpdate(taxon);
269
//
270
//        setComplete();
271
//        endTransaction();
272
//
273
//        writeDbUnitDataSetFile(new String[]{"SpecimenOrObservationBase",
274
//                "DerivationEvent", "DescriptionBase","DescriptionElementBase",
275
//                "GatheringEvent","AgentBase","LanguageString","TaxonName",
276
//                "TaxonBase","Collection",
277
//                "MaterialOrMethodEvent","SpecimenOrObservationBase_DerivationEvent"});
278
    }
279

    
280
}
(1-1/4)