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.local;
17  
18  import org.fourthline.cling.binding.LocalServiceBinder;
19  import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
20  import org.fourthline.cling.binding.annotations.UpnpAction;
21  import org.fourthline.cling.binding.annotations.UpnpInputArgument;
22  import org.fourthline.cling.binding.annotations.UpnpOutputArgument;
23  import org.fourthline.cling.binding.annotations.UpnpService;
24  import org.fourthline.cling.binding.annotations.UpnpServiceId;
25  import org.fourthline.cling.binding.annotations.UpnpServiceType;
26  import org.fourthline.cling.binding.annotations.UpnpStateVariable;
27  import org.fourthline.cling.model.action.ActionInvocation;
28  import org.fourthline.cling.model.meta.DeviceDetails;
29  import org.fourthline.cling.model.meta.LocalDevice;
30  import org.fourthline.cling.model.meta.LocalService;
31  import org.fourthline.cling.model.types.UDADeviceType;
32  import org.fourthline.cling.test.data.SampleData;
33  import org.testng.annotations.DataProvider;
34  import org.testng.annotations.Test;
35  
36  import static org.testng.Assert.assertEquals;
37  
38  public class LocalActionInvocationEnumTest {
39  
40      public LocalDevice createTestDevice(LocalService service) throws Exception {
41          return new LocalDevice(
42                  SampleData.createLocalDeviceIdentity(),
43                  new UDADeviceType("BinaryLight", 1),
44                  new DeviceDetails("Example Binary Light"),
45                  service
46          );
47      }
48  
49      @DataProvider(name = "devices")
50      public Object[][] getDevices() throws Exception {
51          LocalServiceBinder binder = new AnnotationLocalServiceBinder();
52          return new LocalDevice[][]{
53                  {createTestDevice(SampleData.readService(binder, TestServiceOne.class))},
54                  {createTestDevice(SampleData.readService(binder,TestServiceTwo.class))},
55                  {createTestDevice(SampleData.readService(binder, TestServiceThree.class))},
56          };
57      }
58  
59      @Test(dataProvider = "devices")
60      public void invokeActions(LocalDevice device) throws Exception {
61  
62          LocalService svc = SampleData.getFirstService(device);
63  
64          ActionInvocation checkTargetInvocation = new ActionInvocation(svc.getAction("GetTarget"));
65          svc.getExecutor(checkTargetInvocation.getAction()).execute(checkTargetInvocation);
66          assertEquals(checkTargetInvocation.getFailure(), null);
67          assertEquals(checkTargetInvocation.getOutput().length, 1);
68          assertEquals(checkTargetInvocation.getOutput()[0].toString(), "UNKNOWN");
69  
70          ActionInvocation setTargetInvocation = new ActionInvocation(svc.getAction("SetTarget"));
71          setTargetInvocation.setInput("NewTargetValue", "ON");
72          svc.getExecutor(setTargetInvocation.getAction()).execute(setTargetInvocation);
73          assertEquals(setTargetInvocation.getFailure(), null);
74          assertEquals(setTargetInvocation.getOutput().length, 0);
75  
76          ActionInvocation getTargetInvocation = new ActionInvocation(svc.getAction("GetTarget"));
77          svc.getExecutor(getTargetInvocation.getAction()).execute(getTargetInvocation);
78          assertEquals(getTargetInvocation.getFailure(), null);
79          assertEquals(getTargetInvocation.getOutput().length, 1);
80          assertEquals(getTargetInvocation.getOutput()[0].toString(), "ON");
81  
82          ActionInvocation getStatusInvocation = new ActionInvocation(svc.getAction("GetStatus"));
83          svc.getExecutor(getStatusInvocation.getAction()).execute(getStatusInvocation);
84          assertEquals(getStatusInvocation.getFailure(), null);
85          assertEquals(getStatusInvocation.getOutput().length, 1);
86          assertEquals(getStatusInvocation.getOutput()[0].toString(), "1");
87  
88      }
89  
90      /* ####################################################################################################### */
91  
92      @UpnpService(
93              serviceId = @UpnpServiceId("SwitchPower"),
94              serviceType = @UpnpServiceType(value = "SwitchPower", version = 1)
95      )
96      public static class TestServiceOne {
97  
98          public enum Target {
99              ON,
100             OFF,
101             UNKNOWN
102         }
103 
104         @UpnpStateVariable(sendEvents = false)
105         private Target target = Target.UNKNOWN;
106 
107         @UpnpStateVariable
108         private boolean status = false;
109 
110         @UpnpAction
111         public void setTarget(@UpnpInputArgument(name = "NewTargetValue") String newTargetValue) {
112             target = Target.valueOf(newTargetValue);
113 
114             status = target == Target.ON;
115         }
116 
117         @UpnpAction(out = @UpnpOutputArgument(name = "RetTargetValue"))
118         public Target getTarget() {
119             return target;
120         }
121 
122         @UpnpAction(out = @UpnpOutputArgument(name = "ResultStatus"))
123         public boolean getStatus() {
124             return status;
125         }
126     }
127 
128 
129     /* ####################################################################################################### */
130 
131     @UpnpService(
132             serviceId = @UpnpServiceId("SwitchPower"),
133             serviceType = @UpnpServiceType(value = "SwitchPower", version = 1)
134     )
135     public static class TestServiceTwo {
136 
137         public enum Target {
138             ON,
139             OFF,
140             UNKNOWN
141         }
142 
143         @UpnpStateVariable(sendEvents = false)
144         private Target target = Target.UNKNOWN;
145 
146         @UpnpStateVariable
147         private boolean status = false;
148 
149         @UpnpAction
150         public void setTarget(@UpnpInputArgument(name = "NewTargetValue") String newTargetValue) {
151             target = Target.valueOf(newTargetValue);
152 
153             status = target == Target.ON;
154         }
155 
156         @UpnpAction(out = @UpnpOutputArgument(name = "RetTargetValue", stateVariable = "Target", getterName = "getRealTarget"))
157         public void getTarget() {
158         }
159 
160         public Target getRealTarget() {
161             return target;
162         }
163 
164         @UpnpAction(out = @UpnpOutputArgument(name = "ResultStatus"))
165         public boolean getStatus() {
166             return status;
167         }
168     }
169 
170     /* ####################################################################################################### */
171 
172     @UpnpService(
173             serviceId = @UpnpServiceId("SwitchPower"),
174             serviceType = @UpnpServiceType(value = "SwitchPower", version = 1)
175     )
176     public static class TestServiceThree {
177 
178         public enum Target {
179             ON,
180             OFF,
181             UNKNOWN
182         }
183 
184         public class TargetHolder {
185             private Target t;
186 
187             public TargetHolder(Target t) {
188                 this.t = t;
189             }
190 
191             public Target getTarget() {
192                 return t;
193             }
194         }
195 
196         @UpnpStateVariable(sendEvents = false)
197         private Target target = Target.UNKNOWN;
198 
199         @UpnpStateVariable
200         private boolean status = false;
201 
202         @UpnpAction
203         public void setTarget(@UpnpInputArgument(name = "NewTargetValue") String newTargetValue) {
204             target = Target.valueOf(newTargetValue);
205 
206             status = target == Target.ON;
207         }
208 
209         @UpnpAction(name = "GetTarget", out = @UpnpOutputArgument(name = "RetTargetValue", getterName = "getTarget"))
210         public TargetHolder getTargetHolder() {
211             return new TargetHolder(target);
212         }
213 
214         @UpnpAction(out = @UpnpOutputArgument(name = "ResultStatus"))
215         public boolean getStatus() {
216             return status;
217         }
218     }
219 
220     /* ####################################################################################################### */
221 
222     @UpnpService(
223             serviceId = @UpnpServiceId("SwitchPower"),
224             serviceType = @UpnpServiceType(value = "SwitchPower", version = 1)
225     )
226     public static class TestServiceFour {
227 
228         public enum Target {
229             ON,
230             OFF,
231             UNKNOWN
232         }
233 
234         public class TargetHolder {
235             private Target t;
236 
237             public TargetHolder(Target t) {
238                 this.t = t;
239             }
240 
241             public Target getT() {
242                 return t;
243             }
244         }
245 
246         @UpnpStateVariable(sendEvents = false)
247         private Target target = Target.UNKNOWN;
248 
249         @UpnpStateVariable
250         private boolean status = false;
251 
252         @UpnpAction
253         public void setTarget(@UpnpInputArgument(name = "NewTargetValue") String newTargetValue) {
254             target = Target.valueOf(newTargetValue);
255 
256             status = target == Target.ON;
257         }
258 
259         @UpnpAction(name = "GetTarget", out = @UpnpOutputArgument(name = "RetTargetValue", stateVariable = "Target", getterName = "getT"))
260         public TargetHolder getTargetHolder() {
261             return new TargetHolder(target);
262         }
263 
264         @UpnpAction(out = @UpnpOutputArgument(name = "ResultStatus"))
265         public boolean getStatus() {
266             return status;
267         }
268     }
269 
270 }