Project

General

Profile

Download (5.67 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 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.persistence.dao.initializer;
10

    
11
import static org.junit.Assert.assertFalse;
12
import static org.junit.Assert.assertTrue;
13

    
14
import java.io.FileNotFoundException;
15
import java.net.URI;
16
import java.util.ArrayList;
17
import java.util.Arrays;
18
import java.util.HashSet;
19
import java.util.List;
20
import java.util.Set;
21
import java.util.UUID;
22

    
23
import org.hibernate.Hibernate;
24
import org.junit.Test;
25
import org.unitils.dbunit.annotation.DataSet;
26
import org.unitils.spring.annotation.SpringBeanByType;
27

    
28
import eu.etaxonomy.cdm.model.agent.Address;
29
import eu.etaxonomy.cdm.model.agent.Contact;
30
import eu.etaxonomy.cdm.model.agent.Person;
31
import eu.etaxonomy.cdm.model.location.Country;
32
import eu.etaxonomy.cdm.model.location.Point;
33
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
37
import eu.etaxonomy.cdm.model.reference.Reference;
38
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
39
import eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao;
40
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
41
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
42
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
43

    
44
/**
45
 * @author a.mueller
46
 * @date 16.11.2015
47
 */
48
public class AdvancedBeanInitializerTest extends CdmTransactionalIntegrationTest {
49

    
50
    private static final UUID personUuid = UUID.fromString("d0568bb1-4dc8-40dc-a405-d0b9e714a7a9");
51

    
52
    private static final UUID referenceUuid = UUID.fromString("f48196c6-854a-416e-8f2a-67bd39e988dc");
53

    
54
    private static final UUID nameUuid = UUID.fromString("98cbb643-d521-4ca7-86f7-8180bea85d9f");
55

    
56
    @SpringBeanByType
57
    private IAgentDao agentDao;
58

    
59
    @SpringBeanByType
60
    private IReferenceDao referenceDao;
61

    
62
    @SpringBeanByType
63
    private ITaxonNameDao nameDao;
64

    
65
    @SpringBeanByType
66
    private AdvancedBeanInitializer initializer;
67

    
68
    @DataSet
69
    @Test
70
    public void testContact() {
71
        Person person = (Person)agentDao.findByUuid(personUuid);
72

    
73
        final List<String> propPath = Arrays.asList(new String[]{
74
            "contact.urls",
75
            "contact.phoneNumbers",
76
            "contact.addresses",
77
            "contact.faxNumbers",
78
            "contact.emailAddresses",
79
        });
80
        initializer.initialize(person, propPath);
81
    }
82

    
83
    /**
84
     * Attempt to reproduce #7331 without success
85
     */
86
    @DataSet
87
    @Test
88
    public void testFullNameGraphWithPreloadedReference() {
89
        // find the reference by iD (not load!)
90
        Reference ref = referenceDao.findById(5000);
91
        TaxonName name = nameDao.findById(5000);
92
        assertFalse("for this test to be significant the authorship must be uninitialized", Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship()));
93
        initializer.initialize(name, Arrays.asList(new String[]{"nomenclaturalReference.authorship.$"}));
94
        assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship()));
95
    }
96

    
97

    
98

    
99
    @Override
100
    // @Test
101
    public void createTestDataSet() throws FileNotFoundException {
102
        // 1. create person and a reference
103
        Person person = Person.NewTitledInstance("Hallo you");
104
        Set<Address> addresses = new HashSet<Address>();
105
        addresses.add(Address.NewInstance(Country.GERMANY(), "locality", "pobox", "postcode", "region", "street", Point.NewInstance(50.02,33.3, ReferenceSystem.GOOGLE_EARTH(), 3)));
106
        List<String> emailAddresses = new ArrayList<String>();
107
        emailAddresses.add("My.email@web.de");
108
        List<String> faxNumbers = new ArrayList<String>();
109
        faxNumbers.add("0049-30-1234545");
110
        List<String> phoneNumbers = new ArrayList<String>();
111
        phoneNumbers.add("0049-30-1234546");
112
        List<URI> urls = new ArrayList<URI>();
113
        urls.add(URI.create("http://www.test.de"));
114
        Contact contact = Contact.NewInstance(addresses, emailAddresses, faxNumbers, phoneNumbers, urls);
115

    
116
        person.setContact(contact);
117
        person.setUuid(personUuid);
118
        person = (Person)agentDao.save(person);
119

    
120
        Reference ref = ReferenceFactory.newBook();
121
        ref.setUuid(referenceUuid);
122
        ref.setAuthorship(person);
123
        ref.setTitleCache("The Book", true);
124
        referenceDao.save(ref);
125

    
126
        TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
127
        name.setUuid(nameUuid);
128
        name.setNomenclaturalReference(ref);
129
        name.setTitleCache("Species testii", true);
130
        nameDao.save(name);
131

    
132
        // 2. end the transaction so that all data is actually written to the db
133
        setComplete();
134
        endTransaction();
135

    
136
        // use the fileNameAppendix if you are creating a data set file which need to be named differently
137
        // from the standard name. For example if a single test method needs different data then the other
138
        // methods the test class you may want to set the fileNameAppendix when creating the data for this method.
139
        String fileNameAppendix = null;
140

    
141
        // 3.
142
        writeDbUnitDataSetFile(new String[] {
143
            "ADDRESS", "AGENTBASE","AgentBase_contact_emailaddresses",
144
            "AgentBase_contact_faxnumbers","AgentBase_contact_phonenumbers",
145
            "AgentBase_contact_urls","AgentBase_Address",
146
            "REFERENCE", "TaxonName", "HomotypicalGroup",
147
            "HIBERNATE_SEQUENCES" // IMPORTANT!!!
148
            },
149
            fileNameAppendix );
150

    
151
    }
152

    
153
}
    (1-1/1)