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  
16  package org.fourthline.cling.test.resources;
17  
18  import org.fourthline.cling.model.meta.LocalDevice;
19  import org.fourthline.cling.binding.xml.DeviceDescriptorBinder;
20  import org.fourthline.cling.binding.xml.UDA10DeviceDescriptorBinderImpl;
21  import org.fourthline.cling.binding.xml.UDA10DeviceDescriptorBinderSAXImpl;
22  import org.fourthline.cling.mock.MockUpnpService;
23  import org.fourthline.cling.model.meta.RemoteDevice;
24  import org.fourthline.cling.model.profile.RemoteClientInfo;
25  import org.fourthline.cling.test.data.SampleData;
26  import org.fourthline.cling.test.data.SampleDeviceRoot;
27  import org.seamless.util.io.IO;
28  import org.testng.annotations.Test;
29  
30  import static org.testng.Assert.assertEquals;
31  import static org.testng.Assert.assertTrue;
32  
33  
34  public class UDA10DeviceDescriptorParsingTest {
35  
36      @Test
37      public void readUDA10DescriptorDOM() throws Exception {
38  
39          DeviceDescriptorBinder binder = new UDA10DeviceDescriptorBinderImpl();
40  
41          RemoteDevice device = new RemoteDevice(SampleData.createRemoteDeviceIdentity());
42          device = binder.describe(device, IO.readLines(getClass().getResourceAsStream("/descriptors/device/uda10.xml")));
43  
44          SampleDeviceRoot.assertLocalResourcesMatch(
45                  new MockUpnpService().getConfiguration().getNamespace().getResources(device)
46          );
47          SampleDeviceRoot.assertMatch(device, SampleData.createRemoteDevice());
48  
49      }
50  
51      @Test
52      public void readUDA10DescriptorSAX() throws Exception {
53  
54          DeviceDescriptorBinder binder = new UDA10DeviceDescriptorBinderSAXImpl();
55  
56          RemoteDevice device = new RemoteDevice(SampleData.createRemoteDeviceIdentity());
57          device = binder.describe(device, IO.readLines(getClass().getResourceAsStream("/descriptors/device/uda10.xml")));
58  
59          SampleDeviceRoot.assertLocalResourcesMatch(
60                  new MockUpnpService().getConfiguration().getNamespace().getResources(device)
61          );
62          SampleDeviceRoot.assertMatch(device, SampleData.createRemoteDevice());
63  
64      }
65  
66      @Test
67      public void writeUDA10Descriptor() throws Exception {
68  
69          MockUpnpService upnpService = new MockUpnpService();
70          DeviceDescriptorBinder binder = new UDA10DeviceDescriptorBinderImpl();
71          
72          RemoteDevice device = SampleData.createRemoteDevice();
73          String descriptorXml = binder.generate(
74                  device,
75                  new RemoteClientInfo(),
76                  upnpService.getConfiguration().getNamespace()
77          );
78  
79  /*
80          System.out.println("#######################################################################################");
81          System.out.println(descriptorXml);
82          System.out.println("#######################################################################################");
83  */
84  
85          RemoteDevice hydratedDevice = new RemoteDevice(SampleData.createRemoteDeviceIdentity());
86          hydratedDevice = binder.describe(hydratedDevice, descriptorXml);
87  
88          SampleDeviceRoot.assertLocalResourcesMatch(
89                  upnpService.getConfiguration().getNamespace().getResources(hydratedDevice)
90  
91          );
92          SampleDeviceRoot.assertMatch(hydratedDevice, device);
93  
94      }
95  
96      @Test
97      public void writeUDA10DescriptorWithProvider() throws Exception {
98  
99          MockUpnpService upnpService = new MockUpnpService();
100         DeviceDescriptorBinder binder = new UDA10DeviceDescriptorBinderImpl();
101 
102         LocalDevice device = SampleData.createLocalDevice(true);
103         String descriptorXml = binder.generate(
104                 device,
105                 new RemoteClientInfo(),
106                 upnpService.getConfiguration().getNamespace()
107         );
108 
109 
110         //System.out.println("#######################################################################################");
111         //System.out.println(descriptorXml);
112         //System.out.println("#######################################################################################");
113 
114 
115         RemoteDevice hydratedDevice = new RemoteDevice(SampleData.createRemoteDeviceIdentity());
116         hydratedDevice = binder.describe(hydratedDevice, descriptorXml);
117 
118         SampleDeviceRoot.assertLocalResourcesMatch(
119                 upnpService.getConfiguration().getNamespace().getResources(hydratedDevice)
120 
121         );
122         //SampleDeviceRoot.assertMatch(hydratedDevice, device, false);
123 
124     }
125 
126     @Test
127     public void readUDA10DescriptorWithURLBase() throws Exception {
128         MockUpnpService upnpService = new MockUpnpService();
129         DeviceDescriptorBinder binder = upnpService.getConfiguration().getDeviceDescriptorBinderUDA10();
130 
131         RemoteDevice device = new RemoteDevice(SampleData.createRemoteDeviceIdentity());
132         device = binder.describe(
133                 device,
134                 IO.readLines(getClass().getResourceAsStream("/descriptors/device/uda10_withbase.xml"))
135         );
136 
137         assertEquals(
138                 device.normalizeURI(device.getDetails().getManufacturerDetails().getManufacturerURI()).toString(),
139                 SampleData.getLocalBaseURL().toString() + "mfc.html"
140         );
141         assertEquals(
142                 device.normalizeURI(device.getDetails().getModelDetails().getModelURI()).toString(),
143                 SampleData.getLocalBaseURL().toString() + "someotherbase/MY-DEVICE-123/model.html"
144         );
145         assertEquals(
146                 device.normalizeURI(device.getDetails().getPresentationURI()).toString(),
147                 "http://www.4thline.org/some_ui"
148         );
149 
150         assertEquals(
151                 device.normalizeURI(device.getIcons()[0].getUri()).toString(),
152                 SampleData.getLocalBaseURL().toString() + "someotherbase/MY-DEVICE-123/icon.png"
153         );
154 
155         assertEquals(device.normalizeURI(
156                 device.getServices()[0].getDescriptorURI()).toString(),
157                      SampleData.getLocalBaseURL().toString() + "someotherbase/MY-DEVICE-123/svc/upnp-org/MY-SERVICE-123/desc.xml"
158         );
159         assertEquals(
160                 device.normalizeURI(device.getServices()[0].getControlURI()).toString(),
161                 SampleData.getLocalBaseURL().toString() + "someotherbase/MY-DEVICE-123/svc/upnp-org/MY-SERVICE-123/control"
162         );
163         assertEquals(
164                 device.normalizeURI(device.getServices()[0].getEventSubscriptionURI()).toString(),
165                 SampleData.getLocalBaseURL().toString() + "someotherbase/MY-DEVICE-123/svc/upnp-org/MY-SERVICE-123/events"
166         );
167 
168         assertTrue(device.isRoot());
169     }
170 
171     @Test
172     public void readUDA10DescriptorWithURLBase2() throws Exception {
173         MockUpnpService upnpService = new MockUpnpService();
174         DeviceDescriptorBinder binder = upnpService.getConfiguration().getDeviceDescriptorBinderUDA10();
175 
176         RemoteDevice device = new RemoteDevice(SampleData.createRemoteDeviceIdentity());
177         device = binder.describe(
178                 device,
179                 IO.readLines(getClass().getResourceAsStream("/descriptors/device/uda10_withbase2.xml"))
180         );
181 
182         assertEquals(
183                 device.normalizeURI(device.getDetails().getManufacturerDetails().getManufacturerURI()).toString(),
184                 SampleData.getLocalBaseURL().toString() + "mfc.html"
185         );
186 
187         assertEquals(
188                 device.normalizeURI(device.getDetails().getModelDetails().getModelURI()).toString(),
189                 SampleData.getLocalBaseURL().toString() + "model.html"
190         );
191         assertEquals(
192                 device.normalizeURI(device.getDetails().getPresentationURI()).toString(),
193                 "http://www.4thline.org/some_ui"
194         );
195 
196         assertEquals(
197                 device.normalizeURI(device.getIcons()[0].getUri()).toString(),
198                 SampleData.getLocalBaseURL().toString() + "icon.png"
199         );
200 
201         assertEquals(device.normalizeURI(
202                 device.getServices()[0].getDescriptorURI()).toString(),
203                      SampleData.getLocalBaseURL().toString() + "svc.xml"
204         );
205         assertEquals(
206                 device.normalizeURI(device.getServices()[0].getControlURI()).toString(),
207                 SampleData.getLocalBaseURL().toString() + "control"
208         );
209         assertEquals(
210                 device.normalizeURI(device.getServices()[0].getEventSubscriptionURI()).toString(),
211                 SampleData.getLocalBaseURL().toString() + "events"
212         );
213 
214         assertTrue(device.isRoot());
215     }
216 
217     @Test
218     public void readUDA10DescriptorWithEmptyURLBase() throws Exception {
219         DeviceDescriptorBinder binder = new UDA10DeviceDescriptorBinderImpl();
220 
221         RemoteDevice device = new RemoteDevice(SampleData.createRemoteDeviceIdentity());
222         device = binder.describe(device, IO.readLines(getClass().getResourceAsStream("/descriptors/device/uda10_emptybase.xml")));
223 
224         SampleDeviceRoot.assertLocalResourcesMatch(
225                 new MockUpnpService().getConfiguration().getNamespace().getResources(device)
226         );
227         SampleDeviceRoot.assertMatch(device, SampleData.createRemoteDevice());    }
228 }
229