View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * Browsing the Registry
42   * <p>
43   * Although you typically create a <code>RegistryListener</code> to be notified of discovered and
44   * disappearing UPnP devices on your network, sometimes you have to browse the <code>Registry</code>
45   * manually.
46   * </p>
47   * <a class="citation" href="javadoc://this#findDevice" style="read-title: false"/>
48   * <a class="citation" href="javadoc://this#findDeviceByType" style="read-title: false"/>
49   */
50  public class RegistryBrowseTest {
51  
52      /**
53       * <p>
54       * The following call will return a device with the given unique device name, but
55       * only a root device and not any embedded device. Set the second parameter of
56       * <code>registry.getDevice()</code> to <code>false</code> if the device you are
57       * looking for might be an embedded device.
58       * </p>
59       * <a class="citation" href="javacode://this" style="include: FIND_ROOT_UDN"/>
60       * <p>
61       * If you know that the device you need is a <code>LocalDevice</code> - or a
62       * <code>RemoteDevice</code> - you can use the following operation:
63       * </p>
64       * <a class="citation" href="javacode://this" style="include: FIND_LOCAL_DEVICE" id="javacode_find_device_local"/>
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();                          // DOC: FIND_ROOT_UDN
75          Device foundDevice = registry.getDevice(udn, true);
76  
77          assertEquals(foundDevice.getIdentity().getUdn(), udn);                  // DOC: FIND_ROOT_UDN
78  
79          LocalDevice localDevice = registry.getLocalDevice(udn, true);           // DOC: FIND_LOCAL_DEVICE
80          assertEquals(localDevice.getIdentity().getUdn(), udn);
81  
82          SampleDeviceRootLocal.assertLocalResourcesMatch(
83                  upnpService.getConfiguration().getNamespace().getResources(device)
84          );
85      }
86  
87      /**
88       * <p>
89       * Most of the time you need a device that is of a particular type or that implements
90       * a particular service type, because this is what your control point can handle:
91       * </p>
92       * <a class="citation" href="javacode://this" style="include: FIND_DEV_TYPE"/>
93       * <a class="citation" href="javacode://this" style="include: FIND_SERV_TYPE" id="javacode_find_serv_type"/>
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);         // DOC: FIND_DEV_TYPE
105             Collection<Device> devices = registry.getDevices(deviceType);           // DOC: FIND_DEV_TYPE
106             assertEquals(devices.size(), 1);
107         } finally {}
108 
109         try {
110             ServiceType serviceType = new UDAServiceType("MY-SERVICE-TYPE-ONE", 1); // DOC: FIND_SERV_TYPE
111             Collection<Device> devices = registry.getDevices(serviceType);          // DOC: FIND_SERV_TYPE
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     /* TODO: We for now just ignore duplicate devices because we need to test proxies
148     @Test(expectedExceptions = RegistrationException.class)
149     public void registerDuplicateDevices() throws Exception {
150         MockUpnpService upnpService = new MockUpnpService();
151 
152 
153         LocalDevice deviceOne = SampleData.createLocalDevice();
154         upnpService.getRegistry().addDevice(deviceOne);
155 
156         LocalDevice deviceTwo = SampleData.createLocalDevice();
157         upnpService.getRegistry().addDevice(deviceTwo);
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     public Device getDevice(UDN udn, boolean rootOnly);
187 
188     public LocalDevice getLocalDevice(UDN udn, boolean rootOnly);
189 
190     public RemoteDevice getRemoteDevice(UDN udn, boolean rootOnly);
191 
192     public Collection<LocalDevice> getLocalDevices();
193 
194     public Collection<RemoteDevice> getRemoteDevices();
195 
196     public Collection<Device> getDevices();
197 
198     public Collection<Device> getDevices(DeviceType deviceType);
199 
200     public Collection<Device> getDevices(ServiceType serviceType);
201 
202     public Service getService(ServiceReference serviceReference);
203 
204  */
205 }