Removed executable bit from non-executable files.
[platform/upstream/iotivity.git] / android / android_api / base / src / androidTest / java / org / iotivity / base / SmokeTest.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2015 Intel Corporation.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.base;
24
25 import android.test.InstrumentationTestCase;
26 import android.util.Log;
27
28 import java.util.Date;
29 import java.util.EnumSet;
30 import java.util.HashMap;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Random;
35 import java.util.Timer;
36 import java.util.TimerTask;
37 import java.util.concurrent.CountDownLatch;
38 import java.util.concurrent.TimeUnit;
39
40 public class SmokeTest extends InstrumentationTestCase {
41     private static final String TAG = "SmokeTest";
42     private Random rnd = new Random();
43
44     @Override
45     protected void setUp() throws Exception {
46         super.setUp();
47         PlatformConfig cfg = new PlatformConfig(
48                 getInstrumentation().getContext(),
49                 ServiceType.IN_PROC,
50                 ModeType.CLIENT_SERVER,
51                 "0.0.0.0",
52                 0,
53                 QualityOfService.LOW);
54
55         OcPlatform.Configure(cfg);
56     }
57
58     @Override
59     protected void tearDown() throws Exception {
60         super.tearDown();
61     }
62
63     public void testResourceRegisterUnregister() throws InterruptedException {
64         final String resourceType = "unit.test.resource" +
65                 new Date().getTime();
66         final CountDownLatch signal = new CountDownLatch(1);
67
68         OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
69             @Override
70             public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
71                 return EntityHandlerResult.OK;
72             }
73         };
74
75         OcPlatform.OnResourceFoundListener resourceFoundListener =
76                 new OcPlatform.OnResourceFoundListener() {
77                     @Override
78                     public void onResourceFound(OcResource resource) {
79                         Log.i(TAG, "Host: " + resource.getHost());
80                         Log.i(TAG, "Server ID: " + resource.getServerId());
81                         Log.i(TAG, "Connectivity Type: " + resource.getConnectivityType());
82                         signal.countDown();
83                     }
84                 };
85
86         try {
87             //server
88             OcResourceHandle resourceHandle = OcPlatform.registerResource(
89                     "/a/unittest",
90                     resourceType,
91                     OcPlatform.DEFAULT_INTERFACE,
92                     entityHandler,
93                     EnumSet.of(ResourceProperty.DISCOVERABLE)
94             );
95
96             //client
97             OcPlatform.findResource("",
98                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
99                     OcConnectivityType.IPV4,
100                     resourceFoundListener);
101
102             //wait for onResourceFound event
103             assertTrue(signal.await(60, TimeUnit.SECONDS));
104
105             //server
106             OcPlatform.unregisterResource(resourceHandle);
107         } catch (OcException e) {
108             Log.e(TAG, e.getMessage());
109             assertTrue(false);
110         }
111     }
112
113     public void testStartStopListenForPresence() throws InterruptedException {
114         final String resourceType = "unit.test.resource" +
115                 new Date().getTime();
116         final CountDownLatch signal = new CountDownLatch(1);
117
118         OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
119             @Override
120             public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
121                 return EntityHandlerResult.OK;
122             }
123         };
124
125         final OcPlatform.OnPresenceListener presenceListener = new OcPlatform.OnPresenceListener() {
126             @Override
127             public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress) {
128                 Log.i(TAG, "onPresence status " + ocPresenceStatus.toString() + " nonce " + nonce);
129                 signal.countDown();
130             }
131         };
132
133         OcPlatform.OnResourceFoundListener resourceFoundListener =
134                 new OcPlatform.OnResourceFoundListener() {
135                     @Override
136                     public void onResourceFound(OcResource resource) {
137                         try {
138                             //client
139                             OcPresenceHandle presenceHandle = OcPlatform.subscribePresence(
140                                     resource.getHost(),
141                                     OcConnectivityType.IPV4,
142                                     presenceListener
143                             );
144
145                             //wait for onPresence event
146                             assertTrue(signal.await(60, TimeUnit.SECONDS));
147
148                             //client
149                             OcPlatform.unsubscribePresence(presenceHandle);
150                         } catch (OcException e) {
151                             assertTrue(false);
152                         } catch (InterruptedException e) {
153                             assertTrue(false);
154                         }
155                     }
156                 };
157
158         try {
159             //server
160             OcResourceHandle resourceHandle = OcPlatform.registerResource(
161                     "/a/unittest",
162                     resourceType,
163                     OcPlatform.DEFAULT_INTERFACE,
164                     entityHandler,
165                     EnumSet.of(ResourceProperty.DISCOVERABLE)
166             );
167
168             //client
169             OcPlatform.findResource("",
170                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
171                     OcConnectivityType.IPV4,
172                     resourceFoundListener);
173
174             //server
175             OcPlatform.startPresence(OcPlatform.DEFAULT_PRESENCE_TTL);
176
177             //wait for onPresence event
178             assertTrue(signal.await(60, TimeUnit.SECONDS));
179
180             //server
181             OcPlatform.stopPresence();
182
183             //client
184             OcPlatform.unregisterResource(resourceHandle);
185
186         } catch (OcException e) {
187             Log.e(TAG, e.getMessage());
188             assertTrue(false);
189         }
190     }
191
192     public void testHandleGetRequest() throws InterruptedException {
193         final String someKey = "SomeKey";
194         final String someValue = "SomeValue";
195         final String resourceType = "unit.test.resource" + new Date().getTime();
196         final CountDownLatch signal1 = new CountDownLatch(1);
197         final CountDownLatch signal2 = new CountDownLatch(1);
198         final List<OcResource> ocResourceList = new LinkedList<OcResource>();
199
200         //client
201         final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
202             @Override
203             public void onGetCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
204                 if (!headerOptionList.isEmpty()) {
205                     for (OcHeaderOption headerOption : headerOptionList) {
206                         Log.i(TAG, "Header option " +
207                                 headerOption.getOptionId() +
208                                 " : " +
209                                 headerOption.getOptionData());
210                     }
211                 }
212                 try {
213                     Log.i(TAG, "Power: " + ocRepresentation.getValue("power"));
214                 } catch (OcException e) {
215                     Log.e(TAG, e.toString());
216                     assertTrue(false);
217                 }
218                 signal2.countDown();
219             }
220
221             @Override
222             public void onGetFailed(Throwable ex) {
223                 if (ex instanceof OcException) {
224                     OcException ocEx = (OcException) ex;
225                     ErrorCode errCode = ocEx.getErrorCode();
226                 }
227                 Log.e(TAG, ex.toString());
228                 assertTrue(false);
229             }
230         };
231
232         //client
233         final OcPlatform.OnResourceFoundListener resourceFoundListener =
234                 new OcPlatform.OnResourceFoundListener() {
235                     @Override
236                     public void onResourceFound(OcResource resource) {
237                         Map<String, String> queryParamsMap = new HashMap<String, String>();
238                         queryParamsMap.put(someKey, someValue);
239
240                         ocResourceList.add(resource);
241                         try {
242                             resource.get(queryParamsMap, onGetListener);
243                             //TODO there is a bug in the stack that prevents the usage of the following APIs
244 //                            resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE, queryParamsMap,
245 //                                    onGetListener);
246 //
247 //                            resource.get(queryParamsMap, onGetListener, QualityOfService.LOW);
248 //
249 //                            resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE,queryParamsMap,
250 //                                    onGetListener, QualityOfService.LOW);
251                         } catch (OcException e) {
252                             Log.e(TAG, e.toString());
253                             assertTrue(false);
254                         }
255                         signal1.countDown();
256                     }
257                 };
258
259         try {
260             //server
261             OcResourceHandle resourceHandle = OcPlatform.registerResource(
262                     "/a/unittest",
263                     resourceType,
264                     OcPlatform.DEFAULT_INTERFACE,
265                     new OcPlatform.EntityHandler() {
266                         @Override
267                         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
268                             EnumSet<RequestHandlerFlag> handlerFlagSet =
269                                     ocResourceRequest.getRequestHandlerFlagSet();
270
271                             RequestType requestType = ocResourceRequest.getRequestType();
272
273                             if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
274                             }
275                             if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
276                                 OcResourceResponse ocResourceResponse = new OcResourceResponse();
277                                 ocResourceResponse.setRequestHandle(
278                                         ocResourceRequest.getRequestHandle());
279                                 ocResourceResponse.setResourceHandle(
280                                         ocResourceRequest.getResourceHandle());
281
282                                 switch (requestType) {
283                                     case GET:
284                                         Map<String, String> queryParams =
285                                                 ocResourceRequest.getQueryParameters();
286 //TODO after the merge with CA, the query params are missing
287 //                                        if (!(queryParams.containsKey(someKey) &&
288 //                                                someValue.equals(queryParams.get(someKey)))) {
289 //                                            assertTrue(false);
290 //                                        }
291
292                                         ocResourceResponse.setErrorCode(200);
293                                         ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
294                                         ocResourceResponse.setResourceRepresentation(
295                                                 getRepresentation(74));
296                                         break;
297                                 }
298
299                                 try {
300                                     OcPlatform.sendResponse(ocResourceResponse);
301                                 } catch (OcException e) {
302                                     Log.e(TAG, e.getMessage());
303                                     return EntityHandlerResult.ERROR;
304                                 }
305                             }
306                             if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
307                             }
308                             return EntityHandlerResult.OK;
309                         }
310                     },
311                     EnumSet.of(ResourceProperty.DISCOVERABLE)
312             );
313
314             //client
315             OcPlatform.findResource(null,
316                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
317                     OcConnectivityType.IPV4,
318                     resourceFoundListener);
319
320             //wait for onResourceFound event
321             assertTrue(signal1.await(60, TimeUnit.SECONDS));
322
323             //wait for onGetCompleted event
324             assertTrue(signal2.await(60, TimeUnit.SECONDS));
325
326             //server
327             OcPlatform.unregisterResource(resourceHandle);
328
329         } catch (OcException e) {
330             Log.e(TAG, e.getMessage());
331             assertTrue(false);
332         }
333     }
334
335     public void testHandlePutRequest() throws InterruptedException {
336         final String resourceType = "unit.test.resource" + new Date().getTime();
337         final CountDownLatch signal1 = new CountDownLatch(1);
338         final CountDownLatch signal2 = new CountDownLatch(3);
339         final List<OcResource> ocResourceList = new LinkedList<OcResource>();
340
341         final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
342             @Override
343             public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
344                 if (!headerOptionList.isEmpty()) {
345                     for (OcHeaderOption headerOption : headerOptionList) {
346                         Log.i(TAG, "Header option " +
347                                 headerOption.getOptionId() +
348                                 " : " +
349                                 headerOption.getOptionData());
350                     }
351                 }
352                 try {
353                     Log.i(TAG, "onPutCompleted Power: " + ocRepresentation.getValue("power"));
354                 } catch (OcException e) {
355                     Log.e(TAG, e.getMessage());
356                     assertTrue(false);
357                 }
358                 Log.i(TAG, "onPutCompleted Uri: " + ocRepresentation.getUri());
359                 signal2.countDown();
360             }
361
362             @Override
363             public void onPutFailed(Throwable ex) {
364                 if (ex instanceof OcException) {
365                     OcException ocEx = (OcException) ex;
366                     ErrorCode errCode = ocEx.getErrorCode();
367                 }
368                 Log.e(TAG, ex.toString());
369                 assertTrue(false);
370             }
371         };
372
373         final OcPlatform.OnResourceFoundListener resourceFoundListener =
374                 new OcPlatform.OnResourceFoundListener() {
375                     @Override
376                     public void onResourceFound(OcResource resource) {
377                         ocResourceList.add(resource);
378                         try {
379                             resource.put(
380                                     getRepresentation(),
381                                     new HashMap<String, String>(),
382                                     onPutListener);
383
384                             resource.put(
385                                     getRepresentation(),
386                                     new HashMap<String, String>(),
387                                     onPutListener);
388
389                             resource.put(
390                                     getRepresentation(),
391                                     new HashMap<String, String>(),
392                                     new OcResource.OnPutListener() {
393                                         @Override
394                                         public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
395                                             signal2.countDown();
396                                         }
397
398                                         @Override
399                                         public void onPutFailed(Throwable ex) {
400                                             if (ex instanceof OcException) {
401                                                 OcException ocEx = (OcException) ex;
402                                                 ErrorCode errCode = ocEx.getErrorCode();
403                                             }
404                                             Log.e(TAG, ex.toString());
405                                             assertTrue(false);
406                                         }
407                                     });
408
409                         } catch (OcException e) {
410                             assertTrue(false);
411                         }
412                         signal1.countDown();
413                     }
414                 };
415
416         try {
417             //server
418             OcResourceHandle resourceHandle = OcPlatform.registerResource(
419                     "/a/unittest",
420                     resourceType,
421                     OcPlatform.DEFAULT_INTERFACE,
422                     new OcPlatform.EntityHandler() {
423                         @Override
424                         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
425                             EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
426                             RequestType requestType = ocResourceRequest.getRequestType();
427
428                             if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
429                             }
430                             if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
431                                 OcResourceResponse ocResourceResponse = new OcResourceResponse();
432                                 ocResourceResponse.setRequestHandle(
433                                         ocResourceRequest.getRequestHandle());
434                                 ocResourceResponse.setResourceHandle(
435                                         ocResourceRequest.getResourceHandle());
436
437                                 switch (requestType) {
438                                     case GET:
439                                         assertTrue(false);
440                                         break;
441                                     case PUT:
442                                         OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
443                                         try {
444                                             Log.i(TAG, "Put res. power: " + rep.getValue("power"));
445                                         } catch (OcException e) {
446                                             Log.e(TAG, e.toString());
447                                             assertTrue(false);
448                                         }
449                                         Log.i(TAG, "URI: " + rep.getUri());
450
451                                         ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
452                                         ocResourceResponse.setErrorCode(200);
453                                         ocResourceResponse.setResourceRepresentation(rep);
454                                         break;
455                                     case POST:
456                                         assertTrue(false);
457                                         break;
458                                     case DELETE:
459                                         break;
460                                 }
461
462                                 try {
463                                     OcPlatform.sendResponse(ocResourceResponse);
464                                 } catch (OcException e) {
465                                     Log.e(TAG, e.getMessage());
466                                     return EntityHandlerResult.ERROR;
467                                 }
468                             }
469                             if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
470                             }
471                             return EntityHandlerResult.OK;
472                         }
473                     },
474                     EnumSet.of(ResourceProperty.DISCOVERABLE)
475             );
476
477             //client
478             OcPlatform.findResource("",
479                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
480                     OcConnectivityType.IPV4,
481                     resourceFoundListener);
482
483             //wait for onResourceFound event
484             assertTrue(signal1.await(60, TimeUnit.SECONDS));
485
486             //wait for onGetCompleted event
487             assertTrue(signal2.await(60, TimeUnit.SECONDS));
488
489             //server
490             OcPlatform.unregisterResource(resourceHandle);
491
492         } catch (OcException e) {
493             Log.e(TAG, e.getMessage());
494             assertTrue(false);
495         }
496     }
497
498     public void testHandlePostRequest() throws InterruptedException {
499         final String resourceType = "unit.test.resource" + new Date().getTime();
500         final CountDownLatch signal1 = new CountDownLatch(1);
501         final CountDownLatch signal2 = new CountDownLatch(3);
502         final List<OcResource> ocResourceList = new LinkedList<OcResource>();
503
504         final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
505             @Override
506             public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
507                 if (!headerOptionList.isEmpty()) {
508                     for (OcHeaderOption headerOption : headerOptionList) {
509                         Log.i(TAG, "Header option " +
510                                 headerOption.getOptionId() +
511                                 " : " +
512                                 headerOption.getOptionData());
513                     }
514                 }
515                 try {
516                     Log.i(TAG, "onPostCompleted Power: " + ocRepresentation.getValue("power"));
517                 } catch (OcException e) {
518                     Log.e(TAG, e.toString());
519                     assertTrue(false);
520                 }
521                 Log.i(TAG, "onPostCompleted Uri: " + ocRepresentation.getUri());
522                 signal2.countDown();
523             }
524
525             @Override
526             public void onPostFailed(Throwable ex) {
527                 if (ex instanceof OcException) {
528                     OcException ocEx = (OcException) ex;
529                     ErrorCode errCode = ocEx.getErrorCode();
530                 }
531                 Log.e(TAG, ex.toString());
532                 assertTrue(false);
533             }
534         };
535
536         final OcPlatform.OnResourceFoundListener resourceFoundListener =
537                 new OcPlatform.OnResourceFoundListener() {
538                     @Override
539                     public void onResourceFound(OcResource resource) {
540                         ocResourceList.add(resource);
541                         try {
542                             resource.post(
543                                     getRepresentation(),
544                                     new HashMap<String, String>(),
545                                     onPostListener);
546
547                             resource.post(
548                                     getRepresentation(),
549                                     new HashMap<String, String>(),
550                                     onPostListener);
551
552                             resource.post(
553                                     getRepresentation(),
554                                     new HashMap<String, String>(),
555                                     new OcResource.OnPostListener() {
556                                         @Override
557                                         public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
558                                             signal2.countDown();
559                                         }
560
561                                         @Override
562                                         public void onPostFailed(Throwable ex) {
563                                             if (ex instanceof OcException) {
564                                                 OcException ocEx = (OcException) ex;
565                                                 ErrorCode errCode = ocEx.getErrorCode();
566                                             }
567                                             Log.e(TAG, ex.toString());
568                                             assertTrue(false);
569                                         }
570                                     });
571
572                         } catch (OcException e) {
573                             assertTrue(false);
574                         }
575                         signal1.countDown();
576                     }
577                 };
578
579         try {
580             //server
581             OcResourceHandle resourceHandle = OcPlatform.registerResource(
582                     "/a/unittest",
583                     resourceType,
584                     OcPlatform.DEFAULT_INTERFACE,
585                     new OcPlatform.EntityHandler() {
586                         @Override
587                         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
588                             EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
589                             RequestType requestType = ocResourceRequest.getRequestType();
590
591                             if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
592                             }
593                             if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
594                                 OcResourceResponse ocResourceResponse = new OcResourceResponse();
595                                 ocResourceResponse.setRequestHandle(
596                                         ocResourceRequest.getRequestHandle());
597                                 ocResourceResponse.setResourceHandle(
598                                         ocResourceRequest.getResourceHandle());
599
600                                 switch (requestType) {
601                                     case GET:
602                                         assertTrue(false);
603                                         break;
604                                     case PUT:
605                                         assertTrue(false);
606                                         break;
607                                     case POST:
608                                         OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
609                                         try {
610                                             Log.i(TAG, "Post res. power: " + rep.getValue("power"));
611                                         } catch (OcException e) {
612                                             Log.e(TAG, e.toString());
613                                             assertTrue(false);
614                                         }
615                                         Log.i(TAG, "URI: " + rep.getUri());
616
617                                         ocResourceResponse.setErrorCode(200);
618                                         ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
619                                         ocResourceResponse.setResourceRepresentation(
620                                                 getRepresentation(44));
621
622                                         break;
623                                     case DELETE:
624                                         assertTrue(false);
625                                         break;
626                                 }
627
628                                 try {
629                                     OcPlatform.sendResponse(ocResourceResponse);
630                                 } catch (OcException e) {
631                                     Log.e(TAG, e.getMessage());
632                                     return EntityHandlerResult.ERROR;
633                                 }
634                             }
635                             if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
636                             }
637                             return EntityHandlerResult.OK;
638                         }
639                     },
640                     EnumSet.of(ResourceProperty.DISCOVERABLE)
641             );
642
643             //client
644             OcPlatform.findResource("",
645                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
646                     OcConnectivityType.IPV4,
647                     resourceFoundListener);
648
649             //wait for onResourceFound event
650             assertTrue(signal1.await(60, TimeUnit.SECONDS));
651
652             //wait for onPostCompleted event
653             assertTrue(signal2.await(60, TimeUnit.SECONDS));
654
655             //server
656             OcPlatform.unregisterResource(resourceHandle);
657
658         } catch (OcException e) {
659             Log.e(TAG, e.getMessage());
660             assertTrue(false);
661         }
662
663     }
664
665     public void testHandleDeleteRequest() throws InterruptedException {
666         final String resourceType = "unit.test.resource" + new Date().getTime();
667         final CountDownLatch signal1 = new CountDownLatch(1);
668         final CountDownLatch signal2 = new CountDownLatch(1);
669         final List<OcResource> ocResourceList = new LinkedList<OcResource>();
670
671         final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
672             @Override
673             public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
674                 signal2.countDown();
675             }
676
677             @Override
678             public void onDeleteFailed(Throwable ex) {
679                 if (ex instanceof OcException) {
680                     OcException ocEx = (OcException) ex;
681                     ErrorCode errCode = ocEx.getErrorCode();
682                 }
683                 Log.e(TAG, ex.toString());
684                 assertTrue(false);
685             }
686         };
687
688         final OcPlatform.OnResourceFoundListener resourceFoundListener =
689                 new OcPlatform.OnResourceFoundListener() {
690                     @Override
691                     public void onResourceFound(OcResource resource) {
692                         ocResourceList.add(resource);
693                         try {
694                             resource.deleteResource(onDeleteListener);
695                         } catch (OcException e) {
696                             assertTrue(false);
697                         }
698                         signal1.countDown();
699                     }
700                 };
701
702         try {
703             //server
704             OcResourceHandle resourceHandle = OcPlatform.registerResource(
705                     "/a/unittest",
706                     resourceType,
707                     OcPlatform.DEFAULT_INTERFACE,
708                     new OcPlatform.EntityHandler() {
709                         @Override
710                         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
711                             EnumSet<RequestHandlerFlag> handlerFlagSet =
712                                     ocResourceRequest.getRequestHandlerFlagSet();
713                             RequestType requestType = ocResourceRequest.getRequestType();
714
715                             if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
716                             }
717                             if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
718                                 OcResourceResponse ocResourceResponse = new OcResourceResponse();
719                                 ocResourceResponse.setRequestHandle(
720                                         ocResourceRequest.getRequestHandle());
721                                 ocResourceResponse.setResourceHandle(
722                                         ocResourceRequest.getResourceHandle());
723
724                                 switch (requestType) {
725                                     case DELETE:
726                                         ocResourceResponse.setErrorCode(200);
727                                         ocResourceResponse.setResponseResult(
728                                                 EntityHandlerResult.RESOURCE_DELETED);
729                                         break;
730                                 }
731
732                                 try {
733                                     OcPlatform.sendResponse(ocResourceResponse);
734                                 } catch (OcException e) {
735                                     Log.e(TAG, e.getMessage());
736                                     return EntityHandlerResult.ERROR;
737                                 }
738                             }
739                             if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
740                             }
741                             return EntityHandlerResult.OK;
742                         }
743                     },
744                     EnumSet.of(ResourceProperty.DISCOVERABLE)
745             );
746
747             //client
748             OcPlatform.findResource("",
749                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
750                     OcConnectivityType.IPV4,
751                     resourceFoundListener);
752
753             //wait for onResourceFound event
754             assertTrue(signal1.await(60, TimeUnit.SECONDS));
755
756             //wait for onDeleteCompleted event
757             assertTrue(signal2.await(60, TimeUnit.SECONDS));
758
759             //server
760             OcPlatform.unregisterResource(resourceHandle);
761
762         } catch (OcException e) {
763             Log.e(TAG, e.getMessage());
764             assertTrue(false);
765         }
766     }
767
768     public void testHandleGetPutPostDeleteFailures() throws InterruptedException {
769         final String resourceType = "unit.test.resource" + new Date().getTime();
770         final CountDownLatch signal1 = new CountDownLatch(1);
771         final CountDownLatch signal2 = new CountDownLatch(1);
772         final CountDownLatch signal3 = new CountDownLatch(1);
773         final CountDownLatch signal4 = new CountDownLatch(1);
774         final CountDownLatch signal5 = new CountDownLatch(1);
775
776         final List<OcResource> ocResourceList = new LinkedList<OcResource>();
777         final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
778             @Override
779             public void onGetCompleted(List<OcHeaderOption> headerOptionList,
780                                        OcRepresentation ocRepresentation) {
781                 assertTrue(false);
782             }
783
784             @Override
785             public void onGetFailed(Throwable ex) {
786                 if (ex instanceof OcException) {
787                     OcException ocEx = (OcException) ex;
788                     ErrorCode errCode = ocEx.getErrorCode();
789                 }
790                 Log.i(TAG, ex.toString());
791                 signal2.countDown();
792             }
793         };
794
795         final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
796             @Override
797             public void onPutCompleted(List<OcHeaderOption> headerOptionList,
798                                        OcRepresentation ocRepresentation) {
799                 assertTrue(false);
800             }
801
802             @Override
803             public void onPutFailed(Throwable ex) {
804                 if (ex instanceof OcException) {
805                     OcException ocEx = (OcException) ex;
806                     ErrorCode errCode = ocEx.getErrorCode();
807                 }
808                 Log.i(TAG, ex.toString());
809                 signal3.countDown();
810             }
811         };
812         final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
813             @Override
814             public void onPostCompleted(List<OcHeaderOption> headerOptionList,
815                                         OcRepresentation ocRepresentation) {
816                 assertTrue(false);
817             }
818
819             @Override
820             public void onPostFailed(Throwable ex) {
821                 if (ex instanceof OcException) {
822                     OcException ocEx = (OcException) ex;
823                     ErrorCode errCode = ocEx.getErrorCode();
824                 }
825                 Log.i(TAG, ex.toString());
826                 signal4.countDown();
827             }
828         };
829
830         final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
831             @Override
832             public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
833                 assertTrue(false);
834             }
835
836             @Override
837             public void onDeleteFailed(Throwable ex) {
838                 if (ex instanceof OcException) {
839                     OcException ocEx = (OcException) ex;
840                     ErrorCode errCode = ocEx.getErrorCode();
841                 }
842                 Log.i(TAG, ex.toString());
843                 signal5.countDown();
844             }
845         };
846
847         final OcPlatform.OnResourceFoundListener resourceFoundListener =
848                 new OcPlatform.OnResourceFoundListener() {
849                     @Override
850                     public void onResourceFound(OcResource resource) {
851                         ocResourceList.add(resource);
852                         try {
853                             resource.get(new HashMap<String, String>(), onGetListener);
854                             resource.put(new OcRepresentation(), new HashMap<String, String>(),
855                                     onPutListener);
856                             resource.post(new OcRepresentation(), new HashMap<String, String>(),
857                                     onPostListener);
858                             resource.deleteResource(onDeleteListener);
859                         } catch (OcException e) {
860                             assertTrue(false);
861                         }
862                         signal1.countDown();
863                     }
864                 };
865
866         try {
867             //server
868             OcResourceHandle resourceHandle = OcPlatform.registerResource(
869                     "/a/unittest",
870                     resourceType,
871                     OcPlatform.DEFAULT_INTERFACE,
872                     new OcPlatform.EntityHandler() {
873                         @Override
874                         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
875                             EnumSet<RequestHandlerFlag> handlerFlagSet =
876                                     ocResourceRequest.getRequestHandlerFlagSet();
877                             RequestType requestType = ocResourceRequest.getRequestType();
878
879                             if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
880                             }
881                             if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
882                                 OcResourceResponse ocResourceResponse = new OcResourceResponse();
883                                 ocResourceResponse.setRequestHandle(
884                                         ocResourceRequest.getRequestHandle());
885                                 ocResourceResponse.setResourceHandle(
886                                         ocResourceRequest.getResourceHandle());
887
888                                 switch (requestType) {
889                                     case GET:
890                                         ocResourceResponse.setErrorCode(200);
891                                         ocResourceResponse.setResponseResult(
892                                                 EntityHandlerResult.ERROR);
893                                         break;
894                                     case PUT:
895                                         ocResourceResponse.setErrorCode(200);
896                                         ocResourceResponse.setResponseResult(
897                                                 EntityHandlerResult.ERROR);
898                                         break;
899                                     case POST:
900                                         ocResourceResponse.setErrorCode(200);
901                                         ocResourceResponse.setResponseResult(
902                                                 EntityHandlerResult.ERROR);
903                                         break;
904                                     case DELETE:
905                                         ocResourceResponse.setErrorCode(200);
906                                         ocResourceResponse.setResponseResult(
907                                                 EntityHandlerResult.ERROR);
908                                         break;
909                                 }
910                                 try {
911                                     OcPlatform.sendResponse(ocResourceResponse);
912                                 } catch (OcException e) {
913                                     Log.e(TAG, e.getMessage());
914                                     return EntityHandlerResult.ERROR;
915                                 }
916                             }
917                             if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
918                             }
919                             return EntityHandlerResult.OK;
920                         }
921                     },
922                     EnumSet.of(ResourceProperty.DISCOVERABLE)
923             );
924
925             //client
926             OcPlatform.findResource("",
927                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
928                     OcConnectivityType.IPV4,
929                     resourceFoundListener);
930
931             //wait for onResourceFound event
932             assertTrue(signal1.await(60, TimeUnit.SECONDS));
933             //wait for onGetCompleted event
934             assertTrue(signal2.await(60, TimeUnit.SECONDS));
935             //wait for onPutCompleted event
936             assertTrue(signal3.await(60, TimeUnit.SECONDS));
937             //wait for onPostCompleted event
938             assertTrue(signal4.await(60, TimeUnit.SECONDS));
939             //wait for onDeleteCompleted event
940             assertTrue(signal5.await(60, TimeUnit.SECONDS));
941
942             //server
943             OcPlatform.unregisterResource(resourceHandle);
944
945         } catch (OcException e) {
946             Log.e(TAG, e.getMessage());
947             assertTrue(false);
948         }
949     }
950
951     public void testRegisterDeviceInfoGetDeviceInfo() throws InterruptedException {
952         final String resourceType = "unit.test.resource" + new Date().getTime();
953         final CountDownLatch signal = new CountDownLatch(1);
954
955         OcPlatform.OnDeviceFoundListener deviceFoundListener = new OcPlatform.OnDeviceFoundListener() {
956             @Override
957             public void onDeviceFound(OcRepresentation ocRepresentation) {
958                 try {
959                     Log.i(TAG, "Device Name: " + ocRepresentation.getValue("dn"));
960                 } catch (OcException e) {
961                     Log.e(TAG, e.toString());
962                     assertTrue(false);
963                 }
964                 boolean hasDeviceNameAtr = ocRepresentation.hasAttribute("dn");
965                 assertTrue(hasDeviceNameAtr);
966                 boolean hasNonExistingAtr = ocRepresentation.hasAttribute("NonExisting");
967                 assertFalse(hasNonExistingAtr);
968                 Log.i(TAG, "URI: " + ocRepresentation.getUri());
969                 signal.countDown();
970             }
971         };
972
973         OcDeviceInfo devInfo = new OcDeviceInfo();
974
975         devInfo.setContentType("myContentType");
976         devInfo.setDateOfManufacture("myDateOfManufacture");
977         devInfo.setDeviceName("myDeviceName");
978         devInfo.setDeviceUuid("myDeviceUUID");
979         devInfo.setFirmwareVersion("myFirmwareVersion");
980         devInfo.setHostName("myHostName");
981         devInfo.setManufacturerName("myManufacturerNa");
982         devInfo.setManufacturerUrl("myManufacturerUrl");
983         devInfo.setModelNumber("myModelNumber");
984         devInfo.setPlatformVersion("myPlatformVersion");
985         devInfo.setSupportUrl("mySupportUrl");
986         devInfo.setVersion("myVersion");
987
988         try {
989             //server
990             OcPlatform.registerDeviceInfo(devInfo);
991
992             //client
993             OcPlatform.getDeviceInfo(
994                     "",
995                     OcPlatform.WELL_KNOWN_QUERY + "/d",
996                     OcConnectivityType.IPV4,
997                     deviceFoundListener);
998
999             //wait for onDeviceFound event
1000             assertTrue(signal.await(60, TimeUnit.SECONDS));
1001
1002         } catch (OcException e) {
1003             Log.e(TAG, e.getMessage());
1004             assertTrue(false);
1005         }
1006     }
1007
1008     public void testBindUnbindResources() throws InterruptedException {
1009         final String resourceType = "unit.test.resource" + new Date().getTime();
1010         final CountDownLatch signal1 = new CountDownLatch(3);
1011         final CountDownLatch signal2 = new CountDownLatch(2);
1012         final CountDownLatch signal3 = new CountDownLatch(1);
1013         final CountDownLatch signal4 = new CountDownLatch(3);
1014         final CountDownLatch signal5 = new CountDownLatch(3);
1015         final CountDownLatch signal6 = new CountDownLatch(1);
1016
1017         OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1018             @Override
1019             public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1020                 return EntityHandlerResult.OK;
1021             }
1022         };
1023
1024         OcPlatform.OnResourceFoundListener resourceFoundListener1 = new OcPlatform.OnResourceFoundListener() {
1025             @Override
1026             public void onResourceFound(OcResource resource) {
1027                 signal1.countDown();
1028             }
1029         };
1030
1031         OcPlatform.OnResourceFoundListener resourceFoundListener2 = new OcPlatform.OnResourceFoundListener() {
1032             @Override
1033             public void onResourceFound(OcResource resource) {
1034                 signal2.countDown();
1035             }
1036         };
1037
1038         OcPlatform.OnResourceFoundListener resourceFoundListener3 = new OcPlatform.OnResourceFoundListener() {
1039             @Override
1040             public void onResourceFound(OcResource resource) {
1041                 signal3.countDown();
1042             }
1043         };
1044
1045         OcPlatform.OnResourceFoundListener resourceFoundListener4 = new OcPlatform.OnResourceFoundListener() {
1046             @Override
1047             public void onResourceFound(OcResource resource) {
1048                 signal4.countDown();
1049             }
1050         };
1051
1052         OcPlatform.OnResourceFoundListener resourceFoundListener5 = new OcPlatform.OnResourceFoundListener() {
1053             @Override
1054             public void onResourceFound(OcResource resource) {
1055                 signal5.countDown();
1056             }
1057         };
1058
1059         OcPlatform.OnResourceFoundListener resourceFoundListener6 = new OcPlatform.OnResourceFoundListener() {
1060             @Override
1061             public void onResourceFound(OcResource resource) {
1062                 signal6.countDown();
1063             }
1064         };
1065
1066         try {
1067
1068             //server
1069             OcResourceHandle resourceHandleCollection = OcPlatform.registerResource(
1070                     "/a/unittest1",
1071                     resourceType,
1072                     OcPlatform.DEFAULT_INTERFACE,
1073                     entityHandler,
1074                     EnumSet.of(ResourceProperty.DISCOVERABLE)
1075             );
1076
1077             OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1078                     "/a/unittest2",
1079                     resourceType,
1080                     OcPlatform.DEFAULT_INTERFACE,
1081                     entityHandler,
1082                     EnumSet.of(ResourceProperty.DISCOVERABLE)
1083             );
1084
1085             OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1086                     "/a/unittest3",
1087                     resourceType,
1088                     OcPlatform.DEFAULT_INTERFACE,
1089                     entityHandler,
1090                     EnumSet.of(ResourceProperty.DISCOVERABLE)
1091             );
1092
1093             //client
1094             OcPlatform.findResource("",
1095                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1096                     OcConnectivityType.IPV4,
1097                     resourceFoundListener1);
1098
1099             //wait for onResourceFound event to find 3 registered resources
1100             assertTrue(signal1.await(60, TimeUnit.SECONDS));
1101
1102             //server
1103             OcPlatform.bindResource(resourceHandleCollection, resourceHandle1);
1104             OcPlatform.bindResource(resourceHandleCollection, resourceHandle2);
1105
1106             //client
1107             OcPlatform.findResource("",
1108                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1109                     OcConnectivityType.IPV4,
1110                     resourceFoundListener3);
1111
1112             //wait for onResourceFound event to find 1 collection resources
1113             assertTrue(signal3.await(60, TimeUnit.SECONDS));
1114
1115             //server
1116             OcPlatform.unbindResource(resourceHandleCollection, resourceHandle1);
1117
1118             //client
1119             OcPlatform.findResource("",
1120                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1121                     OcConnectivityType.IPV4,
1122                     resourceFoundListener2);
1123
1124             //wait for onResourceFound event to find 2 resources
1125             assertTrue(signal2.await(60, TimeUnit.SECONDS));
1126
1127             //server
1128             OcPlatform.unbindResource(resourceHandleCollection, resourceHandle2);
1129
1130             //client
1131             OcPlatform.findResource("",
1132                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1133                     OcConnectivityType.IPV4,
1134                     resourceFoundListener4);
1135
1136             //wait for onResourceFound event to find 3 registered resources
1137             assertTrue(signal4.await(60, TimeUnit.SECONDS));
1138
1139             //Bind/unbind a list of resource handles
1140             List<OcResourceHandle> resourceHandleList = new LinkedList<OcResourceHandle>();
1141             resourceHandleList.add(resourceHandle1);
1142             resourceHandleList.add(resourceHandle2);
1143             OcPlatform.bindResources(resourceHandleCollection, resourceHandleList);
1144
1145             //client
1146             OcPlatform.findResource("",
1147                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1148                     OcConnectivityType.IPV4,
1149                     resourceFoundListener6);
1150
1151             //wait for onResourceFound event to find 1 collection resources
1152             assertTrue(signal6.await(60, TimeUnit.SECONDS));
1153
1154             OcPlatform.unbindResources(resourceHandleCollection, resourceHandleList);
1155
1156             //client
1157             OcPlatform.findResource("",
1158                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1159                     OcConnectivityType.IPV4,
1160                     resourceFoundListener5);
1161
1162             //wait for onResourceFound event to find 1 collection resources
1163             assertTrue(signal5.await(60, TimeUnit.SECONDS));
1164
1165             //server
1166             OcPlatform.unregisterResource(resourceHandleCollection);
1167             OcPlatform.unregisterResource(resourceHandle1);
1168             OcPlatform.unregisterResource(resourceHandle2);
1169         } catch (OcException e) {
1170             Log.e(TAG, e.getMessage());
1171             assertTrue(false);
1172         }
1173     }
1174
1175     public void testResourceMethods() throws InterruptedException {
1176         final String resourceType1 = "unit.test.resource" + new Date().getTime();
1177         final String resourceType2 = "unit.test.resource" + new Date().getTime();
1178
1179         final CountDownLatch signal = new CountDownLatch(2);
1180         final List<OcResource> resourceList = new LinkedList<OcResource>();
1181
1182         OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1183             @Override
1184             public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1185                 return EntityHandlerResult.OK;
1186             }
1187         };
1188
1189         OcPlatform.OnResourceFoundListener resourceFoundListener =
1190                 new OcPlatform.OnResourceFoundListener() {
1191                     @Override
1192                     public void onResourceFound(OcResource resource) {
1193                         resourceList.add(resource);
1194                         Log.i(TAG, "Host: " + resource.getHost());
1195                         Log.i(TAG, "Uri: " + resource.getUri());
1196                         Log.i(TAG, "Observable: " + resource.isObservable());
1197
1198                         for (String resourceType : resource.getResourceTypes()) {
1199                             Log.i(TAG, "Type: " + resourceType);
1200                         }
1201
1202                         for (String resourceInterface : resource.getResourceInterfaces()) {
1203                             Log.i(TAG, "Interface: " + resourceInterface);
1204                         }
1205
1206                         List<OcHeaderOption> headerOptionList = new LinkedList<OcHeaderOption>();
1207                         headerOptionList.add(new OcHeaderOption(2885, "OptionData1"));
1208                         headerOptionList.add(new OcHeaderOption(2886, "OptionData2"));
1209                         resource.setHeaderOptions(headerOptionList);
1210
1211                         resource.setHeaderOptions(headerOptionList);
1212                         resource.unsetHeaderOptions();
1213
1214                         OcResourceIdentifier resourceIdentifier = resource.getUniqueIdentifier();
1215                         OcResourceIdentifier resourceIdentifier2 = resource.getUniqueIdentifier();
1216                         assertTrue(resourceIdentifier.equals(resourceIdentifier2));
1217
1218                         signal.countDown();
1219                     }
1220                 };
1221
1222         try {
1223             //server
1224             OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1225                     "/a/unittest1",
1226                     resourceType1,
1227                     OcPlatform.DEFAULT_INTERFACE,
1228                     entityHandler,
1229                     EnumSet.of(ResourceProperty.DISCOVERABLE)
1230             );
1231
1232             OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1233                     "/a/unittest2",
1234                     resourceType2,
1235                     OcPlatform.DEFAULT_INTERFACE,
1236                     entityHandler,
1237                     EnumSet.of(ResourceProperty.DISCOVERABLE)
1238             );
1239
1240             //client
1241             OcPlatform.findResource("",
1242                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType1,
1243                     OcConnectivityType.IPV4,
1244                     resourceFoundListener);
1245
1246             OcPlatform.findResource("",
1247                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType2,
1248                     OcConnectivityType.IPV4,
1249                     resourceFoundListener);
1250
1251             //wait for onResourceFound event
1252             assertTrue(signal.await(60, TimeUnit.SECONDS));
1253
1254             assertTrue(2 == resourceList.size());
1255
1256             OcResource res0 = resourceList.get(0);
1257             OcResource res1 = resourceList.get(1);
1258             assertFalse(res0.getUniqueIdentifier().equals(res1.getUniqueIdentifier()));
1259             assertTrue(res0.getUniqueIdentifier().equals(res0.getUniqueIdentifier()));
1260
1261             //server
1262             OcPlatform.unregisterResource(resourceHandle1);
1263             OcPlatform.unregisterResource(resourceHandle2);
1264
1265         } catch (OcException e) {
1266             Log.e(TAG, e.getMessage());
1267             assertTrue(false);
1268         }
1269     }
1270
1271     public void testCreateResourceProxy() throws InterruptedException {
1272         final String resourceType = "unit.test.resource" + new Date().getTime();
1273         final CountDownLatch signal = new CountDownLatch(1);
1274
1275         OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1276             @Override
1277             public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1278                 return EntityHandlerResult.OK;
1279             }
1280         };
1281
1282         OcPlatform.OnResourceFoundListener resourceFoundListener =
1283                 new OcPlatform.OnResourceFoundListener() {
1284                     @Override
1285                     public void onResourceFound(OcResource resource) {
1286
1287                         try {
1288                             //client: construct resource proxy
1289                             OcResource resourceProxy = OcPlatform.constructResourceObject(
1290                                     resource.getHost(),
1291                                     resource.getUri(),
1292                                     OcConnectivityType.IPV4,
1293                                     resource.isObservable(),
1294                                     resource.getResourceTypes(),
1295                                     resource.getResourceInterfaces());
1296
1297                             //client: register resource proxy
1298                             OcResourceHandle resourceProxyHandle =
1299                                     OcPlatform.registerResource(resourceProxy);
1300
1301                             OcPlatform.unregisterResource(resourceProxyHandle);
1302                         } catch (OcException e) {
1303                             assertTrue(false);
1304                         }
1305                         signal.countDown();
1306                     }
1307                 };
1308
1309         try {
1310             //server
1311             OcResourceHandle resourceHandle = OcPlatform.registerResource(
1312                     "/a/unittest",
1313                     resourceType,
1314                     OcPlatform.DEFAULT_INTERFACE,
1315                     entityHandler,
1316                     EnumSet.of(ResourceProperty.DISCOVERABLE)
1317             );
1318
1319             //client
1320             OcPlatform.findResource("",
1321                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1322                     OcConnectivityType.IPV4,
1323                     resourceFoundListener);
1324
1325             //wait for onResourceFound event
1326             assertTrue(signal.await(60, TimeUnit.SECONDS));
1327
1328             //server
1329             OcPlatform.unregisterResource(resourceHandle);
1330
1331         } catch (OcException e) {
1332             Log.e(TAG, e.getMessage());
1333             assertTrue(false);
1334         }
1335
1336         try {
1337             //server
1338             OcResourceHandle resourceHandle = OcPlatform.registerResource(
1339                     "/a/unittest",
1340                     resourceType,
1341                     OcPlatform.DEFAULT_INTERFACE,
1342                     entityHandler,
1343                     EnumSet.of(ResourceProperty.DISCOVERABLE)
1344             );
1345
1346             //client
1347             OcPlatform.findResource("",
1348                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1349                     OcConnectivityType.IPV4,
1350                     resourceFoundListener);
1351
1352             //wait for onResourceFound event
1353             assertTrue(signal.await(60, TimeUnit.SECONDS));
1354
1355             //server
1356             OcPlatform.unregisterResource(resourceHandle);
1357
1358         } catch (OcException e) {
1359             Log.e(TAG, e.getMessage());
1360             assertTrue(false);
1361         }
1362     }
1363
1364     public void testObserveClientServer() throws InterruptedException {
1365         final int NUM_OBSERVES = 20;
1366         final Timer timer = new Timer();
1367         final List<OcResource> resourceList = new LinkedList<OcResource>();
1368         final List<OcResourceHandle> resourceHandleList = new LinkedList<OcResourceHandle>();
1369         final CountDownLatch signal1 = new CountDownLatch(1);
1370         final CountDownLatch signal2 = new CountDownLatch(NUM_OBSERVES);
1371         final CountDownLatch signal3 = new CountDownLatch(1);
1372
1373         final String resourceType = "unit.test.resource" + new Date().getTime();
1374
1375         final OcResource.OnObserveListener observeListener = new OcResource.OnObserveListener() {
1376             @Override
1377             public void onObserveCompleted(
1378                     List<OcHeaderOption> headerOptionList,
1379                     OcRepresentation ocRepresentation,
1380                     int sequenceNumber) {
1381
1382                 try {
1383                     Log.i(TAG, "Observe #" + sequenceNumber + " power: " +
1384                             ocRepresentation.getValue("power"));
1385                 } catch (OcException e) {
1386                     Log.e(TAG, e.toString());
1387                     assertTrue(false);
1388                 }
1389                 signal2.countDown();
1390             }
1391
1392             @Override
1393             public void onObserveFailed(Throwable ex) {
1394                 if (ex instanceof OcException) {
1395                     OcException ocEx = (OcException) ex;
1396                     ErrorCode errCode = ocEx.getErrorCode();
1397                 }
1398                 Log.e(TAG, ex.toString());
1399                 assertTrue(false);
1400             }
1401         };
1402
1403         final List<Byte> observationIdList = new LinkedList<Byte>();
1404         OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1405             @Override
1406             public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1407                 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
1408                 RequestType requestType = ocResourceRequest.getRequestType();
1409
1410                 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
1411
1412                 }
1413                 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
1414                     OcResourceResponse ocResourceResponse = new OcResourceResponse();
1415                     ocResourceResponse.setRequestHandle(ocResourceRequest.getRequestHandle());
1416                     ocResourceResponse.setResourceHandle(ocResourceRequest.getResourceHandle());
1417
1418                     switch (requestType) {
1419                         case GET:
1420                             ocResourceResponse.setErrorCode(200);
1421                             ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
1422                             ocResourceResponse.setResourceRepresentation(
1423                                     getRepresentation(
1424                                             rnd.nextInt(100)));
1425                             try {
1426                                 OcPlatform.sendResponse(ocResourceResponse);
1427                             } catch (OcException e) {
1428                                 Log.e(TAG, e.getMessage());
1429                                 return EntityHandlerResult.ERROR;
1430                             }
1431                             break;
1432                     }
1433                 }
1434
1435                 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
1436                     ObservationInfo observationInfo = ocResourceRequest.getObservationInfo();
1437
1438                     switch (observationInfo.getObserveAction()) {
1439                         case REGISTER:
1440                             synchronized (observationIdList) {
1441                                 observationIdList.add(observationInfo.getOcObservationId());
1442                                 timer.schedule(new TimerTask() {
1443                                     int numNotified = 1;
1444
1445                                     @Override
1446                                     public void run() {
1447                                         if (0 < resourceHandleList.size()) {
1448                                             synchronized (observationIdList) {
1449                                                 if (numNotified > NUM_OBSERVES) {
1450                                                     timer.cancel();
1451                                                     timer.purge();
1452                                                     signal3.countDown();
1453                                                 } else {
1454                                                     try {
1455                                                         OcPlatform.notifyAllObservers(
1456                                                                 resourceHandleList.get(0));
1457                                                     } catch (OcException e) {
1458                                                         if (ErrorCode.NO_OBSERVERS == e.getErrorCode()) {
1459                                                             timer.cancel();
1460                                                             timer.purge();
1461                                                             signal3.countDown();
1462                                                         }
1463                                                         Log.e(TAG, e.getMessage());
1464                                                     }
1465                                                     numNotified++;
1466                                                 }
1467                                             }
1468                                         }
1469
1470                                     }
1471                                 }, 0, 100);
1472                             }
1473                             break;
1474                         case UNREGISTER:
1475                             //TODO unregister isn't implemented in C++ API, yet
1476                             synchronized (observationIdList) {
1477                                 timer.cancel();
1478                                 break;
1479                             }
1480                     }
1481                 }
1482                 return EntityHandlerResult.OK;
1483             }
1484         };
1485
1486         OcPlatform.OnResourceFoundListener resourceFoundListener =
1487                 new OcPlatform.OnResourceFoundListener() {
1488                     @Override
1489                     public void onResourceFound(OcResource resource) {
1490                         resourceList.add(resource);
1491                         if (resource.isObservable()) {
1492                             try {
1493                                 resource.observe(
1494                                         ObserveType.OBSERVE,
1495                                         new HashMap<String, String>(),
1496                                         observeListener);
1497
1498                                 signal1.countDown();
1499                             } catch (OcException e) {
1500                                 Log.e(TAG, e.getMessage());
1501                                 assertTrue(false);
1502                             }
1503                         }
1504                     }
1505                 };
1506         try {
1507             //server
1508             OcResourceHandle resourceHandle = OcPlatform.registerResource(
1509                     "/a/unittest",
1510                     resourceType,
1511                     OcPlatform.DEFAULT_INTERFACE,
1512                     entityHandler,
1513                     EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
1514             );
1515
1516             resourceHandleList.add(resourceHandle);
1517
1518             //client
1519             OcPlatform.findResource("",
1520                     OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1521                     OcConnectivityType.IPV4,
1522                     resourceFoundListener);
1523
1524             //wait for onResourceFound event
1525             assertTrue(signal1.await(60, TimeUnit.SECONDS));
1526
1527             //wait for OnObserveListener event to observe 20 values
1528             assertTrue(signal2.await(60, TimeUnit.SECONDS));
1529
1530             if (resourceList.size() > 0) {
1531                 OcResource resource = resourceList.get(0);
1532                 if (resource.isObservable()) {
1533                     resource.cancelObserve();
1534                 }
1535             }
1536
1537             //wait for server to finish
1538             assertTrue(signal3.await(60, TimeUnit.SECONDS));
1539
1540             //server
1541             OcPlatform.unregisterResource(resourceHandle);
1542
1543         } catch (OcException e) {
1544             Log.e(TAG, e.getMessage());
1545             assertTrue(false);
1546         }
1547     }
1548
1549     private OcRepresentation getRepresentation(int value) {
1550         OcRepresentation rep = new OcRepresentation();
1551         try {
1552             rep.setValue("power", value);
1553         } catch (OcException e) {
1554             Log.e(TAG, e.toString());
1555             assertTrue(false);
1556         }
1557         return rep;
1558     }
1559
1560     private OcRepresentation getRepresentation() {
1561         return getRepresentation(74);
1562     }
1563 }