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.model.meta.Action;
19  import org.fourthline.cling.model.meta.ActionArgument;
20  import org.fourthline.cling.model.meta.Service;
21  import org.fourthline.cling.model.meta.StateVariable;
22  import org.fourthline.cling.model.meta.StateVariableAllowedValueRange;
23  import org.fourthline.cling.model.meta.StateVariableEventDetails;
24  import org.fourthline.cling.model.meta.StateVariableTypeDetails;
25  import org.fourthline.cling.model.types.Datatype;
26  import org.fourthline.cling.model.types.ServiceId;
27  import org.fourthline.cling.model.types.ServiceType;
28  import org.fourthline.cling.model.types.UDAServiceId;
29  import org.fourthline.cling.model.types.UDAServiceType;
30  import org.seamless.util.URIUtil;
31  
32  import java.net.URI;
33  import java.net.URL;
34  
35  import static org.testng.Assert.assertEquals;
36  import static org.testng.Assert.assertTrue;
37  
38  /**
39   * @author Christian Bauer
40   */
41  public class SampleServiceOne extends SampleService {
42  
43      public static URL getDescriptorURL() {
44          return URIUtil.createAbsoluteURL(SampleDeviceRoot.getDeviceDescriptorURL(), getThisDescriptorURI());
45      }
46  
47      public static URI getThisDescriptorURI() {
48          return URI.create("service/upnp-org/MY-SERVICE-123/desc");
49      }
50  
51      public static URI getThisControlURI() {
52          return URI.create("service/upnp-org/MY-SERVICE-123/control");
53      }
54  
55      public static URI getThisEventSubscriptionURI() {
56          return URI.create("service/upnp-org/MY-SERVICE-123/events");
57      }
58  
59      public static ServiceId getThisServiceId() {
60          return new UDAServiceId("MY-SERVICE-123");
61      }
62  
63      public static ServiceType getThisServiceType() {
64          return new UDAServiceType("MY-SERVICE-TYPE-ONE", 1);
65      }
66  
67      @Override
68      public ServiceType getServiceType() {
69          return getThisServiceType();
70      }
71  
72      @Override
73      public ServiceId getServiceId() {
74          return getThisServiceId();
75      }
76  
77      @Override
78      public URI getDescriptorURI() {
79          return getThisDescriptorURI();
80      }
81  
82      @Override
83      public URI getControlURI() {
84          return getThisControlURI();
85      }
86  
87      @Override
88      public URI getEventSubscriptionURI() {
89          return getThisEventSubscriptionURI();
90      }
91  
92      @Override
93      public Action[] getActions() {
94          return new Action[]{
95                  new Action(
96                          "SetTarget",
97                          new ActionArgument[]{
98                                  new ActionArgument("NewTargetValue", "Target", ActionArgument.Direction.IN)
99                          }
100                 ),
101                 new Action(
102                         "GetTarget",
103                         new ActionArgument[]{
104                                 new ActionArgument("RetTargetValue", "Target", ActionArgument.Direction.OUT, true)
105                         }
106                 ),
107                 new Action(
108                         "GetStatus",
109                         new ActionArgument[]{
110                                 new ActionArgument("ResultStatus", "Status", ActionArgument.Direction.OUT)
111                         }
112                 )
113         };
114     }
115 
116     @Override
117     public StateVariable[] getStateVariables() {
118         return new StateVariable[]{
119                 new StateVariable(
120                         "Target",
121                         new StateVariableTypeDetails(Datatype.Builtin.BOOLEAN.getDatatype(), "0"),
122                         new StateVariableEventDetails(false)
123                 ),
124                 new StateVariable(
125                         "Status",
126                         new StateVariableTypeDetails(Datatype.Builtin.BOOLEAN.getDatatype(), "0")
127                 ),
128                 new StateVariable(
129                         "SomeVar",
130                         new StateVariableTypeDetails(Datatype.Builtin.STRING.getDatatype(), "foo", new String[]{"foo", "bar"}, null)
131                 ),
132                 new StateVariable(
133                         "AnotherVar",
134                         new StateVariableTypeDetails(Datatype.Builtin.UI4.getDatatype(), null, null, new StateVariableAllowedValueRange(0, 10, 2)),
135                         new StateVariableEventDetails(false)
136                 ),
137                 new StateVariable(
138                         "ModeratedMaxRateVar",
139                         new StateVariableTypeDetails(Datatype.Builtin.STRING.getDatatype()),
140                         new StateVariableEventDetails(true, 500, 0)
141                 ),
142                 new StateVariable(
143                         "ModeratedMinDeltaVar",
144                         new StateVariableTypeDetails(Datatype.Builtin.I4.getDatatype()),
145                         new StateVariableEventDetails(true, 0, 3)
146                 ),
147         };
148     }
149 
150     public static void assertMatch(Service a, Service b) {
151 
152         assertEquals(a.getActions().length, b.getActions().length);
153 
154         assertEquals(a.getAction("SetTarget").getName(), b.getAction("SetTarget").getName());
155         assertEquals(a.getAction("SetTarget").getArguments().length, b.getAction("SetTarget").getArguments().length);
156         assertEquals(a.getAction("SetTarget").getArguments()[0].getName(), a.getAction("SetTarget").getArguments()[0].getName());
157         assertEquals(a.getAction("SetTarget").getArguments()[0].getDirection(), b.getAction("SetTarget").getArguments()[0].getDirection());
158         assertEquals(a.getAction("SetTarget").getArguments()[0].getRelatedStateVariableName(), b.getAction("SetTarget").getArguments()[0].getRelatedStateVariableName());
159 
160         assertEquals(a.getAction("GetTarget").getArguments()[0].getName(), b.getAction("GetTarget").getArguments()[0].getName());
161         // TODO: UPNP VIOLATION: WMP12 will discard RenderingControl service if it contains <retval> tags
162         // assertEquals(a.getAction("GetTarget").getArguments()[0].isReturnValue(), b.getAction("GetTarget").getArguments()[0].isReturnValue());
163 
164         assertEquals(a.getStateVariables().length, b.getStateVariables().length);
165         assertTrue(a.getStateVariable("Target") != null);
166         assertTrue(b.getStateVariable("Target") != null);
167         assertTrue(a.getStateVariable("Status") != null);
168         assertTrue(b.getStateVariable("Status") != null);
169         assertTrue(a.getStateVariable("SomeVar") != null);
170         assertTrue(b.getStateVariable("SomeVar") != null);
171 
172         assertEquals(a.getStateVariable("Target").getName(), "Target");
173         assertEquals(a.getStateVariable("Target").getEventDetails().isSendEvents(), b.getStateVariable("Target").getEventDetails().isSendEvents());
174 
175         assertEquals(a.getStateVariable("Status").getName(), "Status");
176         assertEquals(a.getStateVariable("Status").getEventDetails().isSendEvents(), b.getStateVariable("Status").getEventDetails().isSendEvents());
177         assertEquals(a.getStateVariable("Status").getTypeDetails().getDatatype(), Datatype.Builtin.BOOLEAN.getDatatype());
178 
179         assertEquals(a.getStateVariable("SomeVar").getTypeDetails().getAllowedValues().length, b.getStateVariable("SomeVar").getTypeDetails().getAllowedValues().length);
180         assertEquals(a.getStateVariable("SomeVar").getTypeDetails().getDefaultValue(), b.getStateVariable("SomeVar").getTypeDetails().getDefaultValue());
181         assertEquals(a.getStateVariable("SomeVar").getTypeDetails().getAllowedValues()[0], b.getStateVariable("SomeVar").getTypeDetails().getAllowedValues()[0]);
182         assertEquals(a.getStateVariable("SomeVar").getTypeDetails().getAllowedValues()[1], b.getStateVariable("SomeVar").getTypeDetails().getAllowedValues()[1]);
183         assertEquals(a.getStateVariable("SomeVar").getEventDetails().isSendEvents(), b.getStateVariable("SomeVar").getEventDetails().isSendEvents());
184 
185         assertEquals(a.getStateVariable("AnotherVar").getTypeDetails().getAllowedValueRange().getMinimum(), b.getStateVariable("AnotherVar").getTypeDetails().getAllowedValueRange().getMinimum());
186         assertEquals(a.getStateVariable("AnotherVar").getTypeDetails().getAllowedValueRange().getMaximum(), b.getStateVariable("AnotherVar").getTypeDetails().getAllowedValueRange().getMaximum());
187         assertEquals(a.getStateVariable("AnotherVar").getTypeDetails().getAllowedValueRange().getStep(), b.getStateVariable("AnotherVar").getTypeDetails().getAllowedValueRange().getStep());
188         assertEquals(a.getStateVariable("AnotherVar").getEventDetails().isSendEvents(), b.getStateVariable("AnotherVar").getEventDetails().isSendEvents());
189     }
190 
191 }