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.Arrays;
29 import java.util.Date;
30 import java.util.EnumSet;
31 import java.util.HashMap;
32 import java.util.LinkedList;
33 import java.util.List;
35 import java.util.Random;
36 import java.util.Timer;
37 import java.util.TimerTask;
38 import java.util.concurrent.CountDownLatch;
39 import java.util.concurrent.TimeUnit;
41 public class SmokeTest extends InstrumentationTestCase {
42 private static final String TAG = "SmokeTest";
43 private Random rnd = new Random();
46 protected void setUp() throws Exception {
48 PlatformConfig cfg = new PlatformConfig(
49 getInstrumentation().getContext(),
51 ModeType.CLIENT_SERVER,
54 QualityOfService.LOW);
56 OcPlatform.Configure(cfg);
60 protected void tearDown() throws Exception {
64 public void testResourceRegisterUnregister() throws InterruptedException {
65 final String resourceType = "unit.test.resource" +
67 final CountDownLatch signal = new CountDownLatch(1);
69 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
71 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
72 return EntityHandlerResult.OK;
76 OcPlatform.OnResourceFoundListener resourceFoundListener =
77 new OcPlatform.OnResourceFoundListener() {
79 public void onResourceFound(OcResource resource) {
80 Log.i(TAG, "Host: " + resource.getHost());
81 Log.i(TAG, "Server ID: " + resource.getServerId());
82 Log.i(TAG, "Connectivity Types: ");
83 for (OcConnectivityType connectivityType : resource.getConnectivityTypeSet()) {
84 Log.i(TAG, " " + connectivityType);
92 OcResourceHandle resourceHandle = OcPlatform.registerResource(
95 OcPlatform.DEFAULT_INTERFACE,
97 EnumSet.of(ResourceProperty.DISCOVERABLE)
101 OcPlatform.findResource("",
102 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
103 EnumSet.of(OcConnectivityType.CT_DEFAULT),
104 resourceFoundListener);
106 //wait for onResourceFound event
107 assertTrue(signal.await(60, TimeUnit.SECONDS));
110 OcPlatform.unregisterResource(resourceHandle);
111 } catch (OcException e) {
112 Log.e(TAG, e.getMessage());
117 public void testStartStopListenForPresence() throws InterruptedException {
118 final String resourceType = "unit.test.resource" +
119 new Date().getTime();
120 final CountDownLatch signal = new CountDownLatch(1);
122 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
124 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
125 return EntityHandlerResult.OK;
129 final OcPlatform.OnPresenceListener presenceListener = new OcPlatform.OnPresenceListener() {
131 public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress) {
132 Log.i(TAG, "onPresence status " + ocPresenceStatus.toString() + " nonce " + nonce);
137 OcPlatform.OnResourceFoundListener resourceFoundListener =
138 new OcPlatform.OnResourceFoundListener() {
140 public void onResourceFound(OcResource resource) {
143 OcPresenceHandle presenceHandle = OcPlatform.subscribePresence(
145 EnumSet.of(OcConnectivityType.CT_DEFAULT),
149 //wait for onPresence event
150 assertTrue(signal.await(60, TimeUnit.SECONDS));
153 OcPlatform.unsubscribePresence(presenceHandle);
154 } catch (OcException e) {
156 } catch (InterruptedException e) {
164 OcResourceHandle resourceHandle = OcPlatform.registerResource(
167 OcPlatform.DEFAULT_INTERFACE,
169 EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
173 OcPlatform.findResource("",
174 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
175 EnumSet.of(OcConnectivityType.CT_DEFAULT),
176 resourceFoundListener);
179 OcPlatform.startPresence(OcPlatform.DEFAULT_PRESENCE_TTL);
181 //wait for onPresence event
182 assertTrue(signal.await(60, TimeUnit.SECONDS));
185 OcPlatform.stopPresence();
188 OcPlatform.unregisterResource(resourceHandle);
190 } catch (OcException e) {
191 Log.e(TAG, e.getMessage());
196 public void testHandleGetRequest() throws InterruptedException {
197 final String someKey = "SomeKey";
198 final String someValue = "SomeValue";
199 final String resourceType = "unit.test.resource" + new Date().getTime();
200 final CountDownLatch signal1 = new CountDownLatch(1);
201 final CountDownLatch signal2 = new CountDownLatch(1);
202 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
205 final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
207 public void onGetCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
208 if (!headerOptionList.isEmpty()) {
209 for (OcHeaderOption headerOption : headerOptionList) {
210 Log.i(TAG, "Header option " +
211 headerOption.getOptionId() +
213 headerOption.getOptionData());
217 Log.i(TAG, "Power: " + ocRepresentation.getValue("power"));
218 } catch (OcException e) {
219 Log.e(TAG, e.toString());
226 public void onGetFailed(Throwable ex) {
227 if (ex instanceof OcException) {
228 OcException ocEx = (OcException) ex;
229 ErrorCode errCode = ocEx.getErrorCode();
230 if (ErrorCode.NO_RESOURCE != errCode) {
231 Log.e(TAG, ocEx.getMessage());
235 Log.e(TAG, ex.getMessage());
242 final OcPlatform.OnResourceFoundListener resourceFoundListener =
243 new OcPlatform.OnResourceFoundListener() {
245 public void onResourceFound(OcResource resource) {
246 Map<String, String> queryParamsMap = new HashMap<String, String>();
247 queryParamsMap.put(someKey, someValue);
249 ocResourceList.add(resource);
251 resource.get(queryParamsMap, onGetListener);
252 // TODO there is a bug in the stack that prevents the usage of the following APIs
253 // resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE, queryParamsMap,
256 // resource.get(queryParamsMap, onGetListener, QualityOfService.LOW);
258 // resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE,queryParamsMap,
259 // onGetListener, QualityOfService.LOW);
260 } catch (OcException e) {
261 Log.e(TAG, e.toString());
270 OcResourceHandle resourceHandle = OcPlatform.registerResource(
273 OcPlatform.DEFAULT_INTERFACE,
274 new OcPlatform.EntityHandler() {
276 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
277 EnumSet<RequestHandlerFlag> handlerFlagSet =
278 ocResourceRequest.getRequestHandlerFlagSet();
280 RequestType requestType = ocResourceRequest.getRequestType();
282 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
284 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
285 OcResourceResponse ocResourceResponse = new OcResourceResponse();
286 ocResourceResponse.setRequestHandle(
287 ocResourceRequest.getRequestHandle());
288 ocResourceResponse.setResourceHandle(
289 ocResourceRequest.getResourceHandle());
291 switch (requestType) {
293 Map<String, String> queryParams =
294 ocResourceRequest.getQueryParameters();
296 if (!(queryParams.containsKey(someKey) &&
297 someValue.equals(queryParams.get(someKey)))) {
301 ocResourceResponse.setErrorCode(200);
302 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
303 ocResourceResponse.setResourceRepresentation(
304 getRepresentation(74));
309 OcPlatform.sendResponse(ocResourceResponse);
310 } catch (OcException e) {
311 Log.e(TAG, e.getMessage());
312 return EntityHandlerResult.ERROR;
315 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
317 return EntityHandlerResult.OK;
320 EnumSet.of(ResourceProperty.DISCOVERABLE)
324 OcPlatform.findResource(null,
325 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
326 EnumSet.of(OcConnectivityType.CT_DEFAULT),
327 resourceFoundListener);
329 //wait for onResourceFound event
330 assertTrue(signal1.await(60, TimeUnit.SECONDS));
332 //wait for onGetCompleted event
333 assertTrue(signal2.await(60, TimeUnit.SECONDS));
336 OcPlatform.unregisterResource(resourceHandle);
338 } catch (OcException e) {
339 Log.e(TAG, e.getMessage());
344 public void testHandlePutRequest() throws InterruptedException {
345 final String resourceType = "unit.test.resource" + new Date().getTime();
346 final CountDownLatch signal1 = new CountDownLatch(1);
347 final CountDownLatch signal2 = new CountDownLatch(3);
348 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
350 final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
352 public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
353 if (!headerOptionList.isEmpty()) {
354 for (OcHeaderOption headerOption : headerOptionList) {
355 Log.i(TAG, "Header option " +
356 headerOption.getOptionId() +
358 headerOption.getOptionData());
362 Log.i(TAG, "onPutCompleted Power: " + ocRepresentation.getValue("power"));
363 } catch (OcException e) {
364 Log.e(TAG, e.getMessage());
367 Log.i(TAG, "onPutCompleted Uri: " + ocRepresentation.getUri());
372 public void onPutFailed(Throwable ex) {
373 if (ex instanceof OcException) {
374 OcException ocEx = (OcException) ex;
375 ErrorCode errCode = ocEx.getErrorCode();
376 if (ErrorCode.NO_RESOURCE != errCode) {
377 Log.e(TAG, ocEx.getMessage());
381 Log.e(TAG, ex.getMessage());
387 final OcPlatform.OnResourceFoundListener resourceFoundListener =
388 new OcPlatform.OnResourceFoundListener() {
390 public void onResourceFound(OcResource resource) {
391 ocResourceList.add(resource);
395 new HashMap<String, String>(),
400 new HashMap<String, String>(),
405 new HashMap<String, String>(),
406 new OcResource.OnPutListener() {
408 public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
413 public void onPutFailed(Throwable ex) {
414 if (ex instanceof OcException) {
415 OcException ocEx = (OcException) ex;
416 ErrorCode errCode = ocEx.getErrorCode();
417 if (ErrorCode.NO_RESOURCE != errCode) {
418 Log.e(TAG, ocEx.getMessage());
422 Log.e(TAG, ex.getMessage());
428 } catch (OcException e) {
437 OcResourceHandle resourceHandle = OcPlatform.registerResource(
440 OcPlatform.DEFAULT_INTERFACE,
441 new OcPlatform.EntityHandler() {
443 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
444 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
445 RequestType requestType = ocResourceRequest.getRequestType();
447 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
449 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
450 OcResourceResponse ocResourceResponse = new OcResourceResponse();
451 ocResourceResponse.setRequestHandle(
452 ocResourceRequest.getRequestHandle());
453 ocResourceResponse.setResourceHandle(
454 ocResourceRequest.getResourceHandle());
456 switch (requestType) {
461 OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
463 Log.i(TAG, "Put res. power: " + rep.getValue("power"));
464 } catch (OcException e) {
465 Log.e(TAG, e.toString());
468 Log.i(TAG, "URI: " + rep.getUri());
470 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
471 ocResourceResponse.setErrorCode(200);
472 ocResourceResponse.setResourceRepresentation(rep);
482 OcPlatform.sendResponse(ocResourceResponse);
483 } catch (OcException e) {
484 Log.e(TAG, e.getMessage());
485 return EntityHandlerResult.ERROR;
488 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
490 return EntityHandlerResult.OK;
493 EnumSet.of(ResourceProperty.DISCOVERABLE)
497 OcPlatform.findResource("",
498 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
499 EnumSet.of(OcConnectivityType.CT_DEFAULT),
500 resourceFoundListener);
502 //wait for onResourceFound event
503 assertTrue(signal1.await(60, TimeUnit.SECONDS));
505 //wait for onGetCompleted event
506 assertTrue(signal2.await(60, TimeUnit.SECONDS));
509 OcPlatform.unregisterResource(resourceHandle);
511 } catch (OcException e) {
512 Log.e(TAG, e.getMessage());
517 public void testHandlePostRequest() throws InterruptedException {
518 final String resourceType = "unit.test.resource" + new Date().getTime();
519 final CountDownLatch signal1 = new CountDownLatch(1);
520 final CountDownLatch signal2 = new CountDownLatch(3);
521 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
523 final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
525 public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
526 if (!headerOptionList.isEmpty()) {
527 for (OcHeaderOption headerOption : headerOptionList) {
528 Log.i(TAG, "Header option " +
529 headerOption.getOptionId() +
531 headerOption.getOptionData());
535 Log.i(TAG, "onPostCompleted Power: " + ocRepresentation.getValue("power"));
536 } catch (OcException e) {
537 Log.e(TAG, e.toString());
540 Log.i(TAG, "onPostCompleted Uri: " + ocRepresentation.getUri());
545 public void onPostFailed(Throwable ex) {
546 if (ex instanceof OcException) {
547 OcException ocEx = (OcException) ex;
548 ErrorCode errCode = ocEx.getErrorCode();
549 if (ErrorCode.NO_RESOURCE != errCode) {
550 Log.e(TAG, ocEx.getMessage());
554 Log.e(TAG, ex.getMessage());
560 final OcPlatform.OnResourceFoundListener resourceFoundListener =
561 new OcPlatform.OnResourceFoundListener() {
563 public void onResourceFound(OcResource resource) {
564 ocResourceList.add(resource);
568 new HashMap<String, String>(),
573 new HashMap<String, String>(),
578 new HashMap<String, String>(),
579 new OcResource.OnPostListener() {
581 public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
586 public void onPostFailed(Throwable ex) {
587 if (ex instanceof OcException) {
588 OcException ocEx = (OcException) ex;
589 ErrorCode errCode = ocEx.getErrorCode();
590 if (ErrorCode.NO_RESOURCE != errCode) {
591 Log.e(TAG, ocEx.getMessage());
595 Log.e(TAG, ex.getMessage());
601 } catch (OcException e) {
610 OcResourceHandle resourceHandle = OcPlatform.registerResource(
613 OcPlatform.DEFAULT_INTERFACE,
614 new OcPlatform.EntityHandler() {
616 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
617 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
618 RequestType requestType = ocResourceRequest.getRequestType();
620 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
622 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
623 OcResourceResponse ocResourceResponse = new OcResourceResponse();
624 ocResourceResponse.setRequestHandle(
625 ocResourceRequest.getRequestHandle());
626 ocResourceResponse.setResourceHandle(
627 ocResourceRequest.getResourceHandle());
629 switch (requestType) {
637 OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
639 Log.i(TAG, "Post res. power: " + rep.getValue("power"));
640 } catch (OcException e) {
641 Log.e(TAG, e.toString());
644 Log.i(TAG, "URI: " + rep.getUri());
646 ocResourceResponse.setErrorCode(200);
647 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
648 ocResourceResponse.setResourceRepresentation(
649 getRepresentation(44));
658 OcPlatform.sendResponse(ocResourceResponse);
659 } catch (OcException e) {
660 Log.e(TAG, e.getMessage());
661 return EntityHandlerResult.ERROR;
664 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
666 return EntityHandlerResult.OK;
669 EnumSet.of(ResourceProperty.DISCOVERABLE)
673 OcPlatform.findResource("",
674 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
675 EnumSet.of(OcConnectivityType.CT_DEFAULT),
676 resourceFoundListener);
678 //wait for onResourceFound event
679 assertTrue(signal1.await(60, TimeUnit.SECONDS));
681 //wait for onPostCompleted event
682 assertTrue(signal2.await(60, TimeUnit.SECONDS));
685 OcPlatform.unregisterResource(resourceHandle);
687 } catch (OcException e) {
688 Log.e(TAG, e.getMessage());
694 public void testHandleDeleteRequest() throws InterruptedException {
695 final String resourceType = "unit.test.resource" + new Date().getTime();
696 final CountDownLatch signal1 = new CountDownLatch(1);
697 final CountDownLatch signal2 = new CountDownLatch(1);
698 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
700 final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
702 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
707 public void onDeleteFailed(Throwable ex) {
708 if (ex instanceof OcException) {
709 OcException ocEx = (OcException) ex;
710 ErrorCode errCode = ocEx.getErrorCode();
711 if (ErrorCode.NO_RESOURCE != errCode) {
712 Log.e(TAG, ocEx.getMessage());
716 Log.e(TAG, ex.getMessage());
722 final OcPlatform.OnResourceFoundListener resourceFoundListener =
723 new OcPlatform.OnResourceFoundListener() {
725 public void onResourceFound(OcResource resource) {
726 ocResourceList.add(resource);
728 resource.deleteResource(onDeleteListener);
729 } catch (OcException e) {
738 OcResourceHandle resourceHandle = OcPlatform.registerResource(
741 OcPlatform.DEFAULT_INTERFACE,
742 new OcPlatform.EntityHandler() {
744 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
745 EnumSet<RequestHandlerFlag> handlerFlagSet =
746 ocResourceRequest.getRequestHandlerFlagSet();
747 RequestType requestType = ocResourceRequest.getRequestType();
749 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
751 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
752 OcResourceResponse ocResourceResponse = new OcResourceResponse();
753 ocResourceResponse.setRequestHandle(
754 ocResourceRequest.getRequestHandle());
755 ocResourceResponse.setResourceHandle(
756 ocResourceRequest.getResourceHandle());
758 switch (requestType) {
760 ocResourceResponse.setErrorCode(200);
761 ocResourceResponse.setResponseResult(
762 EntityHandlerResult.RESOURCE_DELETED);
767 OcPlatform.sendResponse(ocResourceResponse);
768 } catch (OcException e) {
769 Log.e(TAG, e.getMessage());
770 return EntityHandlerResult.ERROR;
773 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
775 return EntityHandlerResult.OK;
778 EnumSet.of(ResourceProperty.DISCOVERABLE)
782 OcPlatform.findResource("",
783 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
784 EnumSet.of(OcConnectivityType.CT_DEFAULT),
785 resourceFoundListener);
787 //wait for onResourceFound event
788 assertTrue(signal1.await(60, TimeUnit.SECONDS));
790 //wait for onDeleteCompleted event
791 assertTrue(signal2.await(60, TimeUnit.SECONDS));
794 OcPlatform.unregisterResource(resourceHandle);
796 } catch (OcException e) {
797 Log.e(TAG, e.getMessage());
802 public void testHandleGetPutPostDeleteFailures() throws InterruptedException {
803 final String resourceType = "unit.test.resource" + new Date().getTime();
804 final CountDownLatch signal1 = new CountDownLatch(1);
805 final CountDownLatch signal2 = new CountDownLatch(1);
806 final CountDownLatch signal3 = new CountDownLatch(1);
807 final CountDownLatch signal4 = new CountDownLatch(1);
808 final CountDownLatch signal5 = new CountDownLatch(1);
810 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
811 final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
813 public void onGetCompleted(List<OcHeaderOption> headerOptionList,
814 OcRepresentation ocRepresentation) {
819 public void onGetFailed(Throwable ex) {
820 if (ex instanceof OcException) {
821 OcException ocEx = (OcException) ex;
822 ErrorCode errCode = ocEx.getErrorCode();
824 Log.i(TAG, ex.toString());
829 final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
831 public void onPutCompleted(List<OcHeaderOption> headerOptionList,
832 OcRepresentation ocRepresentation) {
837 public void onPutFailed(Throwable ex) {
838 if (ex instanceof OcException) {
839 OcException ocEx = (OcException) ex;
840 ErrorCode errCode = ocEx.getErrorCode();
842 Log.i(TAG, ex.toString());
846 final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
848 public void onPostCompleted(List<OcHeaderOption> headerOptionList,
849 OcRepresentation ocRepresentation) {
854 public void onPostFailed(Throwable ex) {
855 if (ex instanceof OcException) {
856 OcException ocEx = (OcException) ex;
857 ErrorCode errCode = ocEx.getErrorCode();
859 Log.i(TAG, ex.toString());
864 final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
866 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
871 public void onDeleteFailed(Throwable ex) {
872 if (ex instanceof OcException) {
873 OcException ocEx = (OcException) ex;
874 ErrorCode errCode = ocEx.getErrorCode();
876 Log.i(TAG, ex.toString());
881 final OcPlatform.OnResourceFoundListener resourceFoundListener =
882 new OcPlatform.OnResourceFoundListener() {
884 public void onResourceFound(OcResource resource) {
885 ocResourceList.add(resource);
887 resource.get(new HashMap<String, String>(), onGetListener);
888 resource.put(new OcRepresentation(), new HashMap<String, String>(),
890 resource.post(new OcRepresentation(), new HashMap<String, String>(),
892 resource.deleteResource(onDeleteListener);
893 } catch (OcException e) {
902 OcResourceHandle resourceHandle = OcPlatform.registerResource(
905 OcPlatform.DEFAULT_INTERFACE,
906 new OcPlatform.EntityHandler() {
908 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
909 EnumSet<RequestHandlerFlag> handlerFlagSet =
910 ocResourceRequest.getRequestHandlerFlagSet();
911 RequestType requestType = ocResourceRequest.getRequestType();
913 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
915 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
916 OcResourceResponse ocResourceResponse = new OcResourceResponse();
917 ocResourceResponse.setRequestHandle(
918 ocResourceRequest.getRequestHandle());
919 ocResourceResponse.setResourceHandle(
920 ocResourceRequest.getResourceHandle());
922 switch (requestType) {
924 ocResourceResponse.setErrorCode(200);
925 ocResourceResponse.setResponseResult(
926 EntityHandlerResult.ERROR);
929 ocResourceResponse.setErrorCode(200);
930 ocResourceResponse.setResponseResult(
931 EntityHandlerResult.ERROR);
934 ocResourceResponse.setErrorCode(200);
935 ocResourceResponse.setResponseResult(
936 EntityHandlerResult.ERROR);
939 ocResourceResponse.setErrorCode(200);
940 ocResourceResponse.setResponseResult(
941 EntityHandlerResult.ERROR);
945 OcPlatform.sendResponse(ocResourceResponse);
946 } catch (OcException e) {
947 Log.e(TAG, e.getMessage());
948 return EntityHandlerResult.ERROR;
951 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
953 return EntityHandlerResult.OK;
956 EnumSet.of(ResourceProperty.DISCOVERABLE)
960 OcPlatform.findResource("",
961 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
962 EnumSet.of(OcConnectivityType.CT_DEFAULT),
963 resourceFoundListener);
965 //wait for onResourceFound event
966 assertTrue(signal1.await(60, TimeUnit.SECONDS));
967 //wait for onGetCompleted event
968 assertTrue(signal2.await(60, TimeUnit.SECONDS));
969 //wait for onPutCompleted event
970 assertTrue(signal3.await(60, TimeUnit.SECONDS));
971 //wait for onPostCompleted event
972 assertTrue(signal4.await(60, TimeUnit.SECONDS));
973 //wait for onDeleteCompleted event
974 assertTrue(signal5.await(60, TimeUnit.SECONDS));
977 OcPlatform.unregisterResource(resourceHandle);
979 } catch (OcException e) {
980 Log.e(TAG, e.getMessage());
985 public void testPlatformInfo() throws InterruptedException {
986 final String resourceType = "unit.test.resource" + new Date().getTime();
987 final CountDownLatch signal = new CountDownLatch(1);
989 OcPlatform.OnPlatformFoundListener platformFoundListener = new OcPlatform.OnPlatformFoundListener() {
991 public void onPlatformFound(OcRepresentation ocRepresentation) {
992 Log.i(TAG, "Platform Info Received: ");
993 Log.i(TAG, "URI: " + ocRepresentation.getUri());
998 OcPlatformInfo platformInfo = null;
1000 platformInfo = new OcPlatformInfo("myPlatformID", "myManuName", "myManuUrl");
1002 platformInfo.setModelNumber("myModelNumber");
1003 platformInfo.setDateOfManufacture("myDateOfManufacture");
1004 platformInfo.setPlatformVersion("myPlatformVersion");
1005 platformInfo.setOperatingSystemVersion("myOperatingSystemVersion");
1006 platformInfo.setHardwareVersion("myHardwareVersion");
1007 platformInfo.setFirmwareVersion("myFirmwareVersion");
1008 platformInfo.setSupportUrl("mySupportUrl");
1009 platformInfo.setSystemTime("mySystemTime");
1013 OcPlatform.registerPlatformInfo(platformInfo);
1016 OcPlatform.getPlatformInfo(
1018 OcPlatform.WELL_KNOWN_PLATFORM_QUERY,
1019 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1020 platformFoundListener);
1022 //wait for onPlatformFound event
1023 assertTrue(signal.await(60, TimeUnit.SECONDS));
1024 } catch (OcException e) {
1025 Log.e(TAG, e.getMessage() + platformInfo.toString());
1030 public void testRegisterDeviceInfoGetDeviceInfo() throws InterruptedException {
1031 final String resourceType = "unit.test.resource" + new Date().getTime();
1032 final CountDownLatch signal = new CountDownLatch(1);
1034 OcPlatform.OnDeviceFoundListener deviceFoundListener = new OcPlatform.OnDeviceFoundListener() {
1036 public void onDeviceFound(OcRepresentation ocRepresentation) {
1038 Log.i(TAG, "Device Name: " + ocRepresentation.getValue("n"));
1039 } catch (OcException e) {
1040 Log.e(TAG, e.toString());
1043 boolean hasDeviceNameAtr = ocRepresentation.hasAttribute("n");
1044 assertTrue(hasDeviceNameAtr);
1045 boolean hasNonExistingAtr = ocRepresentation.hasAttribute("NonExisting");
1046 assertFalse(hasNonExistingAtr);
1047 Log.i(TAG, "URI: " + ocRepresentation.getUri());
1052 OcDeviceInfo devInfo = new OcDeviceInfo(
1054 Arrays.asList(new String[]{"oic.d.test"})
1059 OcPlatform.registerDeviceInfo(devInfo);
1061 OcPlatform.getDeviceInfo(
1063 OcPlatform.WELL_KNOWN_DEVICE_QUERY,
1064 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1065 deviceFoundListener);
1067 //wait for onDeviceFound event
1068 assertTrue(signal.await(60, TimeUnit.SECONDS));
1070 } catch (OcException e) {
1071 Log.e(TAG, e.getMessage());
1076 public void testBindUnbindResources() throws InterruptedException {
1077 final String resourceType = "unit.test.resource" + new Date().getTime();
1078 final CountDownLatch signal1 = new CountDownLatch(3);
1079 final CountDownLatch signal2 = new CountDownLatch(2);
1080 final CountDownLatch signal3 = new CountDownLatch(1);
1081 final CountDownLatch signal4 = new CountDownLatch(3);
1082 final CountDownLatch signal5 = new CountDownLatch(3);
1083 final CountDownLatch signal6 = new CountDownLatch(1);
1085 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1087 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1088 return EntityHandlerResult.OK;
1092 OcPlatform.OnResourceFoundListener resourceFoundListener1 = new OcPlatform.OnResourceFoundListener() {
1094 public void onResourceFound(OcResource resource) {
1095 signal1.countDown();
1099 OcPlatform.OnResourceFoundListener resourceFoundListener2 = new OcPlatform.OnResourceFoundListener() {
1101 public void onResourceFound(OcResource resource) {
1102 signal2.countDown();
1106 OcPlatform.OnResourceFoundListener resourceFoundListener3 = new OcPlatform.OnResourceFoundListener() {
1108 public void onResourceFound(OcResource resource) {
1109 signal3.countDown();
1113 OcPlatform.OnResourceFoundListener resourceFoundListener4 = new OcPlatform.OnResourceFoundListener() {
1115 public void onResourceFound(OcResource resource) {
1116 signal4.countDown();
1120 OcPlatform.OnResourceFoundListener resourceFoundListener5 = new OcPlatform.OnResourceFoundListener() {
1122 public void onResourceFound(OcResource resource) {
1123 signal5.countDown();
1127 OcPlatform.OnResourceFoundListener resourceFoundListener6 = new OcPlatform.OnResourceFoundListener() {
1129 public void onResourceFound(OcResource resource) {
1130 signal6.countDown();
1137 OcResourceHandle resourceHandleCollection = OcPlatform.registerResource(
1140 OcPlatform.DEFAULT_INTERFACE,
1142 EnumSet.of(ResourceProperty.DISCOVERABLE)
1145 OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1148 OcPlatform.DEFAULT_INTERFACE,
1150 EnumSet.of(ResourceProperty.DISCOVERABLE)
1153 OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1156 OcPlatform.DEFAULT_INTERFACE,
1158 EnumSet.of(ResourceProperty.DISCOVERABLE)
1162 OcPlatform.findResource("",
1163 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1164 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1165 resourceFoundListener1);
1167 //wait for onResourceFound event to find 3 registered resources
1168 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1171 OcPlatform.bindResource(resourceHandleCollection, resourceHandle1);
1172 OcPlatform.bindResource(resourceHandleCollection, resourceHandle2);
1175 OcPlatform.findResource("",
1176 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1177 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1178 resourceFoundListener3);
1180 //wait for onResourceFound event to find 1 collection resources
1181 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1184 OcPlatform.unbindResource(resourceHandleCollection, resourceHandle1);
1187 OcPlatform.findResource("",
1188 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1189 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1190 resourceFoundListener2);
1192 //wait for onResourceFound event to find 2 resources
1193 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1196 OcPlatform.unbindResource(resourceHandleCollection, resourceHandle2);
1199 OcPlatform.findResource("",
1200 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1201 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1202 resourceFoundListener4);
1204 //wait for onResourceFound event to find 3 registered resources
1205 assertTrue(signal4.await(60, TimeUnit.SECONDS));
1207 //Bind/unbind a list of resource handles
1208 List<OcResourceHandle> resourceHandleList = new LinkedList<OcResourceHandle>();
1209 resourceHandleList.add(resourceHandle1);
1210 resourceHandleList.add(resourceHandle2);
1211 OcPlatform.bindResources(resourceHandleCollection, resourceHandleList);
1214 OcPlatform.findResource("",
1215 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1216 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1217 resourceFoundListener6);
1219 //wait for onResourceFound event to find 1 collection resources
1220 assertTrue(signal6.await(60, TimeUnit.SECONDS));
1222 OcPlatform.unbindResources(resourceHandleCollection, resourceHandleList);
1225 OcPlatform.findResource("",
1226 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1227 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1228 resourceFoundListener5);
1230 //wait for onResourceFound event to find 1 collection resources
1231 assertTrue(signal5.await(60, TimeUnit.SECONDS));
1234 OcPlatform.unregisterResource(resourceHandleCollection);
1235 OcPlatform.unregisterResource(resourceHandle1);
1236 OcPlatform.unregisterResource(resourceHandle2);
1237 } catch (OcException e) {
1238 Log.e(TAG, e.getMessage());
1243 public void testResourceMethods() throws InterruptedException {
1244 final String resourceType1 = "unit.test.resource" + new Date().getTime();
1245 final String resourceType2 = "unit.test.resource" + new Date().getTime();
1247 final CountDownLatch signal = new CountDownLatch(2);
1248 final List<OcResource> resourceList = new LinkedList<>();
1250 OcPlatform.OnResourceFoundListener resourceFoundListener =
1251 new OcPlatform.OnResourceFoundListener() {
1253 public synchronized void onResourceFound(OcResource resource) {
1254 resourceList.add(resource);
1255 Log.i(TAG, "Host: " + resource.getHost());
1256 Log.i(TAG, "Uri: " + resource.getUri());
1257 Log.i(TAG, "Observable: " + resource.isObservable());
1259 assertFalse(resource.getResourceTypes().isEmpty());
1260 for (String resourceType : resource.getResourceTypes()) {
1261 Log.i(TAG, "Type: " + resourceType);
1264 assertFalse(resource.getResourceTypes().isEmpty());
1265 for (String resourceInterface : resource.getResourceInterfaces()) {
1266 Log.i(TAG, "Interface: " + resourceInterface);
1269 List<OcHeaderOption> headerOptionList = new LinkedList<OcHeaderOption>();
1270 headerOptionList.add(new OcHeaderOption(2885, "OptionData1"));
1271 headerOptionList.add(new OcHeaderOption(2886, "OptionData2"));
1273 resource.setHeaderOptions(headerOptionList);
1274 resource.unsetHeaderOptions();
1276 OcResourceIdentifier resourceIdentifier = resource.getUniqueIdentifier();
1277 OcResourceIdentifier resourceIdentifier2 = resource.getUniqueIdentifier();
1278 assertTrue(resourceIdentifier.equals(resourceIdentifier2));
1286 OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1289 OcPlatform.DEFAULT_INTERFACE,
1291 EnumSet.of(ResourceProperty.DISCOVERABLE)
1294 OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1297 OcPlatform.DEFAULT_INTERFACE,
1299 EnumSet.of(ResourceProperty.DISCOVERABLE)
1303 OcPlatform.findResource("",
1304 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType1,
1305 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1306 resourceFoundListener);
1308 OcPlatform.findResource("",
1309 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType2,
1310 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1311 resourceFoundListener);
1313 //wait for onResourceFound event
1314 assertTrue(signal.await(60, TimeUnit.SECONDS));
1316 assertTrue(2 == resourceList.size());
1318 OcResource res0 = resourceList.get(0);
1319 OcResource res1 = resourceList.get(1);
1320 assertFalse(res0.getUniqueIdentifier().equals(res1.getUniqueIdentifier()));
1321 assertTrue(res0.getUniqueIdentifier().equals(res0.getUniqueIdentifier()));
1324 OcPlatform.unregisterResource(resourceHandle1);
1325 OcPlatform.unregisterResource(resourceHandle2);
1327 } catch (OcException e) {
1328 Log.e(TAG, e.getMessage());
1333 public void testCreateResourceProxy() throws InterruptedException {
1334 final String resourceType = "unit.test.resource" + new Date().getTime();
1335 final CountDownLatch signal = new CountDownLatch(1);
1337 OcPlatform.OnResourceFoundListener resourceFoundListener =
1338 new OcPlatform.OnResourceFoundListener() {
1340 public void onResourceFound(OcResource resource) {
1343 //client: construct resource proxy
1344 OcResource resourceProxy = OcPlatform.constructResourceObject(
1347 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1348 resource.isObservable(),
1349 resource.getResourceTypes(),
1350 resource.getResourceInterfaces());
1352 //client: register resource proxy
1353 OcResourceHandle resourceProxyHandle =
1354 OcPlatform.registerResource(resourceProxy);
1356 OcPlatform.unregisterResource(resourceProxyHandle);
1357 } catch (OcException e) {
1366 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1369 OcPlatform.DEFAULT_INTERFACE,
1371 EnumSet.of(ResourceProperty.DISCOVERABLE)
1375 OcPlatform.findResource("",
1376 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1377 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1378 resourceFoundListener);
1380 //wait for onResourceFound event
1381 assertTrue(signal.await(60, TimeUnit.SECONDS));
1384 OcPlatform.unregisterResource(resourceHandle);
1386 } catch (OcException e) {
1387 Log.e(TAG, e.getMessage());
1393 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1396 OcPlatform.DEFAULT_INTERFACE,
1398 EnumSet.of(ResourceProperty.DISCOVERABLE)
1402 OcPlatform.findResource("",
1403 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1404 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1405 resourceFoundListener);
1407 //wait for onResourceFound event
1408 assertTrue(signal.await(60, TimeUnit.SECONDS));
1411 OcPlatform.unregisterResource(resourceHandle);
1413 } catch (OcException e) {
1414 Log.e(TAG, e.getMessage());
1419 public void testObserveClientServer() throws InterruptedException {
1420 final int NUM_OBSERVES = 20;
1421 final Timer timer = new Timer();
1422 final List<OcResource> resourceList = new LinkedList<OcResource>();
1423 final List<OcResourceHandle> resourceHandleList = new LinkedList<OcResourceHandle>();
1424 final CountDownLatch signal1 = new CountDownLatch(1);
1425 final CountDownLatch signal2 = new CountDownLatch(NUM_OBSERVES);
1426 final CountDownLatch signal3 = new CountDownLatch(1);
1428 final String resourceType = "unit.test.resource" + new Date().getTime();
1430 final OcResource.OnObserveListener observeListener = new OcResource.OnObserveListener() {
1432 public void onObserveCompleted(
1433 List<OcHeaderOption> headerOptionList,
1434 OcRepresentation ocRepresentation,
1435 int sequenceNumber) {
1438 Log.i(TAG, "Observe #" + sequenceNumber + " power: " +
1439 ocRepresentation.getValue("power"));
1440 } catch (OcException e) {
1441 Log.e(TAG, e.toString());
1444 signal2.countDown();
1448 public void onObserveFailed(Throwable ex) {
1449 if (ex instanceof OcException) {
1450 OcException ocEx = (OcException) ex;
1451 ErrorCode errCode = ocEx.getErrorCode();
1453 Log.e(TAG, ex.toString());
1458 final List<Byte> observationIdList = new LinkedList<Byte>();
1459 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1461 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1462 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
1463 RequestType requestType = ocResourceRequest.getRequestType();
1465 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
1468 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
1469 OcResourceResponse ocResourceResponse = new OcResourceResponse();
1470 ocResourceResponse.setRequestHandle(ocResourceRequest.getRequestHandle());
1471 ocResourceResponse.setResourceHandle(ocResourceRequest.getResourceHandle());
1473 switch (requestType) {
1475 ocResourceResponse.setErrorCode(200);
1476 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
1477 ocResourceResponse.setResourceRepresentation(
1481 OcPlatform.sendResponse(ocResourceResponse);
1482 } catch (OcException e) {
1483 Log.e(TAG, e.getMessage());
1484 return EntityHandlerResult.ERROR;
1490 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
1491 ObservationInfo observationInfo = ocResourceRequest.getObservationInfo();
1493 switch (observationInfo.getObserveAction()) {
1495 synchronized (observationIdList) {
1496 observationIdList.add(observationInfo.getOcObservationId());
1497 timer.schedule(new TimerTask() {
1498 int numNotified = 1;
1502 if (0 < resourceHandleList.size()) {
1503 synchronized (observationIdList) {
1504 if (numNotified > NUM_OBSERVES) {
1507 signal3.countDown();
1510 OcPlatform.notifyAllObservers(
1511 resourceHandleList.get(0));
1512 } catch (OcException e) {
1513 if (ErrorCode.NO_OBSERVERS == e.getErrorCode()) {
1516 signal3.countDown();
1518 Log.e(TAG, e.getMessage());
1530 //TODO unregister isn't implemented in C++ API, yet
1531 synchronized (observationIdList) {
1537 return EntityHandlerResult.OK;
1541 OcPlatform.OnResourceFoundListener resourceFoundListener =
1542 new OcPlatform.OnResourceFoundListener() {
1544 public void onResourceFound(OcResource resource) {
1545 resourceList.add(resource);
1546 if (resource.isObservable()) {
1549 ObserveType.OBSERVE,
1550 new HashMap<String, String>(),
1553 signal1.countDown();
1554 } catch (OcException e) {
1555 Log.e(TAG, e.getMessage());
1563 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1566 OcPlatform.DEFAULT_INTERFACE,
1568 EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
1571 resourceHandleList.add(resourceHandle);
1574 OcPlatform.findResource("",
1575 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1576 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1577 resourceFoundListener);
1579 //wait for onResourceFound event
1580 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1582 //wait for OnObserveListener event to observe 20 values
1583 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1585 if (resourceList.size() > 0) {
1586 OcResource resource = resourceList.get(0);
1587 if (resource.isObservable()) {
1588 resource.cancelObserve();
1592 //wait for server to finish
1593 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1596 OcPlatform.unregisterResource(resourceHandle);
1598 } catch (OcException e) {
1599 Log.e(TAG, e.getMessage());
1604 private OcRepresentation getRepresentation(int value) {
1605 OcRepresentation rep = new OcRepresentation();
1607 rep.setValue("power", value);
1608 } catch (OcException e) {
1609 Log.e(TAG, e.toString());
1615 private OcRepresentation getRepresentation() {
1616 return getRepresentation(74);