Java and JNI support for collection resource.
[platform/upstream/iotivity.git] / service / simulator / unittests / SimulatorTest / src / org / oic / simulator / serviceprovider / test / SimlatorResourceServerTest.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.serviceprovider.test;
18
19 import java.util.Vector;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
22
23 import junit.framework.TestCase;
24
25 import org.oic.simulator.InvalidArgsException;
26 import org.oic.simulator.ResourceAttribute;
27 import org.oic.simulator.SimulatorException;
28 import org.oic.simulator.SimulatorManager;
29 import org.oic.simulator.SimulatorResourceModel;
30 import org.oic.simulator.serviceprovider.AutomationType;
31 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
32 import org.oic.simulator.test.ResourceModelChangeListener;
33 import org.oic.simulator.test.ResourceModelObject;
34
35 /**
36  * This class tests the functionality of Simulator Resource Server
37  * class APIs.
38  */
39 public class SimlatorResourceServerTest extends TestCase
40 {
41
42     private static final String CONFIG_PATH = "./ramls/simple-light.raml";
43
44     private static final String KEY = "testkey";
45
46     private CountDownLatch lockObject;
47     private ResourceModelObject resourceModelObject;
48     private ResourceModelChangeListener resourceModelChangeListener;
49
50     private SimulatorResourceServer simulatorResourceServer;
51
52     static
53     {
54         System.loadLibrary("SimulatorManager");
55         System.loadLibrary("RamlParser");
56         System.loadLibrary("oc");
57         System.loadLibrary("oc_logger");
58         System.loadLibrary("octbstack");
59     }
60
61     protected void setUp() throws Exception
62     {
63         super.setUp();
64         lockObject= new CountDownLatch(1);
65         resourceModelObject = new ResourceModelObject();
66         resourceModelChangeListener = new ResourceModelChangeListener(resourceModelObject);
67         simulatorResourceServer = SimulatorManager.createResource(CONFIG_PATH, resourceModelChangeListener);
68     }
69
70     protected void tearDown() throws Exception
71     {
72         super.tearDown();
73
74         SimulatorManager.deleteResource(simulatorResourceServer);
75
76         lockObject = null;
77         resourceModelObject = null;
78         resourceModelChangeListener = null;
79
80         simulatorResourceServer = null;
81     }
82
83     public void testGetURI_P01()
84     {
85         assertNotNull(simulatorResourceServer.getURI());
86     }
87
88     public void testGetResourceType_P01()
89     {
90         assertNotNull(simulatorResourceServer.getResourceType());
91     }
92
93     public void testGetInterfaceType_P01()
94     {
95         assertNotNull(simulatorResourceServer.getInterfaceType());
96     }
97
98     public void testGetModel_P01()
99     {
100         boolean result = false;
101
102         try
103         {
104             if(simulatorResourceServer.getModel() != null && simulatorResourceServer.getModel().size() > 0)
105                 result = true;
106         }
107         catch (InvalidArgsException e)
108         {
109             e.printStackTrace();
110         }
111         catch (SimulatorException e)
112         {
113             e.printStackTrace();
114         }
115
116         assertTrue(result);
117     }
118
119     public void testAddAttributeInteger_P01()
120     {
121         try
122         {
123             simulatorResourceServer.addAttributeInteger(KEY, 2);
124         }
125         catch (Exception e)
126         {
127             e.printStackTrace();
128         }
129         assertEquals(Integer.parseInt(getValue(KEY) + ""), 2);
130     }
131
132     public void testAddAttributeDouble_P01()
133     {
134         try
135         {
136             simulatorResourceServer.addAttributeDouble(KEY, 4.0);
137         }
138         catch (Exception e)
139         {
140             e.printStackTrace();
141         }
142         assertEquals(Double.parseDouble(getValue(KEY) + ""), 4.0);
143     }
144
145     public void testAddAttributeBoolean_P01()
146     {
147         try
148         {
149             simulatorResourceServer.addAttributeBoolean(KEY, Boolean.parseBoolean("true"));
150         }
151         catch (Exception e)
152         {
153             e.printStackTrace();
154         }
155         assertEquals(Boolean.parseBoolean(getValue(KEY).toString() + ""), true);
156     }
157
158     public void testaddAttributeString_P01()
159     {
160         try
161         {
162             simulatorResourceServer.addAttributeString(KEY, "test");
163         }
164         catch (Exception e)
165         {
166             e.printStackTrace();
167         }
168         assertEquals(getValue(KEY) + "", "test");
169     }
170
171     public void testUpdateAttributeInteger_P01()
172     {
173         boolean result = true;
174
175         try
176         {
177             simulatorResourceServer.addAttributeInteger(KEY, 10);
178         }
179         catch (Exception e)
180         {
181             result = false;
182             e.printStackTrace();
183         }
184
185         result = result && Integer.parseInt(getValue(KEY) + "") == 10;
186
187         try
188         {
189             simulatorResourceServer.updateAttributeInteger(KEY, 12);
190         }
191         catch (Exception e)
192         {
193             result = false;
194             e.printStackTrace();
195         }
196         result = result && Integer.parseInt(getValue(KEY) + "") == 12;
197
198         assertTrue(result);
199     }
200
201     public void testUpdateAttributeDouble_P01()
202     {
203         boolean result = true;
204
205         try
206         {
207             simulatorResourceServer.addAttributeDouble(KEY, 22.0);
208         }
209         catch (Exception e)
210         {
211             result = false;
212             e.printStackTrace();
213         }
214
215         result = result && Double.parseDouble(getValue(KEY) + "") == 22.0;
216
217         try
218         {
219             simulatorResourceServer.updateAttributeDouble(KEY, 25.3);
220         }
221         catch (Exception e)
222         {
223             result = false;
224             e.printStackTrace();
225         }
226
227         result = result && Double.parseDouble(getValue(KEY) + "") == 25.3;
228
229         assertTrue(result);
230     }
231
232     public void testUpdateAttributeBoolean_P01()
233     {
234         boolean result = true;
235
236         try
237         {
238             simulatorResourceServer.addAttributeBoolean(KEY, Boolean.parseBoolean("true"));
239         }
240         catch (Exception e)
241         {
242             result = false;
243             e.printStackTrace();
244         }
245
246         result = result && Boolean.parseBoolean(getValue(KEY) + "")==true;
247
248         try
249         {
250             simulatorResourceServer.updateAttributeBoolean(KEY, Boolean.parseBoolean("false"));
251         }
252         catch (Exception e)
253         {
254             result = false;
255             e.printStackTrace();
256         }
257
258         result = result && !Boolean.parseBoolean(getValue(KEY) + "");
259
260         assertTrue(result);
261     }
262
263     public void testupdateAttributeString_P01()
264     {
265         boolean result = true;
266
267         try
268         {
269             simulatorResourceServer.addAttributeString(KEY, "old");
270         }
271         catch (Exception e)
272         {
273             result = false;
274             e.printStackTrace();
275         }
276
277         result = result && getValue(KEY).equals("old");
278
279         try
280         {
281             simulatorResourceServer.updateAttributeString(KEY, "new");
282         }
283         catch (Exception e)
284         {
285             result = false;
286             e.printStackTrace();
287         }
288
289         result = result && getValue(KEY).equals("new");
290
291         assertTrue(result);
292     }
293
294     public void testSetRange_P01()
295     {
296         boolean result = true;
297
298         try
299         {
300             simulatorResourceServer.addAttributeInteger(KEY, 10);
301             simulatorResourceServer.setRange(KEY, 1, 12);
302         }
303         catch (Exception e)
304         {
305             result = false;
306             e.printStackTrace();
307         }
308
309         result = result && Integer.parseInt(getValue(KEY) + "") == 10;
310
311         try
312         {
313             simulatorResourceServer.updateAttributeInteger(KEY, 3);
314         }
315         catch (Exception e)
316         {
317             result = false;
318             e.printStackTrace();
319         }
320
321         result = result && Integer.parseInt(getValue(KEY) + "") == 3;
322
323         assertTrue(result);
324     }
325
326     /**
327      * Try to set the value out of range
328      */
329     public void testSetRange_N01()
330     {
331         boolean result = true;
332
333         try
334         {
335             simulatorResourceServer.addAttributeInteger(KEY, 10);
336             simulatorResourceServer.setRange(KEY, 1, 12);
337         }
338         catch (Exception e)
339         {
340             result = false;
341             e.printStackTrace();
342         }
343
344         result = result && Integer.parseInt(getValue(KEY) + "") == 10;
345
346         try
347         {
348             simulatorResourceServer.updateAttributeInteger(KEY, 13);
349             result = false;
350         }
351         catch (Exception e)
352         {
353             result = true;
354         }
355
356         result = result && Integer.parseInt(getValue(KEY) + "") == 10;
357
358         assertTrue(result);
359     }
360
361     public void testSetAllowedValuesInteger_P01()
362     {
363         boolean result = true;
364
365         try
366         {
367             simulatorResourceServer.addAttributeInteger(KEY, 10);
368         }
369         catch (Exception e)
370         {
371             result = false;
372             e.printStackTrace();
373         }
374
375         Vector<Integer> values = new Vector<Integer>();
376         values.add(1);
377         values.add(10);
378         values.add(20);
379         values.add(50);
380
381         try
382         {
383             simulatorResourceServer.setAllowedValuesInteger(KEY, values);
384         }
385         catch (Exception e)
386         {
387             result = false;
388             e.printStackTrace();
389         }
390
391         result = result && Integer.parseInt(getValue(KEY) + "") == 10;
392
393         try
394         {
395             simulatorResourceServer.updateAttributeInteger(KEY, 20);
396         }
397         catch (Exception e)
398         {
399             result = false;
400             e.printStackTrace();
401         }
402
403         result = result && Integer.parseInt(getValue(KEY) + "") == 20;
404
405         assertTrue(result);
406     }
407
408     /**
409      * Try setting with out of range
410      */
411     public void testSetAllowedValuesInteger_N01()
412     {
413         boolean result = true;
414
415         try
416         {
417             simulatorResourceServer.addAttributeInteger(KEY, 10);
418         }
419         catch (Exception e)
420         {
421             result = false;
422             e.printStackTrace();
423         }
424
425         Vector<Integer> values = new Vector<Integer>();
426         values.add(1);
427         values.add(10);
428         values.add(20);
429         values.add(50);
430
431         try
432         {
433             simulatorResourceServer.setAllowedValuesInteger(KEY, values);
434         }
435         catch (Exception e)
436         {
437             result = false;
438             e.printStackTrace();
439         }
440
441         result = result && Integer.parseInt(getValue(KEY) + "") == 10;
442
443         try
444         {
445             simulatorResourceServer.updateAttributeInteger(KEY, 2);
446             result = false;
447         }
448         catch (Exception e)
449         {
450             result = true;
451         }
452
453         result = result && Integer.parseInt(getValue(KEY) + "") == 10;
454
455         assertTrue(result);
456     }
457
458     /**
459      * Try setting values multiple times
460      */
461     public void testSetAllowedValuesDouble_P01()
462     {
463         boolean result = true;
464
465         try
466         {
467             simulatorResourceServer.addAttributeDouble(KEY, 11.5);
468         }
469         catch (Exception e)
470         {
471             result = false;
472             e.printStackTrace();
473         }
474
475         Vector<Double> values = new Vector<Double>();
476         values.add(11.5);
477         values.add(10.5);
478         values.add(20.5);
479         values.add(50.5);
480
481         try
482         {
483             simulatorResourceServer.setAllowedValuesDouble(KEY, values);
484         }
485         catch (Exception e)
486         {
487             result = false;
488             e.printStackTrace();
489         }
490
491         result = result && Double.parseDouble(getValue(KEY) + "") == 11.5;
492
493         try
494         {
495             simulatorResourceServer.updateAttributeDouble(KEY, 10.5);
496         }
497         catch (Exception e)
498         {
499             result = false;
500             e.printStackTrace();
501         }
502
503         result = result && Double.parseDouble(getValue(KEY) + "") == 10.5;
504
505         assertTrue(result);
506     }
507
508     /**
509      * Try setting with out of range
510      */
511     public void testSetAllowedValuesDouble_N01()
512     {
513         boolean result = true;
514
515         try
516         {
517             simulatorResourceServer.addAttributeDouble(KEY, 10.5);
518         }
519         catch (Exception e)
520         {
521             result = false;
522             e.printStackTrace();
523         }
524
525         Vector<Double> values = new Vector<Double>();
526         values.add(11.5);
527         values.add(10.5);
528         values.add(20.5);
529         values.add(50.5);
530
531         try
532         {
533             simulatorResourceServer.setAllowedValuesDouble(KEY, values);
534         }
535         catch (Exception e)
536         {
537             result = false;
538             e.printStackTrace();
539         }
540
541         result = result && Double.parseDouble(getValue(KEY) + "") == 10.5;
542
543         try
544         {
545             simulatorResourceServer.updateAttributeDouble(KEY, 2.2);
546             result = false;
547         }
548         catch (Exception e)
549         {
550             result = true;
551         }
552
553         result = result && Double.parseDouble(getValue(KEY) + "") == 10.5;
554
555         assertTrue(result);
556     }
557
558     public void testsetAllowedValuesString_P01()
559     {
560         boolean result = true;
561
562         try
563         {
564             simulatorResourceServer.addAttributeString(KEY, "mon");
565         }
566         catch (Exception e)
567         {
568             result = false;
569             e.printStackTrace();
570         }
571
572         Vector<String> values = new Vector<String>();
573         values.add("mon");
574         values.add("tue");
575         values.add("wed");
576
577         try
578         {
579             simulatorResourceServer.setAllowedValuesString(KEY, values);
580         }
581         catch (Exception e)
582         {
583             result = false;
584             e.printStackTrace();
585         }
586
587         result = result && getValue(KEY).equals("mon");
588
589         try
590         {
591             simulatorResourceServer.updateAttributeString(KEY, "tue");
592         }
593         catch (Exception e)
594         {
595             result = false;
596             e.printStackTrace();
597         }
598
599         result = result && getValue(KEY).equals("tue");
600
601         assertTrue(result);
602     }
603
604     /**
605      * Set the value that is not in allowed values
606      */
607     public void testsetAllowedValuesString_N01()
608     {
609         boolean result = true;
610
611         try
612         {
613             simulatorResourceServer.addAttributeString(KEY, "mon");
614         }
615         catch (Exception e)
616         {
617             result = false;
618             e.printStackTrace();
619         }
620
621         Vector<String> values = new Vector<String>();
622         values.add("mon");
623         values.add("tue");
624         values.add("wed");
625
626         try
627         {
628             simulatorResourceServer.setAllowedValuesString(KEY, values);
629         }
630         catch (Exception e)
631         {
632             result = false;
633             e.printStackTrace();
634         }
635
636         result = result && getValue(KEY).equals("mon");
637
638         try
639         {
640             simulatorResourceServer.updateAttributeString(KEY, "thu");
641             result = false;
642         }
643         catch (Exception e)
644         {
645             result = true;
646         }
647
648         result = result && getValue(KEY).equals("mon");
649
650         assertTrue(result);
651     }
652
653     public void testRemoveAttribute_P01()
654     {
655         boolean result = true;
656
657         try
658         {
659             simulatorResourceServer.addAttributeString(KEY, "fri");
660         }
661         catch (Exception e)
662         {
663             result = false;
664             e.printStackTrace();
665         }
666
667         result = result && getValue(KEY).equals("fri");
668
669         try
670         {
671             simulatorResourceServer.removeAttribute(KEY);
672             result = result && !simulatorResourceServer.getModel().getAttributes().containsKey(KEY);
673         }
674         catch (Exception e)
675         {
676             result = false;
677             e.printStackTrace();
678         }
679
680         assertTrue(result);
681     }
682
683     /**
684      * Try removing the attribute thats not present
685      */
686     public void testRemoveAttribute_N01()
687     {
688         boolean result = false;
689
690         try
691         {
692             simulatorResourceServer.removeAttribute("something");
693         }
694         catch (Exception e)
695         {
696             result = true;
697         }
698
699         assertTrue(result);
700     }
701
702     /**
703      * Try removing when null is passed
704      */
705     public void testRemoveAttribute_N02()
706     {
707         boolean result = false;
708
709         try
710         {
711             simulatorResourceServer.removeAttribute(null);
712         }
713         catch (Exception e)
714         {
715             result = true;
716         }
717
718         assertTrue(result);
719     }
720
721     /**
722      * Try removing when attribute is empty
723      */
724     public void testRemoveAttribute_N03()
725     {
726         boolean result = false;
727
728         try
729         {
730             simulatorResourceServer.removeAttribute("");
731         }
732         catch (Exception e)
733         {
734             result = true;
735         }
736
737         assertTrue(result);
738     }
739
740     public void testStartResourceAutomation_P01()
741     {
742         boolean result = true;
743         lockObject = new CountDownLatch(1);
744         AutomationObject automationObject = new AutomationObject();
745         AutomationListener automationListener = new AutomationListener(lockObject, automationObject);
746         int id = 0;
747         try
748         {
749             id = simulatorResourceServer.startResourceAutomation(AutomationType.NORMAL, automationListener);
750         }
751         catch (Exception e)
752         {
753             result = false;
754             e.printStackTrace();
755         }
756
757         try
758         {
759             lockObject.await(15,TimeUnit.SECONDS);
760         }
761         catch (InterruptedException e)
762         {
763             e.printStackTrace();
764         }
765
766         assertTrue(result && automationObject.getResourceURI() != null && automationObject.getAutomationId() != -1 && id != -1);
767
768         try
769         {
770             simulatorResourceServer.stopAutomation(id);
771         }
772         catch (Exception e)
773         {
774             e.printStackTrace();
775         }
776     }
777
778     /**
779      * null listener
780      */
781     public void testStartResourceAutomation_N01()
782     {
783         boolean result = true;
784         int id = 0;
785         try
786         {
787             id = simulatorResourceServer.startResourceAutomation(AutomationType.NORMAL, null);
788         }
789         catch (Exception e)
790         {
791             result = false;
792         }
793         assertTrue(!result && id == 0);
794     }
795
796     public void testStartAttributeAutomation_P01()
797     {
798         boolean result = true;
799         lockObject = new CountDownLatch(1);
800         AutomationObject automationObject = new AutomationObject();
801         AutomationListener automationListener = new AutomationListener(lockObject, automationObject);
802         int id = 0;
803         try
804         {
805             simulatorResourceServer.addAttributeInteger(KEY, 10);
806             id = simulatorResourceServer.startAttributeAutomation(KEY,
807                     AutomationType.NORMAL, automationListener);
808         }
809         catch (Exception e)
810         {
811             result = false;
812         }
813
814         try
815         {
816             lockObject.await(10, TimeUnit.SECONDS);
817         }
818         catch (InterruptedException e)
819         {
820         }
821
822         assertTrue(result && automationObject.getResourceURI() != null && automationObject.getAutomationId() != -1 && id != -1);
823
824         try
825         {
826             simulatorResourceServer.stopAutomation(id);
827         }
828         catch (Exception e)
829         {
830             e.printStackTrace();
831         }
832     }
833
834     /**
835      * null listener
836      */
837     public void testStartAttributeAutomation_N01()
838     {
839         boolean result = false;
840         int id = 0;
841         try
842         {
843             id = simulatorResourceServer.startAttributeAutomation(simulatorResourceServer.getModel().getAttributes().get(0).getName(),
844                     AutomationType.NORMAL, null);
845             result = false;
846         }
847         catch (Exception e)
848         {
849             result = true;
850         }
851
852         assertTrue(result && id != -1);
853     }
854
855     public void testStopAutomation_P01()
856     {
857         boolean result = true;
858
859         lockObject = new CountDownLatch(1);
860
861         AutomationObject automationObject = new AutomationObject();
862         AutomationListener automationListener = new AutomationListener(lockObject, automationObject);
863
864         int id = 0;
865         try
866         {
867             id = simulatorResourceServer.startResourceAutomation(AutomationType.NORMAL, automationListener);
868         }
869         catch(Exception e)
870         {
871             result = false;
872         }
873
874         try
875         {
876             lockObject.await(05, TimeUnit.SECONDS);
877         }
878         catch (InterruptedException e)
879         {
880         }
881
882         try
883         {
884             simulatorResourceServer.stopAutomation(id);
885         }
886         catch(Exception e)
887         {
888             result = false;
889         }
890
891         assertTrue(result);
892     }
893
894     /**
895      * checking for crash
896      * random id
897      */
898     public void testStopAutomation_N01()
899     {
900         boolean result = false;
901         try
902         {
903             simulatorResourceServer.stopAutomation(144353544);
904         }
905         catch (Exception e)
906         {
907             result = true;
908             e.printStackTrace();
909         }
910         assertTrue(result);
911     }
912
913     private Object getValue(Object key)
914     {
915         SimulatorResourceModel simulatorResourceModel = null;
916         try
917         {
918             simulatorResourceModel = simulatorResourceServer.getModel();
919         }
920         catch (Exception e)
921         {
922             e.printStackTrace();
923         }
924
925         ResourceAttribute resourceAttribute = null;
926         try
927         {
928             resourceAttribute = simulatorResourceModel.getAttributes().get(key);
929         }
930         catch (SimulatorException e)
931         {
932             e.printStackTrace();
933         }
934         return resourceAttribute.getValue();
935     }
936 }