Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / unittests / SimulatorTest / src / org / oic / simulator / test / SimulatorManagerTest.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.oic.simulator.test;
18
19 import java.util.Vector;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
22
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;
35
36 import junit.framework.TestCase;
37
38 /**
39  * This class tests the functionality of Simulator Manager class APIs.
40  */
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";
44
45     @Override
46     protected void setUp() throws Exception {
47         super.setUp();
48     }
49
50     @Override
51     protected void tearDown() throws Exception {
52         super.tearDown();
53     }
54
55     public void testCreateResource_P01() {
56         SimulatorResource resource = null;
57         SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
58
59         try {
60             resource = SimulatorManager.createResource(SINGLE_RES_RAML);
61             resType = resource.getType();
62         } catch (InvalidArgsException e) {
63             e.printStackTrace();
64         } catch (SimulatorException e) {
65             e.printStackTrace();
66         }
67
68         assertNotNull(resource);
69         assertTrue(resType == SimulatorResource.Type.SINGLE);
70     }
71
72     public void testCreateResource_P02() {
73         SimulatorResource resource = null;
74         SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
75
76         try {
77             resource = SimulatorManager.createResource(COLLECTION_RES_RAML);
78             resType = resource.getType();
79         } catch (InvalidArgsException e) {
80             e.printStackTrace();
81         } catch (SimulatorException e) {
82             e.printStackTrace();
83         }
84
85         assertNotNull(resource);
86         assertTrue(resType == SimulatorResource.Type.COLLECTION);
87     }
88
89     public void testCreateResource_N01() {
90         ExceptionType exType = ExceptionType.UNKNOWN;
91
92         try {
93             String configPath = "";
94             SimulatorManager.createResource(configPath);
95         } catch (InvalidArgsException e) {
96             exType = ExceptionType.INVALID_ARGS;
97         } catch (SimulatorException e) {
98             exType = ExceptionType.SIMULATOR;
99         }
100
101         assertTrue(exType == ExceptionType.INVALID_ARGS);
102     }
103
104     public void testCreateResource_N02() {
105         ExceptionType exType = ExceptionType.UNKNOWN;
106
107         try {
108             SimulatorManager.createResource(null);
109         } catch (InvalidArgsException e) {
110             exType = ExceptionType.INVALID_ARGS;
111         } catch (SimulatorException e) {
112             exType = ExceptionType.SIMULATOR;
113         }
114
115         assertTrue(exType == ExceptionType.INVALID_ARGS);
116     }
117
118     public void testCreateResourceWithCount_P01() {
119         Vector<SimulatorResource> resources = null;
120         SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
121
122         try {
123             resources = SimulatorManager.createResource(SINGLE_RES_RAML, 1);
124             resType = resources.elementAt(0).getType();
125         } catch (InvalidArgsException e) {
126             e.printStackTrace();
127         } catch (SimulatorException e) {
128             e.printStackTrace();
129         }
130
131         assertNotNull(resources);
132         assertTrue(resources.size() == 1);
133         assertTrue(resType == SimulatorResource.Type.SINGLE);
134     }
135
136     public void testCreateResourceWithCount_P02() {
137         Vector<SimulatorResource> resources = null;
138         SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
139
140         try {
141             resources = SimulatorManager.createResource(COLLECTION_RES_RAML, 1);
142             resType = resources.elementAt(0).getType();
143         } catch (InvalidArgsException e) {
144             e.printStackTrace();
145         } catch (SimulatorException e) {
146             e.printStackTrace();
147         }
148
149         assertNotNull(resources);
150         assertTrue(resources.size() == 1);
151         assertTrue(resType == SimulatorResource.Type.COLLECTION);
152     }
153
154     public void testCreateResourceWithCount_N01() {
155         ExceptionType exType = ExceptionType.UNKNOWN;
156
157         try {
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;
164         }
165
166         assertTrue(exType == ExceptionType.INVALID_ARGS);
167     }
168
169     public void testCreateResourceWithCount_N02() {
170         ExceptionType exType = ExceptionType.UNKNOWN;
171
172         try {
173             SimulatorManager.createResource(null, 1);
174         } catch (InvalidArgsException e) {
175             exType = ExceptionType.INVALID_ARGS;
176         } catch (SimulatorException e) {
177             exType = ExceptionType.SIMULATOR;
178         }
179
180         assertTrue(exType == ExceptionType.INVALID_ARGS);
181     }
182
183     public void testCreateResourceWithCount_N03() {
184         ExceptionType exType = ExceptionType.UNKNOWN;
185
186         try {
187             SimulatorManager.createResource(SINGLE_RES_RAML, 0);
188         } catch (InvalidArgsException e) {
189             exType = ExceptionType.INVALID_ARGS;
190         } catch (SimulatorException e) {
191             exType = ExceptionType.SIMULATOR;
192         }
193
194         assertTrue(exType == ExceptionType.INVALID_ARGS);
195     }
196
197     public void testCreateResourceWithCount_N04() {
198         ExceptionType exType = ExceptionType.UNKNOWN;
199
200         try {
201             SimulatorManager.createResource(SINGLE_RES_RAML, -1);
202         } catch (InvalidArgsException e) {
203             exType = ExceptionType.INVALID_ARGS;
204         } catch (SimulatorException e) {
205             exType = ExceptionType.SIMULATOR;
206         }
207
208         assertTrue(exType == ExceptionType.INVALID_ARGS);
209     }
210
211     public void testCreateResourceByType_P01() {
212         SimulatorResource resource = null;
213         SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
214
215         try {
216             String name = "test-singleresource";
217             String uri = "/test/singleresource";
218             String resourceType = "test.singleresource";
219
220             resource = SimulatorManager.createResource(
221                     SimulatorResource.Type.SINGLE, name, uri, resourceType);
222             resType = resource.getType();
223         } catch (InvalidArgsException e) {
224             e.printStackTrace();
225         } catch (SimulatorException e) {
226             e.printStackTrace();
227         }
228
229         assertNotNull(resource);
230         assertTrue(resType == SimulatorResource.Type.SINGLE);
231     }
232
233     public void testCreateResourceByType_P02() {
234         SimulatorResource resource = null;
235         SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
236
237         try {
238             String name = "test-collectionresource";
239             String uri = "/test/collectionresource";
240             String resourceType = "test.collectionresource";
241
242             resource = SimulatorManager.createResource(
243                     SimulatorResource.Type.COLLECTION, name, uri, resourceType);
244             resType = resource.getType();
245         } catch (InvalidArgsException e) {
246             e.printStackTrace();
247         } catch (SimulatorException e) {
248             e.printStackTrace();
249         }
250
251         assertNotNull(resource);
252         assertTrue(resType == SimulatorResource.Type.COLLECTION);
253     }
254
255     public void testCreateResourceByType_N01() {
256         ExceptionType exType = ExceptionType.INVALID_ARGS;
257
258         try {
259             String name = "";
260             String uri = "/test/resource";
261             String resourceType = "test.resource";
262
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;
269         }
270
271         assertTrue(exType == ExceptionType.INVALID_ARGS);
272     }
273
274     public void testCreateResourceByType_N02() {
275         ExceptionType exType = ExceptionType.INVALID_ARGS;
276
277         try {
278             String name = null;
279             String uri = "/test/resource";
280             String resourceType = "test.resource";
281
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;
288         }
289
290         assertTrue(exType == ExceptionType.INVALID_ARGS);
291     }
292
293     public void testCreateResourceByType_N03() {
294         ExceptionType exType = ExceptionType.INVALID_ARGS;
295
296         try {
297             String name = "test-resource";
298             String uri = "";
299             String resourceType = "test.resource";
300
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;
307         }
308
309         assertTrue(exType == ExceptionType.INVALID_ARGS);
310     }
311
312     public void testCreateResourceByType_N04() {
313         ExceptionType exType = ExceptionType.INVALID_ARGS;
314
315         try {
316             String name = "test-resource";
317             String uri = null;
318             String resourceType = "test.resource";
319
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;
326         }
327
328         assertTrue(exType == ExceptionType.INVALID_ARGS);
329     }
330
331     public void testCreateResourceByType_N05() {
332         ExceptionType exType = ExceptionType.INVALID_ARGS;
333
334         try {
335             String name = "test-resource";
336             String uri = "/test/resource";;
337             String resourceType = "";
338
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;
345         }
346
347         assertTrue(exType == ExceptionType.INVALID_ARGS);
348     }
349
350     public void testCreateResourceByType_N06() {
351         ExceptionType exType = ExceptionType.INVALID_ARGS;
352
353         try {
354             String name = "test-resource";
355             String uri = "/test/resource";;
356             String resourceType = null;
357
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;
364         }
365
366         assertTrue(exType == ExceptionType.INVALID_ARGS);
367     }
368
369     public void testFindResource_P01() {
370         // Creating sample single resource to test this API
371         SampleSingleResource testResource = new SampleSingleResource();
372         if (false == testResource.start())
373             return;
374
375         CountDownLatch lockObject = new CountDownLatch(1);
376         boolean syncResult = false;
377         ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
378         FindResourceCallbackListener listener = new FindResourceCallbackListener(
379                 lockObject, resourceHolder);
380
381         try {
382             String resourceType = testResource.mResourceType;
383             SimulatorManager.findResource(resourceType, listener);
384             syncResult = true;
385         } catch (InvalidArgsException e) {
386             e.printStackTrace();
387         } catch (SimulatorException e) {
388             e.printStackTrace();
389         }
390
391         if (false == syncResult)
392             testResource.stop();
393
394         assertTrue(syncResult);
395
396         // Wait for the resource to found
397         try {
398             lockObject.await(10, TimeUnit.SECONDS);
399         } catch (InterruptedException e) {
400         }
401
402         testResource.stop();
403         assertNotNull(resourceHolder.get());
404     }
405
406     public void testFindResource_N01() {
407         ExceptionType exType = ExceptionType.UNKNOWN;
408
409         try {
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;
416         }
417
418         assertTrue(exType == ExceptionType.INVALID_ARGS);
419     }
420
421     public void testFindResource_N02() {
422         ExceptionType exType = ExceptionType.UNKNOWN;
423
424         try {
425             String resourceType = "";
426             SimulatorManager.findResource(resourceType,
427                     new FindResourceListener() {
428                         @Override
429                         public void onResourceFound(
430                                 SimulatorRemoteResource resource) {
431                         }
432                     });
433         } catch (InvalidArgsException e) {
434             exType = ExceptionType.INVALID_ARGS;
435         } catch (SimulatorException e) {
436             exType = ExceptionType.SIMULATOR;
437         }
438
439         assertTrue(exType == ExceptionType.INVALID_ARGS);
440     }
441
442     public void testFindResource_N03() {
443         ExceptionType exType = ExceptionType.UNKNOWN;
444
445         try {
446             String resourceType = null;
447             SimulatorManager.findResource(resourceType,
448                     new FindResourceListener() {
449                         @Override
450                         public void onResourceFound(
451                                 SimulatorRemoteResource resource) {
452                         }
453                     });
454         } catch (InvalidArgsException e) {
455             exType = ExceptionType.INVALID_ARGS;
456         } catch (SimulatorException e) {
457             exType = ExceptionType.SIMULATOR;
458         }
459
460         assertTrue(exType == ExceptionType.INVALID_ARGS);
461     }
462
463     public void testFindResourceAll_P01() {
464         // Creating sample single resource to test this API
465         SampleSingleResource testResource = new SampleSingleResource();
466         if (false == testResource.start())
467             return;
468
469         CountDownLatch lockObject = new CountDownLatch(1);
470         boolean syncResult = false;
471         ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
472         FindResourceCallbackListener listener = new FindResourceCallbackListener(
473                 lockObject, resourceHolder);
474
475         try {
476             SimulatorManager.findResource(listener);
477             syncResult = true;
478         } catch (InvalidArgsException e) {
479             e.printStackTrace();
480         } catch (SimulatorException e) {
481             e.printStackTrace();
482         }
483
484         if (false == syncResult)
485             testResource.stop();
486
487         assertTrue(syncResult);
488
489         // Wait for the resource to found
490         try {
491             lockObject.await(10, TimeUnit.SECONDS);
492         } catch (InterruptedException e) {
493         }
494
495         testResource.stop();
496         assertNotNull(resourceHolder.get());
497     }
498
499     public void testFindResourceAll_N01() {
500         ExceptionType exType = ExceptionType.UNKNOWN;
501
502         try {
503             SimulatorManager.findResource(null);
504         } catch (InvalidArgsException e) {
505             exType = ExceptionType.INVALID_ARGS;
506         } catch (SimulatorException e) {
507             exType = ExceptionType.SIMULATOR;
508         }
509
510         assertTrue(exType == ExceptionType.INVALID_ARGS);
511     }
512
513     public void testregisterDeviceInfo_P01() {
514         boolean syncResult = false;
515
516         try {
517             String deviceName = "test-device";
518             SimulatorManager.setDeviceInfo(deviceName);
519             syncResult = true;
520         } catch (InvalidArgsException e) {
521             e.printStackTrace();
522         } catch (SimulatorException e) {
523             e.printStackTrace();
524         }
525
526         assertTrue(syncResult);
527     }
528
529     public void testregisterDeviceInfo_N01() {
530         ExceptionType exType = ExceptionType.UNKNOWN;
531
532         try {
533             String deviceName = "";
534             SimulatorManager.setDeviceInfo(deviceName);
535         } catch (InvalidArgsException e) {
536             exType = ExceptionType.INVALID_ARGS;
537         } catch (SimulatorException e) {
538             exType = ExceptionType.SIMULATOR;
539         }
540
541         assertTrue(exType == ExceptionType.INVALID_ARGS);
542     }
543
544     public void testregisterDeviceInfo_N02() {
545         ExceptionType exType = ExceptionType.UNKNOWN;
546
547         try {
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;
554         }
555
556         assertTrue(exType == ExceptionType.INVALID_ARGS);
557     }
558
559     public void testregisterPlatformInfo_P01() {
560         boolean syncResult = false;
561
562         try {
563             PlatformInfo platformInfo = new PlatformInfo();
564             platformInfo.setManufacturerName("Samsung");
565
566             SimulatorManager.setPlatformInfo(platformInfo);
567             syncResult = true;
568         } catch (InvalidArgsException e) {
569             e.printStackTrace();
570         } catch (SimulatorException e) {
571             e.printStackTrace();
572         }
573
574         assertTrue(syncResult);
575     }
576
577     public void testregisterPlatformInfo_N01() {
578         ExceptionType exType = ExceptionType.UNKNOWN;
579
580         try {
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;
587         }
588
589         assertTrue(exType == ExceptionType.INVALID_ARGS);
590     }
591
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,
597                 infoHolder);
598
599         try {
600             SimulatorManager.setDeviceInfo("Samsung");
601             SimulatorManager.findDevices(null, listener);
602             syncResult = true;
603         } catch (InvalidArgsException e) {
604             e.printStackTrace();
605         } catch (SimulatorException e) {
606             e.printStackTrace();
607         }
608
609         assertTrue(syncResult);
610
611         // Wait for the asynchronous response
612         try {
613             lockObject.await(10, TimeUnit.SECONDS);
614         } catch (InterruptedException e) {
615         }
616
617         assertNotNull(infoHolder.get());
618     }
619
620     public void testFindDevices_N01() {
621         ExceptionType exType = ExceptionType.UNKNOWN;
622
623         try {
624             SimulatorManager.findDevices(null, null);
625         } catch (InvalidArgsException e) {
626             exType = ExceptionType.INVALID_ARGS;
627         } catch (SimulatorException e) {
628             exType = ExceptionType.SIMULATOR;
629         }
630
631         assertTrue(exType == ExceptionType.INVALID_ARGS);
632     }
633
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,
639                 infoHolder);
640
641         try {
642             SimulatorManager.getPlatformInformation(null, listener);
643             syncResult = true;
644         } catch (InvalidArgsException e) {
645             e.printStackTrace();
646         } catch (SimulatorException e) {
647             e.printStackTrace();
648         }
649
650         assertTrue(syncResult);
651
652         // Wait for the asynchronous response
653         try {
654             lockObject.await(10, TimeUnit.SECONDS);
655         } catch (InterruptedException e) {
656         }
657
658         assertNotNull(infoHolder.get());
659     }
660
661     public void testGetPlatformInfo_N01() {
662         ExceptionType exType = ExceptionType.UNKNOWN;
663
664         try {
665             SimulatorManager.getPlatformInformation(null, null);
666         } catch (InvalidArgsException e) {
667             exType = ExceptionType.INVALID_ARGS;
668         } catch (SimulatorException e) {
669             exType = ExceptionType.SIMULATOR;
670         }
671
672         assertTrue(exType == ExceptionType.INVALID_ARGS);
673     }
674 }
675
676 class FindResourceCallbackListener implements FindResourceListener {
677     private CountDownLatch                        mLockObject;
678     private ObjectHolder<SimulatorRemoteResource> mResourceHolder;
679
680     public FindResourceCallbackListener(CountDownLatch lockObject,
681             ObjectHolder<SimulatorRemoteResource> resourceHolder) {
682         mLockObject = lockObject;
683         mResourceHolder = resourceHolder;
684     }
685
686     @Override
687     public void onResourceFound(SimulatorRemoteResource resource) {
688         mResourceHolder.set(resource);
689         mLockObject.countDown();
690     }
691 }
692
693 class DeviceInfoListener implements DeviceListener {
694     private CountDownLatch           mLockObject;
695     private ObjectHolder<DeviceInfo> mInfoHolder;
696
697     public DeviceInfoListener(CountDownLatch lockObject,
698             ObjectHolder<DeviceInfo> infoHolder) {
699         mLockObject = lockObject;
700         mInfoHolder = infoHolder;
701     }
702
703     @Override
704     public void onDeviceFound(String hostUri, DeviceInfo devInfo) {
705         mInfoHolder.set(devInfo);
706         mLockObject.countDown();
707     }
708 }
709
710 class PlatformInfoListener implements PlatformListener {
711
712     private CountDownLatch             mLockObject;
713     private ObjectHolder<PlatformInfo> mInfoHolder;
714
715     public PlatformInfoListener(CountDownLatch lockObject,
716             ObjectHolder<PlatformInfo> infoHolder) {
717         mLockObject = lockObject;
718         mInfoHolder = infoHolder;
719     }
720
721     @Override
722     public void onPlatformFound(String hostUri, PlatformInfo platformInfo) {
723         mInfoHolder.set(platformInfo);
724         mLockObject.countDown();
725     }
726 }