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.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              //SampleData.debugMsg(msg);
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              //SampleData.debugMsg(msg);
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  }