View Javadoc

1   package org.cateproject.controller.description;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.UUID;
6   
7   import org.cateproject.controller.editor.ClassPropertyEditor;
8   import org.cateproject.controller.editor.DefinedTermPropertyEditor;
9   import org.cateproject.controller.editor.UUIDPropertyEditor;
10  import org.cateproject.controller.flow.action.descriptionElement.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.beans.MutablePropertyValues;
16  import org.springframework.binding.convert.converters.PropertyEditorConverter;
17  import org.springframework.binding.convert.service.GenericConversionService;
18  import org.springframework.validation.DataBinder;
19  import org.springframework.web.bind.WebDataBinder;
20  import org.springframework.webflow.config.FlowDefinitionResource;
21  import org.springframework.webflow.config.FlowDefinitionResourceFactory;
22  import org.springframework.webflow.core.collection.LocalAttributeMap;
23  import org.springframework.webflow.core.collection.MutableAttributeMap;
24  import org.springframework.webflow.test.MockExternalContext;
25  import org.springframework.webflow.test.MockFlowBuilderContext;
26  import org.springframework.webflow.test.MockParameterMap;
27  import org.springframework.webflow.test.execution.AbstractXmlFlowExecutionTests;
28  
29  import eu.etaxonomy.cdm.api.service.IDescriptionService;
30  import eu.etaxonomy.cdm.api.service.ITermService;
31  import eu.etaxonomy.cdm.model.common.Language;
32  import eu.etaxonomy.cdm.model.common.LanguageString;
33  import eu.etaxonomy.cdm.model.description.DescriptionBase;
34  import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
35  import eu.etaxonomy.cdm.model.description.Feature;
36  import eu.etaxonomy.cdm.model.description.TaxonDescription;
37  import eu.etaxonomy.cdm.model.description.TextData;
38  
39  @Ignore
40  public class CreateDescriptionTest extends AbstractXmlFlowExecutionTests {
41  	
42  	
43  
44  	protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) {
45  		return resourceFactory.createClassPathResource("/org/cateproject/controller/flows/descriptionElement/edit.xml", CreateDescriptionTest.class);
46  	}
47  	
48  	protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext)  {
49  		builderContext.registerBean("editDescriptionElementAction", editDescriptionElementAction);
50  		builderContext.registerBean("descriptionServiceImpl", descriptionService);
51  		builderContext.registerBean("termServiceImpl", termService);
52  		builderContext.registerBean("webRevisionService", webRevisionService);
53  		GenericConversionService genericConversionService = (GenericConversionService)builderContext.getConversionService();
54  	    genericConversionService.addConverter(new PropertyEditorConverter(new ClassPropertyEditor(), java.lang.Class.class));
55  
56      } 
57  	
58  	public void setUp() throws Exception {
59  		editDescriptionElementAction = new EditAction();
60  		descriptionService = EasyMock.createMock(IDescriptionService.class);
61  		termService = EasyMock.createMock(ITermService.class);
62  		webRevisionService = EasyMock.createMock(WebRevisionService.class);
63  		editDescriptionElementAction.setDescriptionService(descriptionService);
64  		editDescriptionElementAction.setTermService(termService);
65  		featureUuid = UUID.randomUUID();
66  		descriptionUuid = UUID.randomUUID();
67  		classes = new ArrayList<Class>();
68  		
69  		languageUuid = UUID.fromString("e7dda5c0-3e42-11de-8a39-0800200c9a66");
70  		language = Language.NewInstance(languageUuid);
71  		feature = Feature.NewInstance();
72  		feature.setSupportsCategoricalData(false);
73  		feature.setSupportsCommonTaxonName(false);
74  		feature.setSupportsDistribution(false);
75  		feature.setSupportsIndividualAssociation(false);
76  		feature.setSupportsQuantitativeData(false);
77  		feature.setSupportsTaxonInteraction(false);
78  		feature.setSupportsTextData(true);
79  		webRevision = new WebRevision();
80  		description = TaxonDescription.NewInstance();
81  		descriptionElement = TextData.NewInstance();
82  		descriptionElement.setFeature(feature);
83  		((TextData)descriptionElement).getMultilanguageText().put(language, LanguageString.NewInstance("", language));
84  	}
85  	
86  	private EditAction editDescriptionElementAction;
87  	private IDescriptionService descriptionService;
88  	private ITermService termService;
89  	private WebRevisionService webRevisionService;
90  	
91  	private UUID featureUuid;
92  	private UUID descriptionUuid;
93  	private List<Class> classes;
94  	private DescriptionBase description;
95  	private WebRevision webRevision;
96  	private DescriptionElementBase descriptionElement;
97  	private Feature feature;
98  	private Language language;
99  	private UUID languageUuid;
100 
101 	public void testStartFlow() {
102 		MutableAttributeMap attributeMap = new LocalAttributeMap();
103 		MockExternalContext context = new MockExternalContext();
104 		attributeMap.put("inDescription", descriptionUuid);
105 		attributeMap.put("feature", featureUuid);
106 
107 		EasyMock.expect(termService.find(EasyMock.eq(featureUuid))).andReturn(feature);
108 		EasyMock.expect(termService.load(EasyMock.eq(featureUuid))).andReturn(feature);
109 		EasyMock.expect(webRevisionService.getWebRevision()).andReturn(webRevision);
110 		
111 		EasyMock.replay(descriptionService,termService,webRevisionService);
112 		startFlow(attributeMap,context);
113 		EasyMock.verify(termService,webRevisionService);
114 
115 		assertCurrentStateEquals("edit");
116 		assertNotNull(this.getRequiredFlowAttribute("descriptionElement", TextData.class));
117 	}
118 	
119 
120 //	public void testSelectDescriptionElementType() {
121 //		MockParameterMap parameterMap = new MockParameterMap();
122 //		parameterMap.put("clazz", "eu.etaxonomy.cdm.model.description.TextData");
123 //		setCurrentState("create");
124 //		this.getFlowScope().put("inDescription", descriptionUuid);
125 //		this.getFlowScope().put("featureObj",feature);
126 //
127 //		
128 //		EasyMock.replay(descriptionService);
129 //		
130 //		
131 //		MockExternalContext context = new MockExternalContext();
132 //		context.setRequestParameterMap(parameterMap);
133 //		
134 //		context.setEventId("proceed");
135 //		
136 //		resumeFlow(context);
137 //		
138 //		EasyMock.verify(descriptionService);
139 //		
140 //		assertCurrentStateEquals("edit");	
141 //	}
142 	
143 	public void testEdit() {
144 		setCurrentState("edit");
145 		getFlowScope().put("descriptionElement", descriptionElement);
146 		
147 		MockExternalContext context = new MockExternalContext();
148 		context.setEventId("proceed");
149 		resumeFlow(context);
150 		
151 		assertCurrentStateEquals("review");
152 	}
153 	
154 	/**
155 	 * Test verifying that my understanding of spring binding, and particularly
156 	 * the use of custom property editors which enable objects to be used as map
157 	 * keys (essential here), is correct
158 	 */
159 	public void testBind() {
160 		MockParameterMap parameterMap = new MockParameterMap();
161 		parameterMap.put("uuid","09958800-3e1a-11de-8a39-0800200c9a66");
162 		parameterMap.put("feature.uuid","09958800-3e1a-11de-8a39-0800200c9a77");
163 		parameterMap.put("id", "2");
164 		parameterMap.put("multilanguageText['e7dda5c0-3e42-11de-8a39-0800200c9a66'].id", "4");
165 		parameterMap.put("multilanguageText['e7dda5c0-3e42-11de-8a39-0800200c9a66'].text", "Donec eget tortor lorem, at tempus purus. Nam eget arcu neque. Sed sed imperdiet elit. In cursus fermentum eros, a ultricies ipsum pellentesque nec.");
166 		DataBinder binder = new WebDataBinder(descriptionElement, "descriptionElement");
167 		binder.registerCustomEditor(Language.class, new DefinedTermPropertyEditor(Language.class,termService));
168 		binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
169 		EasyMock.expect(termService.find(EasyMock.eq(languageUuid))).andReturn(language).times(2);
170 		EasyMock.replay(termService);
171 		
172 		binder.bind(new MutablePropertyValues(parameterMap.asMap()));
173 //		if(binder.getBindingResult().hasErrors()) {
174 //			for(Object o : binder.getBindingResult().getAllErrors()) {
175 //				System.out.println(o);
176 //			}
177 //		} else {
178 		    assertFalse(binder.getBindingResult().hasErrors());
179 			EasyMock.verify(termService);
180 			assertEquals(((TextData)descriptionElement).getMultilanguageText().size(),1);
181 			assertEquals(((TextData)descriptionElement).getMultilanguageText().get(language).getId().longValue(),4l);
182 			assertEquals(((TextData)descriptionElement).getMultilanguageText().get(language).getText(),"Donec eget tortor lorem, at tempus purus. Nam eget arcu neque. Sed sed imperdiet elit. In cursus fermentum eros, a ultricies ipsum pellentesque nec.");
183 			assertEquals(descriptionElement.getId().longValue(),2l);
184 			assertEquals(descriptionElement.getUuid(),UUID.fromString("09958800-3e1a-11de-8a39-0800200c9a66"));
185 			assertEquals(descriptionElement.getFeature().getUuid(),UUID.fromString("09958800-3e1a-11de-8a39-0800200c9a77"));
186 //		}
187 	}
188 }