1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package example.igd;
16
17 import org.fourthline.cling.UpnpService;
18 import org.fourthline.cling.mock.MockUpnpService;
19 import org.fourthline.cling.model.action.ActionInvocation;
20 import org.fourthline.cling.model.message.UpnpResponse;
21 import org.fourthline.cling.model.meta.LocalDevice;
22 import org.fourthline.cling.model.meta.LocalService;
23 import org.fourthline.cling.model.meta.Service;
24 import org.fourthline.cling.model.types.UDAServiceId;
25 import org.fourthline.cling.support.igd.callback.GetExternalIP;
26 import org.fourthline.cling.support.model.Connection;
27 import org.fourthline.cling.support.igd.callback.GetStatusInfo;
28 import org.testng.annotations.Test;
29
30 import static org.testng.Assert.assertEquals;
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class ConnectionInfoTest {
45
46 @Test
47 public void testStatusInfo() throws Exception {
48
49 final boolean[] tests = new boolean[1];
50
51 UpnpService upnpService = new MockUpnpService();
52
53 LocalDevice device = IGDSampleData.createIGDevice(TestConnection.class);
54 upnpService.getRegistry().addDevice(device);
55
56 Service service = device.findService(new UDAServiceId("WANIPConnection"));
57
58 upnpService.getControlPoint().execute(
59 new GetStatusInfo(service) {
60
61 @Override
62 protected void success(Connection.StatusInfo statusInfo) {
63 assertEquals(statusInfo.getStatus(), Connection.Status.Connected);
64 assertEquals(statusInfo.getUptimeSeconds(), 1000);
65 assertEquals(statusInfo.getLastError(), Connection.Error.ERROR_NONE);
66 tests[0] = true;
67 }
68
69 @Override
70 public void failure(ActionInvocation invocation,
71 UpnpResponse operation,
72 String defaultMsg) {
73
74 }
75 }
76 );
77
78 for (boolean test : tests) {
79 assert test;
80 }
81 for (boolean test : ((LocalService<TestConnection>) service).getManager().getImplementation().tests) {
82 assert test;
83 }
84
85 }
86
87 @Test
88 public void testIPAddress() throws Exception {
89
90 final boolean[] tests = new boolean[1];
91
92 UpnpService upnpService = new MockUpnpService();
93
94 LocalDevice device = IGDSampleData.createIGDevice(TestConnection.class);
95 upnpService.getRegistry().addDevice(device);
96
97 Service service = device.findService(new UDAServiceId("WANIPConnection"));
98
99 upnpService.getControlPoint().execute(
100 new GetExternalIP(service) {
101
102 @Override
103 protected void success(String externalIPAddress) {
104 assertEquals(externalIPAddress, "123.123.123.123");
105 tests[0] = true;
106 }
107
108 @Override
109 public void failure(ActionInvocation invocation,
110 UpnpResponse operation,
111 String defaultMsg) {
112
113 }
114 }
115 );
116
117 for (boolean test : tests) {
118 assert test;
119 }
120 for (boolean test : ((LocalService<TestConnection>) service).getManager().getImplementation().tests) {
121 assert test;
122 }
123
124 }
125 public static class TestConnection extends IGDSampleData.WANIPConnectionService {
126
127 boolean[] tests = new boolean[1];
128
129 @Override
130 public Connection.StatusInfo getStatusInfo() {
131 tests[0] = true;
132 return new Connection.StatusInfo(
133 Connection.Status.Connected,
134 1000,
135 Connection.Error.ERROR_NONE
136 );
137 }
138
139 @Override
140 public String getExternalIPAddress() {
141 tests[0] = true;
142 return "123.123.123.123";
143 }
144 }
145
146 }