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.bo.MessageDelivery;
21 import org.kuali.rice.kcb.bo.MessageDeliveryStatus;
22 import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
23 import org.kuali.rice.kcb.service.MessageDeliveryService;
24 import org.kuali.rice.kcb.service.MessageService;
25 import org.kuali.rice.kcb.test.KCBTestCase;
26 import org.kuali.rice.kcb.test.KCBTestData;
27 import org.kuali.rice.krad.data.PersistenceOption;
28 import org.kuali.rice.krad.service.KRADServiceLocator;
29 import org.springframework.dao.DataAccessException;
30
31 import java.util.Collection;
32
33 import static org.junit.Assert.*;
34
35
36
37
38
39
40
41
42 public class MessageDeliveryServiceTest extends KCBTestCase {
43 private MessageService messageService;
44 private MessageDeliveryService messageDeliveryService;
45 private Message MESSAGE;
46 private MessageDelivery MESSAGE_DELIV;
47
48 @Override
49 public void setUp() throws Exception {
50 super.setUp();
51
52 messageService = GlobalKCBServiceLocator.getInstance().getMessageService();
53 messageDeliveryService = GlobalKCBServiceLocator.getInstance().getMessageDeliveryService();
54
55 MESSAGE = KCBTestData.getMessage1();
56 MESSAGE = messageService.saveMessage(MESSAGE);
57
58 MESSAGE_DELIV = KCBTestData.getMessageDelivery1();
59 MESSAGE_DELIV.setMessage(MESSAGE);
60
61 MESSAGE_DELIV = messageDeliveryService.saveMessageDelivery(MESSAGE_DELIV);
62 }
63
64 @Test
65 public void testCreate() {
66 MessageDelivery md = new MessageDelivery();
67 md.setDelivererTypeName("pigeon");
68 md.setMessage(MESSAGE);
69
70 md = messageDeliveryService.saveMessageDelivery(md);
71
72 assertNotNull(md.getId());
73 Collection<MessageDelivery> ms = messageDeliveryService.getAllMessageDeliveries();
74 assertNotNull(ms);
75 assertEquals(2, ms.size());
76
77 MessageDelivery md2 = messageDeliveryService.getMessageDelivery(md.getId());
78 assertNotNull(md2);
79
80 assertEqualsMD(md, md2);
81 }
82
83 @Test
84 public void testDelete() {
85 messageDeliveryService.deleteMessageDelivery(MESSAGE_DELIV);
86
87 Collection<MessageDelivery> ms = messageDeliveryService.getAllMessageDeliveries();
88 assertNotNull(ms);
89 assertEquals(0, ms.size());
90
91 assertNull(messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId()));
92 }
93
94 @Test(expected = DataAccessException.class)
95 public void testDuplicateCreate() {
96
97 final MessageDelivery md = new MessageDelivery();
98 md.setId(KCBTestData.FAKE_ID);
99 md.setDelivererSystemId(MESSAGE_DELIV.getDelivererSystemId());
100 md.setDelivererTypeName(MESSAGE_DELIV.getDelivererTypeName());
101 md.setDeliveryStatus(MESSAGE_DELIV.getDeliveryStatus());
102 md.setLockVerNbr(MESSAGE_DELIV.getLockVerNbr());
103 md.setMessage(MESSAGE);
104 KRADServiceLocator.getDataObjectService().save(md, PersistenceOption.FLUSH);
105 }
106
107 @Test(expected = DataAccessException.class)
108 public void testInvalidCreate() {
109 final MessageDelivery m = new MessageDelivery();
110 messageDeliveryService.saveMessageDelivery(m);
111 KRADServiceLocator.getDataObjectService().save(m, PersistenceOption.FLUSH);
112 }
113
114 @Test
115 public void testInvalidRead() {
116 MessageDelivery m = messageDeliveryService.getMessageDelivery(Long.valueOf(-1));
117 assertNull(m);
118 }
119
120
121 @Test(expected = DataAccessException.class)
122 public void testInvalidUpdate() {
123 final MessageDelivery m = messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId());
124 m.setDelivererTypeName(null);
125 m.setDelivererSystemId(null);
126 KRADServiceLocator.getDataObjectService().save(m, PersistenceOption.FLUSH);
127 }
128
129 @Test
130 public void testReadById() {
131 MessageDelivery m = messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId());
132
133 assertEqualsMD(MESSAGE_DELIV, m);
134 }
135
136 @Test
137 public void testUpdate() {
138 MessageDelivery m = messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId());
139 m.setDelivererTypeName("eagle");
140 m.setDeliveryStatus(MessageDeliveryStatus.UNDELIVERED);
141 m.setDelivererSystemId("1234");
142 messageDeliveryService.saveMessageDelivery(m);
143
144 MessageDelivery m2 = messageDeliveryService.getMessageDelivery(m.getId());
145 assertNotNull(m2);
146
147 assertEqualsMD(m, m2);
148 }
149
150 private void assertEqualsMD(MessageDelivery expected, MessageDelivery actual) {
151 assertEquals(expected.getId(), actual.getId());
152 assertEquals(expected.getDelivererSystemId(), actual.getDelivererSystemId());
153 assertEquals(expected.getDelivererTypeName(), actual.getDelivererTypeName());
154 assertEquals(expected.getDeliveryStatus(), actual.getDeliveryStatus());
155 assertEquals(expected.getMessage().getId(), actual.getMessage().getId());
156 }
157 }