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.meta.StateVariable;
26 import org.fourthline.cling.model.types.Datatype;
27 import org.fourthline.cling.model.types.DeviceType;
28 import org.fourthline.cling.test.data.SampleData;
29 import org.testng.annotations.DataProvider;
30 import org.testng.annotations.Test;
31
32 import static org.testng.Assert.assertEquals;
33
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 public class StringConvertibleTest {
72
73 public LocalDevice createTestDevice(Class serviceClass) throws Exception {
74
75 LocalServiceBinder binder = new AnnotationLocalServiceBinder();
76 LocalService svc = binder.read(serviceClass);
77 svc.setManager(new DefaultServiceManager(svc, serviceClass));
78
79 return new LocalDevice(
80 SampleData.createLocalDeviceIdentity(),
81 new DeviceType("mydomain", "CustomDevice", 1),
82 new DeviceDetails("A Custom Device"),
83 svc
84 );
85 }
86
87 @DataProvider(name = "devices")
88 public Object[][] getDevices() {
89
90
91 try {
92 return new LocalDevice[][]{
93 {createTestDevice(MyServiceWithStringConvertibles.class)},
94 };
95 } catch (Exception ex) {
96 ex.printStackTrace(System.err);
97
98 throw new RuntimeException(ex);
99 }
100 }
101
102 @Test(dataProvider = "devices")
103 public void validateBinding(LocalDevice device) {
104
105 LocalService svc = device.getServices()[0];
106
107 assertEquals(svc.getStateVariables().length, 4);
108 for (StateVariable stateVariable : svc.getStateVariables()) {
109 assertEquals(stateVariable.getTypeDetails().getDatatype().getBuiltin(), Datatype.Builtin.STRING);
110 }
111
112 assertEquals(svc.getActions().length, 9);
113
114 assertEquals(svc.getAction("SetMyURL").getArguments().length, 1);
115 assertEquals(svc.getAction("SetMyURL").getArguments()[0].getName(), "In");
116 assertEquals(svc.getAction("SetMyURL").getArguments()[0].getDirection(), ActionArgument.Direction.IN);
117 assertEquals(svc.getAction("SetMyURL").getArguments()[0].getRelatedStateVariableName(), "MyURL");
118
119
120 }
121
122 @Test(dataProvider = "devices")
123 public void invokeActions(LocalDevice device) {
124 LocalService svc = device.getServices()[0];
125
126 ActionInvocation setMyURL = new ActionInvocation(svc.getAction("SetMyURL"));
127 setMyURL.setInput("In", "http://foo/bar");
128 svc.getExecutor(setMyURL.getAction()).execute(setMyURL);
129 assertEquals(setMyURL.getFailure(), null);
130 assertEquals(setMyURL.getOutput().length, 0);
131
132 ActionInvocation getMyURL = new ActionInvocation(svc.getAction("GetMyURL"));
133 svc.getExecutor(getMyURL.getAction()).execute(getMyURL);
134 assertEquals(getMyURL.getFailure(), null);
135 assertEquals(getMyURL.getOutput().length, 1);
136 assertEquals(getMyURL.getOutput()[0].toString(), "http://foo/bar");
137
138 ActionInvocation setMyURI = new ActionInvocation(svc.getAction("SetMyURI"));
139 setMyURI.setInput("In", "http://foo/bar");
140 svc.getExecutor(setMyURI.getAction()).execute(setMyURI);
141 assertEquals(setMyURI.getFailure(), null);
142 assertEquals(setMyURI.getOutput().length, 0);
143
144 ActionInvocation getMyURI = new ActionInvocation(svc.getAction("GetMyURI"));
145 svc.getExecutor(getMyURI.getAction()).execute(getMyURI);
146 assertEquals(getMyURI.getFailure(), null);
147 assertEquals(getMyURI.getOutput().length, 1);
148 assertEquals(getMyURI.getOutput()[0].toString(), "http://foo/bar");
149
150 ActionInvocation setMyNumbers = new ActionInvocation(svc.getAction("SetMyNumbers"));
151 setMyNumbers.setInput("In", "1,2,3");
152 svc.getExecutor(setMyNumbers.getAction()).execute(setMyNumbers);
153 assertEquals(setMyNumbers.getFailure(), null);
154 assertEquals(setMyNumbers.getOutput().length, 0);
155
156 ActionInvocation getMyNumbers = new ActionInvocation(svc.getAction("GetMyNumbers"));
157 svc.getExecutor(getMyNumbers.getAction()).execute(getMyNumbers);
158 assertEquals(getMyNumbers.getFailure(), null);
159 assertEquals(getMyNumbers.getOutput().length, 1);
160 assertEquals(getMyNumbers.getOutput()[0].toString(), "1,2,3");
161
162 ActionInvocation setMyStringConvertible = new ActionInvocation(svc.getAction("SetMyStringConvertible"));
163 setMyStringConvertible.setInput("In", "foobar");
164 svc.getExecutor(setMyStringConvertible.getAction()).execute(setMyStringConvertible);
165 assertEquals(setMyStringConvertible.getFailure(), null);
166 assertEquals(setMyStringConvertible.getOutput().length, 0);
167
168 ActionInvocation getMyStringConvertible = new ActionInvocation(svc.getAction("GetMyStringConvertible"));
169 svc.getExecutor(getMyStringConvertible.getAction()).execute(getMyStringConvertible);
170 assertEquals(getMyStringConvertible.getFailure(), null);
171 assertEquals(getMyStringConvertible.getOutput().length, 1);
172 assertEquals(getMyStringConvertible.getOutput()[0].toString(), "foobar");
173
174 }
175 }