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.control;
17  
18  import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
19  import org.fourthline.cling.binding.annotations.UpnpAction;
20  import org.fourthline.cling.binding.annotations.UpnpInputArgument;
21  import org.fourthline.cling.binding.annotations.UpnpOutputArgument;
22  import org.fourthline.cling.binding.annotations.UpnpServiceId;
23  import org.fourthline.cling.binding.annotations.UpnpServiceType;
24  import org.fourthline.cling.binding.annotations.UpnpStateVariable;
25  import org.fourthline.cling.model.meta.DeviceDetails;
26  import org.fourthline.cling.model.meta.LocalDevice;
27  import org.fourthline.cling.model.meta.LocalService;
28  import org.fourthline.cling.model.profile.RemoteClientInfo;
29  import org.fourthline.cling.model.types.UDADeviceType;
30  import org.fourthline.cling.test.data.SampleData;
31  
32  import static org.testng.Assert.*;
33  
34  /**
35   * @author Christian Bauer
36   */
37  public class ActionSampleData {
38  
39      public static LocalDevice createTestDevice() throws Exception {
40          return createTestDevice(LocalTestService.class);
41      }
42  
43      public static LocalDevice createTestDevice(Class<?> clazz) throws Exception {
44          return createTestDevice(
45                  SampleData.readService(
46                          new AnnotationLocalServiceBinder(),
47                          clazz
48                  )
49          );
50      }
51  
52      public static LocalDevice createTestDevice(LocalService service) throws Exception {
53          return new LocalDevice(
54                  SampleData.createLocalDeviceIdentity(),
55                  new UDADeviceType("BinaryLight", 1),
56                  new DeviceDetails("Example Binary Light"),
57                  service
58          );
59      }
60  
61      @org.fourthline.cling.binding.annotations.UpnpService(
62              serviceId = @UpnpServiceId("SwitchPower"),
63              serviceType = @UpnpServiceType(value = "SwitchPower", version = 1)
64      )
65      public static class LocalTestService {
66  
67          @UpnpStateVariable(sendEvents = false)
68          private boolean target = false;
69  
70          @UpnpStateVariable
71          private boolean status = false;
72  
73          @UpnpAction
74          public void setTarget(@UpnpInputArgument(name = "NewTargetValue") boolean newTargetValue) {
75              target = newTargetValue;
76              status = newTargetValue;
77          }
78  
79          @UpnpAction(out = @UpnpOutputArgument(name = "RetTargetValue"))
80          public boolean getTarget() {
81              return target;
82          }
83  
84          @UpnpAction(name = "GetStatus", out = @UpnpOutputArgument(name = "ResultStatus", getterName = "getStatus"))
85          public void dummyStatus() {
86              // NOOP
87          }
88  
89          public boolean getStatus() {
90              return status;
91          }
92      }
93  
94      public static class LocalTestServiceThrowsException extends LocalTestService {
95          @Override
96          public void setTarget(@UpnpInputArgument(name = "NewTargetValue") boolean newTargetValue) {
97              throw new RuntimeException("Something is wrong");
98          }
99      }
100 
101     public static class LocalTestServiceDelays extends LocalTestService {
102         @Override
103         public boolean getTarget() {
104             try {
105                 Thread.sleep(50); // A small delay so they are really concurrent
106             } catch (InterruptedException e) {}
107             return super.getTarget();
108         }
109     }
110 
111     public static class LocalTestServiceExtended extends LocalTestService {
112 
113         @UpnpStateVariable
114         String someValue;
115 
116         @UpnpAction
117         public void setSomeValue(@UpnpInputArgument(name = "SomeValue", aliases ={"SomeValue1"}) String someValue) {
118             this.someValue = someValue;
119         }
120 
121         @UpnpAction(out = @UpnpOutputArgument(name = "SomeValue"))
122         public String getSomeValue() {
123             return someValue;
124         }
125 
126     }
127     
128     @org.fourthline.cling.binding.annotations.UpnpService(
129             serviceId = @UpnpServiceId("SwitchPower"),
130             serviceType = @UpnpServiceType(value = "SwitchPower", version = 1)
131     )
132     public static class LocalTestServiceWithClientInfo {
133 
134         @UpnpStateVariable(sendEvents = false)
135         private boolean target = false;
136 
137         @UpnpStateVariable
138         private boolean status = false;
139 
140         @UpnpAction
141         public void setTarget(@UpnpInputArgument(name = "NewTargetValue") boolean newTargetValue) {
142             target = newTargetValue;
143             status = newTargetValue;
144         }
145 
146         @UpnpAction(out = @UpnpOutputArgument(name = "RetTargetValue"))
147         public boolean getTarget(RemoteClientInfo clientInfo) {
148             assertNotNull(clientInfo);
149             assertEquals(clientInfo.getRemoteAddress().getHostAddress(), "10.0.0.1");
150             assertEquals(clientInfo.getLocalAddress().getHostAddress(), "10.0.0.2");
151             assertEquals(clientInfo.getRequestUserAgent(), "foo/bar");
152             clientInfo.getExtraResponseHeaders().add("X-MY-HEADER", "foobar");
153             return target;
154         }
155 
156         @UpnpAction(name = "GetStatus", out = @UpnpOutputArgument(name = "ResultStatus", getterName = "getStatus"))
157         public void dummyStatus() {
158             // NOOP
159         }
160 
161         public boolean getStatus() {
162             return status;
163         }
164 
165     }
166 
167 }