]> git.argeo.org Git - gpl/argeo-slc.git/blob - DetachedTestDefinition.java
11ca2dbb3ae22654136676c14d89846f526558a4
[gpl/argeo-slc.git] / DetachedTestDefinition.java
1 package org.argeo.slc.lib.detached;
2
3 import java.util.Map;
4 import java.util.Properties;
5 import java.util.UUID;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.argeo.slc.SlcException;
10 import org.argeo.slc.core.structure.tree.TreeSRelatedHelper;
11 import org.argeo.slc.core.test.SimpleResultPart;
12 import org.argeo.slc.core.test.context.ContextUtils;
13 import org.argeo.slc.core.test.context.DefaultContextTestData;
14 import org.argeo.slc.detached.DetachedAnswer;
15 import org.argeo.slc.detached.DetachedClient;
16 import org.argeo.slc.detached.DetachedRequest;
17 import org.argeo.slc.detached.DetachedStep;
18 import org.argeo.slc.detached.ui.UiStep;
19 import org.argeo.slc.test.TestDefinition;
20 import org.argeo.slc.test.TestResult;
21 import org.argeo.slc.test.TestRun;
22 import org.argeo.slc.test.TestStatus;
23 import org.springframework.beans.BeansException;
24 import org.springframework.beans.PropertyValue;
25 import org.springframework.beans.factory.BeanFactory;
26 import org.springframework.beans.factory.BeanFactoryAware;
27 import org.springframework.beans.factory.BeanInitializationException;
28 import org.springframework.beans.factory.BeanNameAware;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.beans.factory.config.BeanDefinition;
31 import org.springframework.beans.factory.config.BeanReference;
32 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
33
34 public class DetachedTestDefinition extends TreeSRelatedHelper implements
35 TestDefinition, BeanNameAware, BeanFactoryAware, InitializingBean {
36 private final static Log log = LogFactory
37 .getLog(DetachedTestDefinition.class);
38
39 private DetachedStep step;
40 private DetachedClient client;
41
42 // Spring properties
43 private String testDefBeanName = null;
44 private BeanDefinitionRegistry beanDefinitionRegistry = null;
45 private String stepRef = null;
46
47 public void execute(TestRun testRun) {
48 // Look for step bean name
49 String stepBeanNameT = null;
50 if (this.stepRef == null && step != null) {
51 if (step instanceof UiStep) {
52 stepBeanNameT = ((UiStep) step).getBeanName();
53 } else {
54 // Introspects bean factory in order to find step bean name
55 BeanDefinition thisBeanDef = beanDefinitionRegistry
56 .getBeanDefinition(testDefBeanName);
57 PropertyValue propValue = thisBeanDef.getPropertyValues()
58 .getPropertyValue("step");
59 Object stepBeanRef = propValue.getValue();
60 BeanReference ref = (BeanReference) stepBeanRef;
61 stepBeanNameT = ref.getBeanName();
62 }
63 } else if (this.stepRef != null) {
64 stepBeanNameT = this.stepRef;
65 }
66
67 // Execute
68 DetachedRequest request = new DetachedRequest();
69 request.setPath(getBasePath().toString());
70 request.setUuid(UUID.randomUUID().toString());
71 request.setRef(stepBeanNameT);
72
73 DefaultContextTestData testData = testRun.getTestData();
74 if (testData != null) {
75 Map<String, Object> values = testData.getValues();
76 Properties inputParameters = new Properties();
77 inputParameters.putAll(values);// TODO: check conversions to string
78 request.setProperties(inputParameters);
79 }
80
81 try {
82 client.sendRequest(request);
83 } catch (Exception e) {
84 throw new SlcException("Could not send request for step "
85 + stepBeanNameT, e);
86 }
87
88 DetachedAnswer answer;
89 try {
90 answer = client.receiveAnswer();
91 } catch (Exception e) {
92 throw new SlcException("Could not receive answer #"
93 + request.getUuid() + " for step " + stepBeanNameT, e);
94 }
95
96 if (answer.getStatus() == DetachedAnswer.ERROR)
97 throw new SlcException("Error when executing step "
98 + answer.getUuid() + ": " + answer.getLog());
99 else
100 log.info("Received answer for '" + request.getRef() + "' ("
101 + answer.getStatusAsString() + "):" + answer.getLog());
102
103 if (testData != null) {
104 Properties outputParameters = answer.getProperties();
105 for (Object key : outputParameters.keySet())
106 testData.getValues().put(key.toString(),
107 outputParameters.get(key));
108 }
109
110
111 if (testData != null) {
112 ContextUtils.compareReachedExpected(testData, testRun
113 .getTestResult(), this);
114 } else {
115 ((TestResult)testRun.getTestResult()).addResultPart(
116 new SimpleResultPart(TestStatus.PASSED, "Step "
117 + stepBeanNameT + " executed successfully"));
118 }
119 }
120
121 public void setBeanName(String name) {
122 this.testDefBeanName = name;
123 }
124
125 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
126 if (beanFactory instanceof BeanDefinitionRegistry)
127 beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory;
128 else
129 throw new BeanInitializationException(
130 "Require BeanDefinitionRegistry");
131 }
132
133 public void afterPropertiesSet() throws Exception {
134 }
135
136 public void setStep(DetachedStep step) {
137 this.step = step;
138 }
139
140 public void setClient(DetachedClient client) {
141 this.client = client;
142 }
143
144 public void setStepRef(String stepBeanName) {
145 this.stepRef = stepBeanName;
146 }
147
148 }