1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kcb.service.impl;
17
18 import java.util.Map;
19
20 import org.junit.Test;
21 import org.kuali.rice.kcb.bo.RecipientPreference;
22 import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
23 import org.kuali.rice.kcb.service.RecipientPreferenceService;
24 import org.kuali.rice.kcb.test.BusinessObjectTestCase;
25 import org.kuali.rice.kcb.test.TestData;
26 import org.springframework.dao.DataAccessException;
27 import org.springframework.dao.DataIntegrityViolationException;
28 import org.springframework.test.AssertThrows;
29
30
31
32
33
34
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
83 @Override
84 public void testDuplicateCreate() {
85 final RecipientPreference p = new RecipientPreference();
86 p.setId(TestData.FAKE_ID);
87 p.setRecipientId(PREF.getRecipientId());
88 p.setProperty(PREF.getProperty());
89 p.setValue(PREF.getValue());
90
91 new AssertThrows(DataIntegrityViolationException.class) {
92 public void test() throws Exception {
93 prefsvc.saveRecipientPreference(p);
94 }
95 }.runTest();
96 }
97
98 @Test
99 @Override
100 public void testInvalidCreate() {
101 final RecipientPreference p = new RecipientPreference();
102 new AssertThrows(DataIntegrityViolationException.class) {
103 public void test() throws Exception {
104 prefsvc.saveRecipientPreference(p);
105 }
106 }.runTest();
107 }
108
109 @Test
110 @Override
111 public void testInvalidDelete() {
112 final RecipientPreference p = new RecipientPreference();
113 p.setId(TestData.INVALID_ID);
114
115
116 new AssertThrows(DataAccessException.class) {
117 public void test() {
118 prefsvc.deleteRecipientPreference(p);
119 }
120 }.runTest();
121 }
122
123 @Test
124 @Override
125 public void testInvalidRead() {
126 RecipientPreference p = prefsvc.getRecipientPreference("nobody", "nuthin'");
127 assertNull(p);
128 }
129
130 @Override
131 public void testInvalidUpdate() {
132 RecipientPreference sample = new RecipientPreference();
133 sample.setRecipientId("user1");
134 sample.setProperty("uniqueproperty");
135 sample.setValue("value1");
136 prefsvc.saveRecipientPreference(sample);
137
138
139 final RecipientPreference p1 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
140 p1.setProperty(null);
141 new AssertThrows(DataAccessException.class) {
142 @Override
143 public void test() throws Exception {
144 prefsvc.saveRecipientPreference(p1);
145 }
146 }.runTest();
147
148
149 final RecipientPreference p2 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
150 p2.setProperty("uniqueproperty");
151 new AssertThrows(DataAccessException.class) {
152 @Override
153 public void test() throws Exception {
154 prefsvc.saveRecipientPreference(p2);
155 }
156 }.runTest();
157 }
158
159 @Override
160 public void testReadByQuery() {
161 RecipientPreference p = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
162 assertNotNull(p);
163 assertEquals(PREF, p);
164 }
165
166 @Override
167 public void testUpdate() {
168 RecipientPreference p = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
169 p.setValue("different value");
170
171 prefsvc.saveRecipientPreference(p);
172
173 RecipientPreference p2 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
174 assertEquals(p, p2);
175 }
176 }