View Javadoc

1   /*
2    * Copyright 2006-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.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.parameter.Parameter;
20  import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
21  import org.kuali.rice.core.impl.parameter.ParameterBo;
22  import org.kuali.rice.core.impl.parameter.ParameterId;
23  import org.kuali.rice.core.impl.parameter.ParameterTypeBo;
24  import org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager;
25  import org.kuali.rice.krad.test.document.bo.Account;
26  import org.kuali.rice.krad.test.document.bo.AccountExtension;
27  import org.kuali.rice.shareddata.impl.country.CountryBo;
28  import org.kuali.rice.shareddata.impl.state.StateBo;
29  import org.kuali.rice.shareddata.impl.state.StateId;
30  import org.kuali.test.KRADTestCase;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import static org.junit.Assert.*;
36  
37  /**
38   * Unit tests for MetadataManager methods
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   *
42   */
43  public class MetadataManagerTest extends KRADTestCase {
44  	/**
45  	 * Tests that MetadataManager can convert a primary key Map to a key object correctly
46  	 */
47  	@Test
48  	public void testPKMapToObject() {
49  		Map<String, Object> pkMap = new HashMap<String, Object>();
50  		Object pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
51  		assertNull("An empty map should return a null key", pkValue);
52  		
53  		pkMap.put("code", "AN");
54  		pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
55  		assertEquals("Single pkValue should be of class String", String.class, pkValue.getClass());
56  		assertEquals("Single pkValue should be \"AN\"", "AN", pkValue);
57  		
58  		pkMap.put("name", "ANDORRA");
59  		boolean exceptionThrown = false;
60  		try {
61  			pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
62  		} catch (IllegalArgumentException iae) {
63  			exceptionThrown = true;
64  		}
65  		assertTrue("Multiple keys did not lead to exception", exceptionThrown);
66  		
67  		pkMap.clear();
68  		
69  		pkMap.put("countryCode", "US");
70  		pkMap.put("code", "WV");
71  		pkValue = MetadataManager.convertPrimaryKeyMapToObject(StateBo.class, pkMap);
72  		org.junit.Assert.assertEquals("Composite pkValue for State should have class of StateId", StateId.class, pkValue.getClass());
73  		StateId stateId = (StateId)pkValue;
74  		assertEquals("Country code was not correctly set", "US", stateId.getCountryCode());
75  		assertEquals("State code was not correctly set", "WV", stateId.getCode());
76  		
77  		pkMap.put("name", "WEST VIRGINIA");
78  		exceptionThrown = false;
79  		try {
80  			pkValue = MetadataManager.convertPrimaryKeyMapToObject(StateBo.class, pkMap);
81  		} catch (Exception e) {
82  			exceptionThrown = true;
83  		}
84  		assertTrue("Non primary key field caused exception", exceptionThrown);
85  	}
86  	
87  	/**
88  	 * Tests that MetadataManager.getEntityPrimaryKeyObject correctly pulls primary keys
89  	 * from a BO
90  	 */
91  	@Test
92  	public void testPKObjectForEntity() {
93  		ParameterTypeBo parameterType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(ParameterTypeBo.class, "CONFG");
94  		assertNotNull("ParameterType should not be null", parameterType);
95  		
96  		Object pkValue = MetadataManager.getEntityPrimaryKeyObject(parameterType);
97  		assertEquals("Single pkValue should be of class String", String.class, pkValue.getClass());
98  		assertEquals("Single pkValue should be \"CONFG\"", "CONFG", pkValue);
99  		
100 		Parameter parameter = CoreFrameworkServiceLocator.getParameterService().getParameter("KR-NS", "Lookup", "MULTIPLE_VALUE_RESULTS_PER_PAGE");
101 		assertNotNull("State should not be null", parameter);
102 		
103 		pkValue = MetadataManager.getEntityPrimaryKeyObject(ParameterBo.from(parameter));
104 		org.junit.Assert.assertEquals("Composite pkValue for Parameter should have class of ParameterId", ParameterId.class, pkValue.getClass());
105 		ParameterId parameterId = (ParameterId)pkValue;
106 		assertEquals("namespace code was not correctly set", "KR-NS", parameterId.getNamespaceCode());
107 		assertEquals("parameter detail type code was not correctly set", "Lookup", parameterId.getComponentCode());
108 		assertEquals("parameter name was not correctly set", "MULTIPLE_VALUE_RESULTS_PER_PAGE", parameterId.getName());
109 		assertEquals("parameterApplicationNamespaceCode was not correctly set", "KUALI", parameterId.getApplicationId());
110 	}
111 	
112 	/**
113 	 * Tests that MetadataManager.getPersistableBusinessObjectPrimaryKeyObjectWithValuesForExtension works
114 	 */
115 	@Test
116 	public void testPKObjectForExtension() {
117 		final Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, "a1");
118 		assertNotNull("Account should not be null", account);
119 		final AccountExtension accountExtension = (AccountExtension)account.getExtension();
120 		
121 		final Object pkValue = MetadataManager.getPersistableBusinessObjectPrimaryKeyObjectWithValuesForExtension(account, accountExtension);
122 		assertEquals("Single pkValue should be of class String", String.class, pkValue.getClass());
123 		assertEquals("Single pkValue should be \"a1\"", "a1", pkValue);
124 	}
125 }