2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.io
.FileNotFoundException
;
13 import java
.util
.Arrays
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
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
;
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
;
36 * @created Sep 22, 2009
38 public class ClassificationServiceDeepTest
extends CdmTransactionalIntegrationTest
{
40 private static final Logger logger
= Logger
.getLogger(ClassificationServiceDeepTest
.class);
43 IClassificationService service
;
46 ITaxonNodeService taxonNodeService
;
47 private static final List
<String
> NODE_INIT_STRATEGY
= Arrays
.asList(new String
[]{
50 "childNodes.taxon.name",
58 public final void testFixHierarchy(){
59 Classification classification
= service
.find(UUID
.fromString("52b41b07-5500-43ae-82e6-ea2fd328c3d5"));
61 Set
<TaxonNode
> taxonNodes
= classification
.getAllNodes();//= taxonNodeService.listAllNodesForClassification(classification, 0, null);
62 for (TaxonNode node
: taxonNodes
){
63 taxonNodeService
.load(node
.getUuid(), NODE_INIT_STRATEGY
);
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")));
81 //check if existing parent-child-relation was not touched
82 else if(node
.getTaxon().getUuid().equals(UUID
.fromString("c1cae3aa-960e-482f-b336-f3e657e96c43"))){
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);
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());
102 public void createTestDataSet() throws FileNotFoundException
{
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"};
107 Classification classification
= Classification
.NewInstance("New Classification");
108 classification
.setUuid(UUID
.fromString("52b41b07-5500-43ae-82e6-ea2fd328c3d5"));
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
);
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");
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);
132 taxonNodeService
.saveOrUpdate(p1
);
133 taxonNodeService
.saveOrUpdate(p2
);
134 taxonNodeService
.saveOrUpdate(p3
);
135 taxonNodeService
.saveOrUpdate(p4
);
136 service
.saveOrUpdate(classification
);
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");
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));
151 //save classification
152 service
.saveOrUpdate(classification
);
153 commitAndStartNewTransaction(null);
159 writeDbUnitDataSetFile(new String
[] {
167 } catch (FileNotFoundException e
) {