1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package example.localservice;
16
17 import org.fourthline.cling.binding.LocalServiceBinder;
18 import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
19 import org.fourthline.cling.model.DefaultServiceManager;
20 import org.fourthline.cling.model.action.ActionInvocation;
21 import org.fourthline.cling.model.meta.ActionArgument;
22 import org.fourthline.cling.model.meta.DeviceDetails;
23 import org.fourthline.cling.model.meta.LocalDevice;
24 import org.fourthline.cling.model.meta.LocalService;
25 import org.fourthline.cling.model.types.Datatype;
26 import org.fourthline.cling.model.types.UDADeviceType;
27 import org.fourthline.cling.model.types.UDAServiceId;
28 import org.fourthline.cling.model.types.UDAServiceType;
29 import org.fourthline.cling.test.data.SampleData;
30 import org.testng.annotations.DataProvider;
31 import org.testng.annotations.Test;
32
33 import static org.testng.Assert.assertEquals;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public class BasicBindingTest {
79
80 public LocalDevice createTestDevice(Class serviceClass) throws Exception {
81
82 LocalServiceBinder binder = new AnnotationLocalServiceBinder();
83 LocalService svc = binder.read(serviceClass);
84 svc.setManager(new DefaultServiceManager(svc, serviceClass));
85
86 return new LocalDevice(
87 SampleData.createLocalDeviceIdentity(),
88 new UDADeviceType("BinaryLight", 1),
89 new DeviceDetails("Example Binary Light"),
90 svc
91 );
92 }
93
94 @DataProvider(name = "devices")
95 public Object[][] getDevices() {
96
97
98 try {
99 return new LocalDevice[][]{
100 {createTestDevice(SwitchPowerNamedStateVariable.class)},
101 {createTestDevice(SwitchPowerAnnotatedClass.class)},
102 {createTestDevice(SwitchPowerExtraGetter.class)},
103 {createTestDevice(SwitchPowerBeanReturn.class)},
104 };
105 } catch (Exception ex) {
106 ex.printStackTrace(System.err);
107
108 throw new RuntimeException(ex);
109 }
110 }
111
112 @Test(dataProvider = "devices")
113 public void validateBinding(LocalDevice device) {
114
115 LocalService svc = device.getServices()[0];
116
117
118
119
120
121
122
123
124
125
126
127
128 assertEquals(svc.getServiceId().toString(), "urn:" + UDAServiceId.DEFAULT_NAMESPACE + ":serviceId:SwitchPower");
129 assertEquals(svc.getServiceType().toString(), "urn:" + UDAServiceType.DEFAULT_NAMESPACE + ":service:SwitchPower:1");
130
131 assertEquals(svc.getStateVariables().length, 2);
132 assertEquals(svc.getStateVariable("Target").getTypeDetails().getDatatype().getBuiltin(), Datatype.Builtin.BOOLEAN);
133 assertEquals(svc.getStateVariable("Target").getTypeDetails().getDefaultValue(), "0");
134 assertEquals(svc.getStateVariable("Target").getEventDetails().isSendEvents(), false);
135
136 assertEquals(svc.getStateVariable("Status").getTypeDetails().getDatatype().getBuiltin(), Datatype.Builtin.BOOLEAN);
137 assertEquals(svc.getStateVariable("Status").getTypeDetails().getDefaultValue(), "0");
138 assertEquals(svc.getStateVariable("Status").getEventDetails().isSendEvents(), true);
139
140 assertEquals(svc.getActions().length, 4);
141
142 assertEquals(svc.getAction("SetTarget").getName(), "SetTarget");
143 assertEquals(svc.getAction("SetTarget").getArguments().length, 1);
144 assertEquals(svc.getAction("SetTarget").getArguments()[0].getName(), "NewTargetValue");
145 assertEquals(svc.getAction("SetTarget").getArguments()[0].getDirection(), ActionArgument.Direction.IN);
146 assertEquals(svc.getAction("SetTarget").getArguments()[0].getRelatedStateVariableName(), "Target");
147
148 assertEquals(svc.getAction("GetTarget").getName(), "GetTarget");
149 assertEquals(svc.getAction("GetTarget").getArguments().length, 1);
150 assertEquals(svc.getAction("GetTarget").getArguments()[0].getName(), "RetTargetValue");
151 assertEquals(svc.getAction("GetTarget").getArguments()[0].getDirection(), ActionArgument.Direction.OUT);
152 assertEquals(svc.getAction("GetTarget").getArguments()[0].getRelatedStateVariableName(), "Target");
153 assertEquals(svc.getAction("GetTarget").getArguments()[0].isReturnValue(), true);
154
155 assertEquals(svc.getAction("GetStatus").getName(), "GetStatus");
156 assertEquals(svc.getAction("GetStatus").getArguments().length, 1);
157 assertEquals(svc.getAction("GetStatus").getArguments()[0].getName(), "ResultStatus");
158 assertEquals(svc.getAction("GetStatus").getArguments()[0].getDirection(), ActionArgument.Direction.OUT);
159 assertEquals(svc.getAction("GetStatus").getArguments()[0].getRelatedStateVariableName(), "Status");
160 assertEquals(svc.getAction("GetStatus").getArguments()[0].isReturnValue(), true);
161
162 }
163
164 @Test(dataProvider = "devices")
165 public void invokeActions(LocalDevice device) {
166
167 LocalService svc = device.getServices()[0];
168
169 ActionInvocation setTargetInvocation = new ActionInvocation(svc.getAction("SetTarget"));
170 setTargetInvocation.setInput("NewTargetValue", true);
171 svc.getExecutor(setTargetInvocation.getAction()).execute(setTargetInvocation);
172 assertEquals(setTargetInvocation.getFailure(), null);
173 assertEquals(setTargetInvocation.getOutput().length, 0);
174
175 ActionInvocation getStatusInvocation = new ActionInvocation(svc.getAction("GetStatus"));
176 svc.getExecutor(getStatusInvocation.getAction()).execute(getStatusInvocation);
177 assertEquals(getStatusInvocation.getFailure(), null);
178 assertEquals(getStatusInvocation.getOutput().length, 1);
179 assertEquals(getStatusInvocation.getOutput()[0].toString(), "1");
180
181 setTargetInvocation = new ActionInvocation(svc.getAction("SetTarget"));
182 setTargetInvocation.setInput("NewTargetValue", false);
183 svc.getExecutor(setTargetInvocation.getAction()).execute(setTargetInvocation);
184 assertEquals(setTargetInvocation.getFailure(), null);
185 assertEquals(setTargetInvocation.getOutput().length, 0);
186
187 ActionInvocation queryStateVariableInvocation = new ActionInvocation(svc.getAction("QueryStateVariable"));
188 queryStateVariableInvocation.setInput("varName", "Status");
189 svc.getExecutor(queryStateVariableInvocation.getAction()).execute(queryStateVariableInvocation);
190 assertEquals(queryStateVariableInvocation.getFailure(), null);
191 assertEquals(queryStateVariableInvocation.getOutput().length, 1);
192 assertEquals(queryStateVariableInvocation.getOutput()[0].toString(), "0");
193
194 }
195
196 }