View Javadoc

1   /**
2    * Copyright 2005-2013 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.krad.datadictionary;
17  
18  import org.apache.commons.beanutils.PropertyUtils;
19  import org.junit.After;
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.kuali.rice.kns.service.KNSServiceLocator;
23  import org.kuali.rice.kns.util.FieldUtils;
24  import org.kuali.rice.kns.web.ui.Field;
25  import org.kuali.rice.krad.UserSession;
26  import org.kuali.rice.krad.bo.BusinessObject;
27  import org.kuali.rice.krad.bo.DataObjectRelationship;
28  import org.kuali.rice.krad.bo.PersistableBusinessObjectExtension;
29  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
30  import org.kuali.rice.krad.exception.ValidationException;
31  import org.kuali.rice.krad.keyvalues.PersistableBusinessObjectValuesFinder;
32  import org.kuali.rice.kns.lookup.LookupUtils;
33  import org.kuali.rice.krad.rules.rule.event.RouteDocumentEvent;
34  import org.kuali.rice.krad.service.KRADServiceLocator;
35  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
36  import org.kuali.rice.krad.test.document.bo.Account;
37  import org.kuali.rice.krad.test.document.bo.AccountExtension;
38  import org.kuali.rice.krad.test.document.bo.AccountType;
39  import org.kuali.rice.krad.util.GlobalVariables;
40  import org.kuali.rice.krad.util.ObjectUtils;
41  import org.kuali.rice.krad.test.KRADTestCase;
42  
43  import java.util.ArrayList;
44  
45  import static org.junit.Assert.*;
46  
47  /**
48   * ExtensionAttributeTest tests that {@link org.kuali.rice.krad.bo.PersistableBusinessObject#getExtension()} works as expected
49   *
50   * <p>When running this test, the working directory should be set to two levels down from the root of the project e.g.
51   * it/krad</p>
52   *
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   */
55  public class ExtensionAttributeTest extends KRADTestCase {
56  
57  	DataDictionary dd = null;
58  
59  	@Before
60  	public void setUp() throws Exception {
61  		super.setUp();
62  
63  		dd = new DataDictionary();
64  		dd.addConfigFileLocation("KR-NS", "classpath:org/kuali/rice/krad/uif/UifControlDefinitions.xml");
65          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifFieldDefinitions.xml");
66          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifGroupDefinitions.xml");
67          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifHeaderFooterDefinitions.xml");
68          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifLayoutManagerDefinitions.xml");
69          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifViewPageDefinitions.xml");
70          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifWidgetDefinitions.xml");
71          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifConfigurationDefinitions.xml");
72          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifRiceDefinitions.xml");
73          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifLookupDefinitions.xml");
74          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifInquiryDefinitions.xml");
75          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifMaintenanceDefinitions.xml");
76          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifDocumentDefinitions.xml");
77          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifElementDefinitions.xml");
78          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/uif/UifActionDefinitions.xml");
79          dd.addConfigFileLocation("KR-NS",
80                  "classpath:org/kuali/rice/krad/datadictionary/DataDictionaryBaseTypes.xml");
81          dd.addConfigFileLocation("KR-NS",
82                  "classpath:org/kuali/rice/krad/datadictionary/AttributeReference.xml");
83  		dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/kns/bo/datadictionary/DataDictionaryBaseTypes.xml");
84  		dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/bo/datadictionary/EmploymentStatus.xml");
85  		dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/bo/datadictionary/EmploymentType.xml");
86          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/impl/identity/PersonImpl.xml");
87          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/bo/datadictionary/KimBaseBeans.xml");
88          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/impl/group/Group.xml");
89          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/impl/role/RoleBo.xml");
90          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/impl/type/KimType.xml");
91          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/campus/Campus.xml");
92          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/campus/CampusType.xml");
93          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/country/Country.xml");
94          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/state/State.xml");
95          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/county/County.xml");
96          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/postalcode/PostalCode.xml");
97          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/coreservice/web/parameter/Parameter.xml");
98          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/coreservice/web/parameter/ParameterType.xml");
99          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/coreservice/web/namespace/Namespace.xml");
100         dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/coreservice/web/component/Component.xml");
101 
102         dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/test/document");
103 
104         dd.parseDataDictionaryConfigurationFiles( false );
105 	}
106 
107 	@After
108 	public void tearDown() throws Exception {
109 		super.tearDown();
110 		dd = null;
111 	}
112 
113 	@Test
114     /**
115      * tests that the extension attribute type is present and has all the configured values
116      */
117 	public void testExtensionAttributeType() throws Exception {
118 		BusinessObjectEntry boe = dd.getBusinessObjectEntry( "Account" );
119 		assertNotNull( "BusinessObjectEntry for TravelAccount should not be null", boe );
120 		AttributeDefinition extAttrib = boe.getAttributeDefinition( "extension.accountTypeCode" );
121 		assertNotNull( "AttributeDefinition for 'extension.accountType' should not be null", extAttrib );
122 		assertEquals(PersistableBusinessObjectValuesFinder.class.getName(), extAttrib.getControl().getValuesFinderClass());
123 		assertEquals(AccountType.class.getName(), extAttrib.getControl().getBusinessObjectClass());
124 		assertEquals("accountTypeCode", extAttrib.getControl().getKeyAttribute());
125 		assertEquals("name", extAttrib.getControl().getLabelAttribute());
126 		assertEquals(true, extAttrib.getControl().getIncludeKeyInLabel());
127 		extAttrib = boe.getAttributeDefinition( "extension.accountType.codeAndDescription" );
128 		assertNotNull( "AttributeDefinition for 'extension.accountType.codeAndDescription' should not be null", extAttrib );
129 	}
130 
131 	@Test
132     /**
133      * tests that various properties of the business object extension are of the expected Java Class
134      */
135 	public void testObjectUtils_getPropertyType() throws Exception {
136 		Account ta = new Account();
137 	assertEquals("physical property type mismatch", PersistableBusinessObjectExtension.class, PropertyUtils
138 		.getPropertyType(ta, "extension"));
139 	assertEquals("DD property type mismatch", AccountExtension.class, ObjectUtils.getPropertyType(ta, "extension",
140 		KRADServiceLocator.getPersistenceStructureService()));
141 	assertEquals("extension.accountType attribute class mismatch", AccountType.class, ObjectUtils.getPropertyType(
142 		ta, "extension.accountType", KRADServiceLocator.getPersistenceStructureService()));
143 	assertEquals("extension.accountType.codeAndDescription attribute class mismatch", String.class, ObjectUtils
144 		.getPropertyType(ta, "extension.accountType.codeAndDescription", KRADServiceLocator
145 			.getPersistenceStructureService()));
146 	}
147 
148 	@Test
149     /**
150      * test that a business object relationship definitions have the expected values
151      */
152 	public void testBOMetaDataService() throws Exception {
153 		Account ta = new Account();
154 	DataObjectRelationship br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship(
155 		ta, "extension.accountType");
156 		assertEquals( "mismatch on parent class", Account.class, br.getParentClass() );
157 		assertEquals( "mismatch on related class", AccountType.class, br.getRelatedClass() );
158 		System.out.println( br.getParentToChildReferences() );
159 	assertEquals("parent/child key not correct - should be extension.accountTypeCode/accountTypeCode",
160 		"accountTypeCode", br.getParentToChildReferences().get("extension.accountTypeCode"));
161 		br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship( ta, "extension" );
162 		assertNull( "extension is not lookupable, should have returned null", br );
163 	}
164 
165 	@Test
166     /**
167      * tests that a quick finder, when set on an extension attribute, has the expected values
168      */
169 	public void testQuickFinder() throws Exception {
170 		Account ta = new Account();
171 		ArrayList<String> lookupFieldAttributeList = new ArrayList<String>();
172 		lookupFieldAttributeList.add( "extension.accountTypeCode");
173 
174         Field field = FieldUtils.getPropertyField(ta.getClass(), "extension.accountTypeCode", true);
175 
176 	field = LookupUtils.setFieldQuickfinder((BusinessObject) ta, "extension.accountTypeCode", field,
177 		lookupFieldAttributeList);
178 
179 		assertEquals( "lookup class not correct", AccountType.class.getName(), field.getQuickFinderClassNameImpl() );
180 	assertEquals("field lookup params not correct", "extension.accountTypeCode:accountTypeCode", field
181 		.getLookupParameters());
182 	assertEquals("lookup field conversions not correct", "accountTypeCode:extension.accountTypeCode", field
183 		.getFieldConversions());
184 	}
185 
186     /**
187      * tests validation on the extension attribute
188      *
189      * // TODO: test is currently failing because default existence checks are not implemented in KRAD
190      * // see https://jira.kuali.org/browse/KULRICE-7666
191      *
192      * <p>The values given for attributes that are foreign keys should represent existing objects when auto-update is set to false</p>
193      */
194 	public void testExistenceChecks() throws Exception {
195 		Account account = new Account();
196 		((AccountExtension)account.getExtension()).setAccountTypeCode( "XYZ" ); // invalid account type
197 		account.setName("Test Name");
198 		account.setNumber("1234567");
199         GlobalVariables.setUserSession(new UserSession("quickstart"));
200 	MaintenanceDocument document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(
201 		"AccountMaintenanceDocument");
202         assertNotNull( "new document must not be null", document );
203         document.getDocumentHeader().setDocumentDescription( getClass().getSimpleName() + "test" );
204         document.getOldMaintainableObject().setDataObject(null);
205         document.getOldMaintainableObject().setDataObjectClass(account.getClass());
206         document.getNewMaintainableObject().setDataObject(account);
207         document.getNewMaintainableObject().setDataObjectClass(account.getClass());
208 
209         boolean failedAsExpected = false;
210         try {
211         	document.validateBusinessRules( new RouteDocumentEvent(document) );
212         } catch ( ValidationException expected ) {
213         	failedAsExpected = true;
214         }
215         assertTrue( "validation should have failed", failedAsExpected );
216         System.out.println( "document errors: " + GlobalVariables.getMessageMap() );
217         assertTrue( "there should be errors", GlobalVariables.getMessageMap().getErrorCount() > 0 );
218 	assertTrue("should be an error on the account type code", GlobalVariables.getMessageMap().doesPropertyHaveError(
219 		"document.newMaintainableObject.dataObject.extension.accountTypeCode"));
220 	assertTrue("account type code should have an existence error", GlobalVariables.getMessageMap().fieldHasMessage(
221 		"document.newMaintainableObject.dataObject.extension.accountTypeCode", "error.existence"));
222 	}
223 }