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.UpnpAction;
19  import org.fourthline.cling.binding.annotations.UpnpInputArgument;
20  import org.fourthline.cling.binding.annotations.UpnpService;
21  import org.fourthline.cling.binding.annotations.UpnpServiceId;
22  import org.fourthline.cling.binding.annotations.UpnpServiceType;
23  import org.fourthline.cling.binding.annotations.UpnpStateVariable;
24  import org.fourthline.cling.model.action.ActionInvocation;
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.types.ErrorCode;
29  import org.fourthline.cling.model.types.UDADeviceType;
30  import org.fourthline.cling.test.data.SampleData;
31  import org.testng.annotations.Test;
32  
33  import static org.testng.Assert.assertEquals;
34  
35  /**
36   * @author Christian Bauer
37   */
38  public class LocalActionInvocationNullTest {
39  
40      @Test
41      public void invokeActions() throws Exception {
42  
43          LocalDevice device = new LocalDevice(
44                  SampleData.createLocalDeviceIdentity(),
45                  new UDADeviceType("SomeDevice", 1),
46                  new DeviceDetails("Some Device"),
47                  SampleData.readService(LocalTestServiceOne.class)
48          );
49          LocalService<LocalTestServiceOne> svc = SampleData.getFirstService(device);
50  
51          ActionInvocation invocation;
52  
53          // This succeeds
54          invocation = new ActionInvocation(svc.getAction("SetSomeValues"));
55          invocation.setInput("One", "foo");
56          invocation.setInput("Two", "bar");
57          invocation.setInput("Three", "baz");
58          svc.getExecutor(invocation.getAction()).execute(invocation);
59          assertEquals(invocation.getFailure(), null);
60          assertEquals(svc.getManager().getImplementation().one, "foo");
61          assertEquals(svc.getManager().getImplementation().two, "bar");
62          assertEquals(svc.getManager().getImplementation().three.toString(), "baz");
63  
64          // Empty string is fine, will be converted into "null"
65          invocation = new ActionInvocation(svc.getAction("SetSomeValues"));
66          invocation.setInput("One", "foo");
67          invocation.setInput("Two", "");
68          invocation.setInput("Three", null);
69          svc.getExecutor(invocation.getAction()).execute(invocation);
70          assertEquals(invocation.getFailure(), null);
71          assertEquals(svc.getManager().getImplementation().one, "foo");
72          assertEquals(svc.getManager().getImplementation().two, null);
73          assertEquals(svc.getManager().getImplementation().three, null);
74  
75          // Null is not fine for primitive input arguments
76          invocation = new ActionInvocation(svc.getAction("SetPrimitive"));
77          invocation.setInput("Primitive", "");
78          svc.getExecutor(invocation.getAction()).execute(invocation);
79          assertEquals(invocation.getFailure().getErrorCode(), ErrorCode.ARGUMENT_VALUE_INVALID.getCode());
80          assertEquals(
81                  invocation.getFailure().getMessage(),
82                  "The argument value is invalid. Primitive action method argument 'Primitive' requires input value, can't be null or empty string."
83          );
84  
85          // We forgot to set one and it's a local invocation (no string conversion)
86          invocation = new ActionInvocation(svc.getAction("SetSomeValues"));
87          invocation.setInput("One", null);
88          // OOPS! invocation.setInput("Two", null);
89          invocation.setInput("Three", null);
90          svc.getExecutor(invocation.getAction()).execute(invocation);
91          assertEquals(invocation.getFailure(), null);
92          assertEquals(svc.getManager().getImplementation().one, null);
93          assertEquals(svc.getManager().getImplementation().two, null);
94          assertEquals(svc.getManager().getImplementation().three, null);
95  
96      }
97  
98      @UpnpService(
99              serviceId = @UpnpServiceId("SomeService"),
100             serviceType = @UpnpServiceType(value = "SomeService", version = 1),
101             supportsQueryStateVariables = false,
102             stringConvertibleTypes = MyString.class
103     )
104     public static class LocalTestServiceOne {
105 
106         @UpnpStateVariable(name = "A_ARG_TYPE_One", sendEvents = false)
107         private String one;
108 
109         @UpnpStateVariable(name = "A_ARG_TYPE_Two", sendEvents = false)
110         private String two;
111 
112         @UpnpStateVariable(name = "A_ARG_TYPE_Three", sendEvents = false)
113         private MyString three;
114 
115         @UpnpStateVariable(sendEvents = false)
116         private boolean primitive;
117 
118         @UpnpAction
119         public void setSomeValues(@UpnpInputArgument(name = "One") String one,
120                                   @UpnpInputArgument(name = "Two") String two,
121                                   @UpnpInputArgument(name = "Three") MyString three) {
122             this.one = one;
123             this.two = two;
124             this.three = three;
125         }
126 
127         @UpnpAction
128         public void setPrimitive(@UpnpInputArgument(name = "Primitive") boolean b) {
129             this.primitive = primitive;
130         }
131     }
132 
133     public static class MyString {
134         private String s;
135 
136         public MyString(String s) {
137             this.s = s;
138         }
139 
140         @Override
141         public String toString() {
142             return s;
143         }
144     }
145 }