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