View Javadoc

1   /*
2    * Copyright 2007 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.kns.datadictionary;
17  
18  import java.util.ArrayList;
19  
20  import org.apache.commons.beanutils.PropertyUtils;
21  import org.junit.After;
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.kuali.rice.kns.UserSession;
25  import org.kuali.rice.kns.bo.BusinessObject;
26  import org.kuali.rice.kns.bo.BusinessObjectRelationship;
27  import org.kuali.rice.kns.bo.PersistableBusinessObjectExtension;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.exception.ValidationException;
30  import org.kuali.rice.kns.lookup.LookupUtils;
31  import org.kuali.rice.kns.lookup.keyvalues.PersistableBusinessObjectValuesFinder;
32  import org.kuali.rice.kns.rule.event.RouteDocumentEvent;
33  import org.kuali.rice.kns.service.KNSServiceLocator;
34  import org.kuali.rice.kns.test.document.bo.Account;
35  import org.kuali.rice.kns.test.document.bo.AccountExtension;
36  import org.kuali.rice.kns.test.document.bo.AccountType;
37  import org.kuali.rice.kns.util.FieldUtils;
38  import org.kuali.rice.kns.util.GlobalVariables;
39  import org.kuali.rice.kns.util.ObjectUtils;
40  import org.kuali.rice.kns.web.ui.Field;
41  import org.kuali.test.KNSTestCase;
42  
43  public class ExtensionAttributeTest extends KNSTestCase {
44  
45  	DataDictionary dd = null;
46  
47  	@Before
48  	public void setUp() throws Exception {
49  		super.setUp();
50  
51  		dd = new DataDictionary();
52  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kns/bo/datadictionary");
53  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kns/document/datadictionary");
54  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kim/bo/datadictionary/EmploymentStatus.xml");
55  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kim/bo/datadictionary/EmploymentType.xml");
56  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kim/bo/datadictionary/PersonImpl.xml");
57  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kim/bo/datadictionary/KimBaseBeans.xml");
58  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kim/bo/datadictionary/GroupImpl.xml");
59  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kim/bo/datadictionary/RoleImpl.xml");
60  		dd.addConfigFileLocation("file:" + getBaseDir() + "/../impl/src/main/resources/org/kuali/rice/kim/bo/datadictionary/KimTypeImpl.xml");
61  		dd.addConfigFileLocation("classpath:org/kuali/rice/kns/test/document");
62          dd.parseDataDictionaryConfigurationFiles( false );
63  	}
64  
65  	@After
66  	public void tearDown() throws Exception {
67  		super.tearDown();
68  		dd = null;
69  	}
70  
71  	@Test
72  	public void testExtensionAttributeType() throws Exception {
73  		BusinessObjectEntry boe = dd.getBusinessObjectEntry( "Account" );
74  		assertNotNull( "BusinessObjectEntry for TravelAccount should not be null", boe );
75  		AttributeDefinition extAttrib = boe.getAttributeDefinition( "extension.accountTypeCode" );
76  		assertNotNull( "AttributeDefinition for 'extension.accountType' should not be null", extAttrib );
77  		assertEquals(PersistableBusinessObjectValuesFinder.class.getName(), extAttrib.getControl().getValuesFinderClass());
78  		assertEquals(AccountType.class.getName(), extAttrib.getControl().getBusinessObjectClass());
79  		assertEquals("accountTypeCode", extAttrib.getControl().getKeyAttribute());
80  		assertEquals("name", extAttrib.getControl().getLabelAttribute());
81  		assertEquals(true, extAttrib.getControl().getIncludeKeyInLabel());
82  		extAttrib = boe.getAttributeDefinition( "extension.accountType.codeAndDescription" );
83  		assertNotNull( "AttributeDefinition for 'extension.accountType.codeAndDescription' should not be null", extAttrib );
84  	}
85  
86  	@Test
87  	public void testObjectUtils_getPropertyType() throws Exception {
88  		Account ta = new Account();
89  	assertEquals("physical property type mismatch", PersistableBusinessObjectExtension.class, PropertyUtils
90  		.getPropertyType(ta, "extension"));
91  	assertEquals("DD property type mismatch", AccountExtension.class, ObjectUtils.getPropertyType(ta, "extension",
92  		KNSServiceLocator.getPersistenceStructureService()));
93  	assertEquals("extension.accountType attribute class mismatch", AccountType.class, ObjectUtils.getPropertyType(
94  		ta, "extension.accountType", KNSServiceLocator.getPersistenceStructureService()));
95  	assertEquals("extension.accountType.codeAndDescription attribute class mismatch", String.class, ObjectUtils
96  		.getPropertyType(ta, "extension.accountType.codeAndDescription", KNSServiceLocator
97  			.getPersistenceStructureService()));
98  	}
99  
100 	@Test
101 	public void testBOMetaDataService() throws Exception {
102 		Account ta = new Account();
103 	BusinessObjectRelationship br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship(
104 		ta, "extension.accountType");
105 		assertEquals( "mismatch on parent class", Account.class, br.getParentClass() );
106 		assertEquals( "mismatch on related class", AccountType.class, br.getRelatedClass() );
107 		System.out.println( br.getParentToChildReferences() );
108 	assertEquals("parent/child key not correct - should be extension.accountTypeCode/accountTypeCode",
109 		"accountTypeCode", br.getParentToChildReferences().get("extension.accountTypeCode"));
110 		br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship( ta, "extension" );
111 		assertNull( "extension is not lookupable, should have returned null", br );
112 	}
113 
114 	@Test
115 	public void testQuickFinder() throws Exception {
116 		Account ta = new Account();
117 		ArrayList<String> lookupFieldAttributeList = new ArrayList<String>();
118 		lookupFieldAttributeList.add( "extension.accountTypeCode");
119 
120         Field field = FieldUtils.getPropertyField(ta.getClass(), "extension.accountTypeCode", true);
121 
122 	field = LookupUtils.setFieldQuickfinder((BusinessObject) ta, "extension.accountTypeCode", field,
123 		lookupFieldAttributeList);
124 
125 		assertEquals( "lookup class not correct", AccountType.class.getName(), field.getQuickFinderClassNameImpl() );
126 	assertEquals("field lookup params not correct", "extension.accountTypeCode:accountTypeCode", field
127 		.getLookupParameters());
128 	assertEquals("lookup field conversions not correct", "accountTypeCode:extension.accountTypeCode", field
129 		.getFieldConversions());
130 	}
131 
132 	@Test
133 	public void testExistenceChecks() throws Exception {
134 		Account ta = new Account();
135 		((AccountExtension)ta.getExtension()).setAccountTypeCode( "XYZ" ); // invalid account type
136 		ta.setName( "Test Name" );
137 		ta.setNumber( "1234567" );
138         GlobalVariables.setUserSession(new UserSession("quickstart"));
139 	MaintenanceDocument document = (MaintenanceDocument) KNSServiceLocator.getDocumentService().getNewDocument(
140 		"AccountMaintenanceDocument");
141         assertNotNull( "new document must not be null", document );
142         document.getDocumentHeader().setDocumentDescription( getClass().getSimpleName() + "test" );
143         document.getOldMaintainableObject().setBusinessObject(null);
144         document.getOldMaintainableObject().setBoClass(ta.getClass());
145         document.getNewMaintainableObject().setBusinessObject(ta);
146         document.getNewMaintainableObject().setBoClass(ta.getClass());
147 
148         boolean failedAsExpected = false;
149         try {
150         	document.validateBusinessRules( new RouteDocumentEvent(document) );
151         } catch ( ValidationException expected ) {
152         	failedAsExpected = true;
153         }
154         assertTrue( "validation should have failed", failedAsExpected );
155         System.out.println( "document errors: " + GlobalVariables.getMessageMap() );
156         assertTrue( "there should be errors", GlobalVariables.getMessageMap().getErrorCount() > 0 );
157 	assertTrue("should be an error on the account type code", GlobalVariables.getMessageMap().containsKey(
158 		"document.newMaintainableObject.extension.accountTypeCode"));
159 	assertTrue("account type code should have an existence error", GlobalVariables.getMessageMap().fieldHasMessage(
160 		"document.newMaintainableObject.extension.accountTypeCode", "error.existence"));
161 	}
162 }