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  
16  package org.fourthline.cling.test.data;
17  
18  import org.fourthline.cling.DefaultUpnpServiceConfiguration;
19  import org.fourthline.cling.binding.LocalServiceBinder;
20  import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
21  import org.fourthline.cling.model.DefaultServiceManager;
22  import org.fourthline.cling.model.message.OutgoingDatagramMessage;
23  import org.fourthline.cling.model.meta.Action;
24  import org.fourthline.cling.model.meta.DeviceDetails;
25  import org.fourthline.cling.model.meta.DeviceIdentity;
26  import org.fourthline.cling.model.meta.Icon;
27  import org.fourthline.cling.model.meta.LocalDevice;
28  import org.fourthline.cling.model.meta.LocalService;
29  import org.fourthline.cling.model.meta.RemoteDevice;
30  import org.fourthline.cling.model.meta.RemoteDeviceIdentity;
31  import org.fourthline.cling.model.meta.RemoteService;
32  import org.fourthline.cling.model.meta.StateVariable;
33  import org.fourthline.cling.model.types.DeviceType;
34  import org.fourthline.cling.model.types.ServiceId;
35  import org.fourthline.cling.model.types.ServiceType;
36  import org.fourthline.cling.transport.impl.NetworkAddressFactoryImpl;
37  import org.fourthline.cling.transport.spi.DatagramProcessor;
38  
39  import java.lang.reflect.Constructor;
40  import java.net.InetAddress;
41  import java.net.MalformedURLException;
42  import java.net.URI;
43  import java.net.URL;
44  import java.net.UnknownHostException;
45  import java.util.logging.Logger;
46  
47  import org.fourthline.cling.model.profile.DeviceDetailsProvider;
48  
49  
50  public class SampleData {
51  
52      private static Logger log = Logger.getLogger(SampleData.class.getName());
53  
54      /* ###################################################################################### */
55  
56      public static InetAddress getLocalBaseAddress() {
57          try {
58              return InetAddress.getByName("127.0.0.1");
59          } catch (UnknownHostException ex) {
60              throw new RuntimeException(ex);
61          }
62      }
63  
64      public static URL getLocalBaseURL() {
65          try {
66              return new URL("http://127.0.0.1:" + NetworkAddressFactoryImpl.DEFAULT_TCP_HTTP_LISTEN_PORT + "/");
67          } catch (MalformedURLException e) {
68              throw new RuntimeException(e);
69          }
70      }
71  
72      /* ###################################################################################### */
73  
74      public static DeviceIdentity createLocalDeviceIdentity() {
75          return createLocalDeviceIdentity(1800);
76      }
77  
78      public static DeviceIdentity createLocalDeviceIdentity(int maxAgeSeconds) {
79          return new DeviceIdentity(SampleDeviceRoot.getRootUDN(), maxAgeSeconds);
80      }
81  
82      public static LocalDevice createLocalDevice() {
83          return createLocalDevice(false);
84      }
85  
86      public static LocalDevice createLocalDevice(boolean useProvider) {
87          return createLocalDevice(createLocalDeviceIdentity(), useProvider);
88      }
89  
90      public static Constructor<LocalDevice> getLocalDeviceConstructor() {
91          try {
92              return LocalDevice.class.getConstructor(
93                      DeviceIdentity.class, DeviceType.class, DeviceDetails.class,
94                      Icon[].class, LocalService.class, LocalDevice.class
95              );
96          } catch (Exception ex) {
97              throw new RuntimeException(ex);
98          }
99      }
100 
101     public static Constructor<LocalDevice> getLocalDeviceWithProviderConstructor() {
102         try {
103             return LocalDevice.class.getConstructor(
104                     DeviceIdentity.class, DeviceType.class, DeviceDetailsProvider.class,
105                     Icon[].class, LocalService.class, LocalDevice.class
106             );
107         } catch (Exception ex) {
108             throw new RuntimeException(ex);
109         }
110     }
111 
112     public static Constructor<LocalService> getLocalServiceConstructor() {
113         try {
114             return LocalService.class.getConstructor(
115                     ServiceType.class, ServiceId.class,
116                     Action[].class, StateVariable[].class
117             );
118         } catch (Exception ex) {
119             throw new RuntimeException(ex);
120         }
121     }
122 
123     public static LocalDevice createLocalDevice(DeviceIdentity identity) {
124         return createLocalDevice(identity, false);
125     }
126 
127     public static LocalDevice createLocalDevice(DeviceIdentity identity, boolean useProvider) {
128         try {
129 
130             Constructor<LocalDevice> ctor =
131                     useProvider
132                             ? getLocalDeviceWithProviderConstructor()
133                             : getLocalDeviceConstructor();
134 
135             Constructor<LocalService> serviceConstructor = getLocalServiceConstructor();
136 
137             return new SampleDeviceRootLocal(
138                     identity,
139                     new SampleServiceOne().newInstanceLocal(serviceConstructor),
140                     new SampleDeviceEmbeddedOne(
141                             new DeviceIdentity(SampleDeviceEmbeddedOne.getEmbeddedOneUDN(), identity),
142                             new SampleServiceTwo().newInstanceLocal(serviceConstructor),
143                             new SampleDeviceEmbeddedTwo(
144                                     new DeviceIdentity(SampleDeviceEmbeddedTwo.getEmbeddedTwoUDN(), identity),
145                                     new SampleServiceThree().newInstanceLocal(serviceConstructor),
146                                     null
147                             ).newInstance(ctor, useProvider)
148                     ).newInstance(ctor, useProvider)
149             ).newInstance(ctor, useProvider);
150 
151         } catch (Exception e) {
152             throw new RuntimeException(e);
153         }
154     }
155 
156     public static LocalService getFirstService(LocalDevice device) {
157         return device.getServices()[0];
158     }
159 
160     /* ###################################################################################### */
161 
162     public static RemoteDeviceIdentity createRemoteDeviceIdentity() {
163         return createRemoteDeviceIdentity(1800);
164     }
165 
166     public static RemoteDeviceIdentity createRemoteDeviceIdentity(int maxAgeSeconds) {
167         return new RemoteDeviceIdentity(
168                 SampleDeviceRoot.getRootUDN(),
169                 maxAgeSeconds,
170                 SampleDeviceRoot.getDeviceDescriptorURL(),
171                 null,
172                 getLocalBaseAddress()
173         );
174     }
175 
176     public static RemoteDevice createRemoteDevice() {
177         return createRemoteDevice(createRemoteDeviceIdentity());
178     }
179 
180     public static Constructor<RemoteDevice> getRemoteDeviceConstructor() {
181         try {
182             return RemoteDevice.class.getConstructor(
183                     RemoteDeviceIdentity.class, DeviceType.class, DeviceDetails.class,
184                     Icon[].class, RemoteService.class, RemoteDevice.class
185             );
186         } catch (Exception ex) {
187             throw new RuntimeException(ex);
188         }
189     }
190 
191     public static Constructor<RemoteService> getRemoteServiceConstructor() {
192         try {
193             return RemoteService.class.getConstructor(
194                     ServiceType.class, ServiceId.class,
195                     URI.class, URI.class, URI.class,
196                     Action[].class, StateVariable[].class
197             );
198         } catch (Exception ex) {
199             throw new RuntimeException(ex);
200         }
201     }
202 
203     public static RemoteDevice createRemoteDevice(RemoteDeviceIdentity identity) {
204         try {
205 
206             Constructor<RemoteDevice> ctor = getRemoteDeviceConstructor();
207             Constructor<RemoteService> serviceConstructor = getRemoteServiceConstructor();
208 
209             return new SampleDeviceRoot(
210                     identity,
211                     new SampleServiceOne().newInstanceRemote(serviceConstructor),
212                     new SampleDeviceEmbeddedOne(
213                             new RemoteDeviceIdentity(SampleDeviceEmbeddedOne.getEmbeddedOneUDN(), identity),
214                             new SampleServiceTwo().newInstanceRemote(serviceConstructor),
215                             new SampleDeviceEmbeddedTwo(
216                                     new RemoteDeviceIdentity(SampleDeviceEmbeddedTwo.getEmbeddedTwoUDN(), identity),
217                                     new SampleServiceThree().newInstanceRemote(serviceConstructor),
218                                     null
219                             ).newInstance(ctor)
220                     ).newInstance(ctor)
221             ).newInstance(ctor);
222 
223         } catch (Exception e) {
224 /*
225             Throwable cause = Exceptions.unwrap(e);
226             if (cause instanceof ValidationException) {
227                 ValidationException ex = (ValidationException) cause;
228                 for (ValidationError validationError : ex.getErrors()) {
229                     log.severe(validationError.toString());
230                 }
231             }
232 */
233             throw new RuntimeException(e);
234         }
235     }
236 
237     public static RemoteService getFirstService(RemoteDevice device) {
238         return device.getServices()[0];
239     }
240 
241     public static RemoteService createUndescribedRemoteService() {
242         RemoteService service =
243                 new SampleServiceOneUndescribed().newInstanceRemote(SampleData.getRemoteServiceConstructor());
244         new SampleDeviceRoot(
245                 SampleData.createRemoteDeviceIdentity(),
246                 service,
247                 null
248         ).newInstance(SampleData.getRemoteDeviceConstructor());
249         return service;
250     }
251 
252     /* ###################################################################################### */
253 
254     public static <T> LocalService<T> readService(Class<T> clazz) {
255         return readService(new AnnotationLocalServiceBinder().read(clazz), clazz);
256     }
257 
258     public static <T> LocalService<T> readService(LocalServiceBinder binder, Class<T> clazz) {
259         return readService(binder.read(clazz), clazz);
260     }
261 
262     public static <T> LocalService<T> readService(LocalService<T> service, Class<T> clazz) {
263         service.setManager(
264                 new DefaultServiceManager(service, clazz)
265         );
266         return service;
267     }
268 
269     /* ###################################################################################### */
270 
271     /*   public static void assertTestDataMatchServiceTwo(Service svc) {
272 
273             Service<DeviceService> service = svc;
274 
275             Device sampleDevice = (service.getDeviceService().getDevice().isLocal()) ? getLocalDevice() : getRemoteDevice();
276             Service<DeviceService> sampleService = getServiceTwo((Device) sampleDevice.getEmbeddedDevices().get(0));
277 
278             assertEquals(service.getActions().size(), sampleService.getActions().size());
279 
280             assertEquals(service.getActions().get("GetFoo").getName(), sampleService.getActions().get("GetFoo").getName());
281             assertEquals(service.getActions().get("GetFoo").getArguments().size(), sampleService.getActions().get("GetFoo").getArguments().size());
282             assertEquals(service.getActions().get("GetFoo").getArguments().get(0).getName(), service.getActions().get("GetFoo").getArguments().get(0).getName());
283             assertEquals(service.getActions().get("GetFoo").getArguments().get(0).getDirection(), sampleService.getActions().get("GetFoo").getArguments().get(0).getDirection());
284             assertEquals(service.getActions().get("GetFoo").getArguments().get(0).getRelatedStateVariableName(), sampleService.getActions().get("GetFoo").getArguments().get(0).getRelatedStateVariableName());
285 
286             assertEquals(service.getStateVariables().size(), sampleService.getStateVariables().size());
287             assertTrue(service.getStateVariables().containsKey("Foo"));
288 
289             assertEquals(service.getStateVariables().get("Foo").getName(), "Foo");
290             assertTrue(service.getStateVariables().get("Foo").isSendEvents());
291             assertEquals(service.getStateVariables().get("Foo").getDatatype(), Datatype.Builtin.BOOLEAN.getDatatype());
292 
293         }
294 
295         public static void assertTestDataMatchServiceThree(Service svc) {
296             assertTestDataMatchServiceTwo(svc);
297         }
298     */
299 
300     public static void debugMsg(OutgoingDatagramMessage msg) {
301         DatagramProcessor proc = new DefaultUpnpServiceConfiguration().getDatagramProcessor();
302         proc.write(msg);
303     }
304 
305 
306 }