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.ServiceDescriptorBinder;
19  import org.fourthline.cling.mock.MockUpnpService;
20  import org.fourthline.cling.model.meta.LocalDevice;
21  import org.fourthline.cling.model.meta.LocalService;
22  import org.fourthline.cling.model.meta.RemoteService;
23  import org.fourthline.cling.model.meta.Service;
24  import org.fourthline.cling.model.message.StreamRequestMessage;
25  import org.fourthline.cling.model.message.StreamResponseMessage;
26  import org.fourthline.cling.model.message.UpnpRequest;
27  import org.fourthline.cling.model.message.header.ContentTypeHeader;
28  import org.fourthline.cling.model.message.header.HostHeader;
29  import org.fourthline.cling.model.message.header.UpnpHeader;
30  import org.fourthline.cling.protocol.sync.ReceivingRetrieval;
31  import org.fourthline.cling.test.data.SampleData;
32  import org.fourthline.cling.test.data.SampleServiceOne;
33  import org.testng.annotations.Test;
34  
35  import java.net.URI;
36  
37  import static org.testng.Assert.*;
38  
39  
40  public class ServiceDescriptorRetrievalTest {
41  
42      @Test
43      public void registerAndRetrieveDescriptor() throws Exception {
44  
45          MockUpnpService upnpService = new MockUpnpService();
46  
47          // Register a device
48          LocalDevice localDevice = SampleData.createLocalDevice();
49          LocalService service = SampleData.getFirstService(localDevice);
50          upnpService.getRegistry().addDevice(localDevice);
51  
52          // Retrieve the descriptor
53          URI descriptorURI = upnpService.getConfiguration().getNamespace().getDescriptorPath(service);
54          StreamRequestMessage descRetrievalMessage = new StreamRequestMessage(UpnpRequest.Method.GET, descriptorURI);
55          descRetrievalMessage.getHeaders().add(UpnpHeader.Type.HOST, new HostHeader("localhost", 1234));
56          ReceivingRetrieval prot = new ReceivingRetrieval(upnpService, descRetrievalMessage);
57          prot.run();
58          StreamResponseMessage descriptorMessage = prot.getOutputMessage();
59  
60          // UDA 1.0 spec days this musst be 'text/xml'
61          assertEquals(
62                  descriptorMessage.getHeaders().getFirstHeader(UpnpHeader.Type.CONTENT_TYPE).getValue(),
63                  ContentTypeHeader.DEFAULT_CONTENT_TYPE
64          );
65  
66          // Read the response and compare the returned device descriptor
67          ServiceDescriptorBinder binder = upnpService.getConfiguration().getServiceDescriptorBinderUDA10();
68  
69          RemoteService remoteService = SampleData.createUndescribedRemoteService();
70  
71          remoteService = binder.describe(remoteService, descriptorMessage.getBodyString());
72          SampleServiceOne.assertMatch(remoteService, service);
73      }
74  
75      @Test
76      public void retrieveNonExistentDescriptor() throws Exception {
77  
78          MockUpnpService upnpService = new MockUpnpService();
79  
80          // Retrieve the descriptor
81          LocalDevice localDevice = SampleData.createLocalDevice();
82          Service service = SampleData.getFirstService(localDevice);
83  
84          URI descriptorURI = upnpService.getConfiguration().getNamespace().getDescriptorPath(service);
85          StreamRequestMessage descRetrievalMessage =
86                  new StreamRequestMessage(UpnpRequest.Method.GET, descriptorURI);
87          descRetrievalMessage.getHeaders().add(UpnpHeader.Type.HOST, new HostHeader("localhost", 1234));
88          ReceivingRetrieval prot = new ReceivingRetrieval(upnpService, descRetrievalMessage);
89          prot.run();
90          StreamResponseMessage descriptorMessage = prot.getOutputMessage();
91  
92          // Should be null because it can't be found
93          assertNull(descriptorMessage);
94  
95      }
96  
97  }