Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / simulator / unittests / SimulatorTest / src / org / oic / simulator / server / test / SimulatorResourceTest.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.server.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.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;
34
35 import junit.framework.TestCase;
36
37 /**
38  * This class tests the APIs of SimulatorResource class
39  */
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;
45
46     protected void setUp() throws Exception {
47         super.setUp();
48         resource = SimulatorManager.createResource(
49                 SimulatorResource.Type.SINGLE, RES_NAME, RES_URI, RES_TYPE);
50     }
51
52     protected void tearDown() throws Exception {
53         super.tearDown();
54         resource = null;
55     }
56
57     public void testIsCollection_P01() {
58         boolean isCollection = false;
59         isCollection = resource.isCollection();
60         assertFalse(isCollection);
61     }
62
63     public void testIsCollection_P02() {
64         boolean isCollection = true;
65
66         try {
67             SimulatorResource res = SimulatorManager.createResource(
68                     SimulatorResource.Type.COLLECTION, RES_NAME, RES_URI,
69                     RES_TYPE);
70             isCollection = res.isCollection();
71         } catch (SimulatorException e) {
72             e.printStackTrace();
73         }
74
75         assertTrue(isCollection);
76     }
77
78     public void testGetName_P01() {
79         String name = null;
80
81         try {
82             name = resource.getName();
83         } catch (SimulatorException e) {
84             e.printStackTrace();
85         }
86
87         assertEquals(RES_NAME, name);
88     }
89
90     public void testGetType_P01() {
91         SimulatorResource.Type type = SimulatorResource.Type.SINGLE;
92         type = resource.getType();
93         assertEquals(SimulatorResource.Type.SINGLE, type);
94     }
95
96     public void testGetURI_P01() {
97         String uri = null;
98
99         try {
100             uri = resource.getURI();
101         } catch (SimulatorException e) {
102             e.printStackTrace();
103         }
104
105         assertEquals(RES_URI, uri);
106     }
107
108     public void testGetResourceType_P01() {
109         String resType = null;
110
111         try {
112             resType = resource.getResourceType();
113         } catch (SimulatorException e) {
114             e.printStackTrace();
115         }
116
117         assertEquals(RES_TYPE, resType);
118     }
119
120     public void testGetInterface_P01() {
121         Vector<String> interfaces = null;
122
123         try {
124             interfaces = resource.getInterface();
125         } catch (SimulatorException e) {
126             e.printStackTrace();
127         }
128
129         assertNotNull(interfaces);
130         assertTrue(interfaces.size() > 0);
131     }
132
133     public void testSetName_P01() {
134         String name = "new-name";
135         String newName = null;
136
137         try {
138             resource.setName(name);
139             newName = resource.getName();
140         } catch (InvalidArgsException e) {
141             e.printStackTrace();
142         } catch (SimulatorException e) {
143             e.printStackTrace();
144         }
145
146         assertEquals(name, newName);
147     }
148
149     public void testSetName_N01() {
150         String newName = "";
151         ExceptionType exType = ExceptionType.UNKNOWN;
152
153         try {
154             resource.setName(newName);
155         } catch (InvalidArgsException e) {
156             exType = ExceptionType.INVALID_ARGS;
157         } catch (SimulatorException e) {
158             exType = ExceptionType.SIMULATOR;
159         }
160
161         assertEquals(ExceptionType.INVALID_ARGS, exType);
162     }
163
164     public void testSetName_N02() {
165         String newName = null;
166         ExceptionType exType = ExceptionType.UNKNOWN;
167
168         try {
169             resource.setName(newName);
170         } catch (InvalidArgsException e) {
171             exType = ExceptionType.INVALID_ARGS;
172         } catch (SimulatorException e) {
173             exType = ExceptionType.SIMULATOR;
174         }
175
176         assertEquals(ExceptionType.INVALID_ARGS, exType);
177     }
178
179     public void testSetURI_P01() {
180         String newUri = "/test/newuri/1";
181         String uri = null;
182
183         try {
184             resource.setURI(newUri);
185             uri = resource.getURI();
186         } catch (InvalidArgsException e) {
187             e.printStackTrace();
188         } catch (SimulatorException e) {
189             e.printStackTrace();
190         }
191
192         assertEquals(newUri, uri);
193     }
194
195     public void testSetURI_N01() {
196         String newUri = "";
197         ExceptionType exType = ExceptionType.UNKNOWN;
198
199         try {
200             resource.setURI(newUri);
201         } catch (InvalidArgsException e) {
202             exType = ExceptionType.INVALID_ARGS;
203         } catch (SimulatorException e) {
204             exType = ExceptionType.SIMULATOR;
205         }
206
207         assertEquals(ExceptionType.INVALID_ARGS, exType);
208     }
209
210     public void testSetURI_N02() {
211         String newUri = null;
212         ExceptionType exType = ExceptionType.UNKNOWN;
213
214         try {
215             resource.setURI(newUri);
216         } catch (InvalidArgsException e) {
217             exType = ExceptionType.INVALID_ARGS;
218         } catch (SimulatorException e) {
219             exType = ExceptionType.SIMULATOR;
220         }
221
222         assertEquals(ExceptionType.INVALID_ARGS, exType);
223     }
224
225     public void testSetResourceType_P01() {
226         String newResType = "test.newresource";
227         String resType = null;
228
229         try {
230             resource.setResourceType(newResType);
231             resType = resource.getResourceType();
232         } catch (InvalidArgsException e) {
233             e.printStackTrace();
234         } catch (SimulatorException e) {
235             e.printStackTrace();
236         }
237
238         assertEquals(newResType, resType);
239     }
240
241     public void testSetResourceType_N01() {
242         String newResType = "";
243         ExceptionType exType = ExceptionType.UNKNOWN;
244
245         try {
246             resource.setResourceType(newResType);
247         } catch (InvalidArgsException e) {
248             exType = ExceptionType.INVALID_ARGS;
249         } catch (SimulatorException e) {
250             exType = ExceptionType.SIMULATOR;
251         }
252
253         assertEquals(ExceptionType.INVALID_ARGS, exType);
254     }
255
256     public void testSetResourceType_N02() {
257         String newResType = null;
258         ExceptionType exType = ExceptionType.UNKNOWN;
259
260         try {
261             resource.setResourceType(newResType);
262         } catch (InvalidArgsException e) {
263             exType = ExceptionType.INVALID_ARGS;
264         } catch (SimulatorException e) {
265             exType = ExceptionType.SIMULATOR;
266         }
267
268         assertEquals(ExceptionType.INVALID_ARGS, exType);
269     }
270
271     public void testSetInterface_P01() {
272         String newInterface = "oic.if.r";
273         Vector<String> interfaces = null;
274
275         try {
276             resource.setInterface(newInterface);
277             interfaces = resource.getInterface();
278         } catch (InvalidArgsException e) {
279             e.printStackTrace();
280         } catch (SimulatorException e) {
281             e.printStackTrace();
282         }
283
284         assertNotNull(interfaces);
285         assertTrue(interfaces.size() == 1
286                 && interfaces.get(0).equals(newInterface));
287     }
288
289     public void testSetInterface_N01() {
290         String newInterface = "";
291         ExceptionType exType = ExceptionType.UNKNOWN;
292
293         try {
294             resource.setInterface(newInterface);
295         } catch (InvalidArgsException e) {
296             exType = ExceptionType.INVALID_ARGS;
297         } catch (SimulatorException e) {
298             exType = ExceptionType.SIMULATOR;
299         }
300
301         assertEquals(ExceptionType.INVALID_ARGS, exType);
302     }
303
304     public void testSetInterface_N02() {
305         String newInterface = null;
306         ExceptionType exType = ExceptionType.UNKNOWN;
307
308         try {
309             resource.setInterface(newInterface);
310         } catch (InvalidArgsException e) {
311             exType = ExceptionType.INVALID_ARGS;
312         } catch (SimulatorException e) {
313             exType = ExceptionType.SIMULATOR;
314         }
315
316         assertEquals(ExceptionType.INVALID_ARGS, exType);
317     }
318
319     public void testSetInterface_N03() {
320         String newInterface = "oic.if.s";
321         ExceptionType exType = ExceptionType.UNKNOWN;
322
323         try {
324             resource.start();
325             resource.setInterface(newInterface);
326         } catch (InvalidArgsException e) {
327             exType = ExceptionType.INVALID_ARGS;
328         } catch (SimulatorException e) {
329             exType = ExceptionType.SIMULATOR;
330         }
331
332         try {
333             resource.stop();
334         } catch (SimulatorException e) {
335             e.printStackTrace();
336         }
337
338         assertEquals(ExceptionType.SIMULATOR, exType);
339     }
340
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);
348         try {
349             resource.setInterface(newInterfaces);
350             interfaces = resource.getInterface();
351         } catch (InvalidArgsException e) {
352             e.printStackTrace();
353         } catch (SimulatorException e) {
354             e.printStackTrace();
355         }
356
357         assertNotNull(interfaces);
358         assertTrue(interfaces.size() == 2 && interfaces.contains(interface1)
359                 && interfaces.contains(interface2));
360     }
361
362     public void testSetInterfaces_N01() {
363         Vector<String> newInterfaces = new Vector<String>();
364         ExceptionType exType = ExceptionType.UNKNOWN;
365
366         try {
367             resource.setInterface(newInterfaces);
368         } catch (InvalidArgsException e) {
369             exType = ExceptionType.INVALID_ARGS;
370         } catch (SimulatorException e) {
371             exType = ExceptionType.SIMULATOR;
372         }
373
374         assertEquals(ExceptionType.INVALID_ARGS, exType);
375     }
376
377     public void testSetInterfaces_N02() {
378         Vector<String> newInterfaces = null;
379         ExceptionType exType = ExceptionType.UNKNOWN;
380
381         try {
382             resource.setInterface(newInterfaces);
383         } catch (InvalidArgsException e) {
384             exType = ExceptionType.INVALID_ARGS;
385         } catch (SimulatorException e) {
386             exType = ExceptionType.SIMULATOR;
387         }
388
389         assertEquals(ExceptionType.INVALID_ARGS, exType);
390     }
391
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);
399
400         try {
401             resource.start();
402             resource.setInterface(newInterfaces);
403         } catch (InvalidArgsException e) {
404             exType = ExceptionType.INVALID_ARGS;
405         } catch (SimulatorException e) {
406             exType = ExceptionType.SIMULATOR;
407         }
408
409         try {
410             resource.stop();
411         } catch (SimulatorException e) {
412             e.printStackTrace();
413         }
414
415         assertEquals(ExceptionType.SIMULATOR, exType);
416     }
417
418     public void testAddInterface_P01() {
419         String newInterface = "oic.if.rw";
420         Vector<String> interfaces = null;
421
422         try {
423             resource.addInterface(newInterface);
424             interfaces = resource.getInterface();
425         } catch (InvalidArgsException e) {
426             e.printStackTrace();
427         } catch (NoSupportException e) {
428             e.printStackTrace();
429         } catch (SimulatorException e) {
430             e.printStackTrace();
431         }
432
433         assertNotNull(interfaces);
434         assertTrue(interfaces.contains(newInterface));
435     }
436
437     public void testAddInterface_N01() {
438         String newInterface = "";
439         ExceptionType exType = ExceptionType.UNKNOWN;
440
441         try {
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;
449         }
450
451         assertEquals(ExceptionType.INVALID_ARGS, exType);
452     }
453
454     public void testAddInterface_N02() {
455         String newInterface = null;
456         ExceptionType exType = ExceptionType.UNKNOWN;
457
458         try {
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;
466         }
467
468         assertEquals(ExceptionType.INVALID_ARGS, exType);
469     }
470
471     public void testAddInterface_N03() {
472         String newInterface = "oic.if.a";
473         ExceptionType exType = ExceptionType.UNKNOWN;
474
475         try {
476             SimulatorResource res = SimulatorManager.createResource(
477                     SimulatorResource.Type.COLLECTION, RES_NAME, RES_URI,
478                     RES_TYPE);
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;
486         }
487
488         assertEquals(ExceptionType.NOT_SUPPORTED, exType);
489     }
490
491     public void testSetObservable_P01() {
492         boolean newState = true;
493         boolean state = false;
494         try {
495             resource.setObservable(newState);
496             state = resource.isObservable();
497         } catch (SimulatorException e) {
498             e.printStackTrace();
499         }
500
501         assertEquals(newState, state);
502     }
503
504     public void testSetObservable_P02() {
505         boolean newState = false;
506         boolean state = true;
507         try {
508             resource.setObservable(newState);
509             state = resource.isObservable();
510         } catch (SimulatorException e) {
511             e.printStackTrace();
512         }
513
514         assertEquals(newState, state);
515     }
516
517     public void testSetDiscoverable_P01() {
518         boolean newState = true;
519         boolean state = false;
520         try {
521             resource.setDiscoverable(newState);
522             state = resource.isDiscoverable();
523         } catch (SimulatorException e) {
524             e.printStackTrace();
525         }
526
527         assertEquals(newState, state);
528     }
529
530     public void testSetDiscoverable_P02() {
531         boolean newState = false;
532         boolean state = true;
533         try {
534             resource.setDiscoverable(newState);
535             state = resource.isDiscoverable();
536         } catch (SimulatorException e) {
537             e.printStackTrace();
538         }
539
540         assertEquals(newState, state);
541     }
542
543     public void testIsObservable_P01() {
544         boolean state = false;
545
546         try {
547             state = resource.isObservable();
548         } catch (SimulatorException e) {
549             e.printStackTrace();
550         }
551
552         assertEquals(true, state);
553     }
554
555     public void testIsDiscoverable_P01() {
556         boolean state = false;
557
558         try {
559             state = resource.isDiscoverable();
560         } catch (SimulatorException e) {
561             e.printStackTrace();
562         }
563
564         assertEquals(true, state);
565     }
566
567     public void testGetResourceModel_P01() {
568         SimulatorResourceModel resModel = null;
569         String singleResourceRaml = "./ramls/oic.r.light.raml";
570         try {
571             SimulatorSingleResource resource = (SimulatorSingleResource) SimulatorManager
572                     .createResource(singleResourceRaml);
573             resModel = resource.getResourceModel();
574         } catch (SimulatorException e) {
575             e.printStackTrace();
576         }
577
578         assertNotNull(resModel);
579         assertTrue(resModel.size() > 0);
580     }
581
582     public void testIsStarted_P01() {
583         boolean state = true;
584
585         try {
586             state = resource.isStarted();
587         } catch (SimulatorException e) {
588             e.printStackTrace();
589         }
590
591         assertEquals(false, state);
592     }
593
594     public void testIsStarted_P02() {
595         boolean state = false;
596
597         try {
598             resource.start();
599             state = resource.isStarted();
600             resource.stop();
601         } catch (SimulatorException e) {
602             e.printStackTrace();
603         }
604
605         assertEquals(true, state);
606     }
607
608     public void testIsStarted_P03() {
609         boolean state = true;
610
611         try {
612             resource.start();
613             resource.stop();
614             state = resource.isStarted();
615         } catch (SimulatorException e) {
616             e.printStackTrace();
617         }
618
619         assertEquals(false, state);
620     }
621
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);
627
628         try {
629             resource.start();
630             SimulatorManager.findResource(resource.getResourceType(), listener);
631
632             // Wait for the resource to found
633             try {
634                 lockObject.await(10, TimeUnit.SECONDS);
635             } catch (InterruptedException e) {
636                 e.printStackTrace();
637             }
638         } catch (InvalidArgsException e) {
639             e.printStackTrace();
640         } catch (SimulatorException e) {
641             e.printStackTrace();
642         }
643
644         try {
645             resource.stop();
646         } catch (SimulatorException e) {
647             e.printStackTrace();
648         }
649
650         assertNotNull(resourceHolder.get());
651     }
652
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);
658
659         try {
660             resource.start();
661             resource.stop();
662             SimulatorManager.findResource(resource.getResourceType(), listener);
663
664             // Wait for the resource to found
665             try {
666                 lockObject.await(10, TimeUnit.SECONDS);
667             } catch (InterruptedException e) {
668                 e.printStackTrace();
669             }
670         } catch (InvalidArgsException e) {
671             e.printStackTrace();
672         } catch (SimulatorException e) {
673             e.printStackTrace();
674         }
675
676         try {
677             resource.stop();
678         } catch (SimulatorException e) {
679             e.printStackTrace();
680         }
681
682         assertNull(resourceHolder.get());
683     }
684 }
685
686 class FindResourceCallbackListener implements FindResourceListener {
687
688     private CountDownLatch                        mLockObject;
689     private ObjectHolder<SimulatorRemoteResource> mResourceHolder;
690
691     public FindResourceCallbackListener(CountDownLatch lockObject,
692             ObjectHolder<SimulatorRemoteResource> resourceHolder) {
693         mLockObject = lockObject;
694         mResourceHolder = resourceHolder;
695     }
696
697     @Override
698     public void onResourceFound(SimulatorRemoteResource resource) {
699         mResourceHolder.set(resource);
700         mLockObject.countDown();
701     }
702 }