1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package example.registry;
16
17 import org.fourthline.cling.mock.MockUpnpService;
18 import org.fourthline.cling.model.resource.DeviceDescriptorResource;
19 import org.fourthline.cling.model.resource.Resource;
20 import org.fourthline.cling.model.meta.Device;
21 import org.fourthline.cling.model.meta.LocalDevice;
22 import org.fourthline.cling.model.meta.RemoteDevice;
23 import org.fourthline.cling.model.types.DeviceType;
24 import org.fourthline.cling.model.types.ServiceType;
25 import org.fourthline.cling.model.types.UDADeviceType;
26 import org.fourthline.cling.model.types.UDAServiceType;
27 import org.fourthline.cling.model.types.UDN;
28 import org.fourthline.cling.registry.Registry;
29 import org.fourthline.cling.test.data.SampleData;
30 import org.fourthline.cling.test.data.SampleDeviceRoot;
31 import org.fourthline.cling.test.data.SampleDeviceRootLocal;
32 import org.testng.Assert;
33 import org.testng.annotations.Test;
34
35 import java.net.URI;
36 import java.util.Collection;
37
38 import static org.testng.Assert.*;
39
40
41
42
43
44
45
46
47
48
49
50 public class RegistryBrowseTest {
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @Test
67 public void findDevice() throws Exception {
68 MockUpnpService upnpService = new MockUpnpService();
69 LocalDevice device = SampleData.createLocalDevice();
70 upnpService.getRegistry().addDevice(device);
71
72 UDN udn = device.getIdentity().getUdn();
73
74 Registry registry = upnpService.getRegistry();
75 Device foundDevice = registry.getDevice(udn, true);
76
77 assertEquals(foundDevice.getIdentity().getUdn(), udn);
78
79 LocalDevice localDevice = registry.getLocalDevice(udn, true);
80 assertEquals(localDevice.getIdentity().getUdn(), udn);
81
82 SampleDeviceRootLocal.assertLocalResourcesMatch(
83 upnpService.getConfiguration().getNamespace().getResources(device)
84 );
85 }
86
87
88
89
90
91
92
93
94
95 @Test
96 public void findDeviceByType() throws Exception {
97 MockUpnpService upnpService = new MockUpnpService();
98 LocalDevice device = SampleData.createLocalDevice();
99 upnpService.getRegistry().addDevice(device);
100
101 Registry registry = upnpService.getRegistry();
102
103 try {
104 DeviceType deviceType = new UDADeviceType("MY-DEVICE-TYPE", 1);
105 Collection<Device> devices = registry.getDevices(deviceType);
106 assertEquals(devices.size(), 1);
107 } finally {}
108
109 try {
110 ServiceType serviceType = new UDAServiceType("MY-SERVICE-TYPE-ONE", 1);
111 Collection<Device> devices = registry.getDevices(serviceType);
112 assertEquals(devices.size(), 1);
113 } finally {}
114 }
115
116
117 @Test
118 public void findLocalDevice() throws Exception {
119 MockUpnpService upnpService = new MockUpnpService();
120
121 LocalDevice deviceOne = SampleData.createLocalDevice();
122 upnpService.getRegistry().addDevice(deviceOne);
123
124 DeviceDescriptorResource resource =
125 upnpService.getRegistry().getResource(
126 DeviceDescriptorResource.class,
127 SampleDeviceRoot.getDeviceDescriptorURI()
128 );
129
130 assertNotNull(resource);
131 }
132
133 @Test(expectedExceptions = IllegalArgumentException.class)
134 public void findLocalDeviceInvalidRelativePath() throws Exception {
135 MockUpnpService upnpService = new MockUpnpService();
136
137 LocalDevice deviceOne = SampleData.createLocalDevice();
138 upnpService.getRegistry().addDevice(deviceOne);
139
140 DeviceDescriptorResource resource =
141 upnpService.getRegistry().getResource(
142 DeviceDescriptorResource.class,
143 URI.create("http://host/invalid/absolute/URI")
144 );
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 @Test
162 public void cleanupRemoteDevice() {
163 MockUpnpService upnpService = new MockUpnpService();
164 RemoteDevice rd = SampleData.createRemoteDevice();
165
166 upnpService.getRegistry().addDevice(rd);
167
168 assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
169
170 Resource resource = upnpService.getRegistry().getResource(
171 URI.create("/dev/MY-DEVICE-123/svc/upnp-org/MY-SERVICE-123/event/cb")
172 );
173 assertNotNull(resource);
174
175 upnpService.getRegistry().removeDevice(rd);
176
177 assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 0);
178
179 resource = upnpService.getRegistry().getResource(
180 URI.create("/dev/MY-DEVICE-123/svc/upnp-org/MY-SERVICE-123/event/cb")
181 );
182 assertNull(resource);
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205 }