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