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;
19 import java.util.concurrent.CountDownLatch;
20 import java.util.concurrent.TimeUnit;
22 import org.oic.simulator.AttributeProperty;
23 import org.oic.simulator.AttributeValue;
24 import org.oic.simulator.InvalidArgsException;
25 import org.oic.simulator.SimulatorException;
26 import org.oic.simulator.SimulatorManager;
27 import org.oic.simulator.SimulatorResourceAttribute;
28 import org.oic.simulator.SimulatorResourceModel;
29 import org.oic.simulator.server.SimulatorResource;
30 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
31 import org.oic.simulator.server.SimulatorSingleResource;
32 import org.oic.simulator.test.ExceptionType;
33 import org.oic.simulator.utils.ObjectHolder;
35 import junit.framework.TestCase;
37 public class SimulatorSingleResourceTest extends TestCase {
39 System.loadLibrary("SimulatorManager");
40 System.loadLibrary("RamlParser");
41 System.loadLibrary("oc");
42 System.loadLibrary("oc_logger");
43 System.loadLibrary("octbstack");
46 private static final String SINGLE_RES_RAML = "./ramls/oic.r.light.raml";
47 private static final String INT_KEY = "Interger";
48 private static final String DOUBLE_KEY = "Double";
49 private static final String BOOL_KEY = "Boolean";
50 private static final String STRING_KEY = "String";
51 private SimulatorSingleResource singleResource = null;
53 protected void setUp() throws Exception {
55 singleResource = (SimulatorSingleResource) SimulatorManager
56 .createResource(SimulatorResource.Type.SINGLE, "test-resource",
57 "/test/resource", "test.resource");
60 protected void tearDown() throws Exception {
62 singleResource = null;
65 SimulatorSingleResource createResourceFromRAML() {
67 return (SimulatorSingleResource) SimulatorManager
68 .createResource(SINGLE_RES_RAML);
69 } catch (InvalidArgsException e) {
71 } catch (SimulatorException e) {
78 public void testGetResourceModel_P01() {
79 SimulatorResourceModel resModel = null;
82 SimulatorSingleResource resource = createResourceFromRAML();
83 resModel = resource.getResourceModel();
84 } catch (SimulatorException e) {
88 assertNotNull(resModel);
89 assertTrue(resModel.size() > 0);
92 public void testAddAttributeInteger_P01() {
96 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
97 INT_KEY, new AttributeValue(2), null);
98 singleResource.addAttribute(attribute);
99 result = ((Integer) singleResource.getAttribute(INT_KEY).value()
101 } catch (InvalidArgsException e) {
103 } catch (SimulatorException e) {
107 assertEquals(2, result);
110 public void testAddAttributeDouble_P01() {
114 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
115 DOUBLE_KEY, new AttributeValue(4.0), null);
116 singleResource.addAttribute(attribute);
117 result = ((Double) singleResource.getAttribute(DOUBLE_KEY).value()
118 .get()).doubleValue();
119 } catch (InvalidArgsException e) {
121 } catch (SimulatorException e) {
125 assertEquals(4.0, result);
128 public void testAddAttributeBoolean_P01() {
129 boolean result = false;
132 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
133 BOOL_KEY, new AttributeValue(true), null);
134 singleResource.addAttribute(attribute);
135 result = ((Boolean) singleResource.getAttribute(BOOL_KEY).value()
136 .get()).booleanValue();
137 } catch (InvalidArgsException e) {
139 } catch (SimulatorException e) {
143 assertEquals(true, result);
146 public void testaddAttributeString_P01() {
147 String result = null;
150 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
151 STRING_KEY, new AttributeValue("string-value"), null);
152 singleResource.addAttribute(attribute);
153 result = (String) singleResource.getAttribute(STRING_KEY).value()
155 } catch (InvalidArgsException e) {
157 } catch (SimulatorException e) {
161 assertEquals("string-value", result);
164 public void testUpdateAttributeInteger_P01() {
168 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
169 INT_KEY, new AttributeValue(10), null);
170 singleResource.addAttribute(attribute);
171 singleResource.updateAttribute(INT_KEY, new AttributeValue(12));
172 result = ((Integer) singleResource.getAttribute(INT_KEY).value()
174 } catch (InvalidArgsException e) {
176 } catch (SimulatorException e) {
180 assertEquals(12, result);
183 public void testUpdateAttributeDouble_P01() {
187 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
188 DOUBLE_KEY, new AttributeValue(22.0), null);
189 singleResource.addAttribute(attribute);
191 .updateAttribute(DOUBLE_KEY, new AttributeValue(25.3));
192 result = ((Double) singleResource.getAttribute(DOUBLE_KEY).value()
193 .get()).doubleValue();
194 } catch (InvalidArgsException e) {
196 } catch (SimulatorException e) {
200 assertEquals(25.3, result);
203 public void testUpdateAttributeBoolean_P01() {
204 boolean result = true;
207 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
208 BOOL_KEY, new AttributeValue(true), null);
209 singleResource.addAttribute(attribute);
210 singleResource.updateAttribute(BOOL_KEY, new AttributeValue(false));
211 result = ((Boolean) singleResource.getAttribute(BOOL_KEY).value()
212 .get()).booleanValue();
213 } catch (InvalidArgsException e) {
215 } catch (SimulatorException e) {
219 assertEquals(false, result);
222 public void testupdateAttributeString_P01() {
223 String result = null;
226 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
227 STRING_KEY, new AttributeValue("old-value"), null);
228 singleResource.addAttribute(attribute);
229 singleResource.updateAttribute(STRING_KEY, new AttributeValue(
231 result = (String) singleResource.getAttribute(STRING_KEY).value()
233 } catch (InvalidArgsException e) {
235 } catch (SimulatorException e) {
239 assertEquals("new-value", result);
242 public void testSetRange_P01() {
246 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
247 INT_KEY, new AttributeValue(10), new AttributeProperty(1,
249 singleResource.addAttribute(attribute);
250 singleResource.updateAttribute(INT_KEY, new AttributeValue(3));
251 result = ((Integer) singleResource.getAttribute(INT_KEY).value()
253 } catch (InvalidArgsException e) {
255 } catch (SimulatorException e) {
259 assertEquals(3, result);
262 public void testSetRange_N01() {
266 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
267 INT_KEY, new AttributeValue(10), new AttributeProperty(1,
269 singleResource.addAttribute(attribute);
270 singleResource.updateAttribute(INT_KEY, new AttributeValue(13));
271 result = ((Integer) singleResource.getAttribute(INT_KEY).value()
273 } catch (InvalidArgsException e) {
275 } catch (SimulatorException e) {
279 assertEquals(10, result);
282 public void testSetAllowedValuesInteger_P01() {
286 int[] values = { 1, 10, 20, 50 };
287 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
288 INT_KEY, new AttributeValue(10), new AttributeProperty(
290 singleResource.addAttribute(attribute);
291 singleResource.updateAttribute(INT_KEY, new AttributeValue(20));
292 result = ((Integer) singleResource.getAttribute(INT_KEY).value()
294 } catch (InvalidArgsException e) {
296 } catch (SimulatorException e) {
300 assertEquals(20, result);
303 public void testSetAllowedValuesInteger_N01() {
307 int[] values = { 1, 10, 20, 50 };
308 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
309 INT_KEY, new AttributeValue(10), new AttributeProperty(
311 singleResource.addAttribute(attribute);
312 singleResource.updateAttribute(INT_KEY, new AttributeValue(15));
313 result = ((Integer) singleResource.getAttribute(INT_KEY).value()
315 } catch (InvalidArgsException e) {
317 } catch (SimulatorException e) {
321 assertEquals(10, result);
324 public void testSetAllowedValuesDouble_P01() {
328 double[] values = { 11.5, 10.5, 20.5, 50.5 };
329 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
330 DOUBLE_KEY, new AttributeValue(11.5),
331 new AttributeProperty(values));
332 singleResource.addAttribute(attribute);
334 .updateAttribute(DOUBLE_KEY, new AttributeValue(10.5));
335 result = ((Double) singleResource.getAttribute(DOUBLE_KEY).value()
336 .get()).doubleValue();
337 } catch (InvalidArgsException e) {
339 } catch (SimulatorException e) {
343 assertEquals(10.5, result);
346 public void testSetAllowedValuesDouble_N01() {
350 double[] values = { 11.5, 10.5, 20.5, 50.5 };
351 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
352 DOUBLE_KEY, new AttributeValue(11.5),
353 new AttributeProperty(values));
354 singleResource.addAttribute(attribute);
355 singleResource.updateAttribute(DOUBLE_KEY, new AttributeValue(2.2));
356 result = ((Double) singleResource.getAttribute(DOUBLE_KEY).value()
357 .get()).doubleValue();
358 } catch (InvalidArgsException e) {
360 } catch (SimulatorException e) {
364 assertEquals(11.5, result);
367 public void testsetAllowedValuesString_P01() {
368 String result = null;
371 String[] values = { "monday", "tuesday", "wednesday" };
372 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
373 STRING_KEY, new AttributeValue("monday"),
374 new AttributeProperty(values));
375 singleResource.addAttribute(attribute);
376 singleResource.updateAttribute(STRING_KEY, new AttributeValue(
378 result = (String) singleResource.getAttribute(STRING_KEY).value()
380 } catch (InvalidArgsException e) {
382 } catch (SimulatorException e) {
386 assertEquals("tuesday", result);
389 public void testsetAllowedValuesString_N01() {
390 String result = null;
393 String[] values = { "monday", "tuesday", "wednesday" };
394 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
395 STRING_KEY, new AttributeValue("monday"),
396 new AttributeProperty(values));
397 singleResource.addAttribute(attribute);
398 singleResource.updateAttribute(STRING_KEY, new AttributeValue(
400 result = (String) singleResource.getAttribute(STRING_KEY).value()
402 } catch (InvalidArgsException e) {
404 } catch (SimulatorException e) {
408 assertEquals("monday", result);
411 public void testRemoveAttribute_P01() {
412 SimulatorResourceAttribute result = null;
415 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
416 STRING_KEY, new AttributeValue("friday"), null);
417 singleResource.addAttribute(attribute);
418 singleResource.removeAttribute(STRING_KEY);
419 result = singleResource.getAttribute(STRING_KEY);
420 } catch (InvalidArgsException e) {
422 } catch (SimulatorException e) {
429 public void testRemoveAttribute_N02() {
430 ExceptionType exType = ExceptionType.UNKNOWN;
433 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
434 STRING_KEY, new AttributeValue("friday"), null);
435 singleResource.addAttribute(attribute);
436 singleResource.removeAttribute(null);
437 } catch (InvalidArgsException e) {
438 exType = ExceptionType.INVALID_ARGS;
439 } catch (SimulatorException e) {
440 exType = ExceptionType.SIMULATOR;
443 assertEquals(ExceptionType.INVALID_ARGS, exType);
446 public void testRemoveAttribute_N03() {
447 ExceptionType exType = ExceptionType.UNKNOWN;
450 singleResource.removeAttribute("");
451 } catch (InvalidArgsException e) {
452 exType = ExceptionType.INVALID_ARGS;
453 } catch (SimulatorException e) {
454 exType = ExceptionType.SIMULATOR;
457 assertEquals(ExceptionType.INVALID_ARGS, exType);
460 public void testStartResourceAutomation_P01() {
461 SimulatorSingleResource resource = createResourceFromRAML();
462 if (null == resource)
465 CountDownLatch lockObject = new CountDownLatch(1);
466 ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
467 AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
468 lockObject, autoUpdateHolder);
473 id = resource.startResourceUpdation(
474 SimulatorResource.AutoUpdateType.REPEAT, 1000,
478 lockObject.await(10, TimeUnit.SECONDS);
479 } catch (InterruptedException e) {
483 resource.stopUpdation(id);
484 } catch (InvalidArgsException e) {
486 } catch (SimulatorException e) {
492 resource.stopUpdation(id);
493 } catch (SimulatorException e) {
497 assertNotNull(autoUpdateHolder.get());
498 assertEquals(id, autoUpdateHolder.get().getId());
501 public void testStartResourceAutomation_N01() {
502 ExceptionType exType = ExceptionType.UNKNOWN;
505 singleResource.startResourceUpdation(
506 SimulatorResource.AutoUpdateType.ONE_TIME, 500, null);
507 } catch (InvalidArgsException e) {
508 exType = ExceptionType.INVALID_ARGS;
509 } catch (SimulatorException e) {
510 exType = ExceptionType.SIMULATOR;
513 assertEquals(ExceptionType.INVALID_ARGS, exType);
516 public void testStartAttributeAutomation_P01() {
517 SimulatorSingleResource resource = createResourceFromRAML();
518 if (null == resource)
521 String attributeName = null;
523 for (SimulatorResourceAttribute resAttribute : resource
524 .getResourceModel().getAttributes().values())
525 attributeName = resAttribute.name();
526 } catch (SimulatorException e1) {
527 e1.printStackTrace();
530 if (null == attributeName)
533 CountDownLatch lockObject = new CountDownLatch(1);
534 ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
535 AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
536 lockObject, autoUpdateHolder);
541 id = resource.startAttributeUpdation(attributeName,
542 SimulatorResource.AutoUpdateType.REPEAT, 100,
546 lockObject.await(10, TimeUnit.SECONDS);
547 } catch (InterruptedException e) {
551 resource.stopUpdation(id);
552 } catch (InvalidArgsException e) {
554 } catch (SimulatorException e) {
560 } catch (SimulatorException e) {
564 assertNotNull(autoUpdateHolder.get());
565 assertEquals(id, autoUpdateHolder.get().getId());
568 public void testStartAttributeAutomation_N01() {
569 ExceptionType exType = ExceptionType.UNKNOWN;
572 singleResource.startAttributeUpdation("intensity",
573 SimulatorResource.AutoUpdateType.ONE_TIME, 1000, null);
574 } catch (InvalidArgsException e) {
575 exType = ExceptionType.INVALID_ARGS;
576 } catch (SimulatorException e) {
577 exType = ExceptionType.SIMULATOR;
580 assertEquals(ExceptionType.INVALID_ARGS, exType);
583 public void testStopUpdation_P01() {
584 SimulatorSingleResource resource = createResourceFromRAML();
585 if (null == resource)
588 CountDownLatch lockObject = new CountDownLatch(1);
589 ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
590 AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
591 lockObject, autoUpdateHolder);
592 boolean result = false;
595 int id = resource.startResourceUpdation(
596 SimulatorResource.AutoUpdateType.REPEAT, 1000,
598 resource.stopUpdation(id);
600 } catch (InvalidArgsException e) {
602 } catch (SimulatorException e) {
608 } catch (SimulatorException e) {
616 class AutoUpdateInfo {
617 private String uri = null;
620 AutoUpdateInfo(String uri, int id) {
625 public String getUri() {
634 class AutoUpdateCompleteListener implements AutoUpdateListener {
635 private CountDownLatch lock;
636 private ObjectHolder<AutoUpdateInfo> autoUpdateHolder;
638 public AutoUpdateCompleteListener(CountDownLatch lock,
639 ObjectHolder<AutoUpdateInfo> autoUpdateHolder) {
641 this.autoUpdateHolder = autoUpdateHolder;
645 public void onUpdateComplete(String uri, int id) {
646 autoUpdateHolder.set(new AutoUpdateInfo(uri, id));