2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base;
25 import android.test.InstrumentationTestCase;
26 import android.util.Log;
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;
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;
40 public class SmokeTest extends InstrumentationTestCase {
41 private static final String TAG = "SmokeTest";
42 private Random rnd = new Random();
45 protected void setUp() throws Exception {
47 PlatformConfig cfg = new PlatformConfig(
48 getInstrumentation().getContext(),
50 ModeType.CLIENT_SERVER,
53 QualityOfService.LOW);
55 OcPlatform.Configure(cfg);
59 protected void tearDown() throws Exception {
63 public void testResourceRegisterUnregister() throws InterruptedException {
64 final String resourceType = "unit.test.resource" +
66 final CountDownLatch signal = new CountDownLatch(1);
68 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
70 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
71 return EntityHandlerResult.OK;
75 OcPlatform.OnResourceFoundListener resourceFoundListener =
76 new OcPlatform.OnResourceFoundListener() {
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());
88 OcResourceHandle resourceHandle = OcPlatform.registerResource(
91 OcPlatform.DEFAULT_INTERFACE,
93 EnumSet.of(ResourceProperty.DISCOVERABLE)
97 OcPlatform.findResource("",
98 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
99 OcConnectivityType.IPV4,
100 resourceFoundListener);
102 //wait for onResourceFound event
103 assertTrue(signal.await(60, TimeUnit.SECONDS));
106 OcPlatform.unregisterResource(resourceHandle);
107 } catch (OcException e) {
108 Log.e(TAG, e.getMessage());
113 public void testStartStopListenForPresence() throws InterruptedException {
114 final String resourceType = "unit.test.resource" +
115 new Date().getTime();
116 final CountDownLatch signal = new CountDownLatch(1);
118 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
120 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
121 return EntityHandlerResult.OK;
125 final OcPlatform.OnPresenceListener presenceListener = new OcPlatform.OnPresenceListener() {
127 public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress) {
128 Log.i(TAG, "onPresence status " + ocPresenceStatus.toString() + " nonce " + nonce);
133 OcPlatform.OnResourceFoundListener resourceFoundListener =
134 new OcPlatform.OnResourceFoundListener() {
136 public void onResourceFound(OcResource resource) {
139 OcPresenceHandle presenceHandle = OcPlatform.subscribePresence(
141 OcConnectivityType.IPV4,
145 //wait for onPresence event
146 assertTrue(signal.await(60, TimeUnit.SECONDS));
149 OcPlatform.unsubscribePresence(presenceHandle);
150 } catch (OcException e) {
152 } catch (InterruptedException e) {
160 OcResourceHandle resourceHandle = OcPlatform.registerResource(
163 OcPlatform.DEFAULT_INTERFACE,
165 EnumSet.of(ResourceProperty.DISCOVERABLE)
169 OcPlatform.findResource("",
170 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
171 OcConnectivityType.IPV4,
172 resourceFoundListener);
175 OcPlatform.startPresence(OcPlatform.DEFAULT_PRESENCE_TTL);
177 //wait for onPresence event
178 assertTrue(signal.await(60, TimeUnit.SECONDS));
181 OcPlatform.stopPresence();
184 OcPlatform.unregisterResource(resourceHandle);
186 } catch (OcException e) {
187 Log.e(TAG, e.getMessage());
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>();
201 final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
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() +
209 headerOption.getOptionData());
213 Log.i(TAG, "Power: " + ocRepresentation.getValue("power"));
214 } catch (OcException e) {
215 Log.e(TAG, e.toString());
222 public void onGetFailed(Throwable ex) {
223 if (ex instanceof OcException) {
224 OcException ocEx = (OcException) ex;
225 ErrorCode errCode = ocEx.getErrorCode();
227 Log.e(TAG, ex.toString());
233 final OcPlatform.OnResourceFoundListener resourceFoundListener =
234 new OcPlatform.OnResourceFoundListener() {
236 public void onResourceFound(OcResource resource) {
237 Map<String, String> queryParamsMap = new HashMap<String, String>();
238 queryParamsMap.put(someKey, someValue);
240 ocResourceList.add(resource);
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,
247 // resource.get(queryParamsMap, onGetListener, QualityOfService.LOW);
249 // resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE,queryParamsMap,
250 // onGetListener, QualityOfService.LOW);
251 } catch (OcException e) {
252 Log.e(TAG, e.toString());
261 OcResourceHandle resourceHandle = OcPlatform.registerResource(
264 OcPlatform.DEFAULT_INTERFACE,
265 new OcPlatform.EntityHandler() {
267 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
268 EnumSet<RequestHandlerFlag> handlerFlagSet =
269 ocResourceRequest.getRequestHandlerFlagSet();
271 RequestType requestType = ocResourceRequest.getRequestType();
273 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
275 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
276 OcResourceResponse ocResourceResponse = new OcResourceResponse();
277 ocResourceResponse.setRequestHandle(
278 ocResourceRequest.getRequestHandle());
279 ocResourceResponse.setResourceHandle(
280 ocResourceRequest.getResourceHandle());
282 switch (requestType) {
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);
292 ocResourceResponse.setErrorCode(200);
293 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
294 ocResourceResponse.setResourceRepresentation(
295 getRepresentation(74));
300 OcPlatform.sendResponse(ocResourceResponse);
301 } catch (OcException e) {
302 Log.e(TAG, e.getMessage());
303 return EntityHandlerResult.ERROR;
306 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
308 return EntityHandlerResult.OK;
311 EnumSet.of(ResourceProperty.DISCOVERABLE)
315 OcPlatform.findResource(null,
316 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
317 OcConnectivityType.IPV4,
318 resourceFoundListener);
320 //wait for onResourceFound event
321 assertTrue(signal1.await(60, TimeUnit.SECONDS));
323 //wait for onGetCompleted event
324 assertTrue(signal2.await(60, TimeUnit.SECONDS));
327 OcPlatform.unregisterResource(resourceHandle);
329 } catch (OcException e) {
330 Log.e(TAG, e.getMessage());
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>();
341 final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
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() +
349 headerOption.getOptionData());
353 Log.i(TAG, "onPutCompleted Power: " + ocRepresentation.getValue("power"));
354 } catch (OcException e) {
355 Log.e(TAG, e.getMessage());
358 Log.i(TAG, "onPutCompleted Uri: " + ocRepresentation.getUri());
363 public void onPutFailed(Throwable ex) {
364 if (ex instanceof OcException) {
365 OcException ocEx = (OcException) ex;
366 ErrorCode errCode = ocEx.getErrorCode();
368 Log.e(TAG, ex.toString());
373 final OcPlatform.OnResourceFoundListener resourceFoundListener =
374 new OcPlatform.OnResourceFoundListener() {
376 public void onResourceFound(OcResource resource) {
377 ocResourceList.add(resource);
381 new HashMap<String, String>(),
386 new HashMap<String, String>(),
391 new HashMap<String, String>(),
392 new OcResource.OnPutListener() {
394 public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
399 public void onPutFailed(Throwable ex) {
400 if (ex instanceof OcException) {
401 OcException ocEx = (OcException) ex;
402 ErrorCode errCode = ocEx.getErrorCode();
404 Log.e(TAG, ex.toString());
409 } catch (OcException e) {
418 OcResourceHandle resourceHandle = OcPlatform.registerResource(
421 OcPlatform.DEFAULT_INTERFACE,
422 new OcPlatform.EntityHandler() {
424 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
425 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
426 RequestType requestType = ocResourceRequest.getRequestType();
428 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
430 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
431 OcResourceResponse ocResourceResponse = new OcResourceResponse();
432 ocResourceResponse.setRequestHandle(
433 ocResourceRequest.getRequestHandle());
434 ocResourceResponse.setResourceHandle(
435 ocResourceRequest.getResourceHandle());
437 switch (requestType) {
442 OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
444 Log.i(TAG, "Put res. power: " + rep.getValue("power"));
445 } catch (OcException e) {
446 Log.e(TAG, e.toString());
449 Log.i(TAG, "URI: " + rep.getUri());
451 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
452 ocResourceResponse.setErrorCode(200);
453 ocResourceResponse.setResourceRepresentation(rep);
463 OcPlatform.sendResponse(ocResourceResponse);
464 } catch (OcException e) {
465 Log.e(TAG, e.getMessage());
466 return EntityHandlerResult.ERROR;
469 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
471 return EntityHandlerResult.OK;
474 EnumSet.of(ResourceProperty.DISCOVERABLE)
478 OcPlatform.findResource("",
479 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
480 OcConnectivityType.IPV4,
481 resourceFoundListener);
483 //wait for onResourceFound event
484 assertTrue(signal1.await(60, TimeUnit.SECONDS));
486 //wait for onGetCompleted event
487 assertTrue(signal2.await(60, TimeUnit.SECONDS));
490 OcPlatform.unregisterResource(resourceHandle);
492 } catch (OcException e) {
493 Log.e(TAG, e.getMessage());
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>();
504 final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
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() +
512 headerOption.getOptionData());
516 Log.i(TAG, "onPostCompleted Power: " + ocRepresentation.getValue("power"));
517 } catch (OcException e) {
518 Log.e(TAG, e.toString());
521 Log.i(TAG, "onPostCompleted Uri: " + ocRepresentation.getUri());
526 public void onPostFailed(Throwable ex) {
527 if (ex instanceof OcException) {
528 OcException ocEx = (OcException) ex;
529 ErrorCode errCode = ocEx.getErrorCode();
531 Log.e(TAG, ex.toString());
536 final OcPlatform.OnResourceFoundListener resourceFoundListener =
537 new OcPlatform.OnResourceFoundListener() {
539 public void onResourceFound(OcResource resource) {
540 ocResourceList.add(resource);
544 new HashMap<String, String>(),
549 new HashMap<String, String>(),
554 new HashMap<String, String>(),
555 new OcResource.OnPostListener() {
557 public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
562 public void onPostFailed(Throwable ex) {
563 if (ex instanceof OcException) {
564 OcException ocEx = (OcException) ex;
565 ErrorCode errCode = ocEx.getErrorCode();
567 Log.e(TAG, ex.toString());
572 } catch (OcException e) {
581 OcResourceHandle resourceHandle = OcPlatform.registerResource(
584 OcPlatform.DEFAULT_INTERFACE,
585 new OcPlatform.EntityHandler() {
587 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
588 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
589 RequestType requestType = ocResourceRequest.getRequestType();
591 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
593 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
594 OcResourceResponse ocResourceResponse = new OcResourceResponse();
595 ocResourceResponse.setRequestHandle(
596 ocResourceRequest.getRequestHandle());
597 ocResourceResponse.setResourceHandle(
598 ocResourceRequest.getResourceHandle());
600 switch (requestType) {
608 OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
610 Log.i(TAG, "Post res. power: " + rep.getValue("power"));
611 } catch (OcException e) {
612 Log.e(TAG, e.toString());
615 Log.i(TAG, "URI: " + rep.getUri());
617 ocResourceResponse.setErrorCode(200);
618 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
619 ocResourceResponse.setResourceRepresentation(
620 getRepresentation(44));
629 OcPlatform.sendResponse(ocResourceResponse);
630 } catch (OcException e) {
631 Log.e(TAG, e.getMessage());
632 return EntityHandlerResult.ERROR;
635 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
637 return EntityHandlerResult.OK;
640 EnumSet.of(ResourceProperty.DISCOVERABLE)
644 OcPlatform.findResource("",
645 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
646 OcConnectivityType.IPV4,
647 resourceFoundListener);
649 //wait for onResourceFound event
650 assertTrue(signal1.await(60, TimeUnit.SECONDS));
652 //wait for onPostCompleted event
653 assertTrue(signal2.await(60, TimeUnit.SECONDS));
656 OcPlatform.unregisterResource(resourceHandle);
658 } catch (OcException e) {
659 Log.e(TAG, e.getMessage());
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>();
671 final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
673 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
678 public void onDeleteFailed(Throwable ex) {
679 if (ex instanceof OcException) {
680 OcException ocEx = (OcException) ex;
681 ErrorCode errCode = ocEx.getErrorCode();
683 Log.e(TAG, ex.toString());
688 final OcPlatform.OnResourceFoundListener resourceFoundListener =
689 new OcPlatform.OnResourceFoundListener() {
691 public void onResourceFound(OcResource resource) {
692 ocResourceList.add(resource);
694 resource.deleteResource(onDeleteListener);
695 } catch (OcException e) {
704 OcResourceHandle resourceHandle = OcPlatform.registerResource(
707 OcPlatform.DEFAULT_INTERFACE,
708 new OcPlatform.EntityHandler() {
710 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
711 EnumSet<RequestHandlerFlag> handlerFlagSet =
712 ocResourceRequest.getRequestHandlerFlagSet();
713 RequestType requestType = ocResourceRequest.getRequestType();
715 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
717 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
718 OcResourceResponse ocResourceResponse = new OcResourceResponse();
719 ocResourceResponse.setRequestHandle(
720 ocResourceRequest.getRequestHandle());
721 ocResourceResponse.setResourceHandle(
722 ocResourceRequest.getResourceHandle());
724 switch (requestType) {
726 ocResourceResponse.setErrorCode(200);
727 ocResourceResponse.setResponseResult(
728 EntityHandlerResult.RESOURCE_DELETED);
733 OcPlatform.sendResponse(ocResourceResponse);
734 } catch (OcException e) {
735 Log.e(TAG, e.getMessage());
736 return EntityHandlerResult.ERROR;
739 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
741 return EntityHandlerResult.OK;
744 EnumSet.of(ResourceProperty.DISCOVERABLE)
748 OcPlatform.findResource("",
749 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
750 OcConnectivityType.IPV4,
751 resourceFoundListener);
753 //wait for onResourceFound event
754 assertTrue(signal1.await(60, TimeUnit.SECONDS));
756 //wait for onDeleteCompleted event
757 assertTrue(signal2.await(60, TimeUnit.SECONDS));
760 OcPlatform.unregisterResource(resourceHandle);
762 } catch (OcException e) {
763 Log.e(TAG, e.getMessage());
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);
776 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
777 final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
779 public void onGetCompleted(List<OcHeaderOption> headerOptionList,
780 OcRepresentation ocRepresentation) {
785 public void onGetFailed(Throwable ex) {
786 if (ex instanceof OcException) {
787 OcException ocEx = (OcException) ex;
788 ErrorCode errCode = ocEx.getErrorCode();
790 Log.i(TAG, ex.toString());
795 final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
797 public void onPutCompleted(List<OcHeaderOption> headerOptionList,
798 OcRepresentation ocRepresentation) {
803 public void onPutFailed(Throwable ex) {
804 if (ex instanceof OcException) {
805 OcException ocEx = (OcException) ex;
806 ErrorCode errCode = ocEx.getErrorCode();
808 Log.i(TAG, ex.toString());
812 final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
814 public void onPostCompleted(List<OcHeaderOption> headerOptionList,
815 OcRepresentation ocRepresentation) {
820 public void onPostFailed(Throwable ex) {
821 if (ex instanceof OcException) {
822 OcException ocEx = (OcException) ex;
823 ErrorCode errCode = ocEx.getErrorCode();
825 Log.i(TAG, ex.toString());
830 final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
832 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
837 public void onDeleteFailed(Throwable ex) {
838 if (ex instanceof OcException) {
839 OcException ocEx = (OcException) ex;
840 ErrorCode errCode = ocEx.getErrorCode();
842 Log.i(TAG, ex.toString());
847 final OcPlatform.OnResourceFoundListener resourceFoundListener =
848 new OcPlatform.OnResourceFoundListener() {
850 public void onResourceFound(OcResource resource) {
851 ocResourceList.add(resource);
853 resource.get(new HashMap<String, String>(), onGetListener);
854 resource.put(new OcRepresentation(), new HashMap<String, String>(),
856 resource.post(new OcRepresentation(), new HashMap<String, String>(),
858 resource.deleteResource(onDeleteListener);
859 } catch (OcException e) {
868 OcResourceHandle resourceHandle = OcPlatform.registerResource(
871 OcPlatform.DEFAULT_INTERFACE,
872 new OcPlatform.EntityHandler() {
874 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
875 EnumSet<RequestHandlerFlag> handlerFlagSet =
876 ocResourceRequest.getRequestHandlerFlagSet();
877 RequestType requestType = ocResourceRequest.getRequestType();
879 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
881 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
882 OcResourceResponse ocResourceResponse = new OcResourceResponse();
883 ocResourceResponse.setRequestHandle(
884 ocResourceRequest.getRequestHandle());
885 ocResourceResponse.setResourceHandle(
886 ocResourceRequest.getResourceHandle());
888 switch (requestType) {
890 ocResourceResponse.setErrorCode(200);
891 ocResourceResponse.setResponseResult(
892 EntityHandlerResult.ERROR);
895 ocResourceResponse.setErrorCode(200);
896 ocResourceResponse.setResponseResult(
897 EntityHandlerResult.ERROR);
900 ocResourceResponse.setErrorCode(200);
901 ocResourceResponse.setResponseResult(
902 EntityHandlerResult.ERROR);
905 ocResourceResponse.setErrorCode(200);
906 ocResourceResponse.setResponseResult(
907 EntityHandlerResult.ERROR);
911 OcPlatform.sendResponse(ocResourceResponse);
912 } catch (OcException e) {
913 Log.e(TAG, e.getMessage());
914 return EntityHandlerResult.ERROR;
917 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
919 return EntityHandlerResult.OK;
922 EnumSet.of(ResourceProperty.DISCOVERABLE)
926 OcPlatform.findResource("",
927 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
928 OcConnectivityType.IPV4,
929 resourceFoundListener);
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));
943 OcPlatform.unregisterResource(resourceHandle);
945 } catch (OcException e) {
946 Log.e(TAG, e.getMessage());
951 public void testRegisterDeviceInfoGetDeviceInfo() throws InterruptedException {
952 final String resourceType = "unit.test.resource" + new Date().getTime();
953 final CountDownLatch signal = new CountDownLatch(1);
955 OcPlatform.OnDeviceFoundListener deviceFoundListener = new OcPlatform.OnDeviceFoundListener() {
957 public void onDeviceFound(OcRepresentation ocRepresentation) {
959 Log.i(TAG, "Device Name: " + ocRepresentation.getValue("dn"));
960 } catch (OcException e) {
961 Log.e(TAG, e.toString());
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());
973 OcDeviceInfo devInfo = new OcDeviceInfo();
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");
990 OcPlatform.registerDeviceInfo(devInfo);
993 OcPlatform.getDeviceInfo(
995 OcPlatform.WELL_KNOWN_QUERY + "/d",
996 OcConnectivityType.IPV4,
997 deviceFoundListener);
999 //wait for onDeviceFound event
1000 assertTrue(signal.await(60, TimeUnit.SECONDS));
1002 } catch (OcException e) {
1003 Log.e(TAG, e.getMessage());
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);
1017 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1019 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1020 return EntityHandlerResult.OK;
1024 OcPlatform.OnResourceFoundListener resourceFoundListener1 = new OcPlatform.OnResourceFoundListener() {
1026 public void onResourceFound(OcResource resource) {
1027 signal1.countDown();
1031 OcPlatform.OnResourceFoundListener resourceFoundListener2 = new OcPlatform.OnResourceFoundListener() {
1033 public void onResourceFound(OcResource resource) {
1034 signal2.countDown();
1038 OcPlatform.OnResourceFoundListener resourceFoundListener3 = new OcPlatform.OnResourceFoundListener() {
1040 public void onResourceFound(OcResource resource) {
1041 signal3.countDown();
1045 OcPlatform.OnResourceFoundListener resourceFoundListener4 = new OcPlatform.OnResourceFoundListener() {
1047 public void onResourceFound(OcResource resource) {
1048 signal4.countDown();
1052 OcPlatform.OnResourceFoundListener resourceFoundListener5 = new OcPlatform.OnResourceFoundListener() {
1054 public void onResourceFound(OcResource resource) {
1055 signal5.countDown();
1059 OcPlatform.OnResourceFoundListener resourceFoundListener6 = new OcPlatform.OnResourceFoundListener() {
1061 public void onResourceFound(OcResource resource) {
1062 signal6.countDown();
1069 OcResourceHandle resourceHandleCollection = OcPlatform.registerResource(
1072 OcPlatform.DEFAULT_INTERFACE,
1074 EnumSet.of(ResourceProperty.DISCOVERABLE)
1077 OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1080 OcPlatform.DEFAULT_INTERFACE,
1082 EnumSet.of(ResourceProperty.DISCOVERABLE)
1085 OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1088 OcPlatform.DEFAULT_INTERFACE,
1090 EnumSet.of(ResourceProperty.DISCOVERABLE)
1094 OcPlatform.findResource("",
1095 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1096 OcConnectivityType.IPV4,
1097 resourceFoundListener1);
1099 //wait for onResourceFound event to find 3 registered resources
1100 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1103 OcPlatform.bindResource(resourceHandleCollection, resourceHandle1);
1104 OcPlatform.bindResource(resourceHandleCollection, resourceHandle2);
1107 OcPlatform.findResource("",
1108 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1109 OcConnectivityType.IPV4,
1110 resourceFoundListener3);
1112 //wait for onResourceFound event to find 1 collection resources
1113 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1116 OcPlatform.unbindResource(resourceHandleCollection, resourceHandle1);
1119 OcPlatform.findResource("",
1120 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1121 OcConnectivityType.IPV4,
1122 resourceFoundListener2);
1124 //wait for onResourceFound event to find 2 resources
1125 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1128 OcPlatform.unbindResource(resourceHandleCollection, resourceHandle2);
1131 OcPlatform.findResource("",
1132 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1133 OcConnectivityType.IPV4,
1134 resourceFoundListener4);
1136 //wait for onResourceFound event to find 3 registered resources
1137 assertTrue(signal4.await(60, TimeUnit.SECONDS));
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);
1146 OcPlatform.findResource("",
1147 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1148 OcConnectivityType.IPV4,
1149 resourceFoundListener6);
1151 //wait for onResourceFound event to find 1 collection resources
1152 assertTrue(signal6.await(60, TimeUnit.SECONDS));
1154 OcPlatform.unbindResources(resourceHandleCollection, resourceHandleList);
1157 OcPlatform.findResource("",
1158 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1159 OcConnectivityType.IPV4,
1160 resourceFoundListener5);
1162 //wait for onResourceFound event to find 1 collection resources
1163 assertTrue(signal5.await(60, TimeUnit.SECONDS));
1166 OcPlatform.unregisterResource(resourceHandleCollection);
1167 OcPlatform.unregisterResource(resourceHandle1);
1168 OcPlatform.unregisterResource(resourceHandle2);
1169 } catch (OcException e) {
1170 Log.e(TAG, e.getMessage());
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();
1179 final CountDownLatch signal = new CountDownLatch(2);
1180 final List<OcResource> resourceList = new LinkedList<OcResource>();
1182 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1184 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1185 return EntityHandlerResult.OK;
1189 OcPlatform.OnResourceFoundListener resourceFoundListener =
1190 new OcPlatform.OnResourceFoundListener() {
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());
1198 for (String resourceType : resource.getResourceTypes()) {
1199 Log.i(TAG, "Type: " + resourceType);
1202 for (String resourceInterface : resource.getResourceInterfaces()) {
1203 Log.i(TAG, "Interface: " + resourceInterface);
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);
1211 resource.setHeaderOptions(headerOptionList);
1212 resource.unsetHeaderOptions();
1214 OcResourceIdentifier resourceIdentifier = resource.getUniqueIdentifier();
1215 OcResourceIdentifier resourceIdentifier2 = resource.getUniqueIdentifier();
1216 assertTrue(resourceIdentifier.equals(resourceIdentifier2));
1224 OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1227 OcPlatform.DEFAULT_INTERFACE,
1229 EnumSet.of(ResourceProperty.DISCOVERABLE)
1232 OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1235 OcPlatform.DEFAULT_INTERFACE,
1237 EnumSet.of(ResourceProperty.DISCOVERABLE)
1241 OcPlatform.findResource("",
1242 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType1,
1243 OcConnectivityType.IPV4,
1244 resourceFoundListener);
1246 OcPlatform.findResource("",
1247 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType2,
1248 OcConnectivityType.IPV4,
1249 resourceFoundListener);
1251 //wait for onResourceFound event
1252 assertTrue(signal.await(60, TimeUnit.SECONDS));
1254 assertTrue(2 == resourceList.size());
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()));
1262 OcPlatform.unregisterResource(resourceHandle1);
1263 OcPlatform.unregisterResource(resourceHandle2);
1265 } catch (OcException e) {
1266 Log.e(TAG, e.getMessage());
1271 public void testCreateResourceProxy() throws InterruptedException {
1272 final String resourceType = "unit.test.resource" + new Date().getTime();
1273 final CountDownLatch signal = new CountDownLatch(1);
1275 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1277 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1278 return EntityHandlerResult.OK;
1282 OcPlatform.OnResourceFoundListener resourceFoundListener =
1283 new OcPlatform.OnResourceFoundListener() {
1285 public void onResourceFound(OcResource resource) {
1288 //client: construct resource proxy
1289 OcResource resourceProxy = OcPlatform.constructResourceObject(
1292 OcConnectivityType.IPV4,
1293 resource.isObservable(),
1294 resource.getResourceTypes(),
1295 resource.getResourceInterfaces());
1297 //client: register resource proxy
1298 OcResourceHandle resourceProxyHandle =
1299 OcPlatform.registerResource(resourceProxy);
1301 OcPlatform.unregisterResource(resourceProxyHandle);
1302 } catch (OcException e) {
1311 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1314 OcPlatform.DEFAULT_INTERFACE,
1316 EnumSet.of(ResourceProperty.DISCOVERABLE)
1320 OcPlatform.findResource("",
1321 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1322 OcConnectivityType.IPV4,
1323 resourceFoundListener);
1325 //wait for onResourceFound event
1326 assertTrue(signal.await(60, TimeUnit.SECONDS));
1329 OcPlatform.unregisterResource(resourceHandle);
1331 } catch (OcException e) {
1332 Log.e(TAG, e.getMessage());
1338 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1341 OcPlatform.DEFAULT_INTERFACE,
1343 EnumSet.of(ResourceProperty.DISCOVERABLE)
1347 OcPlatform.findResource("",
1348 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1349 OcConnectivityType.IPV4,
1350 resourceFoundListener);
1352 //wait for onResourceFound event
1353 assertTrue(signal.await(60, TimeUnit.SECONDS));
1356 OcPlatform.unregisterResource(resourceHandle);
1358 } catch (OcException e) {
1359 Log.e(TAG, e.getMessage());
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);
1373 final String resourceType = "unit.test.resource" + new Date().getTime();
1375 final OcResource.OnObserveListener observeListener = new OcResource.OnObserveListener() {
1377 public void onObserveCompleted(
1378 List<OcHeaderOption> headerOptionList,
1379 OcRepresentation ocRepresentation,
1380 int sequenceNumber) {
1383 Log.i(TAG, "Observe #" + sequenceNumber + " power: " +
1384 ocRepresentation.getValue("power"));
1385 } catch (OcException e) {
1386 Log.e(TAG, e.toString());
1389 signal2.countDown();
1393 public void onObserveFailed(Throwable ex) {
1394 if (ex instanceof OcException) {
1395 OcException ocEx = (OcException) ex;
1396 ErrorCode errCode = ocEx.getErrorCode();
1398 Log.e(TAG, ex.toString());
1403 final List<Byte> observationIdList = new LinkedList<Byte>();
1404 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1406 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1407 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
1408 RequestType requestType = ocResourceRequest.getRequestType();
1410 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
1413 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
1414 OcResourceResponse ocResourceResponse = new OcResourceResponse();
1415 ocResourceResponse.setRequestHandle(ocResourceRequest.getRequestHandle());
1416 ocResourceResponse.setResourceHandle(ocResourceRequest.getResourceHandle());
1418 switch (requestType) {
1420 ocResourceResponse.setErrorCode(200);
1421 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
1422 ocResourceResponse.setResourceRepresentation(
1426 OcPlatform.sendResponse(ocResourceResponse);
1427 } catch (OcException e) {
1428 Log.e(TAG, e.getMessage());
1429 return EntityHandlerResult.ERROR;
1435 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
1436 ObservationInfo observationInfo = ocResourceRequest.getObservationInfo();
1438 switch (observationInfo.getObserveAction()) {
1440 synchronized (observationIdList) {
1441 observationIdList.add(observationInfo.getOcObservationId());
1442 timer.schedule(new TimerTask() {
1443 int numNotified = 1;
1447 if (0 < resourceHandleList.size()) {
1448 synchronized (observationIdList) {
1449 if (numNotified > NUM_OBSERVES) {
1452 signal3.countDown();
1455 OcPlatform.notifyAllObservers(
1456 resourceHandleList.get(0));
1457 } catch (OcException e) {
1458 if (ErrorCode.NO_OBSERVERS == e.getErrorCode()) {
1461 signal3.countDown();
1463 Log.e(TAG, e.getMessage());
1475 //TODO unregister isn't implemented in C++ API, yet
1476 synchronized (observationIdList) {
1482 return EntityHandlerResult.OK;
1486 OcPlatform.OnResourceFoundListener resourceFoundListener =
1487 new OcPlatform.OnResourceFoundListener() {
1489 public void onResourceFound(OcResource resource) {
1490 resourceList.add(resource);
1491 if (resource.isObservable()) {
1494 ObserveType.OBSERVE,
1495 new HashMap<String, String>(),
1498 signal1.countDown();
1499 } catch (OcException e) {
1500 Log.e(TAG, e.getMessage());
1508 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1511 OcPlatform.DEFAULT_INTERFACE,
1513 EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
1516 resourceHandleList.add(resourceHandle);
1519 OcPlatform.findResource("",
1520 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1521 OcConnectivityType.IPV4,
1522 resourceFoundListener);
1524 //wait for onResourceFound event
1525 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1527 //wait for OnObserveListener event to observe 20 values
1528 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1530 if (resourceList.size() > 0) {
1531 OcResource resource = resourceList.get(0);
1532 if (resource.isObservable()) {
1533 resource.cancelObserve();
1537 //wait for server to finish
1538 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1541 OcPlatform.unregisterResource(resourceHandle);
1543 } catch (OcException e) {
1544 Log.e(TAG, e.getMessage());
1549 private OcRepresentation getRepresentation(int value) {
1550 OcRepresentation rep = new OcRepresentation();
1552 rep.setValue("power", value);
1553 } catch (OcException e) {
1554 Log.e(TAG, e.toString());
1560 private OcRepresentation getRepresentation() {
1561 return getRepresentation(74);