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.kns.document.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.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  public class ExtensionAttributeTest extends KRADTestCase {
48  
49  	DataDictionary dd = null;
50  
51  	@Before
52  	public void setUp() throws Exception {
53  		super.setUp();
54  
55  		dd = new DataDictionary();
56  		dd.addConfigFileLocation("classpath:org/kuali/rice/krad/uif/UifControlDefinitions.xml");
57          dd.addConfigFileLocation("classpath:org/kuali/rice/krad/uif/UifFieldDefinitions.xml");
58          dd.addConfigFileLocation("classpath:org/kuali/rice/krad/uif/UifGroupDefinitions.xml");
59          dd.addConfigFileLocation("classpath:org/kuali/rice/krad/uif/UifHeaderFooterDefinitions.xml");
60          dd.addConfigFileLocation("classpath:org/kuali/rice/krad/uif/UifLayoutManagerDefinitions.xml");
61          dd.addConfigFileLocation("classpath:org/kuali/rice/krad/uif/UifViewPageDefinitions.xml");
62          dd.addConfigFileLocation("classpath:org/kuali/rice/krad/uif/UifWidgetDefinitions.xml");
63          dd.addConfigFileLocation("file:" + getUserDir() + "/../../impl/src/main/resources/org/kuali/rice/krad/bo/datadictionary");
64  		dd.addConfigFileLocation("classpath:org/kuali/rice/kns/bo/datadictionary/DataDictionaryBaseTypes.xml");
65  		dd.addConfigFileLocation("classpath:org/kuali/rice/kim/bo/datadictionary/EmploymentStatus.xml");
66  		dd.addConfigFileLocation("classpath:org/kuali/rice/kim/bo/datadictionary/EmploymentType.xml");
67          dd.addConfigFileLocation("classpath:org/kuali/rice/kim/impl/identity/PersonImpl.xml");
68          dd.addConfigFileLocation("classpath:org/kuali/rice/kim/bo/datadictionary/KimBaseBeans.xml");
69          dd.addConfigFileLocation("classpath:org/kuali/rice/kim/impl/group/Group.xml");
70          dd.addConfigFileLocation("classpath:org/kuali/rice/kim/impl/role/RoleBo.xml");
71          dd.addConfigFileLocation("classpath:org/kuali/rice/kim/impl/type/KimType.xml");
72          dd.addConfigFileLocation("classpath:org/kuali/rice/location/web/campus/Campus.xml");
73          dd.addConfigFileLocation("classpath:org/kuali/rice/location/web/campus/CampusType.xml");
74          dd.addConfigFileLocation("classpath:org/kuali/rice/location/web/country/Country.xml");
75          dd.addConfigFileLocation("classpath:org/kuali/rice/location/web/state/State.xml");
76          dd.addConfigFileLocation("classpath:org/kuali/rice/location/web/county/County.xml");
77          dd.addConfigFileLocation("classpath:org/kuali/rice/location/web/postalcode/PostalCode.xml");
78          dd.addConfigFileLocation("classpath:org/kuali/rice/core/web/parameter/Parameter.xml");
79          dd.addConfigFileLocation("classpath:org/kuali/rice/core/web/parameter/ParameterType.xml");
80          dd.addConfigFileLocation("classpath:org/kuali/rice/core/web/namespace/Namespace.xml");
81          dd.addConfigFileLocation("classpath:org/kuali/rice/core/web/component/Component.xml");
82  
83          dd.addConfigFileLocation("classpath:org/kuali/rice/krad/test/document");
84  
85          dd.parseDataDictionaryConfigurationFiles( false );
86  	}
87  
88  	@After
89  	public void tearDown() throws Exception {
90  		super.tearDown();
91  		dd = null;
92  	}
93  
94  	@Test
95  	public void testExtensionAttributeType() throws Exception {
96  		BusinessObjectEntry boe = dd.getBusinessObjectEntry( "Account" );
97  		assertNotNull( "BusinessObjectEntry for TravelAccount should not be null", boe );
98  		AttributeDefinition extAttrib = boe.getAttributeDefinition( "extension.accountTypeCode" );
99  		assertNotNull( "AttributeDefinition for 'extension.accountType' should not be null", extAttrib );
100 		assertEquals(PersistableBusinessObjectValuesFinder.class.getName(), extAttrib.getControl().getValuesFinderClass());
101 		assertEquals(AccountType.class.getName(), extAttrib.getControl().getBusinessObjectClass());
102 		assertEquals("accountTypeCode", extAttrib.getControl().getKeyAttribute());
103 		assertEquals("name", extAttrib.getControl().getLabelAttribute());
104 		assertEquals(true, extAttrib.getControl().getIncludeKeyInLabel());
105 		extAttrib = boe.getAttributeDefinition( "extension.accountType.codeAndDescription" );
106 		assertNotNull( "AttributeDefinition for 'extension.accountType.codeAndDescription' should not be null", extAttrib );
107 	}
108 
109 	@Test
110 	public void testObjectUtils_getPropertyType() throws Exception {
111 		Account ta = new Account();
112 	assertEquals("physical property type mismatch", PersistableBusinessObjectExtension.class, PropertyUtils
113 		.getPropertyType(ta, "extension"));
114 	assertEquals("DD property type mismatch", AccountExtension.class, ObjectUtils.getPropertyType(ta, "extension",
115 		KRADServiceLocator.getPersistenceStructureService()));
116 	assertEquals("extension.accountType attribute class mismatch", AccountType.class, ObjectUtils.getPropertyType(
117 		ta, "extension.accountType", KRADServiceLocator.getPersistenceStructureService()));
118 	assertEquals("extension.accountType.codeAndDescription attribute class mismatch", String.class, ObjectUtils
119 		.getPropertyType(ta, "extension.accountType.codeAndDescription", KRADServiceLocator
120 			.getPersistenceStructureService()));
121 	}
122 
123 	@Test
124 	public void testBOMetaDataService() throws Exception {
125 		Account ta = new Account();
126 	DataObjectRelationship br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship(
127 		ta, "extension.accountType");
128 		assertEquals( "mismatch on parent class", Account.class, br.getParentClass() );
129 		assertEquals( "mismatch on related class", AccountType.class, br.getRelatedClass() );
130 		System.out.println( br.getParentToChildReferences() );
131 	assertEquals("parent/child key not correct - should be extension.accountTypeCode/accountTypeCode",
132 		"accountTypeCode", br.getParentToChildReferences().get("extension.accountTypeCode"));
133 		br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship( ta, "extension" );
134 		assertNull( "extension is not lookupable, should have returned null", br );
135 	}
136 
137 	@Test
138 	public void testQuickFinder() throws Exception {
139 		Account ta = new Account();
140 		ArrayList<String> lookupFieldAttributeList = new ArrayList<String>();
141 		lookupFieldAttributeList.add( "extension.accountTypeCode");
142 
143         Field field = FieldUtils.getPropertyField(ta.getClass(), "extension.accountTypeCode", true);
144 
145 	field = LookupUtils.setFieldQuickfinder((BusinessObject) ta, "extension.accountTypeCode", field,
146 		lookupFieldAttributeList);
147 
148 		assertEquals( "lookup class not correct", AccountType.class.getName(), field.getQuickFinderClassNameImpl() );
149 	assertEquals("field lookup params not correct", "extension.accountTypeCode:accountTypeCode", field
150 		.getLookupParameters());
151 	assertEquals("lookup field conversions not correct", "accountTypeCode:extension.accountTypeCode", field
152 		.getFieldConversions());
153 	}
154 
155 	@Test
156 	public void testExistenceChecks() throws Exception {
157 		Account ta = new Account();
158 		((AccountExtension)ta.getExtension()).setAccountTypeCode( "XYZ" ); // invalid account type
159 		ta.setName( "Test Name" );
160 		ta.setNumber( "1234567" );
161         GlobalVariables.setUserSession(new UserSession("quickstart"));
162 	MaintenanceDocument document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(
163 		"AccountMaintenanceDocument");
164         assertNotNull( "new document must not be null", document );
165         document.getDocumentHeader().setDocumentDescription( getClass().getSimpleName() + "test" );
166         document.getOldMaintainableObject().setBusinessObject(null);
167         document.getOldMaintainableObject().setBoClass(ta.getClass());
168         document.getNewMaintainableObject().setBusinessObject(ta);
169         document.getNewMaintainableObject().setBoClass(ta.getClass());
170 
171         boolean failedAsExpected = false;
172         try {
173         	document.validateBusinessRules( new RouteDocumentEvent(document) );
174         } catch ( ValidationException expected ) {
175         	failedAsExpected = true;
176         }
177         assertTrue( "validation should have failed", failedAsExpected );
178         System.out.println( "document errors: " + GlobalVariables.getMessageMap() );
179         assertTrue( "there should be errors", GlobalVariables.getMessageMap().getErrorCount() > 0 );
180 	assertTrue("should be an error on the account type code", GlobalVariables.getMessageMap().doesPropertyHaveError(
181 		"document.newMaintainableObject.extension.accountTypeCode"));
182 	assertTrue("account type code should have an existence error", GlobalVariables.getMessageMap().fieldHasMessage(
183 		"document.newMaintainableObject.extension.accountTypeCode", "error.existence"));
184 	}
185 }