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.binding.xml.DeviceDescriptorBinder;
19  import org.fourthline.cling.mock.MockUpnpService;
20  import org.fourthline.cling.model.meta.LocalDevice;
21  import org.fourthline.cling.model.meta.RemoteDevice;
22  import org.fourthline.cling.model.message.StreamRequestMessage;
23  import org.fourthline.cling.model.message.StreamResponseMessage;
24  import org.fourthline.cling.model.message.UpnpRequest;
25  import org.fourthline.cling.model.message.header.ContentTypeHeader;
26  import org.fourthline.cling.model.message.header.HostHeader;
27  import org.fourthline.cling.model.message.header.UpnpHeader;
28  import org.fourthline.cling.protocol.sync.ReceivingRetrieval;
29  import org.fourthline.cling.test.data.SampleData;
30  import org.fourthline.cling.test.data.SampleDeviceRoot;
31  import org.testng.annotations.Test;
32  
33  import static org.testng.Assert.*;
34  
35  
36  public class DeviceDescriptorRetrievalTest {
37  
38      @Test
39      public void registerAndRetrieveDescriptor() throws Exception {
40  
41          MockUpnpService upnpService = new MockUpnpService();
42  
43          // Register a device
44          LocalDevice localDevice = SampleData.createLocalDevice();
45          upnpService.getRegistry().addDevice(localDevice);
46  
47          // Retrieve the descriptor
48          StreamRequestMessage descRetrievalMessage = new StreamRequestMessage(UpnpRequest.Method.GET, SampleDeviceRoot.getDeviceDescriptorURI());
49          descRetrievalMessage.getHeaders().add(UpnpHeader.Type.HOST, new HostHeader("localhost", 1234));
50          ReceivingRetrieval prot = new ReceivingRetrieval(upnpService, descRetrievalMessage);
51          prot.run();
52          StreamResponseMessage descriptorMessage = prot.getOutputMessage();
53  
54          // UDA 1.0 spec days this musst be 'text/xml'
55          assertEquals(
56                  descriptorMessage.getHeaders().getFirstHeader(UpnpHeader.Type.CONTENT_TYPE).getValue(),
57                  ContentTypeHeader.DEFAULT_CONTENT_TYPE
58          );
59  
60          // Read the response and compare the returned device descriptor (test with LocalDevice for correct assertions)
61          DeviceDescriptorBinder binder = upnpService.getConfiguration().getDeviceDescriptorBinderUDA10();
62  
63          RemoteDevice returnedDevice =
64                  new RemoteDevice(SampleData.createRemoteDeviceIdentity());
65          returnedDevice = binder.describe(returnedDevice, descriptorMessage.getBodyString());
66  
67          SampleDeviceRoot.assertLocalResourcesMatch(
68                  upnpService.getConfiguration().getNamespace().getResources(returnedDevice)
69          );
70      }
71  
72      @Test
73      public void retrieveNonExistentDescriptor() throws Exception {
74  
75          MockUpnpService upnpService = new MockUpnpService();
76  
77          // Retrieve the descriptor
78          StreamRequestMessage descRetrievalMessage = new StreamRequestMessage(UpnpRequest.Method.GET, SampleDeviceRoot.getDeviceDescriptorURI());
79          descRetrievalMessage.getHeaders().add(UpnpHeader.Type.HOST, new HostHeader("localhost", 1234));
80          ReceivingRetrieval prot = new ReceivingRetrieval(upnpService, descRetrievalMessage);
81          prot.run();
82          StreamResponseMessage descriptorMessage = prot.getOutputMessage();
83  
84          assertNull(descriptorMessage);
85      }
86  
87  }