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.Message;
20 import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
21 import org.kuali.rice.kcb.service.MessageService;
22 import org.kuali.rice.kcb.test.KCBTestCase;
23 import org.kuali.rice.kcb.test.KCBTestData;
24 import org.kuali.rice.krad.data.PersistenceOption;
25 import org.kuali.rice.krad.service.KRADServiceLocator;
26 import org.springframework.dao.DataAccessException;
27
28 import java.util.Collection;
29 import static org.junit.Assert.*;
30
31
32
33
34
35
36
37 public class MessageServiceTest extends KCBTestCase {
38 private MessageService messageService;
39 private Message MESSAGE;
40
41 @Override
42 public void setUp() throws Exception {
43 super.setUp();
44
45 messageService = GlobalKCBServiceLocator.getInstance().getMessageService();
46 MESSAGE = KCBTestData.getMessage1();
47 MESSAGE = messageService.saveMessage(MESSAGE);
48 }
49
50 @Test
51 public void testCreate() {
52 Message m = new Message();
53 m.setContent("test content 2");
54 m.setChannel("channel2");
55 m.setContentType("test content type 2");
56 m.setDeliveryType("test delivery type 2");
57 m.setRecipient("test recipient 2");
58 m.setTitle("test title 2");
59
60 m = messageService.saveMessage(m);
61 assertNotNull(m.getId());
62
63 Collection<Message> ms = messageService.getAllMessages();
64 assertNotNull(ms);
65 assertEquals(2, ms.size());
66
67 Message m2 = messageService.getMessage(m.getId());
68 assertNotNull(m2);
69
70 assertEqualsMD(m, m2);
71
72 Message m1 = new Message();
73 m1.setContent("a");
74 m1.setChannel("a");
75 m1.setContentType("a");
76 m1.setDeliveryType("a");
77 m1.setRecipient("a");
78 m1.setTitle("a");
79
80
81 messageService.saveMessage(m1);
82 }
83
84 @Test
85 public void testDelete() {
86 messageService.deleteMessage(MESSAGE);
87
88 Collection<Message> ms = messageService.getAllMessages();
89 assertNotNull(ms);
90 assertEquals(0, ms.size());
91
92 assertNull(messageService.getMessage(MESSAGE.getId()));
93 }
94
95
96
97 @Test
98 public void testDuplicateCreate() {
99 Message m = new Message(MESSAGE);
100 KRADServiceLocator.getDataObjectService().save(MESSAGE, PersistenceOption.FLUSH);
101 }
102
103 @Test(expected = DataAccessException.class)
104 public void testInvalidCreate() {
105 final Message m = new Message();
106 KRADServiceLocator.getDataObjectService().save(m, PersistenceOption.FLUSH);
107 }
108
109 @Test
110 public void testInvalidRead() {
111 Message m = messageService.getMessage(Long.valueOf(-1));
112 assertNull(m);
113 }
114
115 @Test(expected = DataAccessException.class)
116 public void testInvalidUpdate() {
117 final Message m = messageService.getMessage(MESSAGE.getId());
118 m.setChannel(null);
119 KRADServiceLocator.getDataObjectService().save(m, PersistenceOption.FLUSH);
120 }
121
122 @Test
123 public void testReadById() {
124 Message m = messageService.getMessage(MESSAGE.getId());
125
126 assertEqualsMD(MESSAGE, m);
127 }
128
129 @Test
130 public void testUpdate() {
131 Message m = messageService.getMessage(MESSAGE.getId());
132 m.setTitle("A better title");
133 m.setContent("different content");
134 m = messageService.saveMessage(m);
135
136 Message m2 = messageService.getMessage(m.getId());
137 assertNotNull(m2);
138
139 assertEqualsMD(m, m2);
140 }
141
142
143
144
145
146
147 private void assertEqualsMD(Message expected, Message actual) {
148 assertEquals(expected.getId(), actual.getId());
149 assertEquals(expected.getCreationDateTime(), actual.getCreationDateTime());
150 assertEquals(expected.getContent(), actual.getContent());
151 assertEquals(expected.getContentType(), actual.getContentType());
152 assertEquals(expected.getDeliveryType(), actual.getDeliveryType());
153 assertEquals(expected.getRecipient(), actual.getRecipient());
154 assertEquals(expected.getTitle(), actual.getTitle());
155 }
156 }