1 package org.cateproject.controller.taxon;
2
3 import java.util.UUID;
4
5 import org.springframework.binding.convert.converters.StringToObject;
6 import org.springframework.binding.convert.converters.TwoWayConverter;
7
8 import org.cateproject.controller.editor.UUIDPropertyEditor;
9 import org.cateproject.controller.event.taxon.TaxonCreatedEvent;
10 import org.cateproject.controller.flow.action.taxon.EditAction;
11 import org.cateproject.model.WebRevision;
12 import org.cateproject.service.WebRevisionService;
13 import org.easymock.EasyMock;
14 import org.junit.Ignore;
15 import org.springframework.binding.convert.converters.PropertyEditorConverter;
16 import org.springframework.binding.convert.service.GenericConversionService;
17 import org.springframework.binding.mapping.Mapper;
18 import org.springframework.binding.mapping.MappingResults;
19 import org.springframework.binding.message.MessageContext;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.webflow.config.FlowDefinitionResource;
22 import org.springframework.webflow.config.FlowDefinitionResourceFactory;
23 import org.springframework.webflow.core.collection.AttributeMap;
24 import org.springframework.webflow.core.collection.LocalAttributeMap;
25 import org.springframework.webflow.core.collection.MutableAttributeMap;
26 import org.springframework.webflow.engine.EndState;
27 import org.springframework.webflow.engine.Flow;
28 import org.springframework.webflow.test.MockExternalContext;
29 import org.springframework.webflow.test.MockFlowBuilderContext;
30 import org.springframework.webflow.test.execution.AbstractXmlFlowExecutionTests;
31
32 import eu.etaxonomy.cdm.api.service.IDescriptionService;
33 import eu.etaxonomy.cdm.api.service.INameService;
34 import eu.etaxonomy.cdm.api.service.ITaxonService;
35 import eu.etaxonomy.cdm.api.service.ITermService;
36 import eu.etaxonomy.cdm.model.agent.Team;
37 import eu.etaxonomy.cdm.model.description.TaxonDescription;
38 import eu.etaxonomy.cdm.model.location.NamedArea;
39 import eu.etaxonomy.cdm.model.name.BotanicalName;
40 import eu.etaxonomy.cdm.model.reference.WebPage;
41 import eu.etaxonomy.cdm.model.taxon.Taxon;
42 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
43
44 @Ignore
45 public class CreateTaxonTest extends AbstractXmlFlowExecutionTests {
46
47 protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) {
48 return resourceFactory.createClassPathResource("/org/cateproject/controller/flows/taxon/create.xml", CreateTaxonTest.class);
49 }
50
51 class UUIDToStringConverter extends StringToObject implements TwoWayConverter {
52
53 public UUIDToStringConverter() {
54 super(UUID.class);
55 }
56
57 @Override
58 protected Object toObject(String string, Class targetClass)
59 throws Exception {
60 if(string != null) {
61 return UUID.fromString(string);
62 }
63 return null;
64 }
65
66 @Override
67 protected String toString(Object object) throws Exception {
68 if(object != null) {
69 return ((UUID)object).toString();
70 }
71 return null;
72 }
73 }
74
75
76 protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) {
77 builderContext.registerBean("taxonServiceImpl", taxonService);
78 builderContext.registerBean("termServiceImpl", termService);
79 builderContext.registerBean("nameServiceImpl", nameService);
80 builderContext.registerBean("webRevisionService", webRevisionService);
81 builderContext.registerBean("editTaxonAction", editTaxonAction);
82 Flow mockEditTaxonFlow = new Flow("taxon/edit");
83 mockEditTaxonFlow.setInputMapper(new Mapper() {
84 public MappingResults map(Object source, Object target) {
85 assertEquals(parentUUID, ((AttributeMap) source).get("parent"));
86 return null;
87 }
88 });
89
90 new EndState(mockEditTaxonFlow, "created");
91
92 mockEditTaxonFlow.setOutputMapper(new Mapper() {
93 public MappingResults map(Object source, Object target) {
94 ((LocalAttributeMap) target).put("taxon", taxonUUID);
95 return null;
96 }
97 });
98 builderContext.registerSubflow(mockEditTaxonFlow);
99 GenericConversionService conversionService = (GenericConversionService)builderContext.getConversionService();
100 conversionService.addConverter(new UUIDToStringConverter());
101 }
102
103 private String parentUUIDString = "b4104408-6bd2-4d5b-8085-c80905a48d57";
104 private String taxonUUIDString = "148f5989-8374-4ebf-a904-4891635dd726";
105 private UUID parentUUID = UUID.fromString(parentUUIDString);
106 private UUID taxonUUID = UUID.fromString(taxonUUIDString);
107 private ITaxonService taxonService;
108 private INameService nameService;
109 private ITermService termService;
110 private IDescriptionService descriptionService;
111 private WebRevisionService webRevisionService;
112 private EditAction editTaxonAction;
113 private Taxon parent;
114 private BotanicalName parentName;
115 private BotanicalName name;
116 private UUID nameUuid;
117 private TaxonRelationshipType isParentTaxonOf;
118
119
120 private Taxon taxon;
121 private WebPage currentConsensus;
122 private WebRevision webRevision;
123 private ApplicationContext applicationContext;
124 public void setUp() throws Exception {
125 taxonService = EasyMock.createMock(ITaxonService.class);
126 termService = EasyMock.createMock(ITermService.class);
127 webRevisionService = EasyMock.createMock(WebRevisionService.class);
128 descriptionService = EasyMock.createMock(IDescriptionService.class);
129 nameService = EasyMock.createMock(INameService.class);
130 applicationContext = EasyMock.createMock(ApplicationContext.class);
131 editTaxonAction = new EditAction();
132 editTaxonAction.setNameService(nameService);
133 editTaxonAction.setWebRevisionService(webRevisionService);
134 editTaxonAction.setDescriptionService(descriptionService);
135 editTaxonAction.setService(taxonService);
136 editTaxonAction.setApplicationContext(applicationContext);
137 nameUuid = UUID.randomUUID();
138 parentName = BotanicalName.NewInstance(null);
139 parent = Taxon.NewInstance(parentName,null);
140
141 webRevision = new WebRevision();
142 currentConsensus = WebPage.NewInstance();
143 currentConsensus.setAuthorTeam(Team.NewInstance());
144 webRevision.setCurrentConsensus(currentConsensus);
145 webRevision.setRootArea(NamedArea.NewInstance());
146 parent.setUuid(parentUUID);
147 name = BotanicalName.NewInstance(null);
148 isParentTaxonOf = new TaxonRelationshipType();
149 taxon = Taxon.NewInstance(name, null);
150 UUID.randomUUID();
151 new TaxonDescription();
152 UUID.randomUUID();
153 }
154
155 public void testStartFlow() {
156 MutableAttributeMap attributeMap = new LocalAttributeMap();
157 MockExternalContext context = new MockExternalContext();
158 attributeMap.put("parent", parentUUIDString);
159 EasyMock.expect(taxonService.load(EasyMock.eq(parentUUID))).andReturn(parent);
160 EasyMock.expect(taxonService.load(EasyMock.eq(taxonUUID))).andReturn(taxon);
161 EasyMock.expect(taxonService.update(EasyMock.eq(parent))).andReturn(UUID.randomUUID());
162 EasyMock.replay(taxonService,termService,webRevisionService,nameService);
163
164 this.startFlow(attributeMap,context);
165
166 EasyMock.verify(taxonService,termService,webRevisionService,nameService);
167 assertCurrentStateEquals("created");
168 }
169
170 }