1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package example.localservice;
17
18 import org.fourthline.cling.binding.LocalServiceBinder;
19 import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
20 import org.fourthline.cling.model.DefaultServiceManager;
21 import org.fourthline.cling.model.action.ActionInvocation;
22 import org.fourthline.cling.model.action.RemoteActionInvocation;
23 import org.fourthline.cling.model.message.Connection;
24 import org.fourthline.cling.model.message.UpnpHeaders;
25 import org.fourthline.cling.model.message.header.UpnpHeader;
26 import org.fourthline.cling.model.message.header.UserAgentHeader;
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.profile.RemoteClientInfo;
31 import org.fourthline.cling.model.types.UDADeviceType;
32 import org.fourthline.cling.test.data.SampleData;
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35
36 import java.net.InetAddress;
37 import java.net.UnknownHostException;
38
39 import static org.testng.Assert.assertEquals;
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 public class RemoteClientInfoTest {
71
72 public LocalDevice createTestDevice(Class serviceClass) throws Exception {
73
74 LocalServiceBinder binder = new AnnotationLocalServiceBinder();
75 LocalService svc = binder.read(serviceClass);
76 svc.setManager(new DefaultServiceManager(svc, serviceClass));
77
78 return new LocalDevice(
79 SampleData.createLocalDeviceIdentity(),
80 new UDADeviceType("BinaryLight", 1),
81 new DeviceDetails("Example Binary Light"),
82 svc
83 );
84 }
85
86 @DataProvider(name = "devices")
87 public Object[][] getDevices() {
88
89
90 try {
91 return new LocalDevice[][]{
92 {createTestDevice(SwitchPowerWithClientInfo.class)}
93 };
94 } catch (Exception ex) {
95 ex.printStackTrace(System.err);
96
97 throw new RuntimeException(ex);
98 }
99 }
100
101
102 @Test(dataProvider = "devices")
103 public void invokeActions(LocalDevice device) throws Exception {
104 LocalService svc = device.getServices()[0];
105
106 UpnpHeaders requestHeaders = new UpnpHeaders();
107 requestHeaders.add(UpnpHeader.Type.USER_AGENT, new UserAgentHeader("foo/bar"));
108 requestHeaders.add("X-MY-HEADER", "foo");
109
110 RemoteClientInfo clientInfo = new RemoteClientInfo(
111 new Connection() {
112 @Override
113 public boolean isOpen() {
114 return true;
115 }
116
117 @Override
118 public InetAddress getRemoteAddress() {
119 try {
120 return InetAddress.getByName("10.0.0.1");
121 } catch (UnknownHostException ex) {
122 throw new RuntimeException(ex);
123 }
124 }
125
126 @Override
127 public InetAddress getLocalAddress() {
128 try {
129 return InetAddress.getByName("10.0.0.2");
130 } catch (UnknownHostException ex) {
131 throw new RuntimeException(ex);
132 }
133 }
134 },
135 requestHeaders
136 );
137
138 ActionInvocation setTargetInvocation = new RemoteActionInvocation(
139 svc.getAction("SetTarget"), clientInfo
140 );
141
142 setTargetInvocation.setInput("NewTargetValue", true);
143 svc.getExecutor(setTargetInvocation.getAction()).execute(setTargetInvocation);
144 assertEquals(setTargetInvocation.getFailure(), null);
145 assertEquals(setTargetInvocation.getOutput().length, 0);
146
147 assertEquals(clientInfo.getExtraResponseHeaders().getFirstHeader("X-MY-HEADER"), "foobar");
148 }
149
150 }