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.localservice;
16  
17  import org.fourthline.cling.binding.LocalServiceBinder;
18  import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
19  import org.fourthline.cling.model.DefaultServiceManager;
20  import org.fourthline.cling.model.action.ActionInvocation;
21  import org.fourthline.cling.model.meta.ActionArgument;
22  import org.fourthline.cling.model.meta.DeviceDetails;
23  import org.fourthline.cling.model.meta.LocalDevice;
24  import org.fourthline.cling.model.meta.LocalService;
25  import org.fourthline.cling.model.types.Datatype;
26  import org.fourthline.cling.model.types.DeviceType;
27  import org.fourthline.cling.test.data.SampleData;
28  import org.testng.annotations.DataProvider;
29  import org.testng.annotations.Test;
30  
31  import static org.testng.Assert.assertEquals;
32  
33  /**
34   * Working with enums
35   * <p>
36   * Java <code>enum</code>'s are special, unfortunately: Cling can convert
37   * your enum value into a string for transport in UPnP messages, but you
38   * have to convert it back manually from a string. This is shown in the
39   * following service example:
40   * </p>
41   * <a class="citation" href="javacode://example.localservice.MyServiceWithEnum" style="include: INC1"/>
42   * <p>
43   * Cling will automatically assume that the datatype is a UPnP string if the
44   * field (or getter) or getter Java type is an enum. Furthermore, an
45   * <code>&lt;allowedValueList&gt;</code> will be created in your service descriptor
46   * XML, so control points know that this state variable has in fact a defined
47   * set of possible values.
48   * </p>
49   */
50  public class EnumTest {
51  
52      public LocalDevice createTestDevice(Class serviceClass) throws Exception {
53  
54          LocalServiceBinder binder = new AnnotationLocalServiceBinder();
55          LocalService svc = binder.read(serviceClass);
56          svc.setManager(new DefaultServiceManager(svc, serviceClass));
57  
58          return new LocalDevice(
59                  SampleData.createLocalDeviceIdentity(),
60                  new DeviceType("mydomain", "CustomDevice", 1),
61                  new DeviceDetails("A Custom Device"),
62                  svc
63          );
64      }
65  
66      @DataProvider(name = "devices")
67      public Object[][] getDevices() {
68  
69  
70          try {
71              return new LocalDevice[][]{
72                      {createTestDevice(MyServiceWithEnum.class)},
73              };
74          } catch (Exception ex) {
75              ex.printStackTrace(System.err);
76              // Damn testng swallows exceptions in provider/factory methods
77              throw new RuntimeException(ex);
78          }
79      }
80  
81      @Test(dataProvider = "devices")
82      public void validateBinding(LocalDevice device) {
83  
84          LocalService svc = device.getServices()[0];
85  
86          assertEquals(svc.getStateVariables().length, 1);
87          assertEquals(svc.getStateVariables()[0].getTypeDetails().getDatatype().getBuiltin(), Datatype.Builtin.STRING);
88  
89          assertEquals(svc.getActions().length, 3); // Has 2 actions plus QueryStateVariableAction!
90  
91          assertEquals(svc.getAction("GetColor").getArguments().length, 1);
92          assertEquals(svc.getAction("GetColor").getArguments()[0].getName(), "Out");
93          assertEquals(svc.getAction("GetColor").getArguments()[0].getDirection(), ActionArgument.Direction.OUT);
94          assertEquals(svc.getAction("GetColor").getArguments()[0].getRelatedStateVariableName(), "Color");
95  
96          assertEquals(svc.getAction("SetColor").getArguments().length, 1);
97          assertEquals(svc.getAction("SetColor").getArguments()[0].getName(), "In");
98          assertEquals(svc.getAction("SetColor").getArguments()[0].getDirection(), ActionArgument.Direction.IN);
99          assertEquals(svc.getAction("SetColor").getArguments()[0].getRelatedStateVariableName(), "Color");
100 
101     }
102 
103     @Test(dataProvider = "devices")
104     public void invokeActions(LocalDevice device) {
105         LocalService svc = device.getServices()[0];
106 
107         ActionInvocation setColor = new ActionInvocation(svc.getAction("SetColor"));
108         setColor.setInput("In", MyServiceWithEnum.Color.Blue);
109         svc.getExecutor(setColor.getAction()).execute(setColor);
110         assertEquals(setColor.getFailure(), null);
111         assertEquals(setColor.getOutput().length, 0);
112 
113         ActionInvocation getColor = new ActionInvocation(svc.getAction("GetColor"));
114         svc.getExecutor(getColor.getAction()).execute(getColor);
115         assertEquals(getColor.getFailure(), null);
116         assertEquals(getColor.getOutput().length, 1);
117         assertEquals(getColor.getOutput()[0].toString(), MyServiceWithEnum.Color.Blue.name());
118 
119     }
120 }