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.Vector;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
23 import org.oic.simulator.InvalidArgsException;
24 import org.oic.simulator.NoSupportException;
25 import org.oic.simulator.SimulatorException;
26 import org.oic.simulator.SimulatorManager;
27 import org.oic.simulator.SimulatorResourceModel;
28 import org.oic.simulator.client.FindResourceListener;
29 import org.oic.simulator.client.SimulatorRemoteResource;
30 import org.oic.simulator.server.SimulatorResource;
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;
38 * This class tests the APIs of SimulatorResource class
40 public class SimulatorResourceTest extends TestCase {
41 private static final String RES_NAME = "test-resource";
42 private static final String RES_URI = "/test/resource";
43 private static final String RES_TYPE = "test.resource";
44 private SimulatorResource resource = null;
46 protected void setUp() throws Exception {
48 resource = SimulatorManager.createResource(
49 SimulatorResource.Type.SINGLE, RES_NAME, RES_URI, RES_TYPE);
52 protected void tearDown() throws Exception {
57 public void testIsCollection_P01() {
58 boolean isCollection = false;
59 isCollection = resource.isCollection();
60 assertFalse(isCollection);
63 public void testIsCollection_P02() {
64 boolean isCollection = true;
67 SimulatorResource res = SimulatorManager.createResource(
68 SimulatorResource.Type.COLLECTION, RES_NAME, RES_URI,
70 isCollection = res.isCollection();
71 } catch (SimulatorException e) {
75 assertTrue(isCollection);
78 public void testGetName_P01() {
82 name = resource.getName();
83 } catch (SimulatorException e) {
87 assertEquals(RES_NAME, name);
90 public void testGetType_P01() {
91 SimulatorResource.Type type = SimulatorResource.Type.SINGLE;
92 type = resource.getType();
93 assertEquals(SimulatorResource.Type.SINGLE, type);
96 public void testGetURI_P01() {
100 uri = resource.getURI();
101 } catch (SimulatorException e) {
105 assertEquals(RES_URI, uri);
108 public void testGetResourceType_P01() {
109 String resType = null;
112 resType = resource.getResourceType();
113 } catch (SimulatorException e) {
117 assertEquals(RES_TYPE, resType);
120 public void testGetInterface_P01() {
121 Vector<String> interfaces = null;
124 interfaces = resource.getInterface();
125 } catch (SimulatorException e) {
129 assertNotNull(interfaces);
130 assertTrue(interfaces.size() > 0);
133 public void testSetName_P01() {
134 String name = "new-name";
135 String newName = null;
138 resource.setName(name);
139 newName = resource.getName();
140 } catch (InvalidArgsException e) {
142 } catch (SimulatorException e) {
146 assertEquals(name, newName);
149 public void testSetName_N01() {
151 ExceptionType exType = ExceptionType.UNKNOWN;
154 resource.setName(newName);
155 } catch (InvalidArgsException e) {
156 exType = ExceptionType.INVALID_ARGS;
157 } catch (SimulatorException e) {
158 exType = ExceptionType.SIMULATOR;
161 assertEquals(ExceptionType.INVALID_ARGS, exType);
164 public void testSetName_N02() {
165 String newName = null;
166 ExceptionType exType = ExceptionType.UNKNOWN;
169 resource.setName(newName);
170 } catch (InvalidArgsException e) {
171 exType = ExceptionType.INVALID_ARGS;
172 } catch (SimulatorException e) {
173 exType = ExceptionType.SIMULATOR;
176 assertEquals(ExceptionType.INVALID_ARGS, exType);
179 public void testSetURI_P01() {
180 String newUri = "/test/newuri/1";
184 resource.setURI(newUri);
185 uri = resource.getURI();
186 } catch (InvalidArgsException e) {
188 } catch (SimulatorException e) {
192 assertEquals(newUri, uri);
195 public void testSetURI_N01() {
197 ExceptionType exType = ExceptionType.UNKNOWN;
200 resource.setURI(newUri);
201 } catch (InvalidArgsException e) {
202 exType = ExceptionType.INVALID_ARGS;
203 } catch (SimulatorException e) {
204 exType = ExceptionType.SIMULATOR;
207 assertEquals(ExceptionType.INVALID_ARGS, exType);
210 public void testSetURI_N02() {
211 String newUri = null;
212 ExceptionType exType = ExceptionType.UNKNOWN;
215 resource.setURI(newUri);
216 } catch (InvalidArgsException e) {
217 exType = ExceptionType.INVALID_ARGS;
218 } catch (SimulatorException e) {
219 exType = ExceptionType.SIMULATOR;
222 assertEquals(ExceptionType.INVALID_ARGS, exType);
225 public void testSetResourceType_P01() {
226 String newResType = "test.newresource";
227 String resType = null;
230 resource.setResourceType(newResType);
231 resType = resource.getResourceType();
232 } catch (InvalidArgsException e) {
234 } catch (SimulatorException e) {
238 assertEquals(newResType, resType);
241 public void testSetResourceType_N01() {
242 String newResType = "";
243 ExceptionType exType = ExceptionType.UNKNOWN;
246 resource.setResourceType(newResType);
247 } catch (InvalidArgsException e) {
248 exType = ExceptionType.INVALID_ARGS;
249 } catch (SimulatorException e) {
250 exType = ExceptionType.SIMULATOR;
253 assertEquals(ExceptionType.INVALID_ARGS, exType);
256 public void testSetResourceType_N02() {
257 String newResType = null;
258 ExceptionType exType = ExceptionType.UNKNOWN;
261 resource.setResourceType(newResType);
262 } catch (InvalidArgsException e) {
263 exType = ExceptionType.INVALID_ARGS;
264 } catch (SimulatorException e) {
265 exType = ExceptionType.SIMULATOR;
268 assertEquals(ExceptionType.INVALID_ARGS, exType);
271 public void testSetInterface_P01() {
272 String newInterface = "oic.if.r";
273 Vector<String> interfaces = null;
276 resource.setInterface(newInterface);
277 interfaces = resource.getInterface();
278 } catch (InvalidArgsException e) {
280 } catch (SimulatorException e) {
284 assertNotNull(interfaces);
285 assertTrue(interfaces.size() == 1
286 && interfaces.get(0).equals(newInterface));
289 public void testSetInterface_N01() {
290 String newInterface = "";
291 ExceptionType exType = ExceptionType.UNKNOWN;
294 resource.setInterface(newInterface);
295 } catch (InvalidArgsException e) {
296 exType = ExceptionType.INVALID_ARGS;
297 } catch (SimulatorException e) {
298 exType = ExceptionType.SIMULATOR;
301 assertEquals(ExceptionType.INVALID_ARGS, exType);
304 public void testSetInterface_N02() {
305 String newInterface = null;
306 ExceptionType exType = ExceptionType.UNKNOWN;
309 resource.setInterface(newInterface);
310 } catch (InvalidArgsException e) {
311 exType = ExceptionType.INVALID_ARGS;
312 } catch (SimulatorException e) {
313 exType = ExceptionType.SIMULATOR;
316 assertEquals(ExceptionType.INVALID_ARGS, exType);
319 public void testSetInterface_N03() {
320 String newInterface = "oic.if.s";
321 ExceptionType exType = ExceptionType.UNKNOWN;
325 resource.setInterface(newInterface);
326 } catch (InvalidArgsException e) {
327 exType = ExceptionType.INVALID_ARGS;
328 } catch (SimulatorException e) {
329 exType = ExceptionType.SIMULATOR;
334 } catch (SimulatorException e) {
338 assertEquals(ExceptionType.SIMULATOR, exType);
341 public void testSetInterfaces_P01() {
342 String interface1 = "oic.if.s";
343 String interface2 = "oic.if.a";
344 Vector<String> interfaces = null;
345 Vector<String> newInterfaces = new Vector<String>();
346 newInterfaces.add(interface1);
347 newInterfaces.add(interface2);
349 resource.setInterface(newInterfaces);
350 interfaces = resource.getInterface();
351 } catch (InvalidArgsException e) {
353 } catch (SimulatorException e) {
357 assertNotNull(interfaces);
358 assertTrue(interfaces.size() == 2 && interfaces.contains(interface1)
359 && interfaces.contains(interface2));
362 public void testSetInterfaces_N01() {
363 Vector<String> newInterfaces = new Vector<String>();
364 ExceptionType exType = ExceptionType.UNKNOWN;
367 resource.setInterface(newInterfaces);
368 } catch (InvalidArgsException e) {
369 exType = ExceptionType.INVALID_ARGS;
370 } catch (SimulatorException e) {
371 exType = ExceptionType.SIMULATOR;
374 assertEquals(ExceptionType.INVALID_ARGS, exType);
377 public void testSetInterfaces_N02() {
378 Vector<String> newInterfaces = null;
379 ExceptionType exType = ExceptionType.UNKNOWN;
382 resource.setInterface(newInterfaces);
383 } catch (InvalidArgsException e) {
384 exType = ExceptionType.INVALID_ARGS;
385 } catch (SimulatorException e) {
386 exType = ExceptionType.SIMULATOR;
389 assertEquals(ExceptionType.INVALID_ARGS, exType);
392 public void testSetInterfaces_N03() {
393 String interface1 = "oic.if.s";
394 String interface2 = "oic.if.a";
395 ExceptionType exType = ExceptionType.UNKNOWN;
396 Vector<String> newInterfaces = new Vector<String>();
397 newInterfaces.add(interface1);
398 newInterfaces.add(interface2);
402 resource.setInterface(newInterfaces);
403 } catch (InvalidArgsException e) {
404 exType = ExceptionType.INVALID_ARGS;
405 } catch (SimulatorException e) {
406 exType = ExceptionType.SIMULATOR;
411 } catch (SimulatorException e) {
415 assertEquals(ExceptionType.SIMULATOR, exType);
418 public void testAddInterface_P01() {
419 String newInterface = "oic.if.rw";
420 Vector<String> interfaces = null;
423 resource.addInterface(newInterface);
424 interfaces = resource.getInterface();
425 } catch (InvalidArgsException e) {
427 } catch (NoSupportException e) {
429 } catch (SimulatorException e) {
433 assertNotNull(interfaces);
434 assertTrue(interfaces.contains(newInterface));
437 public void testAddInterface_N01() {
438 String newInterface = "";
439 ExceptionType exType = ExceptionType.UNKNOWN;
442 resource.addInterface(newInterface);
443 } catch (InvalidArgsException e) {
444 exType = ExceptionType.INVALID_ARGS;
445 } catch (NoSupportException e) {
446 exType = ExceptionType.NOT_SUPPORTED;
447 } catch (SimulatorException e) {
448 exType = ExceptionType.SIMULATOR;
451 assertEquals(ExceptionType.INVALID_ARGS, exType);
454 public void testAddInterface_N02() {
455 String newInterface = null;
456 ExceptionType exType = ExceptionType.UNKNOWN;
459 resource.addInterface(newInterface);
460 } catch (InvalidArgsException e) {
461 exType = ExceptionType.INVALID_ARGS;
462 } catch (NoSupportException e) {
463 exType = ExceptionType.NOT_SUPPORTED;
464 } catch (SimulatorException e) {
465 exType = ExceptionType.SIMULATOR;
468 assertEquals(ExceptionType.INVALID_ARGS, exType);
471 public void testAddInterface_N03() {
472 String newInterface = "oic.if.a";
473 ExceptionType exType = ExceptionType.UNKNOWN;
476 SimulatorResource res = SimulatorManager.createResource(
477 SimulatorResource.Type.COLLECTION, RES_NAME, RES_URI,
479 res.addInterface(newInterface);
480 } catch (InvalidArgsException e) {
481 exType = ExceptionType.INVALID_ARGS;
482 } catch (NoSupportException e) {
483 exType = ExceptionType.NOT_SUPPORTED;
484 } catch (SimulatorException e) {
485 exType = ExceptionType.SIMULATOR;
488 assertEquals(ExceptionType.NOT_SUPPORTED, exType);
491 public void testSetObservable_P01() {
492 boolean newState = true;
493 boolean state = false;
495 resource.setObservable(newState);
496 state = resource.isObservable();
497 } catch (SimulatorException e) {
501 assertEquals(newState, state);
504 public void testSetObservable_P02() {
505 boolean newState = false;
506 boolean state = true;
508 resource.setObservable(newState);
509 state = resource.isObservable();
510 } catch (SimulatorException e) {
514 assertEquals(newState, state);
517 public void testSetDiscoverable_P01() {
518 boolean newState = true;
519 boolean state = false;
521 resource.setDiscoverable(newState);
522 state = resource.isDiscoverable();
523 } catch (SimulatorException e) {
527 assertEquals(newState, state);
530 public void testSetDiscoverable_P02() {
531 boolean newState = false;
532 boolean state = true;
534 resource.setDiscoverable(newState);
535 state = resource.isDiscoverable();
536 } catch (SimulatorException e) {
540 assertEquals(newState, state);
543 public void testIsObservable_P01() {
544 boolean state = false;
547 state = resource.isObservable();
548 } catch (SimulatorException e) {
552 assertEquals(true, state);
555 public void testIsDiscoverable_P01() {
556 boolean state = false;
559 state = resource.isDiscoverable();
560 } catch (SimulatorException e) {
564 assertEquals(true, state);
567 public void testGetResourceModel_P01() {
568 SimulatorResourceModel resModel = null;
569 String singleResourceRaml = "./ramls/oic.r.light.raml";
571 SimulatorSingleResource resource = (SimulatorSingleResource) SimulatorManager
572 .createResource(singleResourceRaml);
573 resModel = resource.getResourceModel();
574 } catch (SimulatorException e) {
578 assertNotNull(resModel);
579 assertTrue(resModel.size() > 0);
582 public void testIsStarted_P01() {
583 boolean state = true;
586 state = resource.isStarted();
587 } catch (SimulatorException e) {
591 assertEquals(false, state);
594 public void testIsStarted_P02() {
595 boolean state = false;
599 state = resource.isStarted();
601 } catch (SimulatorException e) {
605 assertEquals(true, state);
608 public void testIsStarted_P03() {
609 boolean state = true;
614 state = resource.isStarted();
615 } catch (SimulatorException e) {
619 assertEquals(false, state);
622 public void testStart_P01() {
623 CountDownLatch lockObject = new CountDownLatch(1);
624 ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
625 FindResourceCallbackListener listener = new FindResourceCallbackListener(
626 lockObject, resourceHolder);
630 SimulatorManager.findResource(resource.getResourceType(), listener);
632 // Wait for the resource to found
634 lockObject.await(10, TimeUnit.SECONDS);
635 } catch (InterruptedException e) {
638 } catch (InvalidArgsException e) {
640 } catch (SimulatorException e) {
646 } catch (SimulatorException e) {
650 assertNotNull(resourceHolder.get());
653 public void testStop_P01() {
654 CountDownLatch lockObject = new CountDownLatch(1);
655 ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
656 FindResourceCallbackListener listener = new FindResourceCallbackListener(
657 lockObject, resourceHolder);
662 SimulatorManager.findResource(resource.getResourceType(), listener);
664 // Wait for the resource to found
666 lockObject.await(10, TimeUnit.SECONDS);
667 } catch (InterruptedException e) {
670 } catch (InvalidArgsException e) {
672 } catch (SimulatorException e) {
678 } catch (SimulatorException e) {
682 assertNull(resourceHolder.get());
686 class FindResourceCallbackListener implements FindResourceListener {
688 private CountDownLatch mLockObject;
689 private ObjectHolder<SimulatorRemoteResource> mResourceHolder;
691 public FindResourceCallbackListener(CountDownLatch lockObject,
692 ObjectHolder<SimulatorRemoteResource> resourceHolder) {
693 mLockObject = lockObject;
694 mResourceHolder = resourceHolder;
698 public void onResourceFound(SimulatorRemoteResource resource) {
699 mResourceHolder.set(resource);
700 mLockObject.countDown();