1
2
3
4
5
6
7
8
9
10
11
12
13
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
44 LocalDevice localDevice = SampleData.createLocalDevice();
45 upnpService.getRegistry().addDevice(localDevice);
46
47
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
55 assertEquals(
56 descriptorMessage.getHeaders().getFirstHeader(UpnpHeader.Type.CONTENT_TYPE).getValue(),
57 ContentTypeHeader.DEFAULT_CONTENT_TYPE
58 );
59
60
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
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 }