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.NotificationChannel;
019    import org.kuali.rice.ken.bo.NotificationChannelReviewer;
020    import org.kuali.rice.ken.bo.NotificationProducer;
021    import org.kuali.rice.ken.test.util.MockObjectsUtil;
022    import org.kuali.rice.ken.util.NotificationConstants;
023    import org.kuali.rice.kim.api.KimConstants.KimGroupMemberTypes;
024    import org.kuali.rice.test.BaselineTestCase.BaselineMode;
025    import org.kuali.rice.test.BaselineTestCase.Mode;
026    
027    import java.util.HashMap;
028    
029    import static org.junit.Assert.assertEquals;
030    import static org.junit.Assert.assertNotNull;
031    
032    /**
033     * This class test basic persistence for the NotificationChannel business object.
034     *
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     */
037    @BaselineMode(Mode.CLEAR_DB) // this test can't run in a transaction because of how it is using ojb
038    public class NotificationChannelDaoTest extends BusinessObjectPersistenceTestCaseBase {
039        NotificationChannel channel1 = MockObjectsUtil.getTestChannel1();
040        NotificationChannel channel2 = MockObjectsUtil.getTestChannel2();
041    
042        NotificationProducer mockProducer1 = MockObjectsUtil.getTestProducer1();
043    
044        private String[] updatedDescriptions = {"Test 1 - updated description", "Test 2 - updated description"};
045        private boolean[] updatedSubscribables = {false, true};
046    
047        /**
048         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#setup()
049         */
050        @Override
051        protected void setup() {
052            super.setup();
053            businessObjectDao.save(mockProducer1);
054        }
055    
056        /**
057         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#delete()
058         */
059        @Override
060        protected boolean delete() {
061            NotificationChannel mockChannel1 = MockObjectsUtil.getTestChannel1();
062            NotificationChannel mockChannel2 = MockObjectsUtil.getTestChannel2();
063    
064            channel1 = new NotificationChannel();
065            channel2 = new NotificationChannel();
066    
067            HashMap criteria = new HashMap();
068    
069            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel1.getName());
070            channel1 = (NotificationChannel) (businessObjectDao.findMatching(NotificationChannel.class, criteria)).iterator().next();
071    
072            criteria.clear();
073    
074            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel2.getName());
075            channel2 = (NotificationChannel) (businessObjectDao.findMatching(NotificationChannel.class, criteria)).iterator().next();
076    
077            try {
078                businessObjectDao.delete(channel1);
079                businessObjectDao.delete(channel2);
080            } catch(Exception e) {
081                return false;
082            }
083            return true;
084        }
085    
086        /**
087         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#retrieve()
088         */
089        @Override
090        protected boolean retrieve() {
091            NotificationChannel mockChannel1 = MockObjectsUtil.getTestChannel1();
092            NotificationChannel mockChannel2 = MockObjectsUtil.getTestChannel2();
093    
094            channel1 = new NotificationChannel();
095            channel2 = new NotificationChannel();
096    
097            HashMap criteria = new HashMap();
098    
099            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel1.getName());
100            channel1 = (NotificationChannel) (businessObjectDao.findMatching(NotificationChannel.class, criteria)).iterator().next();
101    
102            criteria.clear();
103    
104            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel2.getName());
105            channel2 = (NotificationChannel) (businessObjectDao.findMatching(NotificationChannel.class, criteria)).iterator().next();
106    
107            boolean success = true;
108    
109            success &= channel1 != null;
110            success &= channel1.getDescription().equals(mockChannel1.getDescription());
111            success &= (channel1.isSubscribable()==mockChannel1.isSubscribable());
112            success &= channel1.getProducers().size() == 1;
113    
114            success &= channel2 != null;
115            success &= channel2.getDescription().equals(mockChannel2.getDescription());
116            success &= (channel2.isSubscribable()==mockChannel2.isSubscribable());
117            success &= channel2.getProducers().size() == 1;
118    
119            return success;
120        }
121    
122        /**
123         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#insert()
124         */
125        @Override
126        protected boolean insert() {
127            // add in a notification channel producer join object
128            try {
129                channel1.getProducers().add(mockProducer1);
130                businessObjectDao.save(channel1);
131    
132                // reload for collections
133                mockProducer1 = (NotificationProducer) businessObjectDao.findById(NotificationProducer.class, mockProducer1.getId());
134    
135                channel2.getProducers().add(mockProducer1);
136                businessObjectDao.save(channel2);
137                assertEquals(1, channel2.getProducers().size());
138    
139                mockProducer1 = (NotificationProducer) businessObjectDao.findById(NotificationProducer.class, mockProducer1.getId());
140                assertEquals(2, mockProducer1.getChannels().size());
141    
142                channel2 = (NotificationChannel) businessObjectDao.findById(NotificationChannel.class, channel2.getId());
143                assertEquals(1, channel2.getProducers().size());
144            } catch(Exception e) {
145                return false;
146            }
147            return true;
148        }
149    
150        /**
151         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#update()
152         */
153        @Override
154        protected boolean update() {
155            try {
156    
157                channel2 = (NotificationChannel) businessObjectDao.findById(NotificationChannel.class, channel2.getId());
158                assertEquals(1, channel2.getProducers().size());
159    
160                channel1.setDescription(updatedDescriptions[0]);
161                channel1.setSubscribable(updatedSubscribables[0]);
162                channel1.getProducers().clear();
163    
164                businessObjectDao.save(channel1);
165    
166                mockProducer1 = (NotificationProducer) businessObjectDao.findById(NotificationProducer.class, mockProducer1.getId());
167                assertNotNull(mockProducer1);
168                assertEquals(1, mockProducer1.getChannels().size());
169    
170                channel2 = (NotificationChannel) businessObjectDao.findById(NotificationChannel.class, channel2.getId());
171                assertEquals(1, channel2.getProducers().size());
172    
173                channel2.setDescription(updatedDescriptions[1]);
174                channel2.setSubscribable(updatedSubscribables[1]);
175                NotificationChannelReviewer reviewer = MockObjectsUtil.buildTestNotificationChannelReviewer(KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE, "aReviewer");
176                reviewer.setChannel(channel2);
177                channel2.getReviewers().add(reviewer);
178    
179                businessObjectDao.save(channel2);
180    
181            } catch(Exception e) {
182                return false;
183            }
184            return true;
185        }
186    
187        /**
188         * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#validateChanges()
189         */
190        @Override
191        protected boolean validateChanges() {
192            //retrieve fresh again
193            NotificationChannel mockChannel1 = MockObjectsUtil.getTestChannel1();
194            NotificationChannel mockChannel2 = MockObjectsUtil.getTestChannel2();
195    
196            channel1 = new NotificationChannel();
197            channel2 = new NotificationChannel();
198    
199            HashMap criteria = new HashMap();
200    
201            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel1.getName());
202            channel1 = (NotificationChannel) (businessObjectDao.findMatching(NotificationChannel.class, criteria)).iterator().next();
203    
204            criteria.clear();
205    
206            criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel2.getName());
207            channel2 = (NotificationChannel) (businessObjectDao.findMatching(NotificationChannel.class, criteria)).iterator().next();
208    
209            boolean success = true;
210    
211            success &= channel1.getDescription().equals(updatedDescriptions[0]);
212            success &= (channel1.isSubscribable()==updatedSubscribables[0]);
213            success &= channel1.getProducers().size() == 0;
214    
215            success &= channel2.getDescription().equals(updatedDescriptions[1]);
216            success &= (channel2.isSubscribable()==updatedSubscribables[1]);
217            success &= channel2.getProducers().size() == 1;
218            success &= channel2.getReviewers().size() == 1;
219    
220            return success;
221        }
222    }