]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestDefinition.java
Remove deprecated APIs
[gpl/argeo-slc.git] / runtime / org.argeo.slc.lib.detached / src / main / java / org / argeo / slc / lib / detached / DetachedTestDefinition.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.lib.detached;
17
18 import java.util.Map;
19 import java.util.Properties;
20 import java.util.UUID;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.slc.SlcException;
25 import org.argeo.slc.core.test.SimpleResultPart;
26 import org.argeo.slc.core.test.context.ContextUtils;
27 import org.argeo.slc.core.test.context.DefaultContextTestData;
28 import org.argeo.slc.detached.DetachedAnswer;
29 import org.argeo.slc.detached.DetachedClient;
30 import org.argeo.slc.detached.DetachedRequest;
31 import org.argeo.slc.detached.DetachedStep;
32 import org.argeo.slc.detached.ui.UiStep;
33 import org.argeo.slc.test.TestDefinition;
34 import org.argeo.slc.test.TestResult;
35 import org.argeo.slc.test.TestRun;
36 import org.argeo.slc.test.TestStatus;
37 import org.springframework.beans.BeansException;
38 import org.springframework.beans.PropertyValue;
39 import org.springframework.beans.factory.BeanFactory;
40 import org.springframework.beans.factory.BeanFactoryAware;
41 import org.springframework.beans.factory.BeanInitializationException;
42 import org.springframework.beans.factory.BeanNameAware;
43 import org.springframework.beans.factory.InitializingBean;
44 import org.springframework.beans.factory.config.BeanDefinition;
45 import org.springframework.beans.factory.config.BeanReference;
46 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
47
48 public class DetachedTestDefinition implements TestDefinition, BeanNameAware,
49 BeanFactoryAware, InitializingBean {
50 private final static Log log = LogFactory
51 .getLog(DetachedTestDefinition.class);
52
53 private DetachedStep step;
54 private DetachedClient client;
55
56 // Spring properties
57 private String testDefBeanName = null;
58 private BeanDefinitionRegistry beanDefinitionRegistry = null;
59 private String stepRef = null;
60
61 public void execute(TestRun testRun) {
62 // Look for step bean name
63 String stepBeanNameT = null;
64 if (this.stepRef == null && step != null) {
65 if (step instanceof UiStep) {
66 stepBeanNameT = ((UiStep) step).getBeanName();
67 } else {
68 // Introspects bean factory in order to find step bean name
69 BeanDefinition thisBeanDef = beanDefinitionRegistry
70 .getBeanDefinition(testDefBeanName);
71 PropertyValue propValue = thisBeanDef.getPropertyValues()
72 .getPropertyValue("step");
73 Object stepBeanRef = propValue.getValue();
74 BeanReference ref = (BeanReference) stepBeanRef;
75 stepBeanNameT = ref.getBeanName();
76 }
77 } else if (this.stepRef != null) {
78 stepBeanNameT = this.stepRef;
79 }
80
81 // Execute
82 DetachedRequest request = new DetachedRequest();
83 request.setUuid(UUID.randomUUID().toString());
84 request.setRef(stepBeanNameT);
85
86 DefaultContextTestData testData = testRun.getTestData();
87 if (testData != null) {
88 Map<String, Object> values = testData.getValues();
89 Properties inputParameters = new Properties();
90 inputParameters.putAll(values);// TODO: check conversions to string
91 request.setProperties(inputParameters);
92 }
93
94 try {
95 client.sendRequest(request);
96 } catch (Exception e) {
97 throw new SlcException("Could not send request for step "
98 + stepBeanNameT, e);
99 }
100
101 DetachedAnswer answer;
102 try {
103 answer = client.receiveAnswer();
104 } catch (Exception e) {
105 throw new SlcException("Could not receive answer #"
106 + request.getUuid() + " for step " + stepBeanNameT, e);
107 }
108
109 if (answer.getStatus() == DetachedAnswer.ERROR)
110 throw new SlcException("Error when executing step "
111 + answer.getUuid() + ": " + answer.getLog());
112 else
113 log.info("Received answer for '" + request.getRef() + "' ("
114 + answer.getStatusAsString() + "):" + answer.getLog());
115
116 if (testData != null) {
117 Properties outputParameters = answer.getProperties();
118 for (Object key : outputParameters.keySet())
119 testData.getValues().put(key.toString(),
120 outputParameters.get(key));
121 }
122
123 if (testData != null) {
124 ContextUtils.compareReachedExpected(testData,
125 testRun.getTestResult());
126 } else {
127 ((TestResult) testRun.getTestResult())
128 .addResultPart(new SimpleResultPart(TestStatus.PASSED,
129 "Step " + stepBeanNameT + " executed successfully"));
130 }
131 }
132
133 public void setBeanName(String name) {
134 this.testDefBeanName = name;
135 }
136
137 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
138 if (beanFactory instanceof BeanDefinitionRegistry)
139 beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory;
140 else
141 throw new BeanInitializationException(
142 "Require BeanDefinitionRegistry");
143 }
144
145 public void afterPropertiesSet() throws Exception {
146 }
147
148 public void setStep(DetachedStep step) {
149 this.step = step;
150 }
151
152 public void setClient(DetachedClient client) {
153 this.client = client;
154 }
155
156 public void setStepRef(String stepBeanName) {
157 this.stepRef = stepBeanName;
158 }
159
160 }