001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.service;
017    
018    import org.junit.Test;
019    import org.kuali.rice.coreservice.api.parameter.Parameter;
020    import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
021    import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
022    import org.kuali.rice.coreservice.impl.parameter.ParameterId;
023    import org.kuali.rice.coreservice.impl.parameter.ParameterTypeBo;
024    import org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager;
025    import org.kuali.rice.krad.test.document.bo.Account;
026    import org.kuali.rice.krad.test.document.bo.AccountExtension;
027    import org.kuali.rice.location.impl.country.CountryBo;
028    import org.kuali.rice.location.impl.state.StateBo;
029    import org.kuali.rice.location.impl.state.StateId;
030    import org.kuali.rice.test.BaselineTestCase;
031    import org.kuali.rice.test.data.PerTestUnitTestData;
032    import org.kuali.rice.test.data.UnitTestData;
033    import org.kuali.rice.test.data.UnitTestFile;
034    import org.kuali.rice.test.data.UnitTestSql;
035    import org.kuali.test.KRADTestCase;
036    
037    import java.util.HashMap;
038    import java.util.Map;
039    
040    import static org.junit.Assert.*;
041    
042    /**
043     * Unit tests for MetadataManager methods
044     * 
045     * @author Kuali Rice Team (rice.collab@kuali.org)
046     *
047     */
048    @PerTestUnitTestData(
049            value = @UnitTestData(
050                    order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
051                    sqlStatements = {
052                            @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
053                            ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
054                    },
055                    sqlFiles = {
056                            @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
057                            , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
058                    }
059            ),
060            tearDown = @UnitTestData(
061                    sqlStatements = {
062                            @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
063                            ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
064                    }
065           )
066    )
067    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
068    public class MetadataManagerTest extends KRADTestCase {
069            /**
070             * Tests that MetadataManager can convert a primary key Map to a key object correctly
071             */
072            @Test
073            public void testPKMapToObject() {
074                    Map<String, Object> pkMap = new HashMap<String, Object>();
075                    Object pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
076                    assertNull("An empty map should return a null key", pkValue);
077                    
078                    pkMap.put("code", "AN");
079                    pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
080                    assertEquals("Single pkValue should be of class String", String.class, pkValue.getClass());
081                    assertEquals("Single pkValue should be \"AN\"", "AN", pkValue);
082                    
083                    pkMap.put("name", "ANDORRA");
084                    boolean exceptionThrown = false;
085                    try {
086                            pkValue = MetadataManager.convertPrimaryKeyMapToObject(CountryBo.class, pkMap);
087                    } catch (IllegalArgumentException iae) {
088                            exceptionThrown = true;
089                    }
090                    assertTrue("Multiple keys did not lead to exception", exceptionThrown);
091                    
092                    pkMap.clear();
093                    
094                    pkMap.put("countryCode", "US");
095                    pkMap.put("code", "WV");
096                    pkValue = MetadataManager.convertPrimaryKeyMapToObject(StateBo.class, pkMap);
097                    org.junit.Assert.assertEquals("Composite pkValue for State should have class of StateId", StateId.class, pkValue.getClass());
098                    StateId stateId = (StateId)pkValue;
099                    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             * Tests that MetadataManager.getEntityPrimaryKeyObject correctly pulls primary keys
114             * from a BO
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             * Tests that MetadataManager.getPersistableBusinessObjectPrimaryKeyObjectWithValuesForExtension works
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    }