1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
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
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
86 invocation = new ActionInvocation(svc.getAction("SetSomeValues"));
87 invocation.setInput("One", null);
88
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 }