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 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
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(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
107
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
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 }