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.KCBTestCase;
23 import org.kuali.rice.kcb.test.KCBTestData;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.springframework.dao.DataAccessException;
26
27 import java.util.Map;
28
29 import static org.junit.Assert.*;
30
31
32
33
34
35
36 public class RecipientPreferenceTest extends KCBTestCase {
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 PREF = prefsvc.saveRecipientPreference(PREF);
52 }
53
54 @Test
55 public void testCreate() {
56 RecipientPreference p2 = new RecipientPreference();
57 p2.setRecipientId("user1");
58 p2.setProperty("property2");
59 p2.setValue("value2");
60
61 p2 = prefsvc.saveRecipientPreference(p2);
62 assertNotNull(p2.getId());
63
64 Map<String, String> p = prefsvc.getRecipientPreferences("user1");
65 assertNotNull(p);
66 assertEquals(2, p.size());
67
68 assertTrue(p.containsKey("property1"));
69 assertTrue(p.containsKey("property2"));
70 }
71
72 @Test
73 public void testDelete() {
74 prefsvc.deleteRecipientPreference(PREF);
75
76 assertNull(prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty()));
77 assertEquals(0, prefsvc.getRecipientPreferences(PREF.getRecipientId()).size());
78 }
79
80 @Test(expected = DataAccessException.class)
81 public void testDuplicateCreate() {
82 final RecipientPreference p = new RecipientPreference();
83 p.setId(KCBTestData.FAKE_ID);
84 p.setRecipientId(PREF.getRecipientId());
85 p.setProperty(PREF.getProperty());
86 p.setValue(PREF.getValue());
87
88 prefsvc.saveRecipientPreference(p);
89 KRADServiceLocator.getDataObjectService().flush(RecipientPreference.class);
90 }
91
92 @Test(expected = DataAccessException.class)
93 public void testInvalidCreate() {
94 final RecipientPreference p = new RecipientPreference();
95 prefsvc.saveRecipientPreference(p);
96 KRADServiceLocator.getDataObjectService().flush(RecipientPreference.class);
97 }
98
99 @Test
100 public void testInvalidRead() {
101 RecipientPreference p = prefsvc.getRecipientPreference("nobody", "nuthin'");
102 assertNull(p);
103 }
104
105 @Test(expected = DataAccessException.class)
106 public void testInvalidUpdate() {
107 RecipientPreference sample = new RecipientPreference();
108 sample.setRecipientId("user1");
109 sample.setProperty("uniqueproperty");
110 sample.setValue("value1");
111 prefsvc.saveRecipientPreference(sample);
112 KRADServiceLocator.getDataObjectService().flush(RecipientPreference.class);
113
114
115 final RecipientPreference p1 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
116 p1.setProperty(null);
117 prefsvc.saveRecipientPreference(p1);
118 KRADServiceLocator.getDataObjectService().flush(RecipientPreference.class);
119 }
120
121 @Test(expected = DataAccessException.class)
122 public void testInvalidUpdateUniqueConstraint() {
123 RecipientPreference sample = new RecipientPreference();
124 sample.setRecipientId("user1");
125 sample.setProperty("uniqueproperty");
126 sample.setValue("value1");
127 prefsvc.saveRecipientPreference(sample);
128
129 final RecipientPreference p2 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
130 p2.setProperty("uniqueproperty");
131 prefsvc.saveRecipientPreference(p2);
132 KRADServiceLocator.getDataObjectService().flush(RecipientPreference.class);
133 }
134
135
136 @Test
137 public void testReadByQuery() {
138 RecipientPreference p = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
139 assertNotNull(p);
140 assertEquals(PREF, p);
141 }
142
143 @Test
144 public void testUpdate() {
145 RecipientPreference p = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
146 p.setValue("different value");
147
148 p = prefsvc.saveRecipientPreference(p);
149
150 RecipientPreference p2 = prefsvc.getRecipientPreference(PREF.getRecipientId(), PREF.getProperty());
151 assertEquals(p, p2);
152 }
153 }