1 package org
.argeo
.slc
.lib
.detached
;
4 import java
.util
.Properties
;
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
;
33 public class DetachedTestDefinition
extends TreeSRelatedHelper
implements
34 TestDefinition
, BeanNameAware
, BeanFactoryAware
, InitializingBean
{
35 private final static Log log
= LogFactory
36 .getLog(DetachedTestDefinition
.class);
38 private DetachedStep step
;
39 private DetachedClient client
;
42 private String testDefBeanName
= null;
43 private BeanDefinitionRegistry beanDefinitionRegistry
= null;
44 private String stepRef
= null;
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();
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();
62 } else if (this.stepRef
!= null) {
63 stepBeanNameT
= this.stepRef
;
67 DetachedRequest request
= new DetachedRequest();
68 request
.setPath(getBasePath().toString());
69 request
.setUuid(UUID
.randomUUID().toString());
70 request
.setRef(stepBeanNameT
);
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
);
81 client
.sendRequest(request
);
82 } catch (Exception e
) {
83 throw new SlcException("Could not send request for step "
88 DetachedAnswer answer
= client
.receiveAnswer();
89 if (answer
.getStatus() == DetachedAnswer
.ERROR
)
90 throw new SlcException("Error when executing step "
91 + answer
.getUuid() + ": " + answer
.getLog());
93 log
.info("Received answer for '" + request
.getRef() + "' ("
94 + answer
.getStatusAsString() + "):" + answer
.getLog());
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
));
102 } catch (Exception e
) {
103 throw new SlcException("Could not receive answer #"
104 + request
.getUuid() + " for step " + stepBeanNameT
, e
);
107 if (testData
!= null) {
108 ContextUtils
.compareReachedExpected(testData
, testRun
109 .getTestResult(), this);
111 ((TestResult
)testRun
.getTestResult()).addResultPart(
112 new SimpleResultPart(TestStatus
.PASSED
, "Step "
113 + stepBeanNameT
+ " executed successfully"));
117 public void setBeanName(String name
) {
118 this.testDefBeanName
= name
;
121 public void setBeanFactory(BeanFactory beanFactory
) throws BeansException
{
122 if (beanFactory
instanceof BeanDefinitionRegistry
)
123 beanDefinitionRegistry
= (BeanDefinitionRegistry
) beanFactory
;
125 throw new BeanInitializationException(
126 "Require BeanDefinitionRegistry");
129 public void afterPropertiesSet() throws Exception
{
132 public void setStep(DetachedStep step
) {
136 public void setClient(DetachedClient client
) {
137 this.client
= client
;
140 public void setStepRef(String stepBeanName
) {
141 this.stepRef
= stepBeanName
;