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