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.test;
19 import java.util.Vector;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
23 import org.oic.simulator.DeviceInfo;
24 import org.oic.simulator.DeviceListener;
25 import org.oic.simulator.InvalidArgsException;
26 import org.oic.simulator.PlatformInfo;
27 import org.oic.simulator.PlatformListener;
28 import org.oic.simulator.SimulatorException;
29 import org.oic.simulator.SimulatorManager;
30 import org.oic.simulator.client.FindResourceListener;
31 import org.oic.simulator.client.SimulatorRemoteResource;
32 import org.oic.simulator.server.SimulatorResource;
33 import org.oic.simulator.utils.ObjectHolder;
34 import org.oic.simulator.utils.SampleSingleResource;
36 import junit.framework.TestCase;
39 * This class tests the functionality of Simulator Manager class APIs.
41 public class SimulatorManagerTest extends TestCase {
42 private static final String SINGLE_RES_RAML = "./ramls/oic.r.light.raml";
43 private static final String COLLECTION_RES_RAML = "./ramls/oic.d.airconditioner.raml";
46 System.loadLibrary("SimulatorManager");
47 System.loadLibrary("RamlParser");
48 System.loadLibrary("oc");
49 System.loadLibrary("oc_logger");
50 System.loadLibrary("octbstack");
54 protected void setUp() throws Exception {
59 protected void tearDown() throws Exception {
63 public void testCreateResource_P01() {
64 SimulatorResource resource = null;
65 SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
68 resource = SimulatorManager.createResource(SINGLE_RES_RAML);
69 resType = resource.getType();
70 } catch (InvalidArgsException e) {
72 } catch (SimulatorException e) {
76 assertNotNull(resource);
77 assertTrue(resType == SimulatorResource.Type.SINGLE);
80 public void testCreateResource_P02() {
81 SimulatorResource resource = null;
82 SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
85 resource = SimulatorManager.createResource(COLLECTION_RES_RAML);
86 resType = resource.getType();
87 } catch (InvalidArgsException e) {
89 } catch (SimulatorException e) {
93 assertNotNull(resource);
94 assertTrue(resType == SimulatorResource.Type.COLLECTION);
97 public void testCreateResource_N01() {
98 ExceptionType exType = ExceptionType.UNKNOWN;
101 String configPath = "";
102 SimulatorResource resource = SimulatorManager
103 .createResource(configPath);
104 } catch (InvalidArgsException e) {
105 exType = ExceptionType.INVALID_ARGS;
106 } catch (SimulatorException e) {
107 exType = ExceptionType.SIMULATOR;
110 assertTrue(exType == ExceptionType.INVALID_ARGS);
113 public void testCreateResource_N02() {
114 ExceptionType exType = ExceptionType.UNKNOWN;
117 SimulatorResource resource = SimulatorManager.createResource(null);
118 } catch (InvalidArgsException e) {
119 exType = ExceptionType.INVALID_ARGS;
120 } catch (SimulatorException e) {
121 exType = ExceptionType.SIMULATOR;
124 assertTrue(exType == ExceptionType.INVALID_ARGS);
127 public void testCreateResourceWithCount_P01() {
128 Vector<SimulatorResource> resources = null;
129 SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
132 resources = SimulatorManager.createResource(SINGLE_RES_RAML, 1);
133 resType = resources.elementAt(0).getType();
134 } catch (InvalidArgsException e) {
136 } catch (SimulatorException e) {
140 assertNotNull(resources);
141 assertTrue(resources.size() == 1);
142 assertTrue(resType == SimulatorResource.Type.SINGLE);
145 public void testCreateResourceWithCount_P02() {
146 Vector<SimulatorResource> resources = null;
147 SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
150 resources = SimulatorManager.createResource(COLLECTION_RES_RAML, 1);
151 resType = resources.elementAt(0).getType();
152 } catch (InvalidArgsException e) {
154 } catch (SimulatorException e) {
158 assertNotNull(resources);
159 assertTrue(resources.size() == 1);
160 assertTrue(resType == SimulatorResource.Type.COLLECTION);
163 public void testCreateResourceWithCount_N01() {
164 ExceptionType exType = ExceptionType.UNKNOWN;
167 String configPath = "";
168 Vector<SimulatorResource> resource = SimulatorManager
169 .createResource(configPath, 1);
170 } catch (InvalidArgsException e) {
171 exType = ExceptionType.INVALID_ARGS;
172 } catch (SimulatorException e) {
173 exType = ExceptionType.SIMULATOR;
176 assertTrue(exType == ExceptionType.INVALID_ARGS);
179 public void testCreateResourceWithCount_N02() {
180 ExceptionType exType = ExceptionType.UNKNOWN;
183 Vector<SimulatorResource> resource = SimulatorManager
184 .createResource(null, 1);
185 } catch (InvalidArgsException e) {
186 exType = ExceptionType.INVALID_ARGS;
187 } catch (SimulatorException e) {
188 exType = ExceptionType.SIMULATOR;
191 assertTrue(exType == ExceptionType.INVALID_ARGS);
194 public void testCreateResourceWithCount_N03() {
195 ExceptionType exType = ExceptionType.UNKNOWN;
198 Vector<SimulatorResource> resource = SimulatorManager
199 .createResource(SINGLE_RES_RAML, 0);
200 } catch (InvalidArgsException e) {
201 exType = ExceptionType.INVALID_ARGS;
202 } catch (SimulatorException e) {
203 exType = ExceptionType.SIMULATOR;
206 assertTrue(exType == ExceptionType.INVALID_ARGS);
209 public void testCreateResourceWithCount_N04() {
210 ExceptionType exType = ExceptionType.UNKNOWN;
213 Vector<SimulatorResource> resource = SimulatorManager
214 .createResource(SINGLE_RES_RAML, -1);
215 } catch (InvalidArgsException e) {
216 exType = ExceptionType.INVALID_ARGS;
217 } catch (SimulatorException e) {
218 exType = ExceptionType.SIMULATOR;
221 assertTrue(exType == ExceptionType.INVALID_ARGS);
224 public void testCreateResourceByType_P01() {
225 SimulatorResource resource = null;
226 SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
229 String name = "test-singleresource";
230 String uri = "/test/singleresource";
231 String resourceType = "test.singleresource";
233 resource = SimulatorManager.createResource(
234 SimulatorResource.Type.SINGLE, name, uri, resourceType);
235 resType = resource.getType();
236 } catch (InvalidArgsException e) {
238 } catch (SimulatorException e) {
242 assertNotNull(resource);
243 assertTrue(resType == SimulatorResource.Type.SINGLE);
246 public void testCreateResourceByType_P02() {
247 SimulatorResource resource = null;
248 SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
251 String name = "test-collectionresource";
252 String uri = "/test/collectionresource";
253 String resourceType = "test.collectionresource";
255 resource = SimulatorManager.createResource(
256 SimulatorResource.Type.COLLECTION, name, uri, resourceType);
257 resType = resource.getType();
258 } catch (InvalidArgsException e) {
260 } catch (SimulatorException e) {
264 assertNotNull(resource);
265 assertTrue(resType == SimulatorResource.Type.COLLECTION);
268 public void testCreateResourceByType_N01() {
269 ExceptionType exType = ExceptionType.INVALID_ARGS;
273 String uri = "/test/resource";
274 String resourceType = "test.resource";
276 SimulatorResource resource = SimulatorManager.createResource(
277 SimulatorResource.Type.SINGLE, name, uri, resourceType);
278 } catch (InvalidArgsException e) {
279 exType = ExceptionType.INVALID_ARGS;
280 } catch (SimulatorException e) {
281 exType = ExceptionType.SIMULATOR;
284 assertTrue(exType == ExceptionType.INVALID_ARGS);
287 public void testCreateResourceByType_N02() {
288 ExceptionType exType = ExceptionType.INVALID_ARGS;
292 String uri = "/test/resource";
293 String resourceType = "test.resource";
295 SimulatorResource resource = SimulatorManager.createResource(
296 SimulatorResource.Type.SINGLE, name, uri, resourceType);
297 } catch (InvalidArgsException e) {
298 exType = ExceptionType.INVALID_ARGS;
299 } catch (SimulatorException e) {
300 exType = ExceptionType.SIMULATOR;
303 assertTrue(exType == ExceptionType.INVALID_ARGS);
306 public void testCreateResourceByType_N03() {
307 ExceptionType exType = ExceptionType.INVALID_ARGS;
310 String name = "test-resource";
312 String resourceType = "test.resource";
314 SimulatorResource resource = SimulatorManager.createResource(
315 SimulatorResource.Type.SINGLE, name, uri, resourceType);
316 } catch (InvalidArgsException e) {
317 exType = ExceptionType.INVALID_ARGS;
318 } catch (SimulatorException e) {
319 exType = ExceptionType.SIMULATOR;
322 assertTrue(exType == ExceptionType.INVALID_ARGS);
325 public void testCreateResourceByType_N04() {
326 ExceptionType exType = ExceptionType.INVALID_ARGS;
329 String name = "test-resource";
331 String resourceType = "test.resource";
333 SimulatorResource resource = SimulatorManager.createResource(
334 SimulatorResource.Type.SINGLE, name, uri, resourceType);
335 } catch (InvalidArgsException e) {
336 exType = ExceptionType.INVALID_ARGS;
337 } catch (SimulatorException e) {
338 exType = ExceptionType.SIMULATOR;
341 assertTrue(exType == ExceptionType.INVALID_ARGS);
344 public void testCreateResourceByType_N05() {
345 ExceptionType exType = ExceptionType.INVALID_ARGS;
348 String name = "test-resource";
349 String uri = "/test/resource";;
350 String resourceType = "";
352 SimulatorResource resource = SimulatorManager.createResource(
353 SimulatorResource.Type.SINGLE, name, uri, resourceType);
354 } catch (InvalidArgsException e) {
355 exType = ExceptionType.INVALID_ARGS;
356 } catch (SimulatorException e) {
357 exType = ExceptionType.SIMULATOR;
360 assertTrue(exType == ExceptionType.INVALID_ARGS);
363 public void testCreateResourceByType_N06() {
364 ExceptionType exType = ExceptionType.INVALID_ARGS;
367 String name = "test-resource";
368 String uri = "/test/resource";;
369 String resourceType = null;
371 SimulatorResource resource = SimulatorManager.createResource(
372 SimulatorResource.Type.SINGLE, name, uri, resourceType);
373 } catch (InvalidArgsException e) {
374 exType = ExceptionType.INVALID_ARGS;
375 } catch (SimulatorException e) {
376 exType = ExceptionType.SIMULATOR;
379 assertTrue(exType == ExceptionType.INVALID_ARGS);
382 public void testFindResource_P01() {
383 // Creating sample single resource to test this API
384 SampleSingleResource testResource = new SampleSingleResource();
385 if (false == testResource.start())
388 CountDownLatch lockObject = new CountDownLatch(1);
389 boolean syncResult = false;
390 ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
391 FindResourceCallbackListener listener = new FindResourceCallbackListener(
392 lockObject, resourceHolder);
395 String resourceType = testResource.mResourceType;
396 SimulatorManager.findResource(resourceType, listener);
398 } catch (InvalidArgsException e) {
400 } catch (SimulatorException e) {
404 if (false == syncResult)
407 assertTrue(syncResult);
409 // Wait for the resource to found
411 lockObject.await(10, TimeUnit.SECONDS);
412 } catch (InterruptedException e) {
416 assertNotNull(resourceHolder.get());
419 public void testFindResource_N01() {
420 ExceptionType exType = ExceptionType.UNKNOWN;
423 String resourceType = "test.singleresource";
424 SimulatorManager.findResource(resourceType, null);
425 } catch (InvalidArgsException e) {
426 exType = ExceptionType.INVALID_ARGS;
427 } catch (SimulatorException e) {
428 exType = ExceptionType.SIMULATOR;
431 assertTrue(exType == ExceptionType.INVALID_ARGS);
434 public void testFindResource_N02() {
435 ExceptionType exType = ExceptionType.UNKNOWN;
438 String resourceType = "";
439 SimulatorManager.findResource(resourceType,
440 new FindResourceListener() {
442 public void onResourceFound(
443 SimulatorRemoteResource resource) {
446 } catch (InvalidArgsException e) {
447 exType = ExceptionType.INVALID_ARGS;
448 } catch (SimulatorException e) {
449 exType = ExceptionType.SIMULATOR;
452 assertTrue(exType == ExceptionType.INVALID_ARGS);
455 public void testFindResource_N03() {
456 ExceptionType exType = ExceptionType.UNKNOWN;
459 String resourceType = null;
460 SimulatorManager.findResource(resourceType,
461 new FindResourceListener() {
463 public void onResourceFound(
464 SimulatorRemoteResource resource) {
467 } catch (InvalidArgsException e) {
468 exType = ExceptionType.INVALID_ARGS;
469 } catch (SimulatorException e) {
470 exType = ExceptionType.SIMULATOR;
473 assertTrue(exType == ExceptionType.INVALID_ARGS);
476 public void testFindResourceAll_P01() {
477 // Creating sample single resource to test this API
478 SampleSingleResource testResource = new SampleSingleResource();
479 if (false == testResource.start())
482 CountDownLatch lockObject = new CountDownLatch(1);
483 boolean syncResult = false;
484 ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
485 FindResourceCallbackListener listener = new FindResourceCallbackListener(
486 lockObject, resourceHolder);
489 SimulatorManager.findResource(listener);
491 } catch (InvalidArgsException e) {
493 } catch (SimulatorException e) {
497 if (false == syncResult)
500 assertTrue(syncResult);
502 // Wait for the resource to found
504 lockObject.await(10, TimeUnit.SECONDS);
505 } catch (InterruptedException e) {
509 assertNotNull(resourceHolder.get());
512 public void testFindResourceAll_N01() {
513 ExceptionType exType = ExceptionType.UNKNOWN;
516 SimulatorManager.findResource(null);
517 } catch (InvalidArgsException e) {
518 exType = ExceptionType.INVALID_ARGS;
519 } catch (SimulatorException e) {
520 exType = ExceptionType.SIMULATOR;
523 assertTrue(exType == ExceptionType.INVALID_ARGS);
526 public void testregisterDeviceInfo_P01() {
527 boolean syncResult = false;
530 String deviceName = "test-device";
531 SimulatorManager.setDeviceInfo(deviceName);
533 } catch (InvalidArgsException e) {
535 } catch (SimulatorException e) {
539 assertTrue(syncResult);
542 public void testregisterDeviceInfo_N01() {
543 ExceptionType exType = ExceptionType.UNKNOWN;
546 String deviceName = "";
547 SimulatorManager.setDeviceInfo(deviceName);
548 } catch (InvalidArgsException e) {
549 exType = ExceptionType.INVALID_ARGS;
550 } catch (SimulatorException e) {
551 exType = ExceptionType.SIMULATOR;
554 assertTrue(exType == ExceptionType.INVALID_ARGS);
557 public void testregisterDeviceInfo_N02() {
558 ExceptionType exType = ExceptionType.UNKNOWN;
561 String deviceName = null;
562 SimulatorManager.setDeviceInfo(deviceName);
563 } catch (InvalidArgsException e) {
564 exType = ExceptionType.INVALID_ARGS;
565 } catch (SimulatorException e) {
566 exType = ExceptionType.SIMULATOR;
569 assertTrue(exType == ExceptionType.INVALID_ARGS);
572 public void testregisterPlatformInfo_P01() {
573 boolean syncResult = false;
576 PlatformInfo platformInfo = new PlatformInfo();
577 platformInfo.setManufacturerName("Samsung");
579 SimulatorManager.setPlatformInfo(platformInfo);
581 } catch (InvalidArgsException e) {
583 } catch (SimulatorException e) {
587 assertTrue(syncResult);
590 public void testregisterPlatformInfo_N01() {
591 ExceptionType exType = ExceptionType.UNKNOWN;
594 PlatformInfo platformInfo = null;
595 SimulatorManager.setPlatformInfo(platformInfo);
596 } catch (InvalidArgsException e) {
597 exType = ExceptionType.INVALID_ARGS;
598 } catch (SimulatorException e) {
599 exType = ExceptionType.SIMULATOR;
602 assertTrue(exType == ExceptionType.INVALID_ARGS);
605 public void testFindDevices_P01() {
606 CountDownLatch lockObject = new CountDownLatch(1);
607 boolean syncResult = false;
608 ObjectHolder<DeviceInfo> infoHolder = new ObjectHolder<>();
609 DeviceInfoListener listener = new DeviceInfoListener(lockObject,
613 SimulatorManager.setDeviceInfo("Samsung");
614 SimulatorManager.findDevices(listener);
616 } catch (InvalidArgsException e) {
618 } catch (SimulatorException e) {
622 assertTrue(syncResult);
624 // Wait for the asynchronous response
626 lockObject.await(10, TimeUnit.SECONDS);
627 } catch (InterruptedException e) {
630 assertNotNull(infoHolder.get());
633 public void testFindDevices_N01() {
634 ExceptionType exType = ExceptionType.UNKNOWN;
637 SimulatorManager.findDevices(null);
638 } catch (InvalidArgsException e) {
639 exType = ExceptionType.INVALID_ARGS;
640 } catch (SimulatorException e) {
641 exType = ExceptionType.SIMULATOR;
644 assertTrue(exType == ExceptionType.INVALID_ARGS);
647 public void testGetPlatformInfo_P01() {
648 CountDownLatch lockObject = new CountDownLatch(1);
649 boolean syncResult = false;
650 ObjectHolder<PlatformInfo> infoHolder = new ObjectHolder<>();
651 PlatformInfoListener listener = new PlatformInfoListener(lockObject,
655 SimulatorManager.getPlatformInformation(listener);
657 } catch (InvalidArgsException e) {
659 } catch (SimulatorException e) {
663 assertTrue(syncResult);
665 // Wait for the asynchronous response
667 lockObject.await(10, TimeUnit.SECONDS);
668 } catch (InterruptedException e) {
671 assertNotNull(infoHolder.get());
674 public void testGetPlatformInfo_N01() {
675 ExceptionType exType = ExceptionType.UNKNOWN;
678 SimulatorManager.getPlatformInformation(null);
679 } catch (InvalidArgsException e) {
680 exType = ExceptionType.INVALID_ARGS;
681 } catch (SimulatorException e) {
682 exType = ExceptionType.SIMULATOR;
685 assertTrue(exType == ExceptionType.INVALID_ARGS);
689 class FindResourceCallbackListener implements FindResourceListener {
690 private CountDownLatch mLockObject;
691 private ObjectHolder<SimulatorRemoteResource> mResourceHolder;
693 public FindResourceCallbackListener(CountDownLatch lockObject,
694 ObjectHolder<SimulatorRemoteResource> resourceHolder) {
695 mLockObject = lockObject;
696 mResourceHolder = resourceHolder;
700 public void onResourceFound(SimulatorRemoteResource resource) {
701 mResourceHolder.set(resource);
702 mLockObject.countDown();
706 class DeviceInfoListener implements DeviceListener {
707 private CountDownLatch mLockObject;
708 private ObjectHolder<DeviceInfo> mInfoHolder;
710 public DeviceInfoListener(CountDownLatch lockObject,
711 ObjectHolder<DeviceInfo> infoHolder) {
712 mLockObject = lockObject;
713 mInfoHolder = infoHolder;
717 public void onDeviceFound(DeviceInfo devInfo) {
718 mInfoHolder.set(devInfo);
719 mLockObject.countDown();
723 class PlatformInfoListener implements PlatformListener {
725 private CountDownLatch mLockObject;
726 private ObjectHolder<PlatformInfo> mInfoHolder;
728 public PlatformInfoListener(CountDownLatch lockObject,
729 ObjectHolder<PlatformInfo> infoHolder) {
730 mLockObject = lockObject;
731 mInfoHolder = infoHolder;
735 public void onPlatformFound(PlatformInfo platformInfo) {
736 mInfoHolder.set(platformInfo);
737 mLockObject.countDown();