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.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.UpnpService;
23  import org.fourthline.cling.binding.annotations.UpnpServiceId;
24  import org.fourthline.cling.binding.annotations.UpnpServiceType;
25  import org.fourthline.cling.binding.annotations.UpnpStateVariable;
26  import org.fourthline.cling.model.action.ActionInvocation;
27  import org.fourthline.cling.model.meta.DeviceDetails;
28  import org.fourthline.cling.model.meta.LocalDevice;
29  import org.fourthline.cling.model.meta.LocalService;
30  import org.fourthline.cling.model.types.UDADeviceType;
31  import org.fourthline.cling.model.types.UnsignedIntegerFourBytes;
32  import org.fourthline.cling.model.types.csv.CSV;
33  import org.fourthline.cling.model.types.csv.CSVBoolean;
34  import org.fourthline.cling.model.types.csv.CSVInteger;
35  import org.fourthline.cling.model.types.csv.CSVString;
36  import org.fourthline.cling.model.types.csv.CSVUnsignedIntegerFourBytes;
37  import org.fourthline.cling.test.data.SampleData;
38  import org.testng.annotations.DataProvider;
39  import org.testng.annotations.Test;
40  
41  import java.util.List;
42  
43  import static org.testng.Assert.assertEquals;
44  
45  public class LocalActionInvocationCSVTest {
46  
47      public LocalDevice createTestDevice(LocalService service) throws Exception {
48          return new LocalDevice(
49                  SampleData.createLocalDeviceIdentity(),
50                  new UDADeviceType("TestDevice", 1),
51                  new DeviceDetails("Test Device"),
52                  service
53          );
54      }
55  
56      @DataProvider(name = "devices")
57      public Object[][] getDevices() throws Exception {
58          return new LocalDevice[][]{
59                  {createTestDevice(
60                          SampleData.readService(
61                                  new AnnotationLocalServiceBinder(), TestServiceOne.class
62                          )
63                  )},
64          };
65      }
66  
67      @Test(dataProvider = "devices")
68      public void invokeActions(LocalDevice device) throws Exception {
69  
70          LocalService svc = SampleData.getFirstService(device);
71  
72          List<String> testStrings = new CSVString();
73          testStrings.add("f\\oo");
74          testStrings.add("bar");
75          testStrings.add("b,az");
76          String result = executeActions(svc, "SetStringVar", "GetStringVar", testStrings);
77          List<String> csvString = new CSVString(result);
78          assert csvString.size() == 3;
79          assertEquals(csvString.get(0), "f\\oo");
80          assertEquals(csvString.get(1), "bar");
81          assertEquals(csvString.get(2), "b,az");
82  
83          List<Integer> testIntegers = new CSVInteger();
84          testIntegers.add(123);
85          testIntegers.add(-456);
86          testIntegers.add(789);
87          result = executeActions(svc, "SetIntVar", "GetIntVar", testIntegers);
88          List<Integer> csvInteger = new CSVInteger(result);
89          assert csvInteger.size() == 3;
90          assertEquals(csvInteger.get(0), new Integer(123));
91          assertEquals(csvInteger.get(1), new Integer(-456));
92          assertEquals(csvInteger.get(2), new Integer(789));
93  
94          List<Boolean> testBooleans = new CSVBoolean();
95          testBooleans.add(true);
96          testBooleans.add(true);
97          testBooleans.add(false);
98          result = executeActions(svc, "SetBooleanVar", "GetBooleanVar", testBooleans);
99          List<Boolean> csvBoolean = new CSVBoolean(result);
100         assert csvBoolean.size() == 3;
101         assertEquals(csvBoolean.get(0), new Boolean(true));
102         assertEquals(csvBoolean.get(1), new Boolean(true));
103         assertEquals(csvBoolean.get(2), new Boolean(false));
104 
105         List<UnsignedIntegerFourBytes> testUifour = new CSVUnsignedIntegerFourBytes();
106         testUifour.add(new UnsignedIntegerFourBytes(123));
107         testUifour.add(new UnsignedIntegerFourBytes(456));
108         testUifour.add(new UnsignedIntegerFourBytes(789));
109         result = executeActions(svc, "SetUifourVar", "GetUifourVar", testUifour);
110         List<UnsignedIntegerFourBytes> csvUifour = new CSVUnsignedIntegerFourBytes(result);
111         assert csvUifour.size() == 3;
112         assertEquals(csvUifour.get(0), new UnsignedIntegerFourBytes(123));
113         assertEquals(csvUifour.get(1), new UnsignedIntegerFourBytes(456));
114         assertEquals(csvUifour.get(2), new UnsignedIntegerFourBytes(789));
115     }
116 
117     protected String executeActions(LocalService svc, String setAction, String getAction, List input) throws Exception {
118         ActionInvocation setActionInvocation = new ActionInvocation(svc.getAction(setAction));
119         setActionInvocation.setInput(svc.getAction(setAction).getFirstInputArgument().getName(), input.toString());
120         svc.getExecutor(setActionInvocation.getAction()).execute(setActionInvocation);
121         assertEquals(setActionInvocation.getFailure(), null);
122         assertEquals(setActionInvocation.getOutput().length, 0);
123 
124         ActionInvocation getActionInvocation = new ActionInvocation(svc.getAction(getAction));
125         svc.getExecutor(getActionInvocation.getAction()).execute(getActionInvocation);
126         assertEquals(getActionInvocation.getFailure(), null);
127         assertEquals(getActionInvocation.getOutput().length, 1);
128         return getActionInvocation.getOutput(svc.getAction(getAction).getFirstOutputArgument()).toString();
129     }
130 
131 
132     /* ####################################################################################################### */
133 
134 
135     @UpnpService(
136             serviceId = @UpnpServiceId("TestService"),
137             serviceType = @UpnpServiceType(value = "TestService", version = 1)
138     )
139     public static class TestServiceOne {
140 
141         @UpnpStateVariable(sendEvents = false)
142         private CSV<String> stringVar;
143 
144         @UpnpStateVariable(sendEvents = false)
145         private CSV<Integer> intVar;
146 
147         @UpnpStateVariable(sendEvents = false)
148         private CSV<Boolean> booleanVar;
149 
150         @UpnpStateVariable(sendEvents = false)
151         private CSV<UnsignedIntegerFourBytes> uifourVar;
152 
153         @UpnpAction
154         public void setStringVar(@UpnpInputArgument(name = "StringVar") CSVString stringVar) {
155             this.stringVar = stringVar;
156             assertEquals(stringVar.size(), 3);
157             assertEquals(stringVar.get(0), "f\\oo");
158             assertEquals(stringVar.get(1), "bar");
159             assertEquals(stringVar.get(2), "b,az");
160         }
161 
162         @UpnpAction(out = @UpnpOutputArgument(name = "StringVar"))
163         public CSV<String> getStringVar() {
164             return stringVar;
165         }
166 
167         @UpnpAction
168         public void setIntVar(@UpnpInputArgument(name = "IntVar") CSVInteger intVar) {
169             this.intVar = intVar;
170             assertEquals(intVar.size(), 3);
171             assertEquals(intVar.get(0), new Integer(123));
172             assertEquals(intVar.get(1), new Integer(-456));
173             assertEquals(intVar.get(2), new Integer(789));
174         }
175 
176         @UpnpAction(out = @UpnpOutputArgument(name = "IntVar"))
177         public CSV<Integer> getIntVar() {
178             return intVar;
179         }
180 
181         @UpnpAction
182         public void setBooleanVar(@UpnpInputArgument(name = "BooleanVar") CSVBoolean booleanVar) {
183             this.booleanVar = booleanVar;
184             assertEquals(booleanVar.size(), 3);
185             assertEquals(booleanVar.get(0), new Boolean(true));
186             assertEquals(booleanVar.get(1), new Boolean(true));
187             assertEquals(booleanVar.get(2), new Boolean(false));
188         }
189 
190         @UpnpAction(out = @UpnpOutputArgument(name = "BooleanVar"))
191         public CSV<Boolean> getBooleanVar() {
192             return booleanVar;
193         }
194 
195         @UpnpAction
196         public void setUifourVar(@UpnpInputArgument(name = "UifourVar") CSVUnsignedIntegerFourBytes uifourVar) {
197             this.uifourVar = uifourVar;
198             assertEquals(uifourVar.size(), 3);
199             assertEquals(uifourVar.get(0), new UnsignedIntegerFourBytes(123));
200             assertEquals(uifourVar.get(1), new UnsignedIntegerFourBytes(456));
201             assertEquals(uifourVar.get(2), new UnsignedIntegerFourBytes(789));
202         }
203 
204         @UpnpAction(out = @UpnpOutputArgument(name = "UifourVar"))
205         public CSV<UnsignedIntegerFourBytes> getUifourVar() {
206             return uifourVar;
207         }
208 
209     }
210 
211 }