View Javadoc

1   /**
2    * Copyright 2005-2011 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.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("classpath:org/kuali/rice/krad/bo/datadictionary");
80          dd.addConfigFileLocation("KR-NS","file:" + getUserDir() + "/../../impl/src/main/resources/org/kuali/rice/krad/bo/datadictionary");
81  		dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/kns/bo/datadictionary/DataDictionaryBaseTypes.xml");
82  		dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/bo/datadictionary/EmploymentStatus.xml");
83  		dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/bo/datadictionary/EmploymentType.xml");
84          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/impl/identity/PersonImpl.xml");
85          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/bo/datadictionary/KimBaseBeans.xml");
86          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/impl/group/Group.xml");
87          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/impl/role/RoleBo.xml");
88          dd.addConfigFileLocation("KR-KIM","classpath:org/kuali/rice/kim/impl/type/KimType.xml");
89          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/campus/Campus.xml");
90          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/campus/CampusType.xml");
91          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/country/Country.xml");
92          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/state/State.xml");
93          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/county/County.xml");
94          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/location/web/postalcode/PostalCode.xml");
95          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/coreservice/web/parameter/Parameter.xml");
96          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/coreservice/web/parameter/ParameterType.xml");
97          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/coreservice/web/namespace/Namespace.xml");
98          dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/coreservice/web/component/Component.xml");
99  
100         dd.addConfigFileLocation("KR-NS","classpath:org/kuali/rice/krad/test/document");
101 
102         dd.parseDataDictionaryConfigurationFiles( false );
103 	}
104 
105 	@After
106 	public void tearDown() throws Exception {
107 		super.tearDown();
108 		dd = null;
109 	}
110 
111 	@Test
112     /**
113      * tests that the extension attribute type is present and has all the configured values
114      */
115 	public void testExtensionAttributeType() throws Exception {
116 		BusinessObjectEntry boe = dd.getBusinessObjectEntry( "Account" );
117 		assertNotNull( "BusinessObjectEntry for TravelAccount should not be null", boe );
118 		AttributeDefinition extAttrib = boe.getAttributeDefinition( "extension.accountTypeCode" );
119 		assertNotNull( "AttributeDefinition for 'extension.accountType' should not be null", extAttrib );
120 		assertEquals(PersistableBusinessObjectValuesFinder.class.getName(), extAttrib.getControl().getValuesFinderClass());
121 		assertEquals(AccountType.class.getName(), extAttrib.getControl().getBusinessObjectClass());
122 		assertEquals("accountTypeCode", extAttrib.getControl().getKeyAttribute());
123 		assertEquals("name", extAttrib.getControl().getLabelAttribute());
124 		assertEquals(true, extAttrib.getControl().getIncludeKeyInLabel());
125 		extAttrib = boe.getAttributeDefinition( "extension.accountType.codeAndDescription" );
126 		assertNotNull( "AttributeDefinition for 'extension.accountType.codeAndDescription' should not be null", extAttrib );
127 	}
128 
129 	@Test
130     /**
131      * tests that various properties of the business object extension are of the expected Java Class
132      */
133 	public void testObjectUtils_getPropertyType() throws Exception {
134 		Account ta = new Account();
135 	assertEquals("physical property type mismatch", PersistableBusinessObjectExtension.class, PropertyUtils
136 		.getPropertyType(ta, "extension"));
137 	assertEquals("DD property type mismatch", AccountExtension.class, ObjectUtils.getPropertyType(ta, "extension",
138 		KRADServiceLocator.getPersistenceStructureService()));
139 	assertEquals("extension.accountType attribute class mismatch", AccountType.class, ObjectUtils.getPropertyType(
140 		ta, "extension.accountType", KRADServiceLocator.getPersistenceStructureService()));
141 	assertEquals("extension.accountType.codeAndDescription attribute class mismatch", String.class, ObjectUtils
142 		.getPropertyType(ta, "extension.accountType.codeAndDescription", KRADServiceLocator
143 			.getPersistenceStructureService()));
144 	}
145 
146 	@Test
147     /**
148      * test that a business object relationship definitions have the expected values
149      */
150 	public void testBOMetaDataService() throws Exception {
151 		Account ta = new Account();
152 	DataObjectRelationship br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship(
153 		ta, "extension.accountType");
154 		assertEquals( "mismatch on parent class", Account.class, br.getParentClass() );
155 		assertEquals( "mismatch on related class", AccountType.class, br.getRelatedClass() );
156 		System.out.println( br.getParentToChildReferences() );
157 	assertEquals("parent/child key not correct - should be extension.accountTypeCode/accountTypeCode",
158 		"accountTypeCode", br.getParentToChildReferences().get("extension.accountTypeCode"));
159 		br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship( ta, "extension" );
160 		assertNull( "extension is not lookupable, should have returned null", br );
161 	}
162 
163 	@Test
164     /**
165      * tests that a quick finder, when set on an extension attribute, has the expected values
166      */
167 	public void testQuickFinder() throws Exception {
168 		Account ta = new Account();
169 		ArrayList<String> lookupFieldAttributeList = new ArrayList<String>();
170 		lookupFieldAttributeList.add( "extension.accountTypeCode");
171 
172         Field field = FieldUtils.getPropertyField(ta.getClass(), "extension.accountTypeCode", true);
173 
174 	field = LookupUtils.setFieldQuickfinder((BusinessObject) ta, "extension.accountTypeCode", field,
175 		lookupFieldAttributeList);
176 
177 		assertEquals( "lookup class not correct", AccountType.class.getName(), field.getQuickFinderClassNameImpl() );
178 	assertEquals("field lookup params not correct", "extension.accountTypeCode:accountTypeCode", field
179 		.getLookupParameters());
180 	assertEquals("lookup field conversions not correct", "accountTypeCode:extension.accountTypeCode", field
181 		.getFieldConversions());
182 	}
183 
184     /**
185      * tests validation on the extension attribute
186      *
187      * // TODO: test is currently failing because default existence checks are not implemented in KRAD
188      * // see https://jira.kuali.org/browse/KULRICE-7666
189      *
190      * <p>The values given for attributes that are foreign keys should represent existing objects when auto-update is set to false</p>
191      */
192 	public void testExistenceChecks() throws Exception {
193 		Account account = new Account();
194 		((AccountExtension)account.getExtension()).setAccountTypeCode( "XYZ" ); // invalid account type
195 		account.setName("Test Name");
196 		account.setNumber("1234567");
197         GlobalVariables.setUserSession(new UserSession("quickstart"));
198 	MaintenanceDocument document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(
199 		"AccountMaintenanceDocument");
200         assertNotNull( "new document must not be null", document );
201         document.getDocumentHeader().setDocumentDescription( getClass().getSimpleName() + "test" );
202         document.getOldMaintainableObject().setDataObject(null);
203         document.getOldMaintainableObject().setDataObjectClass(account.getClass());
204         document.getNewMaintainableObject().setDataObject(account);
205         document.getNewMaintainableObject().setDataObjectClass(account.getClass());
206 
207         boolean failedAsExpected = false;
208         try {
209         	document.validateBusinessRules( new RouteDocumentEvent(document) );
210         } catch ( ValidationException expected ) {
211         	failedAsExpected = true;
212         }
213         assertTrue( "validation should have failed", failedAsExpected );
214         System.out.println( "document errors: " + GlobalVariables.getMessageMap() );
215         assertTrue( "there should be errors", GlobalVariables.getMessageMap().getErrorCount() > 0 );
216 	assertTrue("should be an error on the account type code", GlobalVariables.getMessageMap().doesPropertyHaveError(
217 		"document.newMaintainableObject.dataObject.extension.accountTypeCode"));
218 	assertTrue("account type code should have an existence error", GlobalVariables.getMessageMap().fieldHasMessage(
219 		"document.newMaintainableObject.dataObject.extension.accountTypeCode", "error.existence"));
220 	}
221 }