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.gena;
17  
18  import org.fourthline.cling.mock.MockUpnpService;
19  import org.fourthline.cling.mock.MockUpnpServiceConfiguration;
20  import org.fourthline.cling.model.gena.CancelReason;
21  import org.fourthline.cling.model.gena.LocalGENASubscription;
22  import org.fourthline.cling.model.message.StreamRequestMessage;
23  import org.fourthline.cling.model.message.gena.IncomingEventRequestMessage;
24  import org.fourthline.cling.model.message.gena.OutgoingEventRequestMessage;
25  import org.fourthline.cling.model.meta.LocalDevice;
26  import org.fourthline.cling.model.meta.LocalService;
27  import org.fourthline.cling.model.meta.RemoteDevice;
28  import org.fourthline.cling.model.meta.RemoteService;
29  import org.fourthline.cling.model.state.StateVariableValue;
30  import org.fourthline.cling.test.data.SampleData;
31  import org.fourthline.cling.transport.impl.GENAEventProcessorImpl;
32  import org.fourthline.cling.transport.impl.PullGENAEventProcessorImpl;
33  import org.fourthline.cling.transport.impl.RecoveringGENAEventProcessorImpl;
34  import org.fourthline.cling.transport.spi.GENAEventProcessor;
35  import org.testng.annotations.Test;
36  
37  import java.net.URL;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import static org.testng.Assert.assertEquals;
42  import static org.testng.Assert.assertTrue;
43  
44  
45  public class EventXMLProcessingTest {
46  
47      public static final String EVENT_MSG =
48          "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" +
49              "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">" +
50              "<e:property>" +
51              "<Status>0</Status>" +
52              "</e:property>" +
53              "<e:property>" +
54              "<SomeVar></SomeVar>" +
55              "</e:property>" +
56              "</e:propertyset>";
57  
58      @Test
59      public void writeReadRequest() throws Exception {
60          MockUpnpService upnpService = new MockUpnpService(new MockUpnpServiceConfiguration(){
61              @Override
62              public GENAEventProcessor getGenaEventProcessor() {
63                  return new GENAEventProcessorImpl();
64              }
65          });
66          writeReadRequest(upnpService);
67      }
68  
69      @Test
70      public void writeReadRequestPull() throws Exception {
71          MockUpnpService upnpService = new MockUpnpService(new MockUpnpServiceConfiguration(){
72              @Override
73              public GENAEventProcessor getGenaEventProcessor() {
74                  return new PullGENAEventProcessorImpl();
75              }
76          });
77          writeReadRequest(upnpService);
78      }
79  
80      @Test
81      public void writeReadRequestRecovering() throws Exception {
82          MockUpnpService upnpService = new MockUpnpService(new MockUpnpServiceConfiguration(){
83              @Override
84              public GENAEventProcessor getGenaEventProcessor() {
85                  return new RecoveringGENAEventProcessorImpl();
86              }
87          });
88          writeReadRequest(upnpService);
89      }
90  
91      public void writeReadRequest(MockUpnpService upnpService) throws Exception {
92  
93          LocalDevice localDevice = GenaSampleData.createTestDevice(GenaSampleData.LocalTestService.class);
94          LocalService localService = localDevice.getServices()[0];
95  
96          List<URL> urls = new ArrayList<URL>() {{
97              add(SampleData.getLocalBaseURL());
98          }};
99          
100         LocalGENASubscription subscription =
101                 new LocalGENASubscription(localService, 1800, urls) {
102                     public void failed(Exception ex) {
103                         throw new RuntimeException("TEST SUBSCRIPTION FAILED: " + ex);
104                     }
105 
106                     public void ended(CancelReason reason) {
107 
108                     }
109 
110                     public void established() {
111 
112                     }
113 
114                     public void eventReceived() {
115 
116                     }
117                 };
118 
119         OutgoingEventRequestMessage outgoingCall =
120                 new OutgoingEventRequestMessage(subscription, subscription.getCallbackURLs().get(0));
121 
122         upnpService.getConfiguration().getGenaEventProcessor().writeBody(outgoingCall);
123 
124         assertEquals(outgoingCall.getBody(), EVENT_MSG);
125 
126         StreamRequestMessage incomingStream = new StreamRequestMessage(outgoingCall);
127 
128         RemoteDevice remoteDevice = SampleData.createRemoteDevice();
129         RemoteService remoteService = SampleData.getFirstService(remoteDevice);
130 
131         IncomingEventRequestMessage incomingCall = new IncomingEventRequestMessage(incomingStream, remoteService);
132 
133         upnpService.getConfiguration().getGenaEventProcessor().readBody(incomingCall);
134 
135         assertEquals(incomingCall.getStateVariableValues().size(), 2);
136 
137         boolean gotValueOne = false;
138         boolean gotValueTwo = false;
139         for (StateVariableValue stateVariableValue : incomingCall.getStateVariableValues()) {
140             if (stateVariableValue.getStateVariable().getName().equals("Status")) {
141                 gotValueOne = (!(Boolean) stateVariableValue.getValue());
142             }
143             if (stateVariableValue.getStateVariable().getName().equals("SomeVar")) {
144                 // TODO: So... can it be null at all? It has a default value...
145                 gotValueTwo = stateVariableValue.getValue() == null;
146             }
147         }
148         assertTrue(gotValueOne && gotValueTwo);
149     }
150 
151 
152 }