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.kcb.service.impl;
017    
018    import org.junit.Test;
019    import org.kuali.rice.kcb.bo.RecipientPreference;
020    import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
021    import org.kuali.rice.kcb.service.RecipientPreferenceService;
022    import org.kuali.rice.kcb.test.BusinessObjectTestCase;
023    import org.kuali.rice.kcb.test.KCBTestData;
024    import org.springframework.dao.DataAccessException;
025    import org.springframework.dao.DataIntegrityViolationException;
026    
027    import java.util.Map;
028    
029    import static org.junit.Assert.*;
030    
031    /**
032     * Tests recipient preferences
033     *
034     * @author Kuali Rice Team (rice.collab@kuali.org)
035     */
036    public class RecipientPreferenceTest extends BusinessObjectTestCase {
037        private RecipientPreference PREF;
038        private RecipientPreferenceService prefsvc;
039    
040        @Override
041        public void setUp() throws Exception {
042            super.setUp();
043    
044            prefsvc = GlobalKCBServiceLocator.getInstance().getRecipientPreferenceService();
045    
046            PREF = new RecipientPreference();
047            PREF.setRecipientId("user1");
048            PREF.setProperty("property1");
049            PREF.setValue("value1");
050    
051            prefsvc.saveRecipientPreference(PREF);
052        }
053    
054        @Test
055        @Override
056        public void testCreate() {
057            RecipientPreference p2 = new RecipientPreference();
058            p2.setRecipientId("user1");
059            p2.setProperty("property2");
060            p2.setValue("value2");
061    
062            prefsvc.saveRecipientPreference(p2);
063            assertNotNull(p2.getId());
064    
065            Map<String, String> p = prefsvc.getRecipientPreferences("user1");
066            assertNotNull(p);
067            assertEquals(2, p.size());
068    
069            assertTrue(p.containsKey("property1"));
070            assertTrue(p.containsKey("property2"));
071        }
072    
073        @Test
074        @Override
075        public void testDelete() {
076            prefsvc.deleteRecipientPreference(PREF);
077    
078            assertNull(prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty()));
079            assertEquals(0, prefsvc.getRecipientPreferences(PREF.getRecipientId()).size());
080        }
081    
082        @Test(expected = DataIntegrityViolationException.class)
083        @Override
084        public void testDuplicateCreate() {
085            final RecipientPreference p = new RecipientPreference();
086            p.setId(KCBTestData.FAKE_ID);
087            p.setRecipientId(PREF.getRecipientId());
088            p.setProperty(PREF.getProperty());
089            p.setValue(PREF.getValue());
090    
091            prefsvc.saveRecipientPreference(p);
092        }
093    
094        @Test(expected = DataIntegrityViolationException.class)
095        @Override
096        public void testInvalidCreate() {
097            final RecipientPreference p = new RecipientPreference();
098            prefsvc.saveRecipientPreference(p);
099        }
100    
101        @Test(expected = DataAccessException.class)
102        @Override
103        public void testInvalidDelete() {
104            final RecipientPreference p = new RecipientPreference();
105            p.setId(KCBTestData.INVALID_ID);
106            // OJB yields an org.springmodules.orm.ojb.OjbOperationException/OptimisticLockException and claims the object
107            // may have been deleted by somebody else
108            prefsvc.deleteRecipientPreference(p);
109        }
110    
111        @Test
112        @Override
113        public void testInvalidRead() {
114            RecipientPreference p = prefsvc.getRecipientPreference("nobody", "nuthin'");
115            assertNull(p);
116        }
117    
118        @Test(expected = DataAccessException.class)
119        @Override
120        public void testInvalidUpdate() {
121            RecipientPreference sample = new RecipientPreference();
122            sample.setRecipientId("user1");
123            sample.setProperty("uniqueproperty");
124            sample.setValue("value1");
125            prefsvc.saveRecipientPreference(sample);
126    
127            // violates not null property constraint
128            final RecipientPreference p1 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
129            p1.setProperty(null);
130            prefsvc.saveRecipientPreference(p1);
131        }
132    
133        @Test(expected = DataAccessException.class)
134        public void testInvalidUpdateUniqueConstraint() {
135            RecipientPreference sample = new RecipientPreference();
136            sample.setRecipientId("user1");
137            sample.setProperty("uniqueproperty");
138            sample.setValue("value1");
139            prefsvc.saveRecipientPreference(sample);
140    
141            final RecipientPreference p2 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
142            p2.setProperty("uniqueproperty");
143            prefsvc.saveRecipientPreference(p2);
144        }
145    
146    
147        @Test
148        @Override
149        public void testReadByQuery() {
150            RecipientPreference p = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
151            assertNotNull(p);
152            assertEquals(PREF, p);
153        }
154    
155        @Test
156        @Override
157        public void testUpdate() {
158            RecipientPreference p = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
159            p.setValue("different value");
160    
161            prefsvc.saveRecipientPreference(p);
162    
163            RecipientPreference p2 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
164            assertEquals(p, p2);
165        }
166    }