001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.kcb.service.impl;
017
018import org.junit.Test;
019import org.kuali.rice.core.api.exception.RiceRuntimeException;
020import org.kuali.rice.kcb.bo.RecipientDelivererConfig;
021import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
022import org.kuali.rice.kcb.service.RecipientPreferenceService;
023import org.kuali.rice.kcb.test.KCBTestCase;
024import org.kuali.rice.krad.service.KRADServiceLocator;
025import org.kuali.rice.test.BaselineTestCase.BaselineMode;
026import org.kuali.rice.test.BaselineTestCase.Mode;
027import org.springframework.dao.DataAccessException;
028
029import java.util.Collection;
030
031import static org.junit.Assert.assertEquals;
032
033/**
034 * Tests persisting RecipientDelivererConfig 
035 * 
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038@BaselineMode(Mode.ROLLBACK_CLEAR_DB)
039public class RecipientDelivererConfigTest extends KCBTestCase {
040    //private RecipientDelivererConfig CFG;
041
042    private RecipientPreferenceService prefsvc;
043
044    @Override
045    public void setUp() throws Exception {
046        super.setUp();
047    
048        prefsvc = GlobalKCBServiceLocator.getInstance().getRecipientPreferenceService();
049
050//        CFG = new RecipientDelivererConfig();
051//        CFG.setRecipientId("user1");
052//        CFG.setDelivererName("mock");
053//        CFG.setChannel("channel1");
054
055        prefsvc.saveRecipientDelivererConfig("user1", "mock", new String[] { "channel1" });
056    }
057
058    @Test
059    public void testCreate() throws Exception {
060        prefsvc.saveRecipientDelivererConfig("user1", "mock", new String[] { "channel2" });
061        
062        Collection<RecipientDelivererConfig> deliverers = prefsvc.getDeliverersForRecipient("user1");
063        assertEquals(2, deliverers.size());
064    }
065
066    @Test
067    public void testDelete() throws Exception {
068        prefsvc.removeRecipientDelivererConfigs("user1");
069        
070        assertEquals(0, prefsvc.getDeliverersForRecipient("user1").size());
071    }
072
073    @Test(expected = RiceRuntimeException.class)
074    public void testDuplicateCreate() throws Exception {
075        // duplicate channel/deliverer entry
076        prefsvc.saveRecipientDelivererConfig("user1", "mock", new String[] { "channel1" });
077    }
078
079    @Test(expected = DataAccessException.class)
080    public void testInvalidUpdate() throws Exception {
081        // null channel
082        prefsvc.saveRecipientDelivererConfig("user1", null, new String[] { "channel2" });
083        KRADServiceLocator.getDataObjectService().flush(RecipientDelivererConfig.class);
084    }
085    
086    @Test
087    public void testGetDeliverersForRecipientAndChannel() {
088        Collection<RecipientDelivererConfig> cfgs = prefsvc.getDeliverersForRecipientAndChannel("user1", "channel1");
089        assertEquals(1, cfgs.size());
090        assertEquals("mock", cfgs.iterator().next().getDelivererName());
091    }
092}