View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.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   * Tests invoking the RuleValidationAttribute services.
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * Tests that the TestRuleValidationAttribute exposed by the exporter is invoked (via the resolver).
88       * This test assumes that the only rule validation attribute present is the TestRuleValidationAttribute.
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      * 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 }