View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kcb.service.impl;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kcb.bo.RecipientPreference;
20  import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
21  import org.kuali.rice.kcb.service.RecipientPreferenceService;
22  import org.kuali.rice.kcb.test.BusinessObjectTestCase;
23  import org.kuali.rice.kcb.test.KCBTestData;
24  import org.springframework.dao.DataAccessException;
25  import org.springframework.dao.DataIntegrityViolationException;
26  
27  import java.util.Map;
28  
29  import static org.junit.Assert.*;
30  
31  /**
32   * Tests recipient preferences
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class RecipientPreferenceTest extends BusinessObjectTestCase {
37      private RecipientPreference PREF;
38      private RecipientPreferenceService prefsvc;
39  
40      @Override
41      public void setUp() throws Exception {
42          super.setUp();
43  
44          prefsvc = GlobalKCBServiceLocator.getInstance().getRecipientPreferenceService();
45  
46          PREF = new RecipientPreference();
47          PREF.setRecipientId("user1");
48          PREF.setProperty("property1");
49          PREF.setValue("value1");
50  
51          prefsvc.saveRecipientPreference(PREF);
52      }
53  
54      @Test
55      @Override
56      public void testCreate() {
57          RecipientPreference p2 = new RecipientPreference();
58          p2.setRecipientId("user1");
59          p2.setProperty("property2");
60          p2.setValue("value2");
61  
62          prefsvc.saveRecipientPreference(p2);
63          assertNotNull(p2.getId());
64  
65          Map<String, String> p = prefsvc.getRecipientPreferences("user1");
66          assertNotNull(p);
67          assertEquals(2, p.size());
68  
69          assertTrue(p.containsKey("property1"));
70          assertTrue(p.containsKey("property2"));
71      }
72  
73      @Test
74      @Override
75      public void testDelete() {
76          prefsvc.deleteRecipientPreference(PREF);
77  
78          assertNull(prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty()));
79          assertEquals(0, prefsvc.getRecipientPreferences(PREF.getRecipientId()).size());
80      }
81  
82      @Test(expected = DataIntegrityViolationException.class)
83      @Override
84      public void testDuplicateCreate() {
85          final RecipientPreference p = new RecipientPreference();
86          p.setId(KCBTestData.FAKE_ID);
87          p.setRecipientId(PREF.getRecipientId());
88          p.setProperty(PREF.getProperty());
89          p.setValue(PREF.getValue());
90  
91          prefsvc.saveRecipientPreference(p);
92      }
93  
94      @Test(expected = DataIntegrityViolationException.class)
95      @Override
96      public void testInvalidCreate() {
97          final RecipientPreference p = new RecipientPreference();
98          prefsvc.saveRecipientPreference(p);
99      }
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 }