001/**
002 * Copyright 2011-2013 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015package org.kuali.mobility.academics.dao;
016
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019import org.junit.AfterClass;
020import org.junit.Before;
021import org.junit.BeforeClass;
022import org.junit.Test;
023import org.junit.runner.RunWith;
024import org.kuali.mobility.academics.entity.GradesPostedNotice;
025import org.springframework.test.context.ContextConfiguration;
026import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
027
028import javax.annotation.Resource;
029import javax.persistence.EntityManager;
030import javax.persistence.PersistenceContext;
031import java.sql.Timestamp;
032import java.util.ArrayList;
033import java.util.Calendar;
034import java.util.List;
035
036import static org.junit.Assert.assertFalse;
037import static org.junit.Assert.assertTrue;
038
039/**
040 * @author Kuali Mobility Team (mobility.collab@kuali.org)
041 */
042@RunWith(SpringJUnit4ClassRunner.class)
043@ContextConfiguration(value = "classpath:TestSpringBeans.xml")
044public class GradesPostedNoticeDaoImplTest {
045    private static final Logger LOG = LoggerFactory.getLogger(GradesPostedNoticeDaoImplTest.class);
046
047        @PersistenceContext
048        private EntityManager entityManager;
049
050        @Resource(name="academicsGradesPostedNoticeDao")
051    private GradesPostedNoticeDao dao;
052
053        @BeforeClass
054        public static void setUpClass() throws Exception {
055        }
056
057        @AfterClass
058    public static void tearDownClass() throws Exception {
059    }
060
061    @Before
062    public void preTest() {
063    }
064
065        @Test
066        public void testSaveGradesPostedNotice() {
067                GradesPostedNotice notice = new GradesPostedNotice();
068                notice.setLoginName("mojojojo");
069                notice.setInProcess(false);
070                notice.setTimestampProcessed(null);
071                notice.setTimestampReceived(new Timestamp(Calendar.getInstance().getTimeInMillis()));
072
073                Long id = getDao().saveGradesPostedNotice(notice);
074                LOG.debug("ID for saved notice is "+id);
075                assertFalse("Failed to save notice.",id==null);
076                assertTrue("ID does not match that of the object.",id.compareTo(notice.getId()) == 0);
077
078                GradesPostedNotice notice2 = getDao().loadGradesPostedNotice(id);
079                assertFalse("Notice not found in database and should have been.",notice2==null);
080                assertFalse("Notice is in process and should not be.",notice2.isInProcess());
081                assertTrue("Notice has wrong login name.","mojojojo".equalsIgnoreCase(notice2.getLoginName()));
082
083                notice2.setInProcess(true);
084                Long id2 = getDao().saveGradesPostedNotice(notice2);
085
086                assertTrue("IDs do not match after update.",id.compareTo(id2)==0);
087        }
088
089        @Test
090        public void testCountUnsentNotices() {
091                Long count = getDao().countUnsentGradeNotices();
092                LOG.debug("Count of unsent messages is "+count.toString());
093                assertFalse("Count is null and should not be.",count==null);
094        }
095
096    /**
097     * Test of uploadGradesPostedNotice method, of class GradesPostedNoticeDaoImpl.
098     */
099    @Test
100    public void testUploadGradesPostedNotice() {
101        List<String> uniqnames = new ArrayList<String>();
102        uniqnames.add("akedar");
103        uniqnames.add("amarsh");
104        uniqnames.add("joe");
105        uniqnames.add("mark");
106
107        boolean result = getDao().uploadGradesPostedNotice(uniqnames);
108        assertTrue("Apply grades upload notices successfully.", result);
109
110            List<? extends GradesPostedNotice> notices = getDao().getGradesToProcess(true);
111                assertFalse("Dao returned null for getGradesToProcess:true.", notices == null);
112            assertFalse("Dao returned empty list for getGradesToProcess:true.", notices.isEmpty());
113            assertTrue(notices.size()+" notices found, should have found 6. see import.sql",notices.size()==6);
114
115
116    }
117
118        public EntityManager getEntityManager() {
119                return entityManager;
120        }
121
122        public void setEntityManager(EntityManager entityManager) {
123                this.entityManager = entityManager;
124        }
125
126        public GradesPostedNoticeDao getDao() {
127                return dao;
128        }
129
130        public void setDao(GradesPostedNoticeDao dao) {
131                this.dao = dao;
132        }
133
134}