1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package example.controlpoint;
16
17 import org.fourthline.cling.mock.MockUpnpService;
18 import org.fourthline.cling.model.message.UpnpMessage;
19 import org.fourthline.cling.model.message.header.DeviceTypeHeader;
20 import org.fourthline.cling.model.message.header.HostHeader;
21 import org.fourthline.cling.model.message.header.MANHeader;
22 import org.fourthline.cling.model.message.header.MXHeader;
23 import org.fourthline.cling.model.message.header.RootDeviceHeader;
24 import org.fourthline.cling.model.message.header.STAllHeader;
25 import org.fourthline.cling.model.message.header.ServiceTypeHeader;
26 import org.fourthline.cling.model.message.header.UDADeviceTypeHeader;
27 import org.fourthline.cling.model.message.header.UDAServiceTypeHeader;
28 import org.fourthline.cling.model.message.header.UDNHeader;
29 import org.fourthline.cling.model.message.header.UpnpHeader;
30 import org.fourthline.cling.model.types.DeviceType;
31 import org.fourthline.cling.model.types.NotificationSubtype;
32 import org.fourthline.cling.model.types.ServiceType;
33 import org.fourthline.cling.model.types.UDADeviceType;
34 import org.fourthline.cling.model.types.UDAServiceType;
35 import org.fourthline.cling.model.types.UDN;
36 import org.fourthline.cling.protocol.async.SendingSearch;
37 import org.testng.annotations.Test;
38
39 import java.util.UUID;
40
41 import static org.testng.Assert.*;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class SearchExecuteTest {
63
64
65
66
67
68
69
70
71
72
73
74
75
76 @Test
77 public void searchAll() throws Exception {
78 MockUpnpService upnpService = new MockUpnpService();
79
80 upnpService.getControlPoint().search(
81 new STAllHeader()
82 );
83
84 assertMessages(upnpService, new STAllHeader());
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 @Test
101 public void searchUDN() throws Exception {
102 MockUpnpService upnpService = new MockUpnpService();
103
104 UDN udn = new UDN(UUID.randomUUID());
105 upnpService.getControlPoint().search(
106 new UDNHeader(udn)
107 );
108
109 assertMessages(upnpService, new UDNHeader(udn));
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123 @Test
124 public void searchDeviceType() throws Exception {
125 MockUpnpService upnpService = new MockUpnpService();
126
127 UDADeviceType udaType = new UDADeviceType("BinaryLight");
128 upnpService.getControlPoint().search(
129 new UDADeviceTypeHeader(udaType)
130 );
131
132 assertMessages(upnpService, new UDADeviceTypeHeader(udaType));
133
134 upnpService.getRouter().getOutgoingDatagramMessages().clear();
135
136 DeviceType type = new DeviceType("org-mydomain", "MyDeviceType", 1);
137 upnpService.getControlPoint().search(
138 new DeviceTypeHeader(type)
139 );
140
141 assertMessages(upnpService, new DeviceTypeHeader(type));
142 }
143
144
145
146
147
148
149
150
151 @Test
152 public void searchServiceType() throws Exception {
153 MockUpnpService upnpService = new MockUpnpService();
154
155 UDAServiceType udaType = new UDAServiceType("SwitchPower");
156 upnpService.getControlPoint().search(
157 new UDAServiceTypeHeader(udaType)
158 );
159
160 assertMessages(upnpService, new UDAServiceTypeHeader(udaType));
161
162 upnpService.getRouter().getOutgoingDatagramMessages().clear();
163
164 ServiceType type = new ServiceType("org-mydomain", "MyServiceType", 1);
165 upnpService.getControlPoint().search(
166 new ServiceTypeHeader(type)
167 );
168
169 assertMessages(upnpService, new ServiceTypeHeader(type));
170 }
171
172
173 @Test
174 public void searchRoot() throws Exception {
175 MockUpnpService upnpService = new MockUpnpService();
176 upnpService.getControlPoint().search(new RootDeviceHeader());
177 assertMessages(upnpService, new RootDeviceHeader());
178 }
179
180
181 @Test
182 public void searchDefaults() {
183 SendingSearch search = new SendingSearch(new MockUpnpService());
184 assertEquals(search.getSearchTarget().getString(), new STAllHeader().getString());
185 }
186
187 @Test(expectedExceptions = java.lang.IllegalArgumentException.class)
188 public void searchInvalidST() {
189 SendingSearch search = new SendingSearch(new MockUpnpService(), new MXHeader());
190 }
191
192 protected void assertMessages(MockUpnpService upnpService, UpnpHeader header) throws Exception {
193 assertEquals(upnpService.getRouter().getOutgoingDatagramMessages().size(), 5);
194 for (UpnpMessage msg : upnpService.getRouter().getOutgoingDatagramMessages()) {
195 assertSearchMessage(msg, header);
196 }
197 }
198
199 protected void assertSearchMessage(UpnpMessage msg, UpnpHeader searchTarget) {
200 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.MAN).getString(), new MANHeader(NotificationSubtype.DISCOVER.getHeaderString()).getString());
201 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.MX).getString(), new MXHeader().getString());
202 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.ST).getString(), searchTarget.getString());
203 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.HOST).getString(), new HostHeader().getString());
204 }
205 }