001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.rule;
017    
018    import org.apache.commons.collections.CollectionUtils;
019    import org.apache.commons.collections.Predicate;
020    import org.junit.Assert;
021    import org.junit.Before;
022    import org.junit.Test;
023    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
024    import org.kuali.rice.core.api.resourceloader.ResourceLoader;
025    import org.kuali.rice.kew.api.KewApiConstants;
026    import org.kuali.rice.kew.api.validation.RuleValidationContext;
027    import org.kuali.rice.kew.framework.validation.RuleValidationAttributeExporterService;
028    import org.kuali.rice.kew.rule.bo.RuleAttribute;
029    import org.kuali.rice.kew.service.KEWServiceLocator;
030    import org.kuali.rice.kew.test.KEWTestCase;
031    import org.kuali.rice.kew.api.KewApiConstants;
032    import org.kuali.rice.kew.validation.RuleValidationAttributeResolver;
033    import org.kuali.rice.ksb.messaging.resourceloader.ServiceBusResourceLoader;
034    
035    import javax.xml.namespace.QName;
036    import java.util.Collection;
037    
038    import static org.junit.Assert.assertNotNull;
039    
040    /**
041     * Tests invoking the RuleValidationAttribute services.
042     * 
043     * @author Kuali Rice Team (rice.collab@kuali.org)
044     */
045    public class RuleValidationAttributeRemotingTest extends KEWTestCase {
046    
047        private static RuleValidationAttributeResolver resolver;
048        private static ServiceBusResourceLoader bus;
049        private static final QName EXPORTER_SOAP_SERVICE_NAME = new QName(KewApiConstants.Namespaces.KEW_NAMESPACE_2_0,  "ruleValidationAttributeExporterService");
050    
051        private static final ServiceBusResourceLoader getServiceBusResourceLoader(ResourceLoader loader) {
052            if (loader instanceof ServiceBusResourceLoader) {
053                return (ServiceBusResourceLoader) loader;
054            }
055            for (ResourceLoader rl: loader.getResourceLoaders()) {
056                if (rl instanceof ServiceBusResourceLoader) {
057                    return (ServiceBusResourceLoader) rl;
058                } else {
059                    ResourceLoader rl0 = getServiceBusResourceLoader(rl);
060                    if (rl0 != null) {
061                        return (ServiceBusResourceLoader) rl0;
062                    }
063                }
064            }
065            return null;
066        }
067    
068        @Before
069        public void obtainServices() {
070            resolver = KEWServiceLocator.getRuleValidationAttributeResolver();
071            bus = getServiceBusResourceLoader(GlobalResourceLoader.getResourceLoader());
072            Assert.assertNotNull("could not find service bus resource loader", bus);
073        }
074    
075        protected void loadTestData() throws Exception {
076            loadXmlFile("RuleTemplateAttributeTestConfig.xml");
077        }
078    
079        private static final Predicate RULE_VALIDATION_ATTRIB_PREDICATE = new Predicate() {
080            @Override
081            public boolean evaluate(Object attrib) {
082                return KewApiConstants.RULE_VALIDATION_ATTRIBUTE_TYPE.equals(((RuleAttribute) attrib).getType());
083            }
084        };
085    
086        /**
087         * Tests that the TestRuleValidationAttribute exposed by the exporter is invoked (via the resolver).
088         * This test assumes that the only rule validation attribute present is the TestRuleValidationAttribute.
089         */
090        @Test
091        public void test_resolver() throws Exception {
092            TestRuleValidationAttribute.invocations = 0;
093            int invocations = 0;
094            for (RuleAttribute attrib: (Collection<RuleAttribute>) CollectionUtils.select(KEWServiceLocator.getRuleAttributeService().findAll(), RULE_VALIDATION_ATTRIB_PREDICATE)) {
095                RuleValidationAttribute rva = resolver.resolveRuleValidationAttribute(attrib.getName(), null);
096                assertNotNull("RuleValidationAttribute resolution failed", rva);
097                RuleValidationContext ctx = RuleValidationContext.Builder.create(org.kuali.rice.kew.api.rule.Rule.Builder.create().build()).build();
098                assertNotNull("RuleValidationAttribute returned null result", rva.validate(ctx));
099                invocations++;
100            }
101            Assert.assertEquals("Actual TestRuleValidationAttribute invocations did not match expected invocations",
102                    invocations, TestRuleValidationAttribute.invocations);
103        }
104    
105        /**
106         * Tests invoking the exported SOAP RuleValidationAttributeExporterService.
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    }