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.krad.test.document.bo.Account;
20  import org.kuali.rice.krad.test.document.bo.AccountManager;
21  import org.kuali.test.KRADTestCase;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import static org.junit.Assert.*;
27  
28  /**
29   * This class tests KULRICE-1666: missing Spring mapping for ojbCollectionHelper
30   * (not injected into BusinessObjectDaoTest)
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  public class BusinessObjectServiceTest extends KRADTestCase {
36  
37      public BusinessObjectServiceTest() {}
38  
39      /**
40       * This method tests saving a BO with a collection member
41       *
42       * @throws Exception
43       */
44      @Test
45      public void testSave() throws Exception {
46          BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
47          
48          AccountManager am = new AccountManager();
49          am.setUserName("bhutchin");
50          List<Account> accounts = new ArrayList<Account>();
51          Account account1 = new Account();
52          account1.setNumber("1");
53          account1.setName("account 1");
54          account1.setAccountManager(am);
55          accounts.add(account1);
56  
57          Account account2 = new Account();
58          account2.setNumber("2");
59          account2.setName("account 2");
60          account2.setAccountManager(am);
61  
62          accounts.add(account2);
63          am.setAccounts(accounts);
64  
65          businessObjectService.save(am);
66      }
67      
68      /**
69       * Checks that a business object can be correctly retrieved through BusinessObjectService#retrieve
70       */
71      @Test
72      public void testRetrieve() {
73      	BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
74      	
75      	AccountManager manager = new AccountManager();
76      	manager.setUserName("mgorilla");
77      	List<Account> accounts = new ArrayList<Account>();
78      	Account account1 = new Account();
79      	account1.setNumber("MG1");
80      	account1.setName("Manilla Gorilla Account");
81      	account1.setAccountManager(manager);
82      	accounts.add(account1);
83      	manager.setAccounts(accounts);
84      	
85      	manager = (AccountManager)businessObjectService.save(manager);
86      	
87      	AccountManager manager2 = (AccountManager)businessObjectService.retrieve(manager);
88      	assertNotNull("manager2 should not be null", manager2);
89      	assertEquals("manager2 should have the same user name as manager", manager.getUserName(), manager2.getUserName());
90      	
91      	AccountManager manager3 = new AccountManager();
92      	manager3.setAmId(manager.getAmId());
93      	manager2 = (AccountManager)businessObjectService.retrieve(manager3);
94      	assertNotNull("manager2 should not be null", manager2);
95      	assertEquals("manager2 should have the same user name as manager", manager.getUserName(), manager2.getUserName());
96      	
97      	manager3.setAmId(-99L);
98      	manager2 = (AccountManager)businessObjectService.retrieve(manager3);
99      	assertNull("manager2 should be null", manager2);
100         
101         AccountManager manager4 = new AccountManager();
102         manager4.setAmId(manager.getAmId());
103         manager2 = (AccountManager)businessObjectService.findBySinglePrimaryKey(AccountManager.class, manager4.getAmId());
104         assertNotNull("manager2 should not be null", manager2);
105         assertEquals("manager2 should have the same user name as manager", manager.getUserName(), manager2.getUserName());
106 
107     }
108 
109     /*
110     @Test
111     public void testFindMatchingByCriteria() {
112     	final BusinessObjectService boService = KRADServiceLocatorInternal.getBusinessObjectService();
113     	
114     	final Collection<? extends State> allStates = boService.findAll(StateImpl.class);
115     	
116     	org.kuali.rice.core.jpa.criteria.Criteria criteria = new org.kuali.rice.core.jpa.criteria.Criteria(StateImpl.class.getName());
117     	criteria.eq("code", "US");
118     	Collection<State> states = boService.findMatching(cr fiteria);
119     	Assert.assertEquals("There should be "+allStates.size()+" states with country code US", allStates.size(), states.size());
120     	
121     	criteria = new org.kuali.rice.core.jpa.criteria.Criteria(StateImpl.class.getName());
122     	criteria.eq("postalStateCode", "AZ");
123     	states = boService.findMatching(criteria);
124     	Assert.assertEquals("There should be 1 state with state code AZ", 1, states.size());
125     	
126     	criteria = new org.kuali.rice.core.jpa.criteria.Criteria(StateImpl.class.getName());
127     	criteria.eq("postalStateCode", "MZ");
128     	states = boService.findMatching(criteria);
129     	Assert.assertEquals("There should not be any states with state code MZ", 0, states.size());
130     }
131     */
132 }