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.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.location.impl.country.CountryBo;
28  import org.kuali.rice.location.impl.state.StateBo;
29  import org.kuali.rice.location.impl.state.StateId;
30  import org.kuali.rice.test.BaselineTestCase;
31  import org.kuali.rice.test.data.PerTestUnitTestData;
32  import org.kuali.rice.test.data.UnitTestData;
33  import org.kuali.rice.test.data.UnitTestFile;
34  import org.kuali.rice.test.data.UnitTestSql;
35  import org.kuali.test.KRADTestCase;
36  
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  import static org.junit.Assert.*;
42  
43  /**
44   * Unit tests for MetadataManager methods
45   * 
46   * @author Kuali Rice Team (rice.collab@kuali.org)
47   *
48   */
49  @PerTestUnitTestData(
50          value = @UnitTestData(
51                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
52                  sqlStatements = {
53                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
54                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
55                  },
56                  sqlFiles = {
57                          @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
58                          , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
59                  }
60          ),
61          tearDown = @UnitTestData(
62                  sqlStatements = {
63                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
64                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
65                  }
66         )
67  )
68  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
69  public class MetadataManagerTest extends KRADTestCase {
70  	/**
71  	 * Tests that MetadataManager can convert a primary key Map to a key object correctly
72  	 */
73  	@Test
74  	public void testPKMapToObject() {
75  		Map<String, Object> pkMap = new HashMap<String, Object>();
76  		Object pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
77  		assertNull("An empty map should return a null key", pkValue);
78  		
79  		pkMap.put("code", "AN");
80  		pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
81  		assertEquals("Single pkValue should be of class String", String.class, pkValue.getClass());
82  		assertEquals("Single pkValue should be \"AN\"", "AN", pkValue);
83  		
84  		pkMap.put("name", "ANDORRA");
85  		boolean exceptionThrown = false;
86  		try {
87  			pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
88  		} catch (IllegalArgumentException iae) {
89  			exceptionThrown = true;
90  		}
91  		assertTrue("Multiple keys did not lead to exception", exceptionThrown);
92  		
93  		pkMap.clear();
94  		
95  		pkMap.put("countryCode", "US");
96  		pkMap.put("code", "WV");
97  		pkValue = MetadataManager.convertPrimaryKeyMapToObject(StateBo.class, pkMap);
98  		org.junit.Assert.assertEquals("Composite pkValue for State should have class of StateId", StateId.class, pkValue.getClass());
99  		StateId stateId = (StateId)pkValue;
100 		assertEquals("Country code was not correctly set", "US", stateId.getCountryCode());
101 		assertEquals("State code was not correctly set", "WV", stateId.getCode());
102 		
103 		pkMap.put("name", "WEST VIRGINIA");
104 		exceptionThrown = false;
105 		try {
106 			pkValue = MetadataManager.convertPrimaryKeyMapToObject(StateBo.class, pkMap);
107 		} catch (Exception e) {
108 			exceptionThrown = true;
109 		}
110 		assertTrue("Non primary key field caused exception", exceptionThrown);
111 	}
112 	
113 	/**
114 	 * Tests that MetadataManager.getEntityPrimaryKeyObject correctly pulls primary keys
115 	 * from a BO
116 	 */
117 	@Test
118 	public void testPKObjectForEntity() {
119 		ParameterTypeBo parameterType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(ParameterTypeBo.class, "CONFG");
120 		assertNotNull("ParameterType should not be null", parameterType);
121 		
122 		Object pkValue = MetadataManager.getEntityPrimaryKeyObject(parameterType);
123 		assertEquals("Single pkValue should be of class String", String.class, pkValue.getClass());
124 		assertEquals("Single pkValue should be \"CONFG\"", "CONFG", pkValue);
125 		
126 		Parameter parameter = CoreFrameworkServiceLocator.getParameterService().getParameter("KR-NS", "Lookup", "MULTIPLE_VALUE_RESULTS_PER_PAGE");
127 		assertNotNull("State should not be null", parameter);
128 		
129 		pkValue = MetadataManager.getEntityPrimaryKeyObject(ParameterBo.from(parameter));
130 		org.junit.Assert.assertEquals("Composite pkValue for Parameter should have class of ParameterId", ParameterId.class, pkValue.getClass());
131 		ParameterId parameterId = (ParameterId)pkValue;
132 		assertEquals("namespace code was not correctly set", "KR-NS", parameterId.getNamespaceCode());
133 		assertEquals("parameter detail type code was not correctly set", "Lookup", parameterId.getComponentCode());
134 		assertEquals("parameter name was not correctly set", "MULTIPLE_VALUE_RESULTS_PER_PAGE", parameterId.getName());
135 		assertEquals("parameterApplicationNamespaceCode was not correctly set", "KUALI", parameterId.getApplicationId());
136 	}
137 	
138 	/**
139 	 * Tests that MetadataManager.getPersistableBusinessObjectPrimaryKeyObjectWithValuesForExtension works
140 	 */
141 	@Test
142 	public void testPKObjectForExtension() {
143 		final Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, "b101");
144 		assertNotNull("Account should not be null", account);
145 		final AccountExtension accountExtension = (AccountExtension)account.getExtension();
146 		
147 		final Object pkValue = MetadataManager.getPersistableBusinessObjectPrimaryKeyObjectWithValuesForExtension(account, accountExtension);
148 		assertEquals("Single pkValue should be of class String", String.class, pkValue.getClass());
149 		assertEquals("Single pkValue should be \"b101\"", "b101", pkValue);
150 	}
151 }