View Javadoc
1   /**
2    * Copyright 2005-2014 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 static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.ArrayList;
24  
25  import org.apache.commons.beanutils.PropertyUtils;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.kuali.rice.kns.lookup.LookupUtils;
30  import org.kuali.rice.kns.service.KNSServiceLocator;
31  import org.kuali.rice.kns.util.FieldUtils;
32  import org.kuali.rice.kns.web.ui.Field;
33  import org.kuali.rice.krad.UserSession;
34  import org.kuali.rice.krad.bo.DataObjectRelationship;
35  import org.kuali.rice.krad.bo.PersistableBusinessObjectExtension;
36  import org.kuali.rice.krad.exception.ValidationException;
37  import org.kuali.rice.krad.keyvalues.PersistableBusinessObjectValuesFinder;
38  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
39  import org.kuali.rice.krad.rules.rule.event.RouteDocumentEvent;
40  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
41  import org.kuali.rice.krad.test.KRADTestCase;
42  import org.kuali.rice.krad.test.TestDictionaryConfig;
43  import org.kuali.rice.krad.test.document.bo.Account;
44  import org.kuali.rice.krad.test.document.bo.AccountExtension;
45  import org.kuali.rice.krad.test.document.bo.AccountType;
46  import org.kuali.rice.krad.util.GlobalVariables;
47  import org.kuali.rice.krad.util.ObjectUtils;
48  
49  /**
50   * ExtensionAttributeTest tests that {@link org.kuali.rice.krad.bo.PersistableBusinessObject#getExtension()} works as expected
51   *
52   * <p>When running this test, the working directory should be set to two levels down from the root of the project e.g.
53   * it/krad</p>
54   *
55   * @author Kuali Rice Team (rice.collab@kuali.org)
56   */
57  @TestDictionaryConfig(namespaceCode="KR-NS",dataDictionaryFiles="classpath:org/kuali/rice/krad/test/document")
58  public class ExtensionAttributeTest extends KRADTestCase {
59  
60  	@Test
61      /**
62       * tests that the extension attribute type is present and has all the configured values
63       */
64  	public void testExtensionAttributeType() throws Exception {
65  		BusinessObjectEntry boe = dd.getBusinessObjectEntry( "Account" );
66  		assertNotNull( "BusinessObjectEntry for TravelAccount should not be null", boe );
67  		AttributeDefinition extAttrib = boe.getAttributeDefinition( "extension.accountTypeCode" );
68  		assertNotNull( "AttributeDefinition for 'extension.accountType' should not be null", extAttrib );
69  		assertEquals(PersistableBusinessObjectValuesFinder.class.getName(), extAttrib.getControl().getValuesFinderClass());
70  		assertEquals(AccountType.class.getName(), extAttrib.getControl().getBusinessObjectClass());
71  		assertEquals("accountTypeCode", extAttrib.getControl().getKeyAttribute());
72  		assertEquals("name", extAttrib.getControl().getLabelAttribute());
73  		assertEquals(true, extAttrib.getControl().getIncludeKeyInLabel());
74  		extAttrib = boe.getAttributeDefinition( "extension.accountType.codeAndDescription" );
75  		assertNotNull( "AttributeDefinition for 'extension.accountType.codeAndDescription' should not be null", extAttrib );
76  	}
77  
78  	@Test
79      /**
80       * tests that various properties of the business object extension are of the expected Java Class
81       */
82  	public void testObjectUtils_getPropertyType() throws Exception {
83  		Account ta = new Account();
84  	assertEquals("physical property type mismatch", PersistableBusinessObjectExtension.class, PropertyUtils
85  		.getPropertyType(ta, "extension"));
86  	assertEquals("DD property type mismatch", AccountExtension.class, ObjectUtils.getPropertyType(ta, "extension",
87              KNSServiceLocator.getPersistenceStructureService()));
88  	assertEquals("extension.accountType attribute class mismatch", AccountType.class, ObjectUtils.getPropertyType(
89  		ta, "extension.accountType", KNSServiceLocator.getPersistenceStructureService()));
90  	assertEquals("extension.accountType.codeAndDescription attribute class mismatch", String.class, ObjectUtils
91  		.getPropertyType(ta, "extension.accountType.codeAndDescription", KNSServiceLocator
92  			.getPersistenceStructureService()));
93  	}
94  
95  	@Test
96  	@Legacy
97      /**
98       * test that a business object relationship definitions have the expected values
99       */
100 	public void testBOMetaDataService() throws Exception {
101 		Account ta = new Account();
102 	DataObjectRelationship br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship(
103 		ta, "extension.accountType");
104 		assertEquals( "mismatch on parent class", Account.class, br.getParentClass() );
105 		assertEquals( "mismatch on related class", AccountType.class, br.getRelatedClass() );
106 		System.out.println( br.getParentToChildReferences() );
107 	assertEquals("parent/child key not correct - should be extension.accountTypeCode/accountTypeCode",
108 		"accountTypeCode", br.getParentToChildReferences().get("extension.accountTypeCode"));
109 		br = KNSServiceLocator.getBusinessObjectMetaDataService().getBusinessObjectRelationship( ta, "extension" );
110 		assertNull( "extension is not lookupable, should have returned null", br );
111 	}
112 
113 	@Test
114     /**
115      * tests that a quick finder, when set on an extension attribute, has the expected values
116      */
117 	public void testQuickFinder() throws Exception {
118 		Account ta = new Account();
119 		ArrayList<String> lookupFieldAttributeList = new ArrayList<String>();
120 		lookupFieldAttributeList.add( "extension.accountTypeCode");
121 
122         Field field = FieldUtils.getPropertyField(ta.getClass(), "extension.accountTypeCode", true);
123 
124 	field = LookupUtils.setFieldQuickfinder(ta, "extension.accountTypeCode", field,
125 		lookupFieldAttributeList);
126 
127 		assertEquals( "lookup class not correct", AccountType.class.getName(), field.getQuickFinderClassNameImpl() );
128 	assertEquals("field lookup params not correct", "extension.accountTypeCode:accountTypeCode", field
129 		.getLookupParameters());
130 	assertEquals("lookup field conversions not correct", "accountTypeCode:extension.accountTypeCode", field
131 		.getFieldConversions());
132 	}
133 
134     @Test
135     /**
136      * tests validation on the extension attribute
137      *
138      * <p>The values given for attributes that are foreign keys should represent existing objects when auto-update is set to false</p>
139      */
140     public void testExistenceChecks() throws Exception {
141 		Account account = new Account();
142 		((AccountExtension)account.getExtension()).setAccountTypeCode( "XYZ" ); // invalid account type
143 		account.setName("Test Name");
144 		account.setNumber("1234567");
145         GlobalVariables.setUserSession(new UserSession("quickstart"));
146 	MaintenanceDocument document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument(
147 		"AccountMaintenanceDocument");
148         assertNotNull( "new document must not be null", document );
149         document.getDocumentHeader().setDocumentDescription( getClass().getSimpleName() + "test" );
150         document.getOldMaintainableObject().setDataObject(null);
151         document.getOldMaintainableObject().setDataObjectClass(account.getClass());
152         document.getNewMaintainableObject().setDataObject(account);
153         document.getNewMaintainableObject().setDataObjectClass(account.getClass());
154 
155         boolean failedAsExpected = false;
156         try {
157         	document.validateBusinessRules( new RouteDocumentEvent(document) );
158         } catch ( ValidationException expected ) {
159         	failedAsExpected = true;
160         }
161         assertTrue( "validation should have failed", failedAsExpected );
162         System.out.println( "document errors: " + GlobalVariables.getMessageMap() );
163         assertTrue( "there should be errors", GlobalVariables.getMessageMap().getErrorCount() > 0 );
164 	assertTrue("should be an error on the account type code", GlobalVariables.getMessageMap().doesPropertyHaveError(
165 		"document.newMaintainableObject.dataObject.extension.accountTypeCode"));
166 	assertTrue("account type code should have an existence error", GlobalVariables.getMessageMap().fieldHasMessage(
167 		"document.newMaintainableObject.dataObject.extension.accountTypeCode", "error.existence"));
168 	}
169 }