View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * Getting connection information
34   * <p>
35   * The current connection information, including status, uptime, and last error message can be
36   * retrieved from a <em>WAN*Connection</em> service with the following callback:
37   * </p>
38   * <a class="citation" href="javacode://this#testStatusInfo" style="include: DOC1; exclude: EXC1"/>
39   * <p>
40   * Additionally, a callback for obtaining the external IP address of a connection is available:
41   * </p>
42   * <a class="citation" href="javacode://this#testIPAddress" style="include: DOC1; exclude: EXC1"/>
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"));         // DOC: DOC1
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;                                                        // DOC: EXC1
67                  }
68  
69                  @Override
70                  public void failure(ActionInvocation invocation,
71                                      UpnpResponse operation,
72                                      String defaultMsg) {
73                      // Something is wrong
74                  }
75              }
76          );                                                                                      // DOC: DOC1
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"));         // DOC: DOC1
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;                                                        // DOC: EXC1
106                 }
107 
108                 @Override
109                 public void failure(ActionInvocation invocation,
110                                     UpnpResponse operation,
111                                     String defaultMsg) {
112                     // Something is wrong
113                 }
114             }
115         );                                                                                      // DOC: DOC1
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 }