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.ken.dao;
017    
018    import org.kuali.rice.ken.bo.NotificationChannelBo;
019    import org.kuali.rice.ken.bo.NotificationProducerBo;
020    import org.kuali.rice.ken.test.util.MockObjectsUtil;
021    import org.kuali.rice.ken.util.NotificationConstants;
022    
023    import java.util.HashMap;
024    
025    import static org.junit.Assert.assertEquals;
026    
027    
028    /**
029     * This class tests basic persistence for the NotificationProducer business object.
030     * 
031     * @author Kuali Rice Team (rice.collab@kuali.org)
032     */
033    public class NotificationProducerDaoTest extends BusinessObjectPersistenceTestCaseBase {
034        NotificationChannelBo mockChannel1 = MockObjectsUtil.getTestChannel1();
035        NotificationChannelBo mockChannel2 = MockObjectsUtil.getTestChannel2();
036        
037        NotificationProducerBo mockProducer1 = MockObjectsUtil.getTestProducer1();
038        
039        private String[] updatedDescriptions = {"Test 1 - updated description", "Test 2 - updated description"};
040        
041        /**
042         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#setup()
043         */
044        @Override
045        protected void setup() {
046            super.setup();
047            businessObjectDao.save(mockChannel1);
048            businessObjectDao.save(mockChannel2);
049        }
050        
051        /**
052         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#delete()
053         */
054        @Override
055        protected boolean delete() {
056            HashMap criteria = new HashMap();
057            
058            NotificationProducerBo producer4 = new NotificationProducerBo();
059            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockProducer1.getName());
060            producer4 = (NotificationProducerBo) businessObjectDao.findByUniqueKey(NotificationProducerBo.class, criteria);
061            
062            assertEquals(1, producer4.getChannels().size());
063            
064            criteria.clear();
065            NotificationProducerBo producer5 = new NotificationProducerBo();
066            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockProducer1.getName());
067            producer5 = (NotificationProducerBo) businessObjectDao.findByUniqueKey(NotificationProducerBo.class, criteria);
068                    
069            try {
070                businessObjectDao.delete(producer5);
071            } catch(Exception e) {
072                return false;
073            }
074            return true;
075        }
076        
077        /**
078         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#retrieve()
079         */
080        @Override
081        protected boolean retrieve() {
082            NotificationProducerBo producer2 = new NotificationProducerBo();
083            
084            HashMap criteria = new HashMap();
085            
086            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockProducer1.getName());
087            producer2 = (NotificationProducerBo) businessObjectDao.findByUniqueKey(NotificationProducerBo.class, criteria);
088            
089            boolean success = true;
090            
091            success &= producer2 != null;
092            success &= producer2.getDescription().equals(mockProducer1.getDescription());
093            success &= producer2.getChannels().size()==2;
094            
095            return success;
096        }
097        
098        /**
099         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#insert()
100         */
101        @Override
102        protected boolean insert() {
103            NotificationProducerBo producer1 = MockObjectsUtil.getTestProducer1();
104            
105            //set up the channels
106            producer1.getChannels().add(mockChannel1);
107            producer1.getChannels().add(mockChannel2);
108            
109            try {
110                businessObjectDao.save(producer1);
111            } catch(Exception e) {
112                return false;
113            }
114            return true;
115        }
116        
117        /**
118         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#update()
119         */
120        @Override
121        protected boolean update() {
122            NotificationProducerBo producer2 = new NotificationProducerBo();
123            
124            HashMap criteria = new HashMap();
125            
126            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockProducer1.getName());
127            producer2 = (NotificationProducerBo) businessObjectDao.findByUniqueKey(NotificationProducerBo.class, criteria);
128            
129            producer2.setDescription(updatedDescriptions[0]);
130            producer2.getChannels().remove(0);
131            
132            try {
133                businessObjectDao.save(producer2);
134            } catch(Exception e) {
135                return false;
136            }
137            return true;
138        }
139        
140        /**
141         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#validateChanges()
142         */
143        @Override
144        protected boolean validateChanges() {
145            NotificationProducerBo producer2 = new NotificationProducerBo();
146            
147            HashMap criteria = new HashMap();
148            
149            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockProducer1.getName());
150            producer2 = (NotificationProducerBo) businessObjectDao.findByUniqueKey(NotificationProducerBo.class, criteria);
151            
152            boolean success = true;
153            
154            success &= producer2.getDescription().equals(updatedDescriptions[0]);
155            success &= producer2.getChannels().size()==1;
156            
157            return success;
158        }
159    }