1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package example.messagebox;
16
17 import org.fourthline.cling.UpnpService;
18 import org.fourthline.cling.mock.MockUpnpService;
19 import org.fourthline.cling.model.action.ActionInvocation;
20 import org.fourthline.cling.model.message.UpnpResponse;
21 import org.fourthline.cling.model.meta.LocalDevice;
22 import org.fourthline.cling.model.meta.LocalService;
23 import org.fourthline.cling.model.types.ServiceId;
24 import org.fourthline.cling.support.messagebox.AddMessage;
25 import org.fourthline.cling.support.messagebox.model.DateTime;
26 import org.fourthline.cling.support.messagebox.model.MessageIncomingCall;
27 import org.fourthline.cling.support.messagebox.model.MessageSMS;
28 import org.fourthline.cling.support.messagebox.model.MessageScheduleReminder;
29 import org.fourthline.cling.support.messagebox.model.NumberName;
30 import org.testng.annotations.Test;
31
32 import static org.testng.Assert.assertEquals;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class MessageBoxTest {
60
61 static private final String MESSAGE_SMS =
62 "<Category>SMS</Category>" +
63 "<DisplayType>Maximum</DisplayType>" +
64 "<ReceiveTime><Date>2010-06-21</Date><Time>16:34:12</Time></ReceiveTime>" +
65 "<Receiver><Number>1234</Number><Name>The Receiver</Name></Receiver>" +
66 "<Sender><Number>5678</Number><Name>The Sender</Name></Sender>" +
67 "<Body>Hello World!</Body>";
68
69 static private final String MESSAGE_INCOMING_CALL =
70 "<Category>Incoming Call</Category>" +
71 "<DisplayType>Maximum</DisplayType>" +
72 "<CallTime><Date>2010-06-21</Date><Time>16:34:12</Time></CallTime>" +
73 "<Callee><Number>1234</Number><Name>The Callee</Name></Callee>" +
74 "<Caller><Number>5678</Number><Name>The Caller</Name></Caller>";
75
76 static private final String MESSAGE_SCHEDULE_REMINDER =
77 "<Category>Schedule Reminder</Category>" +
78 "<DisplayType>Maximum</DisplayType>" +
79 "<StartTime><Date>2010-06-21</Date><Time>16:34:12</Time></StartTime>" +
80 "<Owner><Number>1234</Number><Name>The Owner</Name></Owner>" +
81 "<Subject>The Subject</Subject>" +
82 "<EndTime><Date>2010-06-21</Date><Time>17:34:12</Time></EndTime>" +
83 "<Location>The Location</Location>" +
84 "<Body>Hello World!</Body>";
85
86 @Test
87 public void createSMS() {
88 MessageSMS msg = new MessageSMS(
89 new DateTime("2010-06-21", "16:34:12"),
90 new NumberName("1234", "The Receiver"),
91 new NumberName("5678", "The Sender"),
92 "Hello World!"
93 );
94
95 String output = msg.toString();
96 assertEquals(output, MESSAGE_SMS);
97
98 }
99
100 @Test
101 public void createIncomingCall() {
102 MessageIncomingCall msg = new MessageIncomingCall(
103 new DateTime("2010-06-21", "16:34:12"),
104 new NumberName("1234", "The Callee"),
105 new NumberName("5678", "The Caller")
106 );
107
108 String output = msg.toString();
109 assertEquals(output, MESSAGE_INCOMING_CALL);
110
111 }
112
113 @Test
114 public void createScheduleReminder() {
115 MessageScheduleReminder msg = new MessageScheduleReminder(
116 new DateTime("2010-06-21", "16:34:12"),
117 new NumberName("1234", "The Owner"),
118 "The Subject",
119 new DateTime("2010-06-21", "17:34:12"),
120 "The Location",
121 "Hello World!"
122 );
123
124 String output = msg.toString();
125 assertEquals(output, MESSAGE_SCHEDULE_REMINDER);
126
127 }
128
129 @Test
130 public void sendMessageToTV() throws Exception {
131
132 final boolean[] tests = new boolean[1];
133
134 UpnpService upnpService = new MockUpnpService();
135
136 LocalDevice device = MessageBoxSampleData.createDevice(MyTV.class);
137 upnpService.getRegistry().addDevice(device);
138
139 MessageSMS msg = new MessageSMS(
140 new DateTime("2010-06-21", "16:34:12"),
141 new NumberName("1234", "The Receiver"),
142 new NumberName("5678", "The Sender"),
143 "Hello World!"
144 );
145
146 LocalService service = device.findService(new ServiceId("samsung.com", "MessageBoxService"));
147
148 upnpService.getControlPoint().execute(
149 new AddMessage(service, msg) {
150
151 @Override
152 public void success(ActionInvocation invocation) {
153
154 tests[0] = true;
155 }
156
157 @Override
158 public void failure(ActionInvocation invocation,
159 UpnpResponse operation,
160 String defaultMsg) {
161
162 }
163 }
164 );
165
166 for (boolean test : tests) {
167 assert test;
168 }
169 for (boolean test : ((LocalService<MyTV>) service).getManager().getImplementation().tests) {
170 assert test;
171 }
172
173 }
174
175 public static class MyTV extends MessageBoxSampleData.MessageBoxService {
176
177 boolean[] tests = new boolean[1];
178
179 @Override
180 protected void checkMessage(String id, String type, String messageText) {
181 assert id != null;
182 assertEquals(type, "text/xml;charset=\"utf-8\"");
183 assertEquals(messageText, MESSAGE_SMS);
184 tests[0] = true;
185 }
186 }
187
188 }