001    /**
002     * Copyright 2005-2013 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.core;
017    
018    import org.apache.ojb.broker.query.Criteria;
019    import org.apache.ojb.broker.query.Query;
020    import org.apache.ojb.broker.query.QueryFactory;
021    import org.junit.Test;
022    import org.kuali.rice.ken.bo.NotificationBo;
023    import org.kuali.rice.ken.bo.NotificationChannelBo;
024    import org.kuali.rice.ken.bo.NotificationProducerBo;
025    import org.kuali.rice.ken.dao.BusinessObjectDaoTestCaseBase;
026    import org.kuali.rice.ken.test.util.MockObjectsUtil;
027    import org.kuali.rice.ken.util.NotificationConstants;
028    import org.kuali.rice.test.BaselineTestCase.BaselineMode;
029    import org.kuali.rice.test.BaselineTestCase.Mode;
030    
031    import java.sql.Timestamp;
032    
033    import static org.junit.Assert.assertEquals;
034    
035    //import org.apache.ojb.broker.query.Criteria;
036    //import org.kuali.rice.core.jpa.criteria.Criteria;
037    //import org.kuali.rice.core.jpa.criteria.QueryByCriteria;
038    
039    /**
040     * Scratch test case for testing aspects of OJB under test harness
041     * @author Kuali Rice Team (rice.collab@kuali.org)
042     */
043    @BaselineMode(Mode.CLEAR_DB)
044    public class TestOJBTest extends BusinessObjectDaoTestCaseBase {
045        /**
046         * Just prints the SQL generated by a query criteria 
047         */
048            /*
049            @Ignore
050        @Test
051        public void testJPACriteria() {
052            Criteria criteria_STATUS = new Criteria(Notification.class.getName());
053            criteria_STATUS.eq(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED);
054    
055            Criteria criteria_UNDELIVERED = new Criteria(Notification.class.getName());
056            criteria_UNDELIVERED.eq(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED);
057    
058            // now OR the above two together
059            criteria_STATUS.or(criteria_UNDELIVERED);
060    
061            Criteria criteria_NOTLOCKED = new Criteria(Notification.class.getName());
062            criteria_NOTLOCKED.isNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE);
063    
064            Criteria fullQueryCriteria = new Criteria(Notification.class.getName());
065            fullQueryCriteria.and(criteria_NOTLOCKED);
066            fullQueryCriteria.lte(NotificationConstants.BO_PROPERTY_NAMES.NOTIFICATION_AUTO_REMOVE_DATE_TIME, new Timestamp(System.currentTimeMillis()));
067            // now add in the STATUS check
068            fullQueryCriteria.and(criteria_STATUS);
069    
070    
071            System.err.println(fullQueryCriteria.toString());
072    
073            
074            QueryByCriteria q = new QueryByCriteria(KRADServiceLocatorInternal.getApplicationEntityManagerFactory().createEntityManager(), fullQueryCriteria);
075           
076            System.err.println(q.toString());
077    
078        }
079            */
080            @Test
081        public void testCriteria() {
082            Criteria criteria_STATUS = new Criteria();
083            criteria_STATUS.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED);
084    
085            Criteria criteria_UNDELIVERED = new Criteria();
086            criteria_UNDELIVERED.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED);
087    
088            // now OR the above two together
089            criteria_STATUS.addOrCriteria(criteria_UNDELIVERED);
090    
091            Criteria criteria_NOTLOCKED = new Criteria();
092            criteria_NOTLOCKED.addIsNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE);
093    
094            Criteria fullQueryCriteria = new Criteria();
095            fullQueryCriteria.addAndCriteria(criteria_NOTLOCKED);
096            fullQueryCriteria.addLessOrEqualThan(NotificationConstants.BO_PROPERTY_NAMES.NOTIFICATION_AUTO_REMOVE_DATE_TIME, new Timestamp(System.currentTimeMillis()));
097            // now add in the STATUS check
098            fullQueryCriteria.addAndCriteria(criteria_STATUS);
099    
100    
101            System.err.println(fullQueryCriteria.toString());
102    
103            Query q = QueryFactory.newQuery(NotificationBo.class, fullQueryCriteria);
104            System.err.println(q.toString());
105    
106        }
107        @Test
108        public void testUpdateRelationships() {
109            NotificationChannelBo channel1 = MockObjectsUtil.getTestChannel1();
110            NotificationChannelBo channel2 = MockObjectsUtil.getTestChannel2();
111            NotificationProducerBo mockProducer1 = MockObjectsUtil.getTestProducer1();
112    
113            businessObjectDao.save(mockProducer1);
114            assertEquals(0, mockProducer1.getChannels().size());
115    
116            // add in a notification channel producer join object
117            channel1.getProducers().add(mockProducer1);
118    
119            businessObjectDao.save(channel1);
120    
121            assertEquals(1, channel1.getProducers().size());
122    
123            // ojb doesn't update the collections of the child in the relationship on save, despite auto-update...
124            // so I'm forced to load it again
125            mockProducer1 = (NotificationProducerBo) businessObjectDao.findById(NotificationProducerBo.class, mockProducer1.getId());
126            assertEquals(1, mockProducer1.getChannels().size());
127    
128            channel2.getProducers().add(mockProducer1);     
129            businessObjectDao.save(channel2);
130    
131            assertEquals(1, channel2.getProducers().size());
132    
133            mockProducer1 = (NotificationProducerBo) businessObjectDao.findById(NotificationProducerBo.class, mockProducer1.getId());
134    
135            assertEquals(2, mockProducer1.getChannels().size());
136        }
137    }