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.data;
17  
18  import org.fourthline.cling.model.meta.LocalDevice;
19  import org.fourthline.cling.model.message.OutgoingDatagramMessage;
20  import org.fourthline.cling.model.message.UpnpMessage;
21  import org.fourthline.cling.model.message.UpnpOperation;
22  import org.fourthline.cling.model.message.header.DeviceTypeHeader;
23  import org.fourthline.cling.model.message.header.DeviceUSNHeader;
24  import org.fourthline.cling.model.message.header.RootDeviceHeader;
25  import org.fourthline.cling.model.message.header.ServiceTypeHeader;
26  import org.fourthline.cling.model.message.header.ServiceUSNHeader;
27  import org.fourthline.cling.model.message.header.UDNHeader;
28  import org.fourthline.cling.model.message.header.USNRootDeviceHeader;
29  import org.fourthline.cling.model.message.header.UpnpHeader;
30  
31  import java.util.List;
32  
33  import static org.testng.Assert.assertEquals;
34  import static org.testng.Assert.assertTrue;
35  
36  /**
37   * @author Christian Bauer
38   */
39  public class SampleUSNHeaders {
40  
41      public static void assertUSNHeaders(List<OutgoingDatagramMessage> msgs, LocalDevice rootDevice, LocalDevice embeddedDevice, UpnpHeader.Type ntstHeaderType) {
42  
43          // See the tables in UDA 1.0 section 1.1.2
44  
45          boolean gotRootDeviceFirstMsg = false;
46          boolean gotRootDeviceSecondMsg = false;
47          boolean gotRootDeviceThirdMsg = false;
48  
49          boolean gotEmbeddedDeviceFirstMsg = false;
50          boolean gotEmbeddedDeviceSecondMsg = false;
51  
52          boolean gotFirstServiceMsg = false;
53          boolean gotSecondServiceMsg = false;
54  
55          for (UpnpMessage<UpnpOperation> msg : msgs) {
56  
57              if (msg.getHeaders().getFirstHeader(ntstHeaderType, RootDeviceHeader.class) != null) {
58                  assertEquals(
59                          msg.getHeaders().getFirstHeader(UpnpHeader.Type.USN, USNRootDeviceHeader.class).getString(),
60                          new USNRootDeviceHeader(rootDevice.getIdentity().getUdn()).getString()
61                  );
62                  gotRootDeviceFirstMsg = true;
63              }
64  
65              UDNHeader foundUDN = msg.getHeaders().getFirstHeader(ntstHeaderType, UDNHeader.class);
66              if (foundUDN != null && foundUDN.getString().equals(new UDNHeader(rootDevice.getIdentity().getUdn()).getString())) {
67                  assertEquals(
68                          msg.getHeaders().getFirstHeader(ntstHeaderType).getString(),
69                          msg.getHeaders().getFirstHeader(UpnpHeader.Type.USN).getString()
70                  );
71                  gotRootDeviceSecondMsg = true;
72              }
73  
74              if (foundUDN != null && foundUDN.getString().equals(new UDNHeader(embeddedDevice.getIdentity().getUdn()).getString())) {
75                  assertEquals(
76                          msg.getHeaders().getFirstHeader(ntstHeaderType).getString(),
77                          msg.getHeaders().getFirstHeader(UpnpHeader.Type.USN).getString()
78                  );
79                  gotEmbeddedDeviceFirstMsg = true;
80  
81              }
82  
83              DeviceTypeHeader foundDeviceNTST = msg.getHeaders().getFirstHeader(ntstHeaderType, DeviceTypeHeader.class);
84              if (foundDeviceNTST != null && foundDeviceNTST.getString().equals(new DeviceTypeHeader(rootDevice.getType()).getString())) {
85                  assertEquals(
86                          msg.getHeaders().getFirstHeader(UpnpHeader.Type.USN, DeviceUSNHeader.class).getString(),
87                          new DeviceUSNHeader(rootDevice.getIdentity().getUdn(), rootDevice.getType()).getString()
88                  );
89                  gotRootDeviceThirdMsg = true;
90              }
91  
92              if (foundDeviceNTST != null && foundDeviceNTST.getString().equals(new DeviceTypeHeader(embeddedDevice.getType()).getString())) {
93                  assertEquals(
94                          msg.getHeaders().getFirstHeader(UpnpHeader.Type.USN, DeviceUSNHeader.class).getString(),
95                          new DeviceUSNHeader(embeddedDevice.getIdentity().getUdn(), embeddedDevice.getType()).getString()
96                  );
97                  gotEmbeddedDeviceSecondMsg = true;
98              }
99  
100             ServiceTypeHeader foundServiceNTST = msg.getHeaders().getFirstHeader(ntstHeaderType, ServiceTypeHeader.class);
101             if (foundServiceNTST != null && foundServiceNTST.getString().equals(new ServiceTypeHeader(SampleServiceOne.getThisServiceType()).getString())) {
102                 assertEquals(
103                         msg.getHeaders().getFirstHeader(UpnpHeader.Type.USN, ServiceUSNHeader.class).getString(),
104                         new ServiceUSNHeader(rootDevice.getIdentity().getUdn(), SampleServiceOne.getThisServiceType()).getString()
105                 );
106                 gotFirstServiceMsg = true;
107             }
108 
109             if (foundServiceNTST != null && foundServiceNTST.getString().equals(new ServiceTypeHeader(SampleServiceTwo.getThisServiceType()).getString())) {
110                 assertEquals(
111                         msg.getHeaders().getFirstHeader(UpnpHeader.Type.USN, ServiceUSNHeader.class).getString(),
112                         new ServiceUSNHeader(rootDevice.getIdentity().getUdn(), SampleServiceTwo.getThisServiceType()).getString()
113                 );
114                 gotSecondServiceMsg = true;
115             }
116         }
117 
118         assertTrue(gotRootDeviceFirstMsg);
119         assertTrue(gotRootDeviceSecondMsg);
120         assertTrue(gotRootDeviceThirdMsg);
121 
122         assertTrue(gotEmbeddedDeviceFirstMsg);
123         assertTrue(gotEmbeddedDeviceSecondMsg);
124 
125         assertTrue(gotFirstServiceMsg);
126         assertTrue(gotSecondServiceMsg);
127 
128     }
129 }