JUnit test cases for updated APIs.
[platform/upstream/iotivity.git] / service / simulator / unittests / SimulatorTest / src / org / oic / simulator / server / test / SimulatorSingleResourceTest.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.concurrent.CountDownLatch;
20 import java.util.concurrent.TimeUnit;
21
22 import org.oic.simulator.AttributeProperty;
23 import org.oic.simulator.AttributeValue;
24 import org.oic.simulator.InvalidArgsException;
25 import org.oic.simulator.SimulatorException;
26 import org.oic.simulator.SimulatorManager;
27 import org.oic.simulator.SimulatorResourceAttribute;
28 import org.oic.simulator.SimulatorResourceModel;
29 import org.oic.simulator.server.SimulatorResource;
30 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
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 public class SimulatorSingleResourceTest extends TestCase {
38     static {
39         System.loadLibrary("SimulatorManager");
40         System.loadLibrary("RamlParser");
41         System.loadLibrary("oc");
42         System.loadLibrary("oc_logger");
43         System.loadLibrary("octbstack");
44     }
45
46     private static final String     SINGLE_RES_RAML = "./ramls/oic.r.light.raml";
47     private static final String     INT_KEY         = "Interger";
48     private static final String     DOUBLE_KEY      = "Double";
49     private static final String     BOOL_KEY        = "Boolean";
50     private static final String     STRING_KEY      = "String";
51     private SimulatorSingleResource singleResource  = null;
52
53     protected void setUp() throws Exception {
54         super.setUp();
55         singleResource = (SimulatorSingleResource) SimulatorManager
56                 .createResource(SimulatorResource.Type.SINGLE, "test-resource",
57                         "/test/resource", "test.resource");
58     }
59
60     protected void tearDown() throws Exception {
61         super.tearDown();
62         singleResource = null;
63     }
64
65     SimulatorSingleResource createResourceFromRAML() {
66         try {
67             return (SimulatorSingleResource) SimulatorManager
68                     .createResource(SINGLE_RES_RAML);
69         } catch (InvalidArgsException e) {
70             e.printStackTrace();
71         } catch (SimulatorException e) {
72             e.printStackTrace();
73         }
74
75         return null;
76     }
77
78     public void testGetResourceModel_P01() {
79         SimulatorResourceModel resModel = null;
80
81         try {
82             SimulatorSingleResource resource = createResourceFromRAML();
83             resModel = resource.getResourceModel();
84         } catch (SimulatorException e) {
85             e.printStackTrace();
86         }
87
88         assertNotNull(resModel);
89         assertTrue(resModel.size() > 0);
90     }
91
92     public void testAddAttributeInteger_P01() {
93         int result = -1;
94
95         try {
96             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
97                     INT_KEY, new AttributeValue(2), null);
98             singleResource.addAttribute(attribute);
99             result = ((Integer) singleResource.getAttribute(INT_KEY).value()
100                     .get()).intValue();
101         } catch (InvalidArgsException e) {
102             e.printStackTrace();
103         } catch (SimulatorException e) {
104             e.printStackTrace();
105         }
106
107         assertEquals(2, result);
108     }
109
110     public void testAddAttributeDouble_P01() {
111         double result = 0.0;
112
113         try {
114             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
115                     DOUBLE_KEY, new AttributeValue(4.0), null);
116             singleResource.addAttribute(attribute);
117             result = ((Double) singleResource.getAttribute(DOUBLE_KEY).value()
118                     .get()).doubleValue();
119         } catch (InvalidArgsException e) {
120             e.printStackTrace();
121         } catch (SimulatorException e) {
122             e.printStackTrace();
123         }
124
125         assertEquals(4.0, result);
126     }
127
128     public void testAddAttributeBoolean_P01() {
129         boolean result = false;
130
131         try {
132             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
133                     BOOL_KEY, new AttributeValue(true), null);
134             singleResource.addAttribute(attribute);
135             result = ((Boolean) singleResource.getAttribute(BOOL_KEY).value()
136                     .get()).booleanValue();
137         } catch (InvalidArgsException e) {
138             e.printStackTrace();
139         } catch (SimulatorException e) {
140             e.printStackTrace();
141         }
142
143         assertEquals(true, result);
144     }
145
146     public void testaddAttributeString_P01() {
147         String result = null;
148
149         try {
150             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
151                     STRING_KEY, new AttributeValue("string-value"), null);
152             singleResource.addAttribute(attribute);
153             result = (String) singleResource.getAttribute(STRING_KEY).value()
154                     .get();
155         } catch (InvalidArgsException e) {
156             e.printStackTrace();
157         } catch (SimulatorException e) {
158             e.printStackTrace();
159         }
160
161         assertEquals("string-value", result);
162     }
163
164     public void testUpdateAttributeInteger_P01() {
165         int result = -1;
166
167         try {
168             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
169                     INT_KEY, new AttributeValue(10), null);
170             singleResource.addAttribute(attribute);
171             singleResource.updateAttribute(INT_KEY, new AttributeValue(12));
172             result = ((Integer) singleResource.getAttribute(INT_KEY).value()
173                     .get()).intValue();
174         } catch (InvalidArgsException e) {
175             e.printStackTrace();
176         } catch (SimulatorException e) {
177             e.printStackTrace();
178         }
179
180         assertEquals(12, result);
181     }
182
183     public void testUpdateAttributeDouble_P01() {
184         double result = 0.0;
185
186         try {
187             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
188                     DOUBLE_KEY, new AttributeValue(22.0), null);
189             singleResource.addAttribute(attribute);
190             singleResource
191                     .updateAttribute(DOUBLE_KEY, new AttributeValue(25.3));
192             result = ((Double) singleResource.getAttribute(DOUBLE_KEY).value()
193                     .get()).doubleValue();
194         } catch (InvalidArgsException e) {
195             e.printStackTrace();
196         } catch (SimulatorException e) {
197             e.printStackTrace();
198         }
199
200         assertEquals(25.3, result);
201     }
202
203     public void testUpdateAttributeBoolean_P01() {
204         boolean result = true;
205
206         try {
207             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
208                     BOOL_KEY, new AttributeValue(true), null);
209             singleResource.addAttribute(attribute);
210             singleResource.updateAttribute(BOOL_KEY, new AttributeValue(false));
211             result = ((Boolean) singleResource.getAttribute(BOOL_KEY).value()
212                     .get()).booleanValue();
213         } catch (InvalidArgsException e) {
214             e.printStackTrace();
215         } catch (SimulatorException e) {
216             e.printStackTrace();
217         }
218
219         assertEquals(false, result);
220     }
221
222     public void testupdateAttributeString_P01() {
223         String result = null;
224
225         try {
226             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
227                     STRING_KEY, new AttributeValue("old-value"), null);
228             singleResource.addAttribute(attribute);
229             singleResource.updateAttribute(STRING_KEY, new AttributeValue(
230                     "new-value"));
231             result = (String) singleResource.getAttribute(STRING_KEY).value()
232                     .get();
233         } catch (InvalidArgsException e) {
234             e.printStackTrace();
235         } catch (SimulatorException e) {
236             e.printStackTrace();
237         }
238
239         assertEquals("new-value", result);
240     }
241
242     public void testSetRange_P01() {
243         int result = -1;
244
245         try {
246             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
247                     INT_KEY, new AttributeValue(10), new AttributeProperty(1,
248                             12));
249             singleResource.addAttribute(attribute);
250             singleResource.updateAttribute(INT_KEY, new AttributeValue(3));
251             result = ((Integer) singleResource.getAttribute(INT_KEY).value()
252                     .get()).intValue();
253         } catch (InvalidArgsException e) {
254             e.printStackTrace();
255         } catch (SimulatorException e) {
256             e.printStackTrace();
257         }
258
259         assertEquals(3, result);
260     }
261
262     public void testSetRange_N01() {
263         int result = -1;
264
265         try {
266             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
267                     INT_KEY, new AttributeValue(10), new AttributeProperty(1,
268                             12));
269             singleResource.addAttribute(attribute);
270             singleResource.updateAttribute(INT_KEY, new AttributeValue(13));
271             result = ((Integer) singleResource.getAttribute(INT_KEY).value()
272                     .get()).intValue();
273         } catch (InvalidArgsException e) {
274             e.printStackTrace();
275         } catch (SimulatorException e) {
276             e.printStackTrace();
277         }
278
279         assertEquals(10, result);
280     }
281
282     public void testSetAllowedValuesInteger_P01() {
283         int result = -1;
284
285         try {
286             int[] values = { 1, 10, 20, 50 };
287             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
288                     INT_KEY, new AttributeValue(10), new AttributeProperty(
289                             values));
290             singleResource.addAttribute(attribute);
291             singleResource.updateAttribute(INT_KEY, new AttributeValue(20));
292             result = ((Integer) singleResource.getAttribute(INT_KEY).value()
293                     .get()).intValue();
294         } catch (InvalidArgsException e) {
295             e.printStackTrace();
296         } catch (SimulatorException e) {
297             e.printStackTrace();
298         }
299
300         assertEquals(20, result);
301     }
302
303     public void testSetAllowedValuesInteger_N01() {
304         int result = -1;
305
306         try {
307             int[] values = { 1, 10, 20, 50 };
308             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
309                     INT_KEY, new AttributeValue(10), new AttributeProperty(
310                             values));
311             singleResource.addAttribute(attribute);
312             singleResource.updateAttribute(INT_KEY, new AttributeValue(15));
313             result = ((Integer) singleResource.getAttribute(INT_KEY).value()
314                     .get()).intValue();
315         } catch (InvalidArgsException e) {
316             e.printStackTrace();
317         } catch (SimulatorException e) {
318             e.printStackTrace();
319         }
320
321         assertEquals(10, result);
322     }
323
324     public void testSetAllowedValuesDouble_P01() {
325         double result = 0.0;
326
327         try {
328             double[] values = { 11.5, 10.5, 20.5, 50.5 };
329             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
330                     DOUBLE_KEY, new AttributeValue(11.5),
331                     new AttributeProperty(values));
332             singleResource.addAttribute(attribute);
333             singleResource
334                     .updateAttribute(DOUBLE_KEY, new AttributeValue(10.5));
335             result = ((Double) singleResource.getAttribute(DOUBLE_KEY).value()
336                     .get()).doubleValue();
337         } catch (InvalidArgsException e) {
338             e.printStackTrace();
339         } catch (SimulatorException e) {
340             e.printStackTrace();
341         }
342
343         assertEquals(10.5, result);
344     }
345
346     public void testSetAllowedValuesDouble_N01() {
347         double result = 0.0;
348
349         try {
350             double[] values = { 11.5, 10.5, 20.5, 50.5 };
351             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
352                     DOUBLE_KEY, new AttributeValue(11.5),
353                     new AttributeProperty(values));
354             singleResource.addAttribute(attribute);
355             singleResource.updateAttribute(DOUBLE_KEY, new AttributeValue(2.2));
356             result = ((Double) singleResource.getAttribute(DOUBLE_KEY).value()
357                     .get()).doubleValue();
358         } catch (InvalidArgsException e) {
359             e.printStackTrace();
360         } catch (SimulatorException e) {
361             e.printStackTrace();
362         }
363
364         assertEquals(11.5, result);
365     }
366
367     public void testsetAllowedValuesString_P01() {
368         String result = null;
369
370         try {
371             String[] values = { "monday", "tuesday", "wednesday" };
372             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
373                     STRING_KEY, new AttributeValue("monday"),
374                     new AttributeProperty(values));
375             singleResource.addAttribute(attribute);
376             singleResource.updateAttribute(STRING_KEY, new AttributeValue(
377                     "tuesday"));
378             result = (String) singleResource.getAttribute(STRING_KEY).value()
379                     .get();
380         } catch (InvalidArgsException e) {
381             e.printStackTrace();
382         } catch (SimulatorException e) {
383             e.printStackTrace();
384         }
385
386         assertEquals("tuesday", result);
387     }
388
389     public void testsetAllowedValuesString_N01() {
390         String result = null;
391
392         try {
393             String[] values = { "monday", "tuesday", "wednesday" };
394             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
395                     STRING_KEY, new AttributeValue("monday"),
396                     new AttributeProperty(values));
397             singleResource.addAttribute(attribute);
398             singleResource.updateAttribute(STRING_KEY, new AttributeValue(
399                     "friday"));
400             result = (String) singleResource.getAttribute(STRING_KEY).value()
401                     .get();
402         } catch (InvalidArgsException e) {
403             e.printStackTrace();
404         } catch (SimulatorException e) {
405             e.printStackTrace();
406         }
407
408         assertEquals("monday", result);
409     }
410
411     public void testRemoveAttribute_P01() {
412         SimulatorResourceAttribute result = null;
413
414         try {
415             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
416                     STRING_KEY, new AttributeValue("friday"), null);
417             singleResource.addAttribute(attribute);
418             singleResource.removeAttribute(STRING_KEY);
419             result = singleResource.getAttribute(STRING_KEY);
420         } catch (InvalidArgsException e) {
421             e.printStackTrace();
422         } catch (SimulatorException e) {
423             e.printStackTrace();
424         }
425
426         assertNull(result);
427     }
428
429     public void testRemoveAttribute_N02() {
430         ExceptionType exType = ExceptionType.UNKNOWN;
431
432         try {
433             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
434                     STRING_KEY, new AttributeValue("friday"), null);
435             singleResource.addAttribute(attribute);
436             singleResource.removeAttribute(null);
437         } catch (InvalidArgsException e) {
438             exType = ExceptionType.INVALID_ARGS;
439         } catch (SimulatorException e) {
440             exType = ExceptionType.SIMULATOR;
441         }
442
443         assertEquals(ExceptionType.INVALID_ARGS, exType);
444     }
445
446     public void testRemoveAttribute_N03() {
447         ExceptionType exType = ExceptionType.UNKNOWN;
448
449         try {
450             singleResource.removeAttribute("");
451         } catch (InvalidArgsException e) {
452             exType = ExceptionType.INVALID_ARGS;
453         } catch (SimulatorException e) {
454             exType = ExceptionType.SIMULATOR;
455         }
456
457         assertEquals(ExceptionType.INVALID_ARGS, exType);
458     }
459
460     public void testStartResourceAutomation_P01() {
461         SimulatorSingleResource resource = createResourceFromRAML();
462         if (null == resource)
463             return;
464
465         CountDownLatch lockObject = new CountDownLatch(1);
466         ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
467         AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
468                 lockObject, autoUpdateHolder);
469         int id = -1;
470
471         try {
472             resource.start();
473             id = resource.startResourceUpdation(
474                     SimulatorResource.AutoUpdateType.REPEAT, 1000,
475                     automationListener);
476
477             try {
478                 lockObject.await(10, TimeUnit.SECONDS);
479             } catch (InterruptedException e) {
480                 e.printStackTrace();
481             }
482
483             resource.stopUpdation(id);
484         } catch (InvalidArgsException e) {
485             e.printStackTrace();
486         } catch (SimulatorException e) {
487             e.printStackTrace();
488         }
489
490         try {
491             resource.stop();
492             resource.stopUpdation(id);
493         } catch (SimulatorException e) {
494             e.printStackTrace();
495         }
496
497         assertNotNull(autoUpdateHolder.get());
498         assertEquals(id, autoUpdateHolder.get().getId());
499     }
500
501     public void testStartResourceAutomation_N01() {
502         ExceptionType exType = ExceptionType.UNKNOWN;
503
504         try {
505             singleResource.startResourceUpdation(
506                     SimulatorResource.AutoUpdateType.ONE_TIME, 500, null);
507         } catch (InvalidArgsException e) {
508             exType = ExceptionType.INVALID_ARGS;
509         } catch (SimulatorException e) {
510             exType = ExceptionType.SIMULATOR;
511         }
512
513         assertEquals(ExceptionType.INVALID_ARGS, exType);
514     }
515
516     public void testStartAttributeAutomation_P01() {
517         SimulatorSingleResource resource = createResourceFromRAML();
518         if (null == resource)
519             return;
520
521         String attributeName = null;
522         try {
523             for (SimulatorResourceAttribute resAttribute : resource
524                     .getResourceModel().getAttributes().values())
525                 attributeName = resAttribute.name();
526         } catch (SimulatorException e1) {
527             e1.printStackTrace();
528         }
529
530         if (null == attributeName)
531             return;
532
533         CountDownLatch lockObject = new CountDownLatch(1);
534         ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
535         AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
536                 lockObject, autoUpdateHolder);
537         int id = -1;
538
539         try {
540             resource.start();
541             id = resource.startAttributeUpdation(attributeName,
542                     SimulatorResource.AutoUpdateType.REPEAT, 100,
543                     automationListener);
544
545             try {
546                 lockObject.await(10, TimeUnit.SECONDS);
547             } catch (InterruptedException e) {
548                 e.printStackTrace();
549             }
550
551             resource.stopUpdation(id);
552         } catch (InvalidArgsException e) {
553             e.printStackTrace();
554         } catch (SimulatorException e) {
555             e.printStackTrace();
556         }
557
558         try {
559             resource.stop();
560         } catch (SimulatorException e) {
561             e.printStackTrace();
562         }
563
564         assertNotNull(autoUpdateHolder.get());
565         assertEquals(id, autoUpdateHolder.get().getId());
566     }
567
568     public void testStartAttributeAutomation_N01() {
569         ExceptionType exType = ExceptionType.UNKNOWN;
570
571         try {
572             singleResource.startAttributeUpdation("intensity",
573                     SimulatorResource.AutoUpdateType.ONE_TIME, 1000, null);
574         } catch (InvalidArgsException e) {
575             exType = ExceptionType.INVALID_ARGS;
576         } catch (SimulatorException e) {
577             exType = ExceptionType.SIMULATOR;
578         }
579
580         assertEquals(ExceptionType.INVALID_ARGS, exType);
581     }
582
583     public void testStopUpdation_P01() {
584         SimulatorSingleResource resource = createResourceFromRAML();
585         if (null == resource)
586             return;
587
588         CountDownLatch lockObject = new CountDownLatch(1);
589         ObjectHolder<AutoUpdateInfo> autoUpdateHolder = new ObjectHolder<>();
590         AutoUpdateCompleteListener automationListener = new AutoUpdateCompleteListener(
591                 lockObject, autoUpdateHolder);
592         boolean result = false;
593         try {
594             resource.start();
595             int id = resource.startResourceUpdation(
596                     SimulatorResource.AutoUpdateType.REPEAT, 1000,
597                     automationListener);
598             resource.stopUpdation(id);
599             result = true;
600         } catch (InvalidArgsException e) {
601             e.printStackTrace();
602         } catch (SimulatorException e) {
603             e.printStackTrace();
604         }
605
606         try {
607             resource.stop();
608         } catch (SimulatorException e) {
609             e.printStackTrace();
610         }
611
612         assertTrue(result);
613     }
614 }
615
616 class AutoUpdateInfo {
617     private String uri = null;
618     private int    id  = -1;
619
620     AutoUpdateInfo(String uri, int id) {
621         this.uri = uri;
622         this.id = id;
623     }
624
625     public String getUri() {
626         return uri;
627     }
628
629     public int getId() {
630         return id;
631     }
632 }
633
634 class AutoUpdateCompleteListener implements AutoUpdateListener {
635     private CountDownLatch               lock;
636     private ObjectHolder<AutoUpdateInfo> autoUpdateHolder;
637
638     public AutoUpdateCompleteListener(CountDownLatch lock,
639             ObjectHolder<AutoUpdateInfo> autoUpdateHolder) {
640         this.lock = lock;
641         this.autoUpdateHolder = autoUpdateHolder;
642     }
643
644     @Override
645     public void onUpdateComplete(String uri, int id) {
646         autoUpdateHolder.set(new AutoUpdateInfo(uri, id));
647         lock.countDown();
648     }
649 }