Project

General

Profile

Download (7.09 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.persistence.dao.hibernate.common;
11

    
12
import java.io.FileNotFoundException;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.junit.Assert;
17
import org.junit.Test;
18
import org.unitils.dbunit.annotation.DataSet;
19
import org.unitils.spring.annotation.SpringBeanByType;
20

    
21
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
22
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
23
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
24
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
25
import eu.etaxonomy.cdm.model.taxon.Classification;
26
import eu.etaxonomy.cdm.model.taxon.Taxon;
27
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28
import eu.etaxonomy.cdm.persistence.dao.common.IPreferenceDao;
29
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
30
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
31

    
32
/**
33
 * @author a.mueller
34
 *
35
 */
36
public class PreferenceDaoTest  extends CdmTransactionalIntegrationTest {
37

    
38
	@SpringBeanByType
39
	IPreferenceDao dao;
40

    
41
	@SpringBeanByType
42
	IClassificationDao classificationDao;
43

    
44
/************ TESTS ********************************/
45

    
46
    @Test
47
    @DataSet
48
    public void testDao() {
49
    	Assert.assertNotNull(dao);
50
    }
51

    
52
    @Test
53
    @DataSet
54
    public void testGet() {
55
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
56
//        PrefKey key = CdmPreference.NewKey(PreferenceSubjectEnum.Database, PreferencePredicate.NomenclaturalCode);
57
        CdmPreference pref = dao.get(key);
58
        Assert.assertNotNull("CdmPreference for given key must exist", pref);
59
        Assert.assertEquals("ICNAFP", pref.getValue());
60

    
61
        key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.Test);
62
//        key = CdmPreference.NewKey(PreferenceSubjectEnum.Database, PreferencePredicate.Test);
63
        pref = dao.get(key);
64
        Assert.assertNull("CdmPreference for given key must not exist", pref);
65
    }
66

    
67

    
68
    @Test
69
    @DataSet
70
    public void testCount() {
71
    	 long countStart = dao.count();
72
         Assert.assertEquals("There should be 1 preference in the CDM store", 1, countStart);
73
    }
74

    
75
    @Test
76
    @DataSet
77
    public void testList() {
78
         List<CdmPreference> list = dao.list();
79
         Assert.assertEquals("There should be 1 preference in the CDM store", 1, list.size());
80
         CdmPreference pref = list.get(0);
81
         Assert.assertNotNull("CdmPreference for given key must exist", pref);
82
         Assert.assertEquals("ICNAFP", pref.getValue());
83
         Assert.assertTrue(pref.isDatabasePref());
84
    }
85

    
86
    @Test
87
    @DataSet
88
    public void testSet() {
89
    	 long countStart = dao.count();
90
         Assert.assertEquals(1, countStart);
91

    
92
         CdmPreference pref = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.Test, "200");
93
//         CdmPreference pref = CdmPreference.NewInstance(PreferenceSubjectEnum.Database, PreferencePredicate.Test, "200");
94
        int addedPrefs = 1;
95
        dao.set(pref);
96
	   	long count = dao.count();
97
	    Assert.assertEquals("There should be 1 new preference", countStart + addedPrefs, count);
98

    
99
	    pref = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, "ICZN");
100
//        pref = CdmPreference.NewInstance(PreferenceSubjectEnum.Database, PreferencePredicate.NomenclaturalCode, "ICZN");
101
        dao.set(pref);
102

    
103
	   	count = dao.count();
104
	    Assert.assertEquals("There should still be only 1 new preference", countStart + addedPrefs, count);
105

    
106
	    //delete default values
107
	    pref = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, PreferencePredicate.NomenclaturalCode.getDefaultValue().toString());
108
	    pref.setAllowOverride(true);
109
	    dao.set(pref);
110
	    count = dao.count();
111
        Assert.assertEquals("There should be only 1 preference left. Nomenclatural Code should be delete", addedPrefs, count);
112

    
113
        pref = dao.get(CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode));
114
        Assert.assertNull(pref);
115
    }
116

    
117
    @Test
118
    @DataSet(value="eu.etaxonomy.cdm.persistence.dao.hibernate.common.PreferenceDaoTest.testFindTaxonNodeString.xml")
119
    public void testFindTaxonNodeString() {
120
        Classification classification = classificationDao.findByUuid(uuidClassification);
121
        TaxonNode genusNode = classification.getRootNode().getChildNodes().get(0);
122
        TaxonNode speciesNode = genusNode.getChildNodes().get(0);
123

    
124
        CdmPreference pref = dao.find(speciesNode, PreferencePredicate.NomenclaturalCode.getKey());
125
        Assert.assertNotNull("CdmPreference for given key must exist", pref);
126
        Assert.assertEquals("ICZN", pref.getValue());
127

    
128
        pref = dao.find(genusNode, PreferencePredicate.NomenclaturalCode.getKey());
129
        Assert.assertNotNull("CdmPreference for given key must exist", pref);
130
        Assert.assertEquals("ICVCN", pref.getValue());
131

    
132
        pref = dao.find(classification.addChildTaxon(null, null, null), PreferencePredicate.NomenclaturalCode.getKey());
133
        Assert.assertNotNull("CdmPreference for given key must exist", pref);
134
        Assert.assertEquals("ICNAFP", pref.getValue());
135
        Assert.assertTrue(pref.isDatabasePref());
136

    
137
    }
138

    
139
    private UUID uuidClassification = UUID.fromString("bbd2cdb4-8b83-4ef9-a553-c9629c3890aa");
140

    
141
//    @Test
142
    @Override
143
    public void createTestDataSet() throws FileNotFoundException {
144
        // 1. create the entities   and save them
145
        Classification classification = Classification.NewInstance("European Abies");
146
        classification.setUuid(uuidClassification);
147

    
148
        Taxon taxonGenus = Taxon.NewInstance(null, null);
149
        TaxonNode genusNode = classification.addChildTaxon(taxonGenus, null, null);
150

    
151
        Taxon taxonSpecies = Taxon.NewInstance(null, null);
152
        TaxonNode speciesNode = genusNode.addChildTaxon(taxonSpecies, null, null);
153

    
154
        Taxon taxonSubSpecies = Taxon.NewInstance(null, null);
155
        speciesNode.addChildTaxon(taxonSubSpecies, null, null);
156

    
157
        classificationDao.save(classification);
158

    
159

    
160
        // 2. end the transaction so that all data is actually written to the db
161
        setComplete();
162
        endTransaction();
163

    
164
        // use the fileNameAppendix if you are creating a data set file which need to be named differently
165
        // from the standard name. For example if a single test method needs different data then the other
166
        // methods the test class you may want to set the fileNameAppendix when creating the data for this method.
167
        String fileNameAppendix = "xxx";
168

    
169
        // 3.
170
        writeDbUnitDataSetFile(new String[] {
171
            "CLASSIFICATION","TAXONBASE", "TAXONNAME",
172
            "TAXONNODE",
173
            "HOMOTYPICALGROUP",
174
            "HIBERNATE_SEQUENCES" // IMPORTANT!!!
175
            },
176
            fileNameAppendix, true );
177
    }
178
}
(9-9/11)