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