JUnit test cases for updated APIs.
[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     static {
46         System.loadLibrary("SimulatorManager");
47         System.loadLibrary("RamlParser");
48         System.loadLibrary("oc");
49         System.loadLibrary("oc_logger");
50         System.loadLibrary("octbstack");
51     }
52
53     @Override
54     protected void setUp() throws Exception {
55         super.setUp();
56     }
57
58     @Override
59     protected void tearDown() throws Exception {
60         super.tearDown();
61     }
62
63     public void testCreateResource_P01() {
64         SimulatorResource resource = null;
65         SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
66
67         try {
68             resource = SimulatorManager.createResource(SINGLE_RES_RAML);
69             resType = resource.getType();
70         } catch (InvalidArgsException e) {
71             e.printStackTrace();
72         } catch (SimulatorException e) {
73             e.printStackTrace();
74         }
75
76         assertNotNull(resource);
77         assertTrue(resType == SimulatorResource.Type.SINGLE);
78     }
79
80     public void testCreateResource_P02() {
81         SimulatorResource resource = null;
82         SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
83
84         try {
85             resource = SimulatorManager.createResource(COLLECTION_RES_RAML);
86             resType = resource.getType();
87         } catch (InvalidArgsException e) {
88             e.printStackTrace();
89         } catch (SimulatorException e) {
90             e.printStackTrace();
91         }
92
93         assertNotNull(resource);
94         assertTrue(resType == SimulatorResource.Type.COLLECTION);
95     }
96
97     public void testCreateResource_N01() {
98         ExceptionType exType = ExceptionType.UNKNOWN;
99
100         try {
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;
108         }
109
110         assertTrue(exType == ExceptionType.INVALID_ARGS);
111     }
112
113     public void testCreateResource_N02() {
114         ExceptionType exType = ExceptionType.UNKNOWN;
115
116         try {
117             SimulatorResource resource = SimulatorManager.createResource(null);
118         } catch (InvalidArgsException e) {
119             exType = ExceptionType.INVALID_ARGS;
120         } catch (SimulatorException e) {
121             exType = ExceptionType.SIMULATOR;
122         }
123
124         assertTrue(exType == ExceptionType.INVALID_ARGS);
125     }
126
127     public void testCreateResourceWithCount_P01() {
128         Vector<SimulatorResource> resources = null;
129         SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
130
131         try {
132             resources = SimulatorManager.createResource(SINGLE_RES_RAML, 1);
133             resType = resources.elementAt(0).getType();
134         } catch (InvalidArgsException e) {
135             e.printStackTrace();
136         } catch (SimulatorException e) {
137             e.printStackTrace();
138         }
139
140         assertNotNull(resources);
141         assertTrue(resources.size() == 1);
142         assertTrue(resType == SimulatorResource.Type.SINGLE);
143     }
144
145     public void testCreateResourceWithCount_P02() {
146         Vector<SimulatorResource> resources = null;
147         SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
148
149         try {
150             resources = SimulatorManager.createResource(COLLECTION_RES_RAML, 1);
151             resType = resources.elementAt(0).getType();
152         } catch (InvalidArgsException e) {
153             e.printStackTrace();
154         } catch (SimulatorException e) {
155             e.printStackTrace();
156         }
157
158         assertNotNull(resources);
159         assertTrue(resources.size() == 1);
160         assertTrue(resType == SimulatorResource.Type.COLLECTION);
161     }
162
163     public void testCreateResourceWithCount_N01() {
164         ExceptionType exType = ExceptionType.UNKNOWN;
165
166         try {
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;
174         }
175
176         assertTrue(exType == ExceptionType.INVALID_ARGS);
177     }
178
179     public void testCreateResourceWithCount_N02() {
180         ExceptionType exType = ExceptionType.UNKNOWN;
181
182         try {
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;
189         }
190
191         assertTrue(exType == ExceptionType.INVALID_ARGS);
192     }
193
194     public void testCreateResourceWithCount_N03() {
195         ExceptionType exType = ExceptionType.UNKNOWN;
196
197         try {
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;
204         }
205
206         assertTrue(exType == ExceptionType.INVALID_ARGS);
207     }
208
209     public void testCreateResourceWithCount_N04() {
210         ExceptionType exType = ExceptionType.UNKNOWN;
211
212         try {
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;
219         }
220
221         assertTrue(exType == ExceptionType.INVALID_ARGS);
222     }
223
224     public void testCreateResourceByType_P01() {
225         SimulatorResource resource = null;
226         SimulatorResource.Type resType = SimulatorResource.Type.SINGLE;
227
228         try {
229             String name = "test-singleresource";
230             String uri = "/test/singleresource";
231             String resourceType = "test.singleresource";
232
233             resource = SimulatorManager.createResource(
234                     SimulatorResource.Type.SINGLE, name, uri, resourceType);
235             resType = resource.getType();
236         } catch (InvalidArgsException e) {
237             e.printStackTrace();
238         } catch (SimulatorException e) {
239             e.printStackTrace();
240         }
241
242         assertNotNull(resource);
243         assertTrue(resType == SimulatorResource.Type.SINGLE);
244     }
245
246     public void testCreateResourceByType_P02() {
247         SimulatorResource resource = null;
248         SimulatorResource.Type resType = SimulatorResource.Type.COLLECTION;
249
250         try {
251             String name = "test-collectionresource";
252             String uri = "/test/collectionresource";
253             String resourceType = "test.collectionresource";
254
255             resource = SimulatorManager.createResource(
256                     SimulatorResource.Type.COLLECTION, name, uri, resourceType);
257             resType = resource.getType();
258         } catch (InvalidArgsException e) {
259             e.printStackTrace();
260         } catch (SimulatorException e) {
261             e.printStackTrace();
262         }
263
264         assertNotNull(resource);
265         assertTrue(resType == SimulatorResource.Type.COLLECTION);
266     }
267
268     public void testCreateResourceByType_N01() {
269         ExceptionType exType = ExceptionType.INVALID_ARGS;
270
271         try {
272             String name = "";
273             String uri = "/test/resource";
274             String resourceType = "test.resource";
275
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;
282         }
283
284         assertTrue(exType == ExceptionType.INVALID_ARGS);
285     }
286
287     public void testCreateResourceByType_N02() {
288         ExceptionType exType = ExceptionType.INVALID_ARGS;
289
290         try {
291             String name = null;
292             String uri = "/test/resource";
293             String resourceType = "test.resource";
294
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;
301         }
302
303         assertTrue(exType == ExceptionType.INVALID_ARGS);
304     }
305
306     public void testCreateResourceByType_N03() {
307         ExceptionType exType = ExceptionType.INVALID_ARGS;
308
309         try {
310             String name = "test-resource";
311             String uri = "";
312             String resourceType = "test.resource";
313
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;
320         }
321
322         assertTrue(exType == ExceptionType.INVALID_ARGS);
323     }
324
325     public void testCreateResourceByType_N04() {
326         ExceptionType exType = ExceptionType.INVALID_ARGS;
327
328         try {
329             String name = "test-resource";
330             String uri = null;
331             String resourceType = "test.resource";
332
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;
339         }
340
341         assertTrue(exType == ExceptionType.INVALID_ARGS);
342     }
343
344     public void testCreateResourceByType_N05() {
345         ExceptionType exType = ExceptionType.INVALID_ARGS;
346
347         try {
348             String name = "test-resource";
349             String uri = "/test/resource";;
350             String resourceType = "";
351
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;
358         }
359
360         assertTrue(exType == ExceptionType.INVALID_ARGS);
361     }
362
363     public void testCreateResourceByType_N06() {
364         ExceptionType exType = ExceptionType.INVALID_ARGS;
365
366         try {
367             String name = "test-resource";
368             String uri = "/test/resource";;
369             String resourceType = null;
370
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;
377         }
378
379         assertTrue(exType == ExceptionType.INVALID_ARGS);
380     }
381
382     public void testFindResource_P01() {
383         // Creating sample single resource to test this API
384         SampleSingleResource testResource = new SampleSingleResource();
385         if (false == testResource.start())
386             return;
387
388         CountDownLatch lockObject = new CountDownLatch(1);
389         boolean syncResult = false;
390         ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
391         FindResourceCallbackListener listener = new FindResourceCallbackListener(
392                 lockObject, resourceHolder);
393
394         try {
395             String resourceType = testResource.mResourceType;
396             SimulatorManager.findResource(resourceType, listener);
397             syncResult = true;
398         } catch (InvalidArgsException e) {
399             e.printStackTrace();
400         } catch (SimulatorException e) {
401             e.printStackTrace();
402         }
403
404         if (false == syncResult)
405             testResource.stop();
406
407         assertTrue(syncResult);
408
409         // Wait for the resource to found
410         try {
411             lockObject.await(10, TimeUnit.SECONDS);
412         } catch (InterruptedException e) {
413         }
414
415         testResource.stop();
416         assertNotNull(resourceHolder.get());
417     }
418
419     public void testFindResource_N01() {
420         ExceptionType exType = ExceptionType.UNKNOWN;
421
422         try {
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;
429         }
430
431         assertTrue(exType == ExceptionType.INVALID_ARGS);
432     }
433
434     public void testFindResource_N02() {
435         ExceptionType exType = ExceptionType.UNKNOWN;
436
437         try {
438             String resourceType = "";
439             SimulatorManager.findResource(resourceType,
440                     new FindResourceListener() {
441                         @Override
442                         public void onResourceFound(
443                                 SimulatorRemoteResource resource) {
444                         }
445                     });
446         } catch (InvalidArgsException e) {
447             exType = ExceptionType.INVALID_ARGS;
448         } catch (SimulatorException e) {
449             exType = ExceptionType.SIMULATOR;
450         }
451
452         assertTrue(exType == ExceptionType.INVALID_ARGS);
453     }
454
455     public void testFindResource_N03() {
456         ExceptionType exType = ExceptionType.UNKNOWN;
457
458         try {
459             String resourceType = null;
460             SimulatorManager.findResource(resourceType,
461                     new FindResourceListener() {
462                         @Override
463                         public void onResourceFound(
464                                 SimulatorRemoteResource resource) {
465                         }
466                     });
467         } catch (InvalidArgsException e) {
468             exType = ExceptionType.INVALID_ARGS;
469         } catch (SimulatorException e) {
470             exType = ExceptionType.SIMULATOR;
471         }
472
473         assertTrue(exType == ExceptionType.INVALID_ARGS);
474     }
475
476     public void testFindResourceAll_P01() {
477         // Creating sample single resource to test this API
478         SampleSingleResource testResource = new SampleSingleResource();
479         if (false == testResource.start())
480             return;
481
482         CountDownLatch lockObject = new CountDownLatch(1);
483         boolean syncResult = false;
484         ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
485         FindResourceCallbackListener listener = new FindResourceCallbackListener(
486                 lockObject, resourceHolder);
487
488         try {
489             SimulatorManager.findResource(listener);
490             syncResult = true;
491         } catch (InvalidArgsException e) {
492             e.printStackTrace();
493         } catch (SimulatorException e) {
494             e.printStackTrace();
495         }
496
497         if (false == syncResult)
498             testResource.stop();
499
500         assertTrue(syncResult);
501
502         // Wait for the resource to found
503         try {
504             lockObject.await(10, TimeUnit.SECONDS);
505         } catch (InterruptedException e) {
506         }
507
508         testResource.stop();
509         assertNotNull(resourceHolder.get());
510     }
511
512     public void testFindResourceAll_N01() {
513         ExceptionType exType = ExceptionType.UNKNOWN;
514
515         try {
516             SimulatorManager.findResource(null);
517         } catch (InvalidArgsException e) {
518             exType = ExceptionType.INVALID_ARGS;
519         } catch (SimulatorException e) {
520             exType = ExceptionType.SIMULATOR;
521         }
522
523         assertTrue(exType == ExceptionType.INVALID_ARGS);
524     }
525
526     public void testregisterDeviceInfo_P01() {
527         boolean syncResult = false;
528
529         try {
530             String deviceName = "test-device";
531             SimulatorManager.setDeviceInfo(deviceName);
532             syncResult = true;
533         } catch (InvalidArgsException e) {
534             e.printStackTrace();
535         } catch (SimulatorException e) {
536             e.printStackTrace();
537         }
538
539         assertTrue(syncResult);
540     }
541
542     public void testregisterDeviceInfo_N01() {
543         ExceptionType exType = ExceptionType.UNKNOWN;
544
545         try {
546             String deviceName = "";
547             SimulatorManager.setDeviceInfo(deviceName);
548         } catch (InvalidArgsException e) {
549             exType = ExceptionType.INVALID_ARGS;
550         } catch (SimulatorException e) {
551             exType = ExceptionType.SIMULATOR;
552         }
553
554         assertTrue(exType == ExceptionType.INVALID_ARGS);
555     }
556
557     public void testregisterDeviceInfo_N02() {
558         ExceptionType exType = ExceptionType.UNKNOWN;
559
560         try {
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;
567         }
568
569         assertTrue(exType == ExceptionType.INVALID_ARGS);
570     }
571
572     public void testregisterPlatformInfo_P01() {
573         boolean syncResult = false;
574
575         try {
576             PlatformInfo platformInfo = new PlatformInfo();
577             platformInfo.setManufacturerName("Samsung");
578
579             SimulatorManager.setPlatformInfo(platformInfo);
580             syncResult = true;
581         } catch (InvalidArgsException e) {
582             e.printStackTrace();
583         } catch (SimulatorException e) {
584             e.printStackTrace();
585         }
586
587         assertTrue(syncResult);
588     }
589
590     public void testregisterPlatformInfo_N01() {
591         ExceptionType exType = ExceptionType.UNKNOWN;
592
593         try {
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;
600         }
601
602         assertTrue(exType == ExceptionType.INVALID_ARGS);
603     }
604
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,
610                 infoHolder);
611
612         try {
613             SimulatorManager.setDeviceInfo("Samsung");
614             SimulatorManager.findDevices(listener);
615             syncResult = true;
616         } catch (InvalidArgsException e) {
617             e.printStackTrace();
618         } catch (SimulatorException e) {
619             e.printStackTrace();
620         }
621
622         assertTrue(syncResult);
623
624         // Wait for the asynchronous response
625         try {
626             lockObject.await(10, TimeUnit.SECONDS);
627         } catch (InterruptedException e) {
628         }
629
630         assertNotNull(infoHolder.get());
631     }
632
633     public void testFindDevices_N01() {
634         ExceptionType exType = ExceptionType.UNKNOWN;
635
636         try {
637             SimulatorManager.findDevices(null);
638         } catch (InvalidArgsException e) {
639             exType = ExceptionType.INVALID_ARGS;
640         } catch (SimulatorException e) {
641             exType = ExceptionType.SIMULATOR;
642         }
643
644         assertTrue(exType == ExceptionType.INVALID_ARGS);
645     }
646
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,
652                 infoHolder);
653
654         try {
655             SimulatorManager.getPlatformInformation(listener);
656             syncResult = true;
657         } catch (InvalidArgsException e) {
658             e.printStackTrace();
659         } catch (SimulatorException e) {
660             e.printStackTrace();
661         }
662
663         assertTrue(syncResult);
664
665         // Wait for the asynchronous response
666         try {
667             lockObject.await(10, TimeUnit.SECONDS);
668         } catch (InterruptedException e) {
669         }
670
671         assertNotNull(infoHolder.get());
672     }
673
674     public void testGetPlatformInfo_N01() {
675         ExceptionType exType = ExceptionType.UNKNOWN;
676
677         try {
678             SimulatorManager.getPlatformInformation(null);
679         } catch (InvalidArgsException e) {
680             exType = ExceptionType.INVALID_ARGS;
681         } catch (SimulatorException e) {
682             exType = ExceptionType.SIMULATOR;
683         }
684
685         assertTrue(exType == ExceptionType.INVALID_ARGS);
686     }
687 }
688
689 class FindResourceCallbackListener implements FindResourceListener {
690     private CountDownLatch                        mLockObject;
691     private ObjectHolder<SimulatorRemoteResource> mResourceHolder;
692
693     public FindResourceCallbackListener(CountDownLatch lockObject,
694             ObjectHolder<SimulatorRemoteResource> resourceHolder) {
695         mLockObject = lockObject;
696         mResourceHolder = resourceHolder;
697     }
698
699     @Override
700     public void onResourceFound(SimulatorRemoteResource resource) {
701         mResourceHolder.set(resource);
702         mLockObject.countDown();
703     }
704 }
705
706 class DeviceInfoListener implements DeviceListener {
707     private CountDownLatch           mLockObject;
708     private ObjectHolder<DeviceInfo> mInfoHolder;
709
710     public DeviceInfoListener(CountDownLatch lockObject,
711             ObjectHolder<DeviceInfo> infoHolder) {
712         mLockObject = lockObject;
713         mInfoHolder = infoHolder;
714     }
715
716     @Override
717     public void onDeviceFound(DeviceInfo devInfo) {
718         mInfoHolder.set(devInfo);
719         mLockObject.countDown();
720     }
721 }
722
723 class PlatformInfoListener implements PlatformListener {
724
725     private CountDownLatch             mLockObject;
726     private ObjectHolder<PlatformInfo> mInfoHolder;
727
728     public PlatformInfoListener(CountDownLatch lockObject,
729             ObjectHolder<PlatformInfo> infoHolder) {
730         mLockObject = lockObject;
731         mInfoHolder = infoHolder;
732     }
733
734     @Override
735     public void onPlatformFound(PlatformInfo platformInfo) {
736         mInfoHolder.set(platformInfo);
737         mLockObject.countDown();
738     }
739 }