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.model;
17  
18  import org.fourthline.cling.binding.LocalServiceBinder;
19  import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
20  import org.fourthline.cling.binding.annotations.UpnpAction;
21  import org.fourthline.cling.binding.annotations.UpnpOutputArgument;
22  import org.fourthline.cling.binding.annotations.UpnpService;
23  import org.fourthline.cling.binding.annotations.UpnpServiceId;
24  import org.fourthline.cling.binding.annotations.UpnpServiceType;
25  import org.fourthline.cling.binding.annotations.UpnpStateVariable;
26  import org.fourthline.cling.model.meta.ActionArgument;
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.types.Datatype;
31  import org.fourthline.cling.model.types.UDADeviceType;
32  import org.fourthline.cling.test.data.SampleData;
33  import org.seamless.util.ByteArray;
34  import org.testng.annotations.DataProvider;
35  import org.testng.annotations.Test;
36  
37  import java.util.Random;
38  
39  import static org.testng.Assert.assertEquals;
40  
41  /**
42   * @author Christian Bauer
43   */
44  public class LocalServiceBindingDatatypesTest {
45  
46      public LocalDevice createTestDevice(LocalService service) throws Exception {
47          return new LocalDevice(
48                  SampleData.createLocalDeviceIdentity(),
49                  new UDADeviceType("TestDevice", 1),
50                  new DeviceDetails("Test Device"),
51                  service
52          );
53      }
54  
55      @DataProvider(name = "devices")
56      public Object[][] getDevices() throws Exception {
57  
58          // This is what we are actually testing
59          LocalServiceBinder binder = new AnnotationLocalServiceBinder();
60  
61          return new LocalDevice[][]{
62                  {createTestDevice(binder.read(TestServiceOne.class))},
63          };
64      }
65  
66      @Test(dataProvider = "devices")
67      public void validateBinding(LocalDevice device) {
68  
69          LocalService svc = SampleData.getFirstService(device);
70  
71          //System.out.println("############################################################################");
72          //ServiceDescriptorBinder binder = new DefaultRouterConfiguration().getServiceDescriptorBinderUDA10();
73          //System.out.println(binder.generate(svc));
74          //System.out.println("############################################################################");
75  
76          assertEquals(svc.getStateVariables().length, 1);
77          assertEquals(svc.getStateVariable("Data").getTypeDetails().getDatatype().getBuiltin(), Datatype.Builtin.BIN_BASE64);
78          assertEquals(svc.getStateVariable("Data").getEventDetails().isSendEvents(), false);
79  
80          assertEquals(svc.getActions().length, 1);
81  
82          assertEquals(svc.getAction("GetData").getName(), "GetData");
83          assertEquals(svc.getAction("GetData").getArguments().length, 1);
84          assertEquals(svc.getAction("GetData").getArguments()[0].getName(), "RandomData");
85          assertEquals(svc.getAction("GetData").getArguments()[0].getDirection(), ActionArgument.Direction.OUT);
86          assertEquals(svc.getAction("GetData").getArguments()[0].getRelatedStateVariableName(), "Data");
87          assertEquals(svc.getAction("GetData").getArguments()[0].isReturnValue(), true);
88  
89      }
90  
91      /* ####################################################################################################### */
92  
93      @UpnpService(
94              serviceId = @UpnpServiceId("SomeService"),
95              serviceType = @UpnpServiceType(value = "SomeService", version = 1),
96              supportsQueryStateVariables = false
97      )
98      public static class TestServiceOne {
99  
100         public TestServiceOne() {
101             data = new byte[8];
102             new Random().nextBytes(data);
103         }
104 
105         @UpnpStateVariable(sendEvents = false)
106         private byte[] data;
107 
108         @UpnpAction(out = @UpnpOutputArgument(name = "RandomData"))
109         public byte[] getData() {
110             return data;
111         }
112     }
113 
114 
115 }