1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.test.ssdp;
17
18 import org.fourthline.cling.mock.MockUpnpService;
19 import org.fourthline.cling.model.ServerClientTokens;
20 import org.fourthline.cling.model.message.OutgoingDatagramMessage;
21 import org.fourthline.cling.model.message.UpnpMessage;
22 import org.fourthline.cling.model.message.header.UpnpHeader;
23 import org.fourthline.cling.model.meta.LocalDevice;
24 import org.fourthline.cling.model.types.NotificationSubtype;
25 import org.fourthline.cling.protocol.async.SendingNotificationAlive;
26 import org.fourthline.cling.protocol.async.SendingNotificationByebye;
27 import org.fourthline.cling.test.data.SampleData;
28 import org.fourthline.cling.test.data.SampleDeviceRoot;
29 import org.fourthline.cling.test.data.SampleUSNHeaders;
30 import org.testng.annotations.Test;
31
32 import static org.testng.Assert.assertEquals;
33
34
35 public class AdvertisementTest {
36
37 @Test
38 public void sendAliveMessages() throws Exception {
39
40 MockUpnpService upnpService = new MockUpnpService();
41
42 LocalDevice rootDevice = SampleData.createLocalDevice();
43 LocalDevice embeddedDevice = rootDevice.getEmbeddedDevices()[0];
44
45 SendingNotificationAlive prot = new SendingNotificationAlive(upnpService, rootDevice);
46 prot.run();
47
48 for (OutgoingDatagramMessage msg : upnpService.getRouter().getOutgoingDatagramMessages()) {
49 assertAliveMsgBasics(msg);
50
51 }
52
53 SampleUSNHeaders.assertUSNHeaders(
54 upnpService.getRouter().getOutgoingDatagramMessages(),
55 rootDevice, embeddedDevice, UpnpHeader.Type.NT);
56 }
57
58 @Test
59 public void sendByebyeMessages() throws Exception {
60
61 MockUpnpService upnpService = new MockUpnpService();
62
63 LocalDevice rootDevice = SampleData.createLocalDevice();
64 LocalDevice embeddedDevice = rootDevice.getEmbeddedDevices()[0];
65
66 SendingNotificationByebye prot = new SendingNotificationByebye(upnpService, rootDevice);
67 prot.run();
68
69 for (OutgoingDatagramMessage msg : upnpService.getRouter().getOutgoingDatagramMessages()) {
70 assertByebyeMsgBasics(msg);
71
72 }
73
74 SampleUSNHeaders.assertUSNHeaders(
75 upnpService.getRouter().getOutgoingDatagramMessages(),
76 rootDevice, embeddedDevice, UpnpHeader.Type.NT);
77 }
78
79 protected void assertAliveMsgBasics(UpnpMessage msg) {
80 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.NTS).getValue(), NotificationSubtype.ALIVE);
81 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.LOCATION).getValue().toString(), SampleDeviceRoot.getDeviceDescriptorURL().toString());
82 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.MAX_AGE).getValue(), 1800);
83 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.SERVER).getValue(), new ServerClientTokens());
84 }
85
86 protected void assertByebyeMsgBasics(UpnpMessage msg) {
87 assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.NTS).getValue(), NotificationSubtype.BYEBYE);
88 }
89
90 }