2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.oic.simulator.server.test;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
23 import org.oic.simulator.AttributeValue;
24 import org.oic.simulator.BooleanProperty;
25 import org.oic.simulator.DoubleProperty;
26 import org.oic.simulator.IntegerProperty;
27 import org.oic.simulator.InvalidArgsException;
28 import org.oic.simulator.SimulatorException;
29 import org.oic.simulator.SimulatorManager;
30 import org.oic.simulator.SimulatorResourceAttribute;
31 import org.oic.simulator.StringProperty;
32 import org.oic.simulator.server.SimulatorResource;
33 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
34 import org.oic.simulator.server.SimulatorSingleResource;
35 import org.oic.simulator.test.ExceptionType;
36 import org.oic.simulator.utils.ObjectHolder;
38 import junit.framework.TestCase;
40 public class SimulatorSingleResourceTest extends TestCase {
41 private static final String SINGLE_RES_RAML = "./ramls/oic.r.light.raml";
42 private static final String INT_KEY = "Integer";
43 private static final String DOUBLE_KEY = "Double";
44 private static final String BOOL_KEY = "Boolean";
45 private static final String STRING_KEY = "String";
46 private SimulatorSingleResource singleResource = null;
48 protected void setUp() throws Exception {
50 singleResource = (SimulatorSingleResource) SimulatorManager
51 .createResource(SimulatorResource.Type.SINGLE, "test-resource",
52 "/test/resource", "test.resource");
55 protected void tearDown() throws Exception {
57 singleResource = null;
60 public void testGetAttribute_P01() {
61 SimulatorResourceAttribute attribute = null;
64 IntegerProperty property = new IntegerProperty.Builder().build();
65 SimulatorResourceAttribute intAttribute = new SimulatorResourceAttribute(
66 INT_KEY, new AttributeValue(2), property);
68 assertTrue(singleResource.addAttribute(intAttribute));
69 attribute = singleResource.getAttribute(INT_KEY);
70 assertTrue(singleResource.removeAttribute(INT_KEY));
71 } catch (InvalidArgsException e) {
73 } catch (SimulatorException e) {
77 assertNotNull(attribute);
80 public void testGetAttribute_N01() {
81 ExceptionType exType = ExceptionType.UNKNOWN;
84 singleResource.getAttribute(null);
85 } catch (InvalidArgsException e) {
86 exType = ExceptionType.INVALID_ARGS;
87 } catch (SimulatorException e) {
88 exType = ExceptionType.SIMULATOR;
91 assertEquals(ExceptionType.INVALID_ARGS, exType);
94 public void testGetAttribute_N02() {
95 SimulatorResourceAttribute att = null;
98 att = singleResource.getAttribute("PQRS");
99 } catch (InvalidArgsException e) {
101 } catch (SimulatorException e) {
108 public void testAddAttributeInteger_P01() {
112 IntegerProperty property = new IntegerProperty.Builder().build();
113 SimulatorResourceAttribute intAttribute = new SimulatorResourceAttribute(
114 INT_KEY, new AttributeValue(2), property);
116 assertTrue(singleResource.addAttribute(intAttribute));
118 SimulatorResourceAttribute attribute = singleResource
119 .getAttribute(INT_KEY);
120 assertNotNull(attribute);
122 result = ((Integer) attribute.value().get()).intValue();
123 } catch (InvalidArgsException e) {
125 } catch (SimulatorException e) {
129 assertEquals(2, result);
132 public void testAddAttributeDouble_P01() {
136 DoubleProperty property = new DoubleProperty.Builder().build();
137 SimulatorResourceAttribute doubleAttribute = new SimulatorResourceAttribute(
138 DOUBLE_KEY, new AttributeValue(4.0), property);
140 assertTrue(singleResource.addAttribute(doubleAttribute));
142 SimulatorResourceAttribute attribute = singleResource
143 .getAttribute(DOUBLE_KEY);
144 assertNotNull(attribute);
146 result = ((Double) attribute.value().get()).doubleValue();
147 } catch (InvalidArgsException e) {
149 } catch (SimulatorException e) {
153 assertEquals(4.0, result);
156 public void testAddAttributeBoolean_P01() {
157 boolean result = false;
160 BooleanProperty property = new BooleanProperty.Builder().build();
161 SimulatorResourceAttribute boolAttribute = new SimulatorResourceAttribute(
162 BOOL_KEY, new AttributeValue(true), property);
164 assertTrue(singleResource.addAttribute(boolAttribute));
166 SimulatorResourceAttribute attribute = singleResource
167 .getAttribute(BOOL_KEY);
168 assertNotNull(attribute);
170 result = ((Boolean) attribute.value().get()).booleanValue();
171 } catch (InvalidArgsException e) {
173 } catch (SimulatorException e) {
177 assertEquals(true, result);
180 public void testAddAttributeString_P01() {
181 String result = null;
184 StringProperty property = new StringProperty.Builder().build();
185 SimulatorResourceAttribute stringAttribute = new SimulatorResourceAttribute(
186 STRING_KEY, new AttributeValue("string-value"), property);
188 assertTrue(singleResource.addAttribute(stringAttribute));
190 SimulatorResourceAttribute attribute = singleResource
191 .getAttribute(STRING_KEY);
192 assertNotNull(attribute);
194 result = (String) attribute.value().get();
195 } catch (InvalidArgsException e) {
197 } catch (SimulatorException e) {
201 assertEquals("string-value", result);
204 public void testRemoveAttribute_P01() {
205 SimulatorResourceAttribute result = null;
208 StringProperty property = new StringProperty.Builder().build();
209 SimulatorResourceAttribute stringAttribute = new SimulatorResourceAttribute(
210 STRING_KEY, new AttributeValue("friday"), property);
212 assertTrue(singleResource.addAttribute(stringAttribute));
213 assertTrue(singleResource.removeAttribute(STRING_KEY));
215 result = singleResource.getAttribute(STRING_KEY);
216 } catch (InvalidArgsException e) {
218 } catch (SimulatorException e) {
225 public void testRemoveAttribute_N02() {
226 ExceptionType exType = ExceptionType.UNKNOWN;
229 StringProperty property = new StringProperty.Builder().build();
230 SimulatorResourceAttribute stringAttribute = new SimulatorResourceAttribute(
231 STRING_KEY, new AttributeValue("friday"), property);
233 assertTrue(singleResource.addAttribute(stringAttribute));
234 singleResource.removeAttribute(null);
235 } catch (InvalidArgsException e) {
236 exType = ExceptionType.INVALID_ARGS;
237 } catch (SimulatorException e) {
238 exType = ExceptionType.SIMULATOR;
241 assertEquals(ExceptionType.INVALID_ARGS, exType);
244 public void testRemoveAttribute_N03() {
245 boolean result = false;
247 result = singleResource.removeAttribute("");
248 } catch (InvalidArgsException e) {
249 } catch (SimulatorException e) {
255 public void testUpdateAttributeInteger_P01() {
259 IntegerProperty property = new IntegerProperty.Builder().build();
260 SimulatorResourceAttribute intAttribute = new SimulatorResourceAttribute(
261 INT_KEY, new AttributeValue(10), property);
263 assertTrue(singleResource.addAttribute(intAttribute));
264 assertTrue(singleResource.updateAttribute(INT_KEY,
265 new AttributeValue(12)));
267 SimulatorResourceAttribute attribute = singleResource
268 .getAttribute(INT_KEY);
269 assertNotNull(attribute);
271 result = ((Integer) attribute.value().get()).intValue();
272 } catch (InvalidArgsException e) {
274 } catch (SimulatorException e) {
278 assertEquals(12, result);
281 public void testUpdateAttributeDouble_P01() {
285 DoubleProperty property = new DoubleProperty.Builder().build();
286 SimulatorResourceAttribute doubleAttribute = new SimulatorResourceAttribute(
287 DOUBLE_KEY, new AttributeValue(22.0), property);
289 assertTrue(singleResource.addAttribute(doubleAttribute));
290 assertTrue(singleResource.updateAttribute(DOUBLE_KEY,
291 new AttributeValue(25.3)));
293 SimulatorResourceAttribute attribute = singleResource
294 .getAttribute(DOUBLE_KEY);
295 assertNotNull(attribute);
297 result = ((Double) attribute.value().get()).doubleValue();
298 } catch (InvalidArgsException e) {
300 } catch (SimulatorException e) {
304 assertEquals(25.3, result);
307 public void testUpdateAttributeBoolean_P01() {
308 boolean result = true;
311 BooleanProperty property = new BooleanProperty.Builder().build();
312 SimulatorResourceAttribute boolAttribute = new SimulatorResourceAttribute(
313 BOOL_KEY, new AttributeValue(true), property);
315 assertTrue(singleResource.addAttribute(boolAttribute));
316 assertTrue(singleResource.updateAttribute(BOOL_KEY,
317 new AttributeValue(false)));
319 SimulatorResourceAttribute attribute = singleResource
320 .getAttribute(BOOL_KEY);
321 assertNotNull(attribute);
323 result = ((Boolean) attribute.value().get()).booleanValue();
324 } catch (InvalidArgsException e) {
326 } catch (SimulatorException e) {
330 assertEquals(false, result);
333 public void testupdateAttributeString_P01() {
334 String result = null;
337 StringProperty property = new StringProperty.Builder().build();
338 SimulatorResourceAttribute stringAttribute = new SimulatorResourceAttribute(
339 STRING_KEY, new AttributeValue("old-value"), property);
341 assertTrue(singleResource.addAttribute(stringAttribute));
342 assertTrue(singleResource.updateAttribute(STRING_KEY,
343 new AttributeValue("new-value")));
345 SimulatorResourceAttribute attribute = singleResource
346 .getAttribute(STRING_KEY);
347 assertNotNull(attribute);
349 result = (String) attribute.value().get();
350 } catch (InvalidArgsException e) {
352 } catch (SimulatorException e) {
356 assertEquals("new-value", result);
359 public void testStartResourceAutomation_P01() {
360 SimulatorSingleResource resource = createResourceFromRAML();
361 if (null == resource)
364 CountDownLatch lockObject = new CountDownLatch(1);
365 ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
366 AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
367 lockObject, autoUpdateHolder);
372 id = resource.startResourceUpdation(
373 SimulatorResource.AutoUpdateType.REPEAT, 1000,
377 lockObject.await(3, TimeUnit.SECONDS);
378 } catch (InterruptedException e) {
382 resource.stopUpdation(id);
383 } catch (InvalidArgsException e) {
385 } catch (SimulatorException e) {
391 } catch (SimulatorException e) {
395 assertNotNull(autoUpdateHolder.get());
396 assertEquals(id, autoUpdateHolder.get().getId());
399 public void testStartResourceAutomation_N01() {
400 ExceptionType exType = ExceptionType.UNKNOWN;
403 singleResource.startResourceUpdation(
404 SimulatorResource.AutoUpdateType.ONE_TIME, 500, null);
405 } catch (InvalidArgsException e) {
406 exType = ExceptionType.INVALID_ARGS;
407 } catch (SimulatorException e) {
408 exType = ExceptionType.SIMULATOR;
411 assertEquals(ExceptionType.INVALID_ARGS, exType);
414 public void testStartAttributeAutomation_P01() {
415 SimulatorSingleResource resource = createResourceFromRAML();
416 if (null == resource)
419 String attributeName = null;
421 for (Map.Entry<String, AttributeValue> entry : resource
422 .getResourceModel().get().entrySet()) {
423 attributeName = entry.getKey();
424 if (null != attributeName && !attributeName.isEmpty()) {
428 } catch (SimulatorException e1) {
429 e1.printStackTrace();
432 if (null == attributeName)
435 CountDownLatch lockObject = new CountDownLatch(1);
436 ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
437 AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
438 lockObject, autoUpdateHolder);
443 id = resource.startAttributeUpdation(attributeName,
444 SimulatorResource.AutoUpdateType.REPEAT, 1000,
448 lockObject.await(3, TimeUnit.SECONDS);
449 } catch (InterruptedException e) {
453 resource.stopUpdation(id);
454 } catch (InvalidArgsException e) {
456 } catch (SimulatorException e) {
462 } catch (SimulatorException e) {
466 assertNotNull(autoUpdateHolder.get());
467 assertEquals(id, autoUpdateHolder.get().getId());
470 public void testStartAttributeAutomation_N01() {
471 ExceptionType exType = ExceptionType.UNKNOWN;
474 singleResource.startAttributeUpdation("intensity",
475 SimulatorResource.AutoUpdateType.ONE_TIME, 1000, null);
476 } catch (InvalidArgsException e) {
477 exType = ExceptionType.INVALID_ARGS;
478 } catch (SimulatorException e) {
479 exType = ExceptionType.SIMULATOR;
482 assertEquals(ExceptionType.INVALID_ARGS, exType);
485 public void testStopUpdation_P01() {
486 SimulatorSingleResource resource = createResourceFromRAML();
487 if (null == resource)
490 CountDownLatch lockObject = new CountDownLatch(1);
491 ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
492 AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
493 lockObject, autoUpdateHolder);
494 boolean result = false;
497 int id = resource.startResourceUpdation(
498 SimulatorResource.AutoUpdateType.REPEAT, 1000,
500 resource.stopUpdation(id);
502 } catch (InvalidArgsException e) {
504 } catch (SimulatorException e) {
510 } catch (SimulatorException e) {
517 private SimulatorSingleResource createResourceFromRAML() {
519 return (SimulatorSingleResource) SimulatorManager
520 .createResource(SINGLE_RES_RAML);
521 } catch (InvalidArgsException e) {
523 } catch (SimulatorException e) {
531 class AutoUpdateInfo {
532 private String uri = null;
535 AutoUpdateInfo(String uri, int id) {
540 public String getUri() {
549 class AutoUpdateCompleteListener implements AutoUpdateListener {
550 private CountDownLatch lock;
551 private ObjectHolder<AutoUpdateInfo> autoUpdateHolder;
553 public AutoUpdateCompleteListener(CountDownLatch lock,
554 ObjectHolder<AutoUpdateInfo> autoUpdateHolder) {
556 this.autoUpdateHolder = autoUpdateHolder;
560 public void onUpdateComplete(String uri, int id) {
561 autoUpdateHolder.set(new AutoUpdateInfo(uri, id));