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