Project

General

Profile

Download (6.72 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.api.service;
11

    
12
import java.io.FileNotFoundException;
13
import java.util.Arrays;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import org.apache.log4j.Logger;
21
import org.junit.Assert;
22
import org.junit.Test;
23
import org.unitils.dbunit.annotation.DataSet;
24
import org.unitils.spring.annotation.SpringBeanByType;
25

    
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.name.TaxonName;
28
import eu.etaxonomy.cdm.model.taxon.Classification;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
32
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
33

    
34
/**
35
 * @author n.hoffmann
36
 * @since Sep 22, 2009
37
 */
38
public class ClassificationServiceDeepTest extends CdmTransactionalIntegrationTest{
39

    
40
    private static final Logger logger = Logger.getLogger(ClassificationServiceDeepTest.class);
41

    
42
    @SpringBeanByType
43
    IClassificationService service;
44

    
45
    @SpringBeanByType
46
    ITaxonNodeService taxonNodeService;
47
    private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[]{
48
            "childNodes",
49
            "childNodes.taxon",
50
            "childNodes.taxon.name",
51
            "taxon.sec",
52
            "taxon.name.*"
53
            });
54

    
55

    
56
    @Test
57
    @DataSet
58
    public final void testFixHierarchy(){
59
        Classification classification = service.find(UUID.fromString("52b41b07-5500-43ae-82e6-ea2fd328c3d5"));
60

    
61
        Set<TaxonNode> taxonNodes = classification.getAllNodes();//= taxonNodeService.listAllNodesForClassification(classification, 0, null);
62
        for (TaxonNode node: taxonNodes){
63
            taxonNodeService.load(node.getUuid(), NODE_INIT_STRATEGY);
64
        }
65
        UpdateResult result = service.createHierarchyInClassification(classification, null);
66
        Classification classification2 = CdmBase.deproxy(result.getCdmEntity(), Classification.class);
67
        //creating the classification was succesful
68
        Assert.assertNotNull(classification2);
69
        for(TaxonNode node: classification2.getAllNodes()){
70
        	node = taxonNodeService.load(node.getUuid(), NODE_INIT_STRATEGY);
71
        	//check if TaxonNode was moved and has a new parent-child-relation
72
        	if(node.getTaxon().getUuid().equals(UUID.fromString("e8bc5566-eca6-4814-b18a-814c16c66144"))){
73
        		//parent has new Child
74
        		Assert.assertTrue(node.getCountChildren() == 1);
75
        		//child is Griftus grifatus subsp. fikus
76
        		Assert.assertTrue(node.getChildNodes().get(0).getTaxon().getUuid().equals(UUID.fromString("15d719a2-d27d-4366-92de-0898b2f3ebc8")));
77
        	}else if(node.getTaxon().getUuid().equals(UUID.fromString("15d719a2-d27d-4366-92de-0898b2f3ebc8"))){
78
        		//Assert that taxon has a parent and that parent is Griftus
79
        		Assert.assertTrue(node.getParent().getTaxon().getUuid().equals(UUID.fromString("e8bc5566-eca6-4814-b18a-814c16c66144")));
80
        	}
81
        	//check if existing parent-child-relation was not touched
82
        	else if(node.getTaxon().getUuid().equals(UUID.fromString("c1cae3aa-960e-482f-b336-f3e657e96c43"))){
83
        		//node genus genus
84
        		Assert.assertTrue(node.getCountChildren()==3);
85
        		//check if existing parent-child-relation was not touched and has a newly created parentNode
86
        		//assert this node is not anymore in the highest taxa
87
        		Assert.assertTrue(!node.isTopmostNode());
88
        		//assert this node has a parent
89
        		Assert.assertTrue(node.getParent()!=null);
90
        	}
91
        }
92
        UUID uuid = classification2.getUuid();
93
        logger.debug("New Classification: " + uuid.toString());
94
        List<TaxonNode> taxonNodes2 = taxonNodeService.listAllNodesForClassification(classification2, 0, null);
95
        Assert.assertNotEquals(taxonNodes.size(), taxonNodes2.size());
96
    }
97

    
98

    
99

    
100
    @Override
101
//    @Test
102
    public void createTestDataSet() throws FileNotFoundException {
103

    
104
    	String[] stringTaxonNames= new String[]{"Griftus grifatus subsp. fikus", "Griftus", "Genus genus subsp. tri", "Genus genus subsp. alt" ,
105
    			"Genus genus", "Garstig alter subsp. ekel", "Garstig", "Genus genus subsp. genus"};
106

    
107
    	Classification classification = Classification.NewInstance("New Classification");
108
    	classification.setUuid(UUID.fromString("52b41b07-5500-43ae-82e6-ea2fd328c3d5"));
109

    
110
    	//create Taxa from list
111
    	Map<String, Taxon> map = new HashMap<String, Taxon>();
112
    	for(String strName : stringTaxonNames){
113
    		NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
114
    		TaxonName nameBase = (TaxonName)parser.parseFullName(strName);
115
    		Taxon taxon = Taxon.NewInstance(nameBase, null);
116
    		map.put(strName, taxon);
117
    	}
118

    
119
    	//create Hierarchy
120
    	Taxon tp1 = map.get("Garstig");
121
    	Taxon tp2 = map.get("Griftus");
122
    	Taxon tp3 = map.get("Genus genus");
123
    	Taxon tp4 = map.get("Griftus grifatus subsp. fikus");
124

    
125
    	//create parents:
126
    	TaxonNode p1 = classification.addChildTaxon(tp1, null, null);
127
    	TaxonNode p2 = classification.addChildTaxon(tp2, null, null);
128
    	TaxonNode p3 = classification.addChildTaxon(tp3, null, null);
129
    	TaxonNode p4 = classification.addChildTaxon(tp4, null, null);
130

    
131

    
132
    	taxonNodeService.saveOrUpdate(p1);
133
    	taxonNodeService.saveOrUpdate(p2);
134
    	taxonNodeService.saveOrUpdate(p3);
135
    	taxonNodeService.saveOrUpdate(p4);
136
    	service.saveOrUpdate(classification);
137

    
138

    
139
    	//create children
140
    	Taxon tc1 = map.get("Garstig alter subsp. ekel");
141
    	Taxon tc2 = map.get("Genus genus subsp. alt");
142
    	Taxon tc3 = map.get("Genus genus subsp. tri");
143
    	Taxon tc4 = map.get("Genus genus subsp. genus");
144

    
145
    	//add to parent node
146
    	taxonNodeService.saveOrUpdate(p1.addChildTaxon(tc1, null, null));
147
    	taxonNodeService.saveOrUpdate(p3.addChildTaxon(tc2, null, null));
148
    	taxonNodeService.saveOrUpdate(p3.addChildTaxon(tc3, null, null));
149
    	taxonNodeService.saveOrUpdate(p3.addChildTaxon(tc4, null, null));
150

    
151
    	//save classification
152
    	service.saveOrUpdate(classification);
153
        commitAndStartNewTransaction(null);
154

    
155
        setComplete();
156
        endTransaction();
157

    
158
        try {
159
            writeDbUnitDataSetFile(new String[] {
160
                    "Classification",
161
                    "LanguageString",
162
                    "TaxonNode",
163
                    "TaxonBase",
164
                    "TaxonName",
165
                    "HomotypicalGroup"
166
            });
167
        } catch (FileNotFoundException e) {
168
            e.printStackTrace();
169
        }
170
    }
171
}
(3-3/40)