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
.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
;
34 public class DetachedTestDefinition
extends TreeSRelatedHelper
implements
35 TestDefinition
, BeanNameAware
, BeanFactoryAware
, InitializingBean
{
36 private final static Log log
= LogFactory
37 .getLog(DetachedTestDefinition
.class);
39 private DetachedStep step
;
40 private DetachedClient client
;
43 private String testDefBeanName
= null;
44 private BeanDefinitionRegistry beanDefinitionRegistry
= null;
45 private String stepRef
= null;
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();
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();
63 } else if (this.stepRef
!= null) {
64 stepBeanNameT
= this.stepRef
;
68 DetachedRequest request
= new DetachedRequest();
69 request
.setPath(getBasePath().toString());
70 request
.setUuid(UUID
.randomUUID().toString());
71 request
.setRef(stepBeanNameT
);
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
);
82 client
.sendRequest(request
);
83 } catch (Exception e
) {
84 throw new SlcException("Could not send request for step "
88 DetachedAnswer answer
;
90 answer
= client
.receiveAnswer();
91 } catch (Exception e
) {
92 throw new SlcException("Could not receive answer #"
93 + request
.getUuid() + " for step " + stepBeanNameT
, e
);
96 if (answer
.getStatus() == DetachedAnswer
.ERROR
)
97 throw new SlcException("Error when executing step "
98 + answer
.getUuid() + ": " + answer
.getLog());
100 log
.info("Received answer for '" + request
.getRef() + "' ("
101 + answer
.getStatusAsString() + "):" + answer
.getLog());
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
));
111 if (testData
!= null) {
112 ContextUtils
.compareReachedExpected(testData
, testRun
113 .getTestResult(), this);
115 ((TestResult
)testRun
.getTestResult()).addResultPart(
116 new SimpleResultPart(TestStatus
.PASSED
, "Step "
117 + stepBeanNameT
+ " executed successfully"));
121 public void setBeanName(String name
) {
122 this.testDefBeanName
= name
;
125 public void setBeanFactory(BeanFactory beanFactory
) throws BeansException
{
126 if (beanFactory
instanceof BeanDefinitionRegistry
)
127 beanDefinitionRegistry
= (BeanDefinitionRegistry
) beanFactory
;
129 throw new BeanInitializationException(
130 "Require BeanDefinitionRegistry");
133 public void afterPropertiesSet() throws Exception
{
136 public void setStep(DetachedStep step
) {
140 public void setClient(DetachedClient client
) {
141 this.client
= client
;
144 public void setStepRef(String stepBeanName
) {
145 this.stepRef
= stepBeanName
;