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 protected void setUp() throws Exception {
51 protected void tearDown() throws Exception {
55 public void testCreateResource_P01() {
56 SimulatorResource resource = null;
57 SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
60 resource = SimulatorManager.createResource(SINGLE_RES_RAML);
61 resType = resource.getType();
62 } catch (InvalidArgsException e) {
64 } catch (SimulatorException e) {
68 assertNotNull(resource);
69 assertTrue(resType == SimulatorResource.Type.SINGLE);
72 public void testCreateResource_P02() {
73 SimulatorResource resource = null;
74 SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
77 resource = SimulatorManager.createResource(COLLECTION_RES_RAML);
78 resType = resource.getType();
79 } catch (InvalidArgsException e) {
81 } catch (SimulatorException e) {
85 assertNotNull(resource);
86 assertTrue(resType == SimulatorResource.Type.COLLECTION);
89 public void testCreateResource_N01() {
90 ExceptionType exType = ExceptionType.UNKNOWN;
93 String configPath = "";
94 SimulatorManager.createResource(configPath);
95 } catch (InvalidArgsException e) {
96 exType = ExceptionType.INVALID_ARGS;
97 } catch (SimulatorException e) {
98 exType = ExceptionType.SIMULATOR;
101 assertTrue(exType == ExceptionType.INVALID_ARGS);
104 public void testCreateResource_N02() {
105 ExceptionType exType = ExceptionType.UNKNOWN;
108 SimulatorManager.createResource(null);
109 } catch (InvalidArgsException e) {
110 exType = ExceptionType.INVALID_ARGS;
111 } catch (SimulatorException e) {
112 exType = ExceptionType.SIMULATOR;
115 assertTrue(exType == ExceptionType.INVALID_ARGS);
118 public void testCreateResourceWithCount_P01() {
119 Vector<SimulatorResource> resources = null;
120 SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
123 resources = SimulatorManager.createResource(SINGLE_RES_RAML, 1);
124 resType = resources.elementAt(0).getType();
125 } catch (InvalidArgsException e) {
127 } catch (SimulatorException e) {
131 assertNotNull(resources);
132 assertTrue(resources.size() == 1);
133 assertTrue(resType == SimulatorResource.Type.SINGLE);
136 public void testCreateResourceWithCount_P02() {
137 Vector<SimulatorResource> resources = null;
138 SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
141 resources = SimulatorManager.createResource(COLLECTION_RES_RAML, 1);
142 resType = resources.elementAt(0).getType();
143 } catch (InvalidArgsException e) {
145 } catch (SimulatorException e) {
149 assertNotNull(resources);
150 assertTrue(resources.size() == 1);
151 assertTrue(resType == SimulatorResource.Type.COLLECTION);
154 public void testCreateResourceWithCount_N01() {
155 ExceptionType exType = ExceptionType.UNKNOWN;
158 String configPath = "";
159 SimulatorManager.createResource(configPath, 1);
160 } catch (InvalidArgsException e) {
161 exType = ExceptionType.INVALID_ARGS;
162 } catch (SimulatorException e) {
163 exType = ExceptionType.SIMULATOR;
166 assertTrue(exType == ExceptionType.INVALID_ARGS);
169 public void testCreateResourceWithCount_N02() {
170 ExceptionType exType = ExceptionType.UNKNOWN;
173 SimulatorManager.createResource(null, 1);
174 } catch (InvalidArgsException e) {
175 exType = ExceptionType.INVALID_ARGS;
176 } catch (SimulatorException e) {
177 exType = ExceptionType.SIMULATOR;
180 assertTrue(exType == ExceptionType.INVALID_ARGS);
183 public void testCreateResourceWithCount_N03() {
184 ExceptionType exType = ExceptionType.UNKNOWN;
187 SimulatorManager.createResource(SINGLE_RES_RAML, 0);
188 } catch (InvalidArgsException e) {
189 exType = ExceptionType.INVALID_ARGS;
190 } catch (SimulatorException e) {
191 exType = ExceptionType.SIMULATOR;
194 assertTrue(exType == ExceptionType.INVALID_ARGS);
197 public void testCreateResourceWithCount_N04() {
198 ExceptionType exType = ExceptionType.UNKNOWN;
201 SimulatorManager.createResource(SINGLE_RES_RAML, -1);
202 } catch (InvalidArgsException e) {
203 exType = ExceptionType.INVALID_ARGS;
204 } catch (SimulatorException e) {
205 exType = ExceptionType.SIMULATOR;
208 assertTrue(exType == ExceptionType.INVALID_ARGS);
211 public void testCreateResourceByType_P01() {
212 SimulatorResource resource = null;
213 SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
216 String name = "test-singleresource";
217 String uri = "/test/singleresource";
218 String resourceType = "test.singleresource";
220 resource = SimulatorManager.createResource(
221 SimulatorResource.Type.SINGLE, name, uri, resourceType);
222 resType = resource.getType();
223 } catch (InvalidArgsException e) {
225 } catch (SimulatorException e) {
229 assertNotNull(resource);
230 assertTrue(resType == SimulatorResource.Type.SINGLE);
233 public void testCreateResourceByType_P02() {
234 SimulatorResource resource = null;
235 SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
238 String name = "test-collectionresource";
239 String uri = "/test/collectionresource";
240 String resourceType = "test.collectionresource";
242 resource = SimulatorManager.createResource(
243 SimulatorResource.Type.COLLECTION, name, uri, resourceType);
244 resType = resource.getType();
245 } catch (InvalidArgsException e) {
247 } catch (SimulatorException e) {
251 assertNotNull(resource);
252 assertTrue(resType == SimulatorResource.Type.COLLECTION);
255 public void testCreateResourceByType_N01() {
256 ExceptionType exType = ExceptionType.INVALID_ARGS;
260 String uri = "/test/resource";
261 String resourceType = "test.resource";
263 SimulatorManager.createResource(SimulatorResource.Type.SINGLE,
264 name, uri, resourceType);
265 } catch (InvalidArgsException e) {
266 exType = ExceptionType.INVALID_ARGS;
267 } catch (SimulatorException e) {
268 exType = ExceptionType.SIMULATOR;
271 assertTrue(exType == ExceptionType.INVALID_ARGS);
274 public void testCreateResourceByType_N02() {
275 ExceptionType exType = ExceptionType.INVALID_ARGS;
279 String uri = "/test/resource";
280 String resourceType = "test.resource";
282 SimulatorManager.createResource(SimulatorResource.Type.SINGLE,
283 name, uri, resourceType);
284 } catch (InvalidArgsException e) {
285 exType = ExceptionType.INVALID_ARGS;
286 } catch (SimulatorException e) {
287 exType = ExceptionType.SIMULATOR;
290 assertTrue(exType == ExceptionType.INVALID_ARGS);
293 public void testCreateResourceByType_N03() {
294 ExceptionType exType = ExceptionType.INVALID_ARGS;
297 String name = "test-resource";
299 String resourceType = "test.resource";
301 SimulatorManager.createResource(SimulatorResource.Type.SINGLE,
302 name, uri, resourceType);
303 } catch (InvalidArgsException e) {
304 exType = ExceptionType.INVALID_ARGS;
305 } catch (SimulatorException e) {
306 exType = ExceptionType.SIMULATOR;
309 assertTrue(exType == ExceptionType.INVALID_ARGS);
312 public void testCreateResourceByType_N04() {
313 ExceptionType exType = ExceptionType.INVALID_ARGS;
316 String name = "test-resource";
318 String resourceType = "test.resource";
320 SimulatorManager.createResource(SimulatorResource.Type.SINGLE,
321 name, uri, resourceType);
322 } catch (InvalidArgsException e) {
323 exType = ExceptionType.INVALID_ARGS;
324 } catch (SimulatorException e) {
325 exType = ExceptionType.SIMULATOR;
328 assertTrue(exType == ExceptionType.INVALID_ARGS);
331 public void testCreateResourceByType_N05() {
332 ExceptionType exType = ExceptionType.INVALID_ARGS;
335 String name = "test-resource";
336 String uri = "/test/resource";;
337 String resourceType = "";
339 SimulatorManager.createResource(SimulatorResource.Type.SINGLE,
340 name, uri, resourceType);
341 } catch (InvalidArgsException e) {
342 exType = ExceptionType.INVALID_ARGS;
343 } catch (SimulatorException e) {
344 exType = ExceptionType.SIMULATOR;
347 assertTrue(exType == ExceptionType.INVALID_ARGS);
350 public void testCreateResourceByType_N06() {
351 ExceptionType exType = ExceptionType.INVALID_ARGS;
354 String name = "test-resource";
355 String uri = "/test/resource";;
356 String resourceType = null;
358 SimulatorManager.createResource(SimulatorResource.Type.SINGLE,
359 name, uri, resourceType);
360 } catch (InvalidArgsException e) {
361 exType = ExceptionType.INVALID_ARGS;
362 } catch (SimulatorException e) {
363 exType = ExceptionType.SIMULATOR;
366 assertTrue(exType == ExceptionType.INVALID_ARGS);
369 public void testFindResource_P01() {
370 // Creating sample single resource to test this API
371 SampleSingleResource testResource = new SampleSingleResource();
372 if (false == testResource.start())
375 CountDownLatch lockObject = new CountDownLatch(1);
376 boolean syncResult = false;
377 ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
378 FindResourceCallbackListener listener = new FindResourceCallbackListener(
379 lockObject, resourceHolder);
382 String resourceType = testResource.mResourceType;
383 SimulatorManager.findResource(resourceType, listener);
385 } catch (InvalidArgsException e) {
387 } catch (SimulatorException e) {
391 if (false == syncResult)
394 assertTrue(syncResult);
396 // Wait for the resource to found
398 lockObject.await(10, TimeUnit.SECONDS);
399 } catch (InterruptedException e) {
403 assertNotNull(resourceHolder.get());
406 public void testFindResource_N01() {
407 ExceptionType exType = ExceptionType.UNKNOWN;
410 String resourceType = "test.singleresource";
411 SimulatorManager.findResource(resourceType, null);
412 } catch (InvalidArgsException e) {
413 exType = ExceptionType.INVALID_ARGS;
414 } catch (SimulatorException e) {
415 exType = ExceptionType.SIMULATOR;
418 assertTrue(exType == ExceptionType.INVALID_ARGS);
421 public void testFindResource_N02() {
422 ExceptionType exType = ExceptionType.UNKNOWN;
425 String resourceType = "";
426 SimulatorManager.findResource(resourceType,
427 new FindResourceListener() {
429 public void onResourceFound(
430 SimulatorRemoteResource resource) {
433 } catch (InvalidArgsException e) {
434 exType = ExceptionType.INVALID_ARGS;
435 } catch (SimulatorException e) {
436 exType = ExceptionType.SIMULATOR;
439 assertTrue(exType == ExceptionType.INVALID_ARGS);
442 public void testFindResource_N03() {
443 ExceptionType exType = ExceptionType.UNKNOWN;
446 String resourceType = null;
447 SimulatorManager.findResource(resourceType,
448 new FindResourceListener() {
450 public void onResourceFound(
451 SimulatorRemoteResource resource) {
454 } catch (InvalidArgsException e) {
455 exType = ExceptionType.INVALID_ARGS;
456 } catch (SimulatorException e) {
457 exType = ExceptionType.SIMULATOR;
460 assertTrue(exType == ExceptionType.INVALID_ARGS);
463 public void testFindResourceAll_P01() {
464 // Creating sample single resource to test this API
465 SampleSingleResource testResource = new SampleSingleResource();
466 if (false == testResource.start())
469 CountDownLatch lockObject = new CountDownLatch(1);
470 boolean syncResult = false;
471 ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
472 FindResourceCallbackListener listener = new FindResourceCallbackListener(
473 lockObject, resourceHolder);
476 SimulatorManager.findResource(listener);
478 } catch (InvalidArgsException e) {
480 } catch (SimulatorException e) {
484 if (false == syncResult)
487 assertTrue(syncResult);
489 // Wait for the resource to found
491 lockObject.await(10, TimeUnit.SECONDS);
492 } catch (InterruptedException e) {
496 assertNotNull(resourceHolder.get());
499 public void testFindResourceAll_N01() {
500 ExceptionType exType = ExceptionType.UNKNOWN;
503 SimulatorManager.findResource(null);
504 } catch (InvalidArgsException e) {
505 exType = ExceptionType.INVALID_ARGS;
506 } catch (SimulatorException e) {
507 exType = ExceptionType.SIMULATOR;
510 assertTrue(exType == ExceptionType.INVALID_ARGS);
513 public void testregisterDeviceInfo_P01() {
514 boolean syncResult = false;
517 String deviceName = "test-device";
518 SimulatorManager.setDeviceInfo(deviceName);
520 } catch (InvalidArgsException e) {
522 } catch (SimulatorException e) {
526 assertTrue(syncResult);
529 public void testregisterDeviceInfo_N01() {
530 ExceptionType exType = ExceptionType.UNKNOWN;
533 String deviceName = "";
534 SimulatorManager.setDeviceInfo(deviceName);
535 } catch (InvalidArgsException e) {
536 exType = ExceptionType.INVALID_ARGS;
537 } catch (SimulatorException e) {
538 exType = ExceptionType.SIMULATOR;
541 assertTrue(exType == ExceptionType.INVALID_ARGS);
544 public void testregisterDeviceInfo_N02() {
545 ExceptionType exType = ExceptionType.UNKNOWN;
548 String deviceName = null;
549 SimulatorManager.setDeviceInfo(deviceName);
550 } catch (InvalidArgsException e) {
551 exType = ExceptionType.INVALID_ARGS;
552 } catch (SimulatorException e) {
553 exType = ExceptionType.SIMULATOR;
556 assertTrue(exType == ExceptionType.INVALID_ARGS);
559 public void testregisterPlatformInfo_P01() {
560 boolean syncResult = false;
563 PlatformInfo platformInfo = new PlatformInfo();
564 platformInfo.setManufacturerName("Samsung");
566 SimulatorManager.setPlatformInfo(platformInfo);
568 } catch (InvalidArgsException e) {
570 } catch (SimulatorException e) {
574 assertTrue(syncResult);
577 public void testregisterPlatformInfo_N01() {
578 ExceptionType exType = ExceptionType.UNKNOWN;
581 PlatformInfo platformInfo = null;
582 SimulatorManager.setPlatformInfo(platformInfo);
583 } catch (InvalidArgsException e) {
584 exType = ExceptionType.INVALID_ARGS;
585 } catch (SimulatorException e) {
586 exType = ExceptionType.SIMULATOR;
589 assertTrue(exType == ExceptionType.INVALID_ARGS);
592 public void testFindDevices_P01() {
593 CountDownLatch lockObject = new CountDownLatch(1);
594 boolean syncResult = false;
595 ObjectHolder<DeviceInfo> infoHolder = new ObjectHolder<>();
596 DeviceInfoListener listener = new DeviceInfoListener(lockObject,
600 SimulatorManager.setDeviceInfo("Samsung");
601 SimulatorManager.findDevices(null, listener);
603 } catch (InvalidArgsException e) {
605 } catch (SimulatorException e) {
609 assertTrue(syncResult);
611 // Wait for the asynchronous response
613 lockObject.await(10, TimeUnit.SECONDS);
614 } catch (InterruptedException e) {
617 assertNotNull(infoHolder.get());
620 public void testFindDevices_N01() {
621 ExceptionType exType = ExceptionType.UNKNOWN;
624 SimulatorManager.findDevices(null, null);
625 } catch (InvalidArgsException e) {
626 exType = ExceptionType.INVALID_ARGS;
627 } catch (SimulatorException e) {
628 exType = ExceptionType.SIMULATOR;
631 assertTrue(exType == ExceptionType.INVALID_ARGS);
634 public void testGetPlatformInfo_P01() {
635 CountDownLatch lockObject = new CountDownLatch(1);
636 boolean syncResult = false;
637 ObjectHolder<PlatformInfo> infoHolder = new ObjectHolder<>();
638 PlatformInfoListener listener = new PlatformInfoListener(lockObject,
642 SimulatorManager.getPlatformInformation(null, listener);
644 } catch (InvalidArgsException e) {
646 } catch (SimulatorException e) {
650 assertTrue(syncResult);
652 // Wait for the asynchronous response
654 lockObject.await(10, TimeUnit.SECONDS);
655 } catch (InterruptedException e) {
658 assertNotNull(infoHolder.get());
661 public void testGetPlatformInfo_N01() {
662 ExceptionType exType = ExceptionType.UNKNOWN;
665 SimulatorManager.getPlatformInformation(null, null);
666 } catch (InvalidArgsException e) {
667 exType = ExceptionType.INVALID_ARGS;
668 } catch (SimulatorException e) {
669 exType = ExceptionType.SIMULATOR;
672 assertTrue(exType == ExceptionType.INVALID_ARGS);
676 class FindResourceCallbackListener implements FindResourceListener {
677 private CountDownLatch mLockObject;
678 private ObjectHolder<SimulatorRemoteResource> mResourceHolder;
680 public FindResourceCallbackListener(CountDownLatch lockObject,
681 ObjectHolder<SimulatorRemoteResource> resourceHolder) {
682 mLockObject = lockObject;
683 mResourceHolder = resourceHolder;
687 public void onResourceFound(SimulatorRemoteResource resource) {
688 mResourceHolder.set(resource);
689 mLockObject.countDown();
693 class DeviceInfoListener implements DeviceListener {
694 private CountDownLatch mLockObject;
695 private ObjectHolder<DeviceInfo> mInfoHolder;
697 public DeviceInfoListener(CountDownLatch lockObject,
698 ObjectHolder<DeviceInfo> infoHolder) {
699 mLockObject = lockObject;
700 mInfoHolder = infoHolder;
704 public void onDeviceFound(String hostUri, DeviceInfo devInfo) {
705 mInfoHolder.set(devInfo);
706 mLockObject.countDown();
710 class PlatformInfoListener implements PlatformListener {
712 private CountDownLatch mLockObject;
713 private ObjectHolder<PlatformInfo> mInfoHolder;
715 public PlatformInfoListener(CountDownLatch lockObject,
716 ObjectHolder<PlatformInfo> infoHolder) {
717 mLockObject = lockObject;
718 mInfoHolder = infoHolder;
722 public void onPlatformFound(String hostUri, PlatformInfo platformInfo) {
723 mInfoHolder.set(platformInfo);
724 mLockObject.countDown();