1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.rule;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.collections.Predicate;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
24 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
25 import org.kuali.rice.kew.api.KewApiConstants;
26 import org.kuali.rice.kew.api.validation.RuleValidationContext;
27 import org.kuali.rice.kew.framework.validation.RuleValidationAttributeExporterService;
28 import org.kuali.rice.kew.rule.bo.RuleAttribute;
29 import org.kuali.rice.kew.service.KEWServiceLocator;
30 import org.kuali.rice.kew.test.KEWTestCase;
31 import org.kuali.rice.kew.api.KewApiConstants;
32 import org.kuali.rice.kew.validation.RuleValidationAttributeResolver;
33 import org.kuali.rice.ksb.messaging.resourceloader.ServiceBusResourceLoader;
34
35 import javax.xml.namespace.QName;
36 import java.util.Collection;
37
38 import static org.junit.Assert.assertNotNull;
39
40
41
42
43
44
45 public class RuleValidationAttributeRemotingTest extends KEWTestCase {
46
47 private static RuleValidationAttributeResolver resolver;
48 private static ServiceBusResourceLoader bus;
49 private static final QName EXPORTER_SOAP_SERVICE_NAME = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0, "ruleValidationAttributeExporterService");
50
51 private static final ServiceBusResourceLoader getServiceBusResourceLoader(ResourceLoader loader) {
52 if (loader instanceof ServiceBusResourceLoader) {
53 return (ServiceBusResourceLoader) loader;
54 }
55 for (ResourceLoader rl: loader.getResourceLoaders()) {
56 if (rl instanceof ServiceBusResourceLoader) {
57 return (ServiceBusResourceLoader) rl;
58 } else {
59 ResourceLoader rl0 = getServiceBusResourceLoader(rl);
60 if (rl0 != null) {
61 return (ServiceBusResourceLoader) rl0;
62 }
63 }
64 }
65 return null;
66 }
67
68 @Before
69 public void obtainServices() {
70 resolver = KEWServiceLocator.getRuleValidationAttributeResolver();
71 bus = getServiceBusResourceLoader(GlobalResourceLoader.getResourceLoader());
72 Assert.assertNotNull("could not find service bus resource loader", bus);
73 }
74
75 protected void loadTestData() throws Exception {
76 loadXmlFile("RuleTemplateAttributeTestConfig.xml");
77 }
78
79 private static final Predicate RULE_VALIDATION_ATTRIB_PREDICATE = new Predicate() {
80 @Override
81 public boolean evaluate(Object attrib) {
82 return KewApiConstants.RULE_VALIDATION_ATTRIBUTE_TYPE.equals(((RuleAttribute) attrib).getType());
83 }
84 };
85
86
87
88
89
90 @Test
91 public void test_resolver() throws Exception {
92 TestRuleValidationAttribute.invocations = 0;
93 int invocations = 0;
94 for (RuleAttribute attrib: (Collection<RuleAttribute>) CollectionUtils.select(KEWServiceLocator.getRuleAttributeService().findAll(), RULE_VALIDATION_ATTRIB_PREDICATE)) {
95 RuleValidationAttribute rva = resolver.resolveRuleValidationAttribute(attrib.getName(), null);
96 assertNotNull("RuleValidationAttribute resolution failed", rva);
97 RuleValidationContext ctx = RuleValidationContext.Builder.create(org.kuali.rice.kew.api.rule.Rule.Builder.create().build()).build();
98 assertNotNull("RuleValidationAttribute returned null result", rva.validate(ctx));
99 invocations++;
100 }
101 Assert.assertEquals("Actual TestRuleValidationAttribute invocations did not match expected invocations",
102 invocations, TestRuleValidationAttribute.invocations);
103 }
104
105
106
107
108 @Test
109 public void test_exporter() throws Exception {
110 RuleValidationAttributeExporterService exporter = (RuleValidationAttributeExporterService) bus.getService(EXPORTER_SOAP_SERVICE_NAME);
111 Assert.assertNotNull("Could not find the RuleValidationAttributeExporterService SOAP endpoint on the bus!", exporter);
112
113 TestRuleValidationAttribute.invocations = 0;
114 int invocations = 0;
115 for (RuleAttribute attrib: (Collection<RuleAttribute>) CollectionUtils.select(KEWServiceLocator.getRuleAttributeService().findAll(), RULE_VALIDATION_ATTRIB_PREDICATE)) {
116 RuleValidationContext ctx = RuleValidationContext.Builder.create(org.kuali.rice.kew.api.rule.Rule.Builder.create().build()).build();
117 assertNotNull("RuleValidationAttributeExporterService return value was null", exporter.validate(attrib.getName(), ctx));
118 invocations++;
119 }
120 Assert.assertEquals("Actual TestRuleValidationAttribute invocations did not match expected invocations",
121 invocations, TestRuleValidationAttribute.invocations);
122 }
123
124 }