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 Types: ");
82 for (OcConnectivityType connectivityType : resource.getConnectivityTypeSet()) {
83 Log.i(TAG, " " + connectivityType);
91 OcResourceHandle resourceHandle = OcPlatform.registerResource(
94 OcPlatform.DEFAULT_INTERFACE,
96 EnumSet.of(ResourceProperty.DISCOVERABLE)
100 OcPlatform.findResource("",
101 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
102 EnumSet.of(OcConnectivityType.CT_DEFAULT),
103 resourceFoundListener);
105 //wait for onResourceFound event
106 assertTrue(signal.await(60, TimeUnit.SECONDS));
109 OcPlatform.unregisterResource(resourceHandle);
110 } catch (OcException e) {
111 Log.e(TAG, e.getMessage());
115 // TODO - this test fails currently
116 // public void testStartStopListenForPresence() throws InterruptedException {
117 // final String resourceType = "unit.test.resource" +
118 // new Date().getTime();
119 // final CountDownLatch signal = new CountDownLatch(1);
121 // OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
123 // public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
124 // return EntityHandlerResult.OK;
128 // final OcPlatform.OnPresenceListener presenceListener = new OcPlatform.OnPresenceListener() {
130 // public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress) {
131 // Log.i(TAG, "onPresence status " + ocPresenceStatus.toString() + " nonce " + nonce);
132 // signal.countDown();
136 // OcPlatform.OnResourceFoundListener resourceFoundListener =
137 // new OcPlatform.OnResourceFoundListener() {
139 // public void onResourceFound(OcResource resource) {
142 // OcPresenceHandle presenceHandle = OcPlatform.subscribePresence(
143 // resource.getHost(),
144 // EnumSet.of(OcConnectivityType.CT_DEFAULT),
148 // //wait for onPresence event
149 // assertTrue(signal.await(60, TimeUnit.SECONDS));
152 // OcPlatform.unsubscribePresence(presenceHandle);
153 // } catch (OcException e) {
154 // assertTrue(false);
155 // } catch (InterruptedException e) {
156 // assertTrue(false);
163 // OcResourceHandle resourceHandle = OcPlatform.registerResource(
166 // OcPlatform.DEFAULT_INTERFACE,
168 // EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
172 // OcPlatform.findResource("",
173 // OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
174 // EnumSet.of(OcConnectivityType.CT_DEFAULT),
175 // resourceFoundListener);
178 // OcPlatform.startPresence(OcPlatform.DEFAULT_PRESENCE_TTL);
180 // //wait for onPresence event
181 // assertTrue(signal.await(60, TimeUnit.SECONDS));
184 // OcPlatform.stopPresence();
187 // OcPlatform.unregisterResource(resourceHandle);
189 // } catch (OcException e) {
190 // Log.e(TAG, e.getMessage());
191 // assertTrue(false);
195 public void testHandleGetRequest() throws InterruptedException {
196 final String someKey = "SomeKey";
197 final String someValue = "SomeValue";
198 final String resourceType = "unit.test.resource" + new Date().getTime();
199 final CountDownLatch signal1 = new CountDownLatch(1);
200 final CountDownLatch signal2 = new CountDownLatch(1);
201 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
204 final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
206 public void onGetCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
207 if (!headerOptionList.isEmpty()) {
208 for (OcHeaderOption headerOption : headerOptionList) {
209 Log.i(TAG, "Header option " +
210 headerOption.getOptionId() +
212 headerOption.getOptionData());
216 Log.i(TAG, "Power: " + ocRepresentation.getValue("power"));
217 } catch (OcException e) {
218 Log.e(TAG, e.toString());
225 public void onGetFailed(Throwable ex) {
226 if (ex instanceof OcException) {
227 OcException ocEx = (OcException) ex;
228 ErrorCode errCode = ocEx.getErrorCode();
229 if (ErrorCode.NO_RESOURCE != errCode) {
230 Log.e(TAG, ocEx.getMessage());
234 Log.e(TAG, ex.getMessage());
241 final OcPlatform.OnResourceFoundListener resourceFoundListener =
242 new OcPlatform.OnResourceFoundListener() {
244 public void onResourceFound(OcResource resource) {
245 Map<String, String> queryParamsMap = new HashMap<String, String>();
246 queryParamsMap.put(someKey, someValue);
248 ocResourceList.add(resource);
250 resource.get(queryParamsMap, onGetListener);
251 //TODO there is a bug in the stack that prevents the usage of the following APIs
252 // resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE, queryParamsMap,
255 // resource.get(queryParamsMap, onGetListener, QualityOfService.LOW);
257 // resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE,queryParamsMap,
258 // onGetListener, QualityOfService.LOW);
259 } catch (OcException e) {
260 Log.e(TAG, e.toString());
269 OcResourceHandle resourceHandle = OcPlatform.registerResource(
272 OcPlatform.DEFAULT_INTERFACE,
273 new OcPlatform.EntityHandler() {
275 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
276 EnumSet<RequestHandlerFlag> handlerFlagSet =
277 ocResourceRequest.getRequestHandlerFlagSet();
279 RequestType requestType = ocResourceRequest.getRequestType();
281 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
283 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
284 OcResourceResponse ocResourceResponse = new OcResourceResponse();
285 ocResourceResponse.setRequestHandle(
286 ocResourceRequest.getRequestHandle());
287 ocResourceResponse.setResourceHandle(
288 ocResourceRequest.getResourceHandle());
290 switch (requestType) {
292 Map<String, String> queryParams =
293 ocResourceRequest.getQueryParameters();
294 //TODO after the merge with CA, the query params are missing
295 // if (!(queryParams.containsKey(someKey) &&
296 // someValue.equals(queryParams.get(someKey)))) {
297 // assertTrue(false);
300 ocResourceResponse.setErrorCode(200);
301 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
302 ocResourceResponse.setResourceRepresentation(
303 getRepresentation(74));
308 OcPlatform.sendResponse(ocResourceResponse);
309 } catch (OcException e) {
310 Log.e(TAG, e.getMessage());
311 return EntityHandlerResult.ERROR;
314 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
316 return EntityHandlerResult.OK;
319 EnumSet.of(ResourceProperty.DISCOVERABLE)
323 OcPlatform.findResource(null,
324 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
325 EnumSet.of(OcConnectivityType.CT_DEFAULT),
326 resourceFoundListener);
328 //wait for onResourceFound event
329 assertTrue(signal1.await(60, TimeUnit.SECONDS));
331 //wait for onGetCompleted event
332 assertTrue(signal2.await(60, TimeUnit.SECONDS));
335 OcPlatform.unregisterResource(resourceHandle);
337 } catch (OcException e) {
338 Log.e(TAG, e.getMessage());
343 public void testHandlePutRequest() throws InterruptedException {
344 final String resourceType = "unit.test.resource" + new Date().getTime();
345 final CountDownLatch signal1 = new CountDownLatch(1);
346 final CountDownLatch signal2 = new CountDownLatch(3);
347 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
349 final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
351 public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
352 if (!headerOptionList.isEmpty()) {
353 for (OcHeaderOption headerOption : headerOptionList) {
354 Log.i(TAG, "Header option " +
355 headerOption.getOptionId() +
357 headerOption.getOptionData());
361 Log.i(TAG, "onPutCompleted Power: " + ocRepresentation.getValue("power"));
362 } catch (OcException e) {
363 Log.e(TAG, e.getMessage());
366 Log.i(TAG, "onPutCompleted Uri: " + ocRepresentation.getUri());
371 public void onPutFailed(Throwable ex) {
372 if (ex instanceof OcException) {
373 OcException ocEx = (OcException) ex;
374 ErrorCode errCode = ocEx.getErrorCode();
375 if (ErrorCode.NO_RESOURCE != errCode) {
376 Log.e(TAG, ocEx.getMessage());
380 Log.e(TAG, ex.getMessage());
386 final OcPlatform.OnResourceFoundListener resourceFoundListener =
387 new OcPlatform.OnResourceFoundListener() {
389 public void onResourceFound(OcResource resource) {
390 ocResourceList.add(resource);
394 new HashMap<String, String>(),
399 new HashMap<String, String>(),
404 new HashMap<String, String>(),
405 new OcResource.OnPutListener() {
407 public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
412 public void onPutFailed(Throwable ex) {
413 if (ex instanceof OcException) {
414 OcException ocEx = (OcException) ex;
415 ErrorCode errCode = ocEx.getErrorCode();
416 if (ErrorCode.NO_RESOURCE != errCode) {
417 Log.e(TAG, ocEx.getMessage());
421 Log.e(TAG, ex.getMessage());
427 } catch (OcException e) {
436 OcResourceHandle resourceHandle = OcPlatform.registerResource(
439 OcPlatform.DEFAULT_INTERFACE,
440 new OcPlatform.EntityHandler() {
442 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
443 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
444 RequestType requestType = ocResourceRequest.getRequestType();
446 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
448 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
449 OcResourceResponse ocResourceResponse = new OcResourceResponse();
450 ocResourceResponse.setRequestHandle(
451 ocResourceRequest.getRequestHandle());
452 ocResourceResponse.setResourceHandle(
453 ocResourceRequest.getResourceHandle());
455 switch (requestType) {
460 OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
462 Log.i(TAG, "Put res. power: " + rep.getValue("power"));
463 } catch (OcException e) {
464 Log.e(TAG, e.toString());
467 Log.i(TAG, "URI: " + rep.getUri());
469 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
470 ocResourceResponse.setErrorCode(200);
471 ocResourceResponse.setResourceRepresentation(rep);
481 OcPlatform.sendResponse(ocResourceResponse);
482 } catch (OcException e) {
483 Log.e(TAG, e.getMessage());
484 return EntityHandlerResult.ERROR;
487 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
489 return EntityHandlerResult.OK;
492 EnumSet.of(ResourceProperty.DISCOVERABLE)
496 OcPlatform.findResource("",
497 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
498 EnumSet.of(OcConnectivityType.CT_DEFAULT),
499 resourceFoundListener);
501 //wait for onResourceFound event
502 assertTrue(signal1.await(60, TimeUnit.SECONDS));
504 //wait for onGetCompleted event
505 assertTrue(signal2.await(60, TimeUnit.SECONDS));
508 OcPlatform.unregisterResource(resourceHandle);
510 } catch (OcException e) {
511 Log.e(TAG, e.getMessage());
516 public void testHandlePostRequest() throws InterruptedException {
517 final String resourceType = "unit.test.resource" + new Date().getTime();
518 final CountDownLatch signal1 = new CountDownLatch(1);
519 final CountDownLatch signal2 = new CountDownLatch(3);
520 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
522 final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
524 public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
525 if (!headerOptionList.isEmpty()) {
526 for (OcHeaderOption headerOption : headerOptionList) {
527 Log.i(TAG, "Header option " +
528 headerOption.getOptionId() +
530 headerOption.getOptionData());
534 Log.i(TAG, "onPostCompleted Power: " + ocRepresentation.getValue("power"));
535 } catch (OcException e) {
536 Log.e(TAG, e.toString());
539 Log.i(TAG, "onPostCompleted Uri: " + ocRepresentation.getUri());
544 public void onPostFailed(Throwable ex) {
545 if (ex instanceof OcException) {
546 OcException ocEx = (OcException) ex;
547 ErrorCode errCode = ocEx.getErrorCode();
548 if (ErrorCode.NO_RESOURCE != errCode) {
549 Log.e(TAG, ocEx.getMessage());
553 Log.e(TAG, ex.getMessage());
559 final OcPlatform.OnResourceFoundListener resourceFoundListener =
560 new OcPlatform.OnResourceFoundListener() {
562 public void onResourceFound(OcResource resource) {
563 ocResourceList.add(resource);
567 new HashMap<String, String>(),
572 new HashMap<String, String>(),
577 new HashMap<String, String>(),
578 new OcResource.OnPostListener() {
580 public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
585 public void onPostFailed(Throwable ex) {
586 if (ex instanceof OcException) {
587 OcException ocEx = (OcException) ex;
588 ErrorCode errCode = ocEx.getErrorCode();
589 if (ErrorCode.NO_RESOURCE != errCode) {
590 Log.e(TAG, ocEx.getMessage());
594 Log.e(TAG, ex.getMessage());
600 } catch (OcException e) {
609 OcResourceHandle resourceHandle = OcPlatform.registerResource(
612 OcPlatform.DEFAULT_INTERFACE,
613 new OcPlatform.EntityHandler() {
615 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
616 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
617 RequestType requestType = ocResourceRequest.getRequestType();
619 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
621 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
622 OcResourceResponse ocResourceResponse = new OcResourceResponse();
623 ocResourceResponse.setRequestHandle(
624 ocResourceRequest.getRequestHandle());
625 ocResourceResponse.setResourceHandle(
626 ocResourceRequest.getResourceHandle());
628 switch (requestType) {
636 OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
638 Log.i(TAG, "Post res. power: " + rep.getValue("power"));
639 } catch (OcException e) {
640 Log.e(TAG, e.toString());
643 Log.i(TAG, "URI: " + rep.getUri());
645 ocResourceResponse.setErrorCode(200);
646 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
647 ocResourceResponse.setResourceRepresentation(
648 getRepresentation(44));
657 OcPlatform.sendResponse(ocResourceResponse);
658 } catch (OcException e) {
659 Log.e(TAG, e.getMessage());
660 return EntityHandlerResult.ERROR;
663 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
665 return EntityHandlerResult.OK;
668 EnumSet.of(ResourceProperty.DISCOVERABLE)
672 OcPlatform.findResource("",
673 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
674 EnumSet.of(OcConnectivityType.CT_DEFAULT),
675 resourceFoundListener);
677 //wait for onResourceFound event
678 assertTrue(signal1.await(60, TimeUnit.SECONDS));
680 //wait for onPostCompleted event
681 assertTrue(signal2.await(60, TimeUnit.SECONDS));
684 OcPlatform.unregisterResource(resourceHandle);
686 } catch (OcException e) {
687 Log.e(TAG, e.getMessage());
693 public void testHandleDeleteRequest() throws InterruptedException {
694 final String resourceType = "unit.test.resource" + new Date().getTime();
695 final CountDownLatch signal1 = new CountDownLatch(1);
696 final CountDownLatch signal2 = new CountDownLatch(1);
697 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
699 final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
701 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
706 public void onDeleteFailed(Throwable ex) {
707 if (ex instanceof OcException) {
708 OcException ocEx = (OcException) ex;
709 ErrorCode errCode = ocEx.getErrorCode();
710 if (ErrorCode.NO_RESOURCE != errCode) {
711 Log.e(TAG, ocEx.getMessage());
715 Log.e(TAG, ex.getMessage());
721 final OcPlatform.OnResourceFoundListener resourceFoundListener =
722 new OcPlatform.OnResourceFoundListener() {
724 public void onResourceFound(OcResource resource) {
725 ocResourceList.add(resource);
727 resource.deleteResource(onDeleteListener);
728 } catch (OcException e) {
737 OcResourceHandle resourceHandle = OcPlatform.registerResource(
740 OcPlatform.DEFAULT_INTERFACE,
741 new OcPlatform.EntityHandler() {
743 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
744 EnumSet<RequestHandlerFlag> handlerFlagSet =
745 ocResourceRequest.getRequestHandlerFlagSet();
746 RequestType requestType = ocResourceRequest.getRequestType();
748 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
750 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
751 OcResourceResponse ocResourceResponse = new OcResourceResponse();
752 ocResourceResponse.setRequestHandle(
753 ocResourceRequest.getRequestHandle());
754 ocResourceResponse.setResourceHandle(
755 ocResourceRequest.getResourceHandle());
757 switch (requestType) {
759 ocResourceResponse.setErrorCode(200);
760 ocResourceResponse.setResponseResult(
761 EntityHandlerResult.RESOURCE_DELETED);
766 OcPlatform.sendResponse(ocResourceResponse);
767 } catch (OcException e) {
768 Log.e(TAG, e.getMessage());
769 return EntityHandlerResult.ERROR;
772 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
774 return EntityHandlerResult.OK;
777 EnumSet.of(ResourceProperty.DISCOVERABLE)
781 OcPlatform.findResource("",
782 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
783 EnumSet.of(OcConnectivityType.CT_DEFAULT),
784 resourceFoundListener);
786 //wait for onResourceFound event
787 assertTrue(signal1.await(60, TimeUnit.SECONDS));
789 //wait for onDeleteCompleted event
790 assertTrue(signal2.await(60, TimeUnit.SECONDS));
793 OcPlatform.unregisterResource(resourceHandle);
795 } catch (OcException e) {
796 Log.e(TAG, e.getMessage());
801 public void testHandleGetPutPostDeleteFailures() throws InterruptedException {
802 final String resourceType = "unit.test.resource" + new Date().getTime();
803 final CountDownLatch signal1 = new CountDownLatch(1);
804 final CountDownLatch signal2 = new CountDownLatch(1);
805 final CountDownLatch signal3 = new CountDownLatch(1);
806 final CountDownLatch signal4 = new CountDownLatch(1);
807 final CountDownLatch signal5 = new CountDownLatch(1);
809 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
810 final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
812 public void onGetCompleted(List<OcHeaderOption> headerOptionList,
813 OcRepresentation ocRepresentation) {
818 public void onGetFailed(Throwable ex) {
819 if (ex instanceof OcException) {
820 OcException ocEx = (OcException) ex;
821 ErrorCode errCode = ocEx.getErrorCode();
823 Log.i(TAG, ex.toString());
828 final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
830 public void onPutCompleted(List<OcHeaderOption> headerOptionList,
831 OcRepresentation ocRepresentation) {
836 public void onPutFailed(Throwable ex) {
837 if (ex instanceof OcException) {
838 OcException ocEx = (OcException) ex;
839 ErrorCode errCode = ocEx.getErrorCode();
841 Log.i(TAG, ex.toString());
845 final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
847 public void onPostCompleted(List<OcHeaderOption> headerOptionList,
848 OcRepresentation ocRepresentation) {
853 public void onPostFailed(Throwable ex) {
854 if (ex instanceof OcException) {
855 OcException ocEx = (OcException) ex;
856 ErrorCode errCode = ocEx.getErrorCode();
858 Log.i(TAG, ex.toString());
863 final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
865 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
870 public void onDeleteFailed(Throwable ex) {
871 if (ex instanceof OcException) {
872 OcException ocEx = (OcException) ex;
873 ErrorCode errCode = ocEx.getErrorCode();
875 Log.i(TAG, ex.toString());
880 final OcPlatform.OnResourceFoundListener resourceFoundListener =
881 new OcPlatform.OnResourceFoundListener() {
883 public void onResourceFound(OcResource resource) {
884 ocResourceList.add(resource);
886 resource.get(new HashMap<String, String>(), onGetListener);
887 resource.put(new OcRepresentation(), new HashMap<String, String>(),
889 resource.post(new OcRepresentation(), new HashMap<String, String>(),
891 resource.deleteResource(onDeleteListener);
892 } catch (OcException e) {
901 OcResourceHandle resourceHandle = OcPlatform.registerResource(
904 OcPlatform.DEFAULT_INTERFACE,
905 new OcPlatform.EntityHandler() {
907 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
908 EnumSet<RequestHandlerFlag> handlerFlagSet =
909 ocResourceRequest.getRequestHandlerFlagSet();
910 RequestType requestType = ocResourceRequest.getRequestType();
912 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
914 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
915 OcResourceResponse ocResourceResponse = new OcResourceResponse();
916 ocResourceResponse.setRequestHandle(
917 ocResourceRequest.getRequestHandle());
918 ocResourceResponse.setResourceHandle(
919 ocResourceRequest.getResourceHandle());
921 switch (requestType) {
923 ocResourceResponse.setErrorCode(200);
924 ocResourceResponse.setResponseResult(
925 EntityHandlerResult.ERROR);
928 ocResourceResponse.setErrorCode(200);
929 ocResourceResponse.setResponseResult(
930 EntityHandlerResult.ERROR);
933 ocResourceResponse.setErrorCode(200);
934 ocResourceResponse.setResponseResult(
935 EntityHandlerResult.ERROR);
938 ocResourceResponse.setErrorCode(200);
939 ocResourceResponse.setResponseResult(
940 EntityHandlerResult.ERROR);
944 OcPlatform.sendResponse(ocResourceResponse);
945 } catch (OcException e) {
946 Log.e(TAG, e.getMessage());
947 return EntityHandlerResult.ERROR;
950 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
952 return EntityHandlerResult.OK;
955 EnumSet.of(ResourceProperty.DISCOVERABLE)
959 OcPlatform.findResource("",
960 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
961 EnumSet.of(OcConnectivityType.CT_DEFAULT),
962 resourceFoundListener);
964 //wait for onResourceFound event
965 assertTrue(signal1.await(60, TimeUnit.SECONDS));
966 //wait for onGetCompleted event
967 assertTrue(signal2.await(60, TimeUnit.SECONDS));
968 //wait for onPutCompleted event
969 assertTrue(signal3.await(60, TimeUnit.SECONDS));
970 //wait for onPostCompleted event
971 assertTrue(signal4.await(60, TimeUnit.SECONDS));
972 //wait for onDeleteCompleted event
973 assertTrue(signal5.await(60, TimeUnit.SECONDS));
976 OcPlatform.unregisterResource(resourceHandle);
978 } catch (OcException e) {
979 Log.e(TAG, e.getMessage());
984 public void testPlatformInfo() throws InterruptedException {
985 final String resourceType = "unit.test.resource" + new Date().getTime();
986 final CountDownLatch signal = new CountDownLatch(1);
988 OcPlatform.OnPlatformFoundListener platformFoundListener = new OcPlatform.OnPlatformFoundListener() {
990 public void onPlatformFound(OcRepresentation ocRepresentation) {
991 Log.i(TAG, "Platform Info Received: ");
992 Log.i(TAG, "URI: " + ocRepresentation.getUri());
997 OcPlatformInfo platformInfo = null;
999 platformInfo = new OcPlatformInfo("myPlatformID", "myManuName", "myManuUrl");
1000 } catch (OcException e) {
1001 Log.e(TAG, "Could not construct platformInfo. " + e.getMessage());
1005 platformInfo.setModelNumber("myModelNumber");
1006 platformInfo.setDateOfManufacture("myDateOfManufacture");
1007 platformInfo.setPlatformVersion("myPlatformVersion");
1008 platformInfo.setOperatingSystemVersion("myOperatingSystemVersion");
1009 platformInfo.setHardwareVersion("myHardwareVersion");
1010 platformInfo.setFirmwareVersion("myFirmwareVersion");
1011 platformInfo.setSupportUrl("mySupportUrl");
1012 platformInfo.setSystemTime("mySystemTime");
1017 OcPlatform.registerPlatformInfo(platformInfo);
1020 OcPlatform.getPlatformInfo(
1022 OcPlatform.MULTICAST_PREFIX + "/oic/p",
1023 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1024 platformFoundListener);
1026 //wait for onPlatformFound event
1027 assertTrue(signal.await(60, TimeUnit.SECONDS));
1028 } catch (OcException e) {
1029 Log.e(TAG, e.getMessage() + platformInfo.toString());
1034 public void testRegisterDeviceInfoGetDeviceInfo() throws InterruptedException {
1035 final String resourceType = "unit.test.resource" + new Date().getTime();
1036 final CountDownLatch signal = new CountDownLatch(1);
1038 OcPlatform.OnDeviceFoundListener deviceFoundListener = new OcPlatform.OnDeviceFoundListener() {
1040 public void onDeviceFound(OcRepresentation ocRepresentation) {
1042 Log.i(TAG, "Device Name: " + ocRepresentation.getValue("n"));
1043 } catch (OcException e) {
1044 Log.e(TAG, e.toString());
1047 boolean hasDeviceNameAtr = ocRepresentation.hasAttribute("n");
1048 assertTrue(hasDeviceNameAtr);
1049 boolean hasNonExistingAtr = ocRepresentation.hasAttribute("NonExisting");
1050 assertFalse(hasNonExistingAtr);
1051 Log.i(TAG, "URI: " + ocRepresentation.getUri());
1056 OcDeviceInfo devInfo = new OcDeviceInfo();
1057 devInfo.setDeviceName("myDeviceName");
1061 OcPlatform.registerDeviceInfo(devInfo);
1064 OcPlatform.getDeviceInfo(
1066 OcPlatform.MULTICAST_PREFIX + OcPlatform.DEVICE_URI,
1067 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1068 deviceFoundListener);
1070 //wait for onDeviceFound event
1071 assertTrue(signal.await(60, TimeUnit.SECONDS));
1073 } catch (OcException e) {
1074 Log.e(TAG, e.getMessage());
1079 public void testBindUnbindResources() throws InterruptedException {
1080 final String resourceType = "unit.test.resource" + new Date().getTime();
1081 final CountDownLatch signal1 = new CountDownLatch(3);
1082 final CountDownLatch signal2 = new CountDownLatch(2);
1083 final CountDownLatch signal3 = new CountDownLatch(1);
1084 final CountDownLatch signal4 = new CountDownLatch(3);
1085 final CountDownLatch signal5 = new CountDownLatch(3);
1086 final CountDownLatch signal6 = new CountDownLatch(1);
1088 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1090 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1091 return EntityHandlerResult.OK;
1095 OcPlatform.OnResourceFoundListener resourceFoundListener1 = new OcPlatform.OnResourceFoundListener() {
1097 public void onResourceFound(OcResource resource) {
1098 signal1.countDown();
1102 OcPlatform.OnResourceFoundListener resourceFoundListener2 = new OcPlatform.OnResourceFoundListener() {
1104 public void onResourceFound(OcResource resource) {
1105 signal2.countDown();
1109 OcPlatform.OnResourceFoundListener resourceFoundListener3 = new OcPlatform.OnResourceFoundListener() {
1111 public void onResourceFound(OcResource resource) {
1112 signal3.countDown();
1116 OcPlatform.OnResourceFoundListener resourceFoundListener4 = new OcPlatform.OnResourceFoundListener() {
1118 public void onResourceFound(OcResource resource) {
1119 signal4.countDown();
1123 OcPlatform.OnResourceFoundListener resourceFoundListener5 = new OcPlatform.OnResourceFoundListener() {
1125 public void onResourceFound(OcResource resource) {
1126 signal5.countDown();
1130 OcPlatform.OnResourceFoundListener resourceFoundListener6 = new OcPlatform.OnResourceFoundListener() {
1132 public void onResourceFound(OcResource resource) {
1133 signal6.countDown();
1140 OcResourceHandle resourceHandleCollection = OcPlatform.registerResource(
1143 OcPlatform.DEFAULT_INTERFACE,
1145 EnumSet.of(ResourceProperty.DISCOVERABLE)
1148 OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1151 OcPlatform.DEFAULT_INTERFACE,
1153 EnumSet.of(ResourceProperty.DISCOVERABLE)
1156 OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1159 OcPlatform.DEFAULT_INTERFACE,
1161 EnumSet.of(ResourceProperty.DISCOVERABLE)
1165 OcPlatform.findResource("",
1166 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1167 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1168 resourceFoundListener1);
1170 //wait for onResourceFound event to find 3 registered resources
1171 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1174 OcPlatform.bindResource(resourceHandleCollection, resourceHandle1);
1175 OcPlatform.bindResource(resourceHandleCollection, resourceHandle2);
1178 OcPlatform.findResource("",
1179 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1180 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1181 resourceFoundListener3);
1183 //wait for onResourceFound event to find 1 collection resources
1184 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1187 OcPlatform.unbindResource(resourceHandleCollection, resourceHandle1);
1190 OcPlatform.findResource("",
1191 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1192 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1193 resourceFoundListener2);
1195 //wait for onResourceFound event to find 2 resources
1196 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1199 OcPlatform.unbindResource(resourceHandleCollection, resourceHandle2);
1202 OcPlatform.findResource("",
1203 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1204 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1205 resourceFoundListener4);
1207 //wait for onResourceFound event to find 3 registered resources
1208 assertTrue(signal4.await(60, TimeUnit.SECONDS));
1210 //Bind/unbind a list of resource handles
1211 List<OcResourceHandle> resourceHandleList = new LinkedList<OcResourceHandle>();
1212 resourceHandleList.add(resourceHandle1);
1213 resourceHandleList.add(resourceHandle2);
1214 OcPlatform.bindResources(resourceHandleCollection, resourceHandleList);
1217 OcPlatform.findResource("",
1218 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1219 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1220 resourceFoundListener6);
1222 //wait for onResourceFound event to find 1 collection resources
1223 assertTrue(signal6.await(60, TimeUnit.SECONDS));
1225 OcPlatform.unbindResources(resourceHandleCollection, resourceHandleList);
1228 OcPlatform.findResource("",
1229 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1230 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1231 resourceFoundListener5);
1233 //wait for onResourceFound event to find 1 collection resources
1234 assertTrue(signal5.await(60, TimeUnit.SECONDS));
1237 OcPlatform.unregisterResource(resourceHandleCollection);
1238 OcPlatform.unregisterResource(resourceHandle1);
1239 OcPlatform.unregisterResource(resourceHandle2);
1240 } catch (OcException e) {
1241 Log.e(TAG, e.getMessage());
1246 public void testResourceMethods() throws InterruptedException {
1247 final String resourceType1 = "unit.test.resource" + new Date().getTime();
1248 final String resourceType2 = "unit.test.resource" + new Date().getTime();
1250 final CountDownLatch signal = new CountDownLatch(2);
1251 final List<OcResource> resourceList = new LinkedList<OcResource>();
1253 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1255 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1256 return EntityHandlerResult.OK;
1260 OcPlatform.OnResourceFoundListener resourceFoundListener =
1261 new OcPlatform.OnResourceFoundListener() {
1263 public void onResourceFound(OcResource resource) {
1264 resourceList.add(resource);
1265 Log.i(TAG, "Host: " + resource.getHost());
1266 Log.i(TAG, "Uri: " + resource.getUri());
1267 Log.i(TAG, "Observable: " + resource.isObservable());
1269 for (String resourceType : resource.getResourceTypes()) {
1270 Log.i(TAG, "Type: " + resourceType);
1273 for (String resourceInterface : resource.getResourceInterfaces()) {
1274 Log.i(TAG, "Interface: " + resourceInterface);
1277 List<OcHeaderOption> headerOptionList = new LinkedList<OcHeaderOption>();
1278 headerOptionList.add(new OcHeaderOption(2885, "OptionData1"));
1279 headerOptionList.add(new OcHeaderOption(2886, "OptionData2"));
1280 resource.setHeaderOptions(headerOptionList);
1282 resource.setHeaderOptions(headerOptionList);
1283 resource.unsetHeaderOptions();
1285 OcResourceIdentifier resourceIdentifier = resource.getUniqueIdentifier();
1286 OcResourceIdentifier resourceIdentifier2 = resource.getUniqueIdentifier();
1287 assertTrue(resourceIdentifier.equals(resourceIdentifier2));
1295 OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1298 OcPlatform.DEFAULT_INTERFACE,
1300 EnumSet.of(ResourceProperty.DISCOVERABLE)
1303 OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1306 OcPlatform.DEFAULT_INTERFACE,
1308 EnumSet.of(ResourceProperty.DISCOVERABLE)
1312 OcPlatform.findResource("",
1313 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType1,
1314 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1315 resourceFoundListener);
1317 OcPlatform.findResource("",
1318 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType2,
1319 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1320 resourceFoundListener);
1322 //wait for onResourceFound event
1323 assertTrue(signal.await(60, TimeUnit.SECONDS));
1325 assertTrue(2 == resourceList.size());
1327 OcResource res0 = resourceList.get(0);
1328 OcResource res1 = resourceList.get(1);
1329 assertFalse(res0.getUniqueIdentifier().equals(res1.getUniqueIdentifier()));
1330 assertTrue(res0.getUniqueIdentifier().equals(res0.getUniqueIdentifier()));
1333 OcPlatform.unregisterResource(resourceHandle1);
1334 OcPlatform.unregisterResource(resourceHandle2);
1336 } catch (OcException e) {
1337 Log.e(TAG, e.getMessage());
1342 public void testCreateResourceProxy() throws InterruptedException {
1343 final String resourceType = "unit.test.resource" + new Date().getTime();
1344 final CountDownLatch signal = new CountDownLatch(1);
1346 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1348 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1349 return EntityHandlerResult.OK;
1353 OcPlatform.OnResourceFoundListener resourceFoundListener =
1354 new OcPlatform.OnResourceFoundListener() {
1356 public void onResourceFound(OcResource resource) {
1359 //client: construct resource proxy
1360 OcResource resourceProxy = OcPlatform.constructResourceObject(
1363 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1364 resource.isObservable(),
1365 resource.getResourceTypes(),
1366 resource.getResourceInterfaces());
1368 //client: register resource proxy
1369 OcResourceHandle resourceProxyHandle =
1370 OcPlatform.registerResource(resourceProxy);
1372 OcPlatform.unregisterResource(resourceProxyHandle);
1373 } catch (OcException e) {
1382 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1385 OcPlatform.DEFAULT_INTERFACE,
1387 EnumSet.of(ResourceProperty.DISCOVERABLE)
1391 OcPlatform.findResource("",
1392 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1393 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1394 resourceFoundListener);
1396 //wait for onResourceFound event
1397 assertTrue(signal.await(60, TimeUnit.SECONDS));
1400 OcPlatform.unregisterResource(resourceHandle);
1402 } catch (OcException e) {
1403 Log.e(TAG, e.getMessage());
1409 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1412 OcPlatform.DEFAULT_INTERFACE,
1414 EnumSet.of(ResourceProperty.DISCOVERABLE)
1418 OcPlatform.findResource("",
1419 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1420 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1421 resourceFoundListener);
1423 //wait for onResourceFound event
1424 assertTrue(signal.await(60, TimeUnit.SECONDS));
1427 OcPlatform.unregisterResource(resourceHandle);
1429 } catch (OcException e) {
1430 Log.e(TAG, e.getMessage());
1435 public void testObserveClientServer() throws InterruptedException {
1436 final int NUM_OBSERVES = 20;
1437 final Timer timer = new Timer();
1438 final List<OcResource> resourceList = new LinkedList<OcResource>();
1439 final List<OcResourceHandle> resourceHandleList = new LinkedList<OcResourceHandle>();
1440 final CountDownLatch signal1 = new CountDownLatch(1);
1441 final CountDownLatch signal2 = new CountDownLatch(NUM_OBSERVES);
1442 final CountDownLatch signal3 = new CountDownLatch(1);
1444 final String resourceType = "unit.test.resource" + new Date().getTime();
1446 final OcResource.OnObserveListener observeListener = new OcResource.OnObserveListener() {
1448 public void onObserveCompleted(
1449 List<OcHeaderOption> headerOptionList,
1450 OcRepresentation ocRepresentation,
1451 int sequenceNumber) {
1454 Log.i(TAG, "Observe #" + sequenceNumber + " power: " +
1455 ocRepresentation.getValue("power"));
1456 } catch (OcException e) {
1457 Log.e(TAG, e.toString());
1460 signal2.countDown();
1464 public void onObserveFailed(Throwable ex) {
1465 if (ex instanceof OcException) {
1466 OcException ocEx = (OcException) ex;
1467 ErrorCode errCode = ocEx.getErrorCode();
1469 Log.e(TAG, ex.toString());
1474 final List<Byte> observationIdList = new LinkedList<Byte>();
1475 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1477 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1478 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
1479 RequestType requestType = ocResourceRequest.getRequestType();
1481 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
1484 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
1485 OcResourceResponse ocResourceResponse = new OcResourceResponse();
1486 ocResourceResponse.setRequestHandle(ocResourceRequest.getRequestHandle());
1487 ocResourceResponse.setResourceHandle(ocResourceRequest.getResourceHandle());
1489 switch (requestType) {
1491 ocResourceResponse.setErrorCode(200);
1492 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
1493 ocResourceResponse.setResourceRepresentation(
1497 OcPlatform.sendResponse(ocResourceResponse);
1498 } catch (OcException e) {
1499 Log.e(TAG, e.getMessage());
1500 return EntityHandlerResult.ERROR;
1506 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
1507 ObservationInfo observationInfo = ocResourceRequest.getObservationInfo();
1509 switch (observationInfo.getObserveAction()) {
1511 synchronized (observationIdList) {
1512 observationIdList.add(observationInfo.getOcObservationId());
1513 timer.schedule(new TimerTask() {
1514 int numNotified = 1;
1518 if (0 < resourceHandleList.size()) {
1519 synchronized (observationIdList) {
1520 if (numNotified > NUM_OBSERVES) {
1523 signal3.countDown();
1526 OcPlatform.notifyAllObservers(
1527 resourceHandleList.get(0));
1528 } catch (OcException e) {
1529 if (ErrorCode.NO_OBSERVERS == e.getErrorCode()) {
1532 signal3.countDown();
1534 Log.e(TAG, e.getMessage());
1546 //TODO unregister isn't implemented in C++ API, yet
1547 synchronized (observationIdList) {
1553 return EntityHandlerResult.OK;
1557 OcPlatform.OnResourceFoundListener resourceFoundListener =
1558 new OcPlatform.OnResourceFoundListener() {
1560 public void onResourceFound(OcResource resource) {
1561 resourceList.add(resource);
1562 if (resource.isObservable()) {
1565 ObserveType.OBSERVE,
1566 new HashMap<String, String>(),
1569 signal1.countDown();
1570 } catch (OcException e) {
1571 Log.e(TAG, e.getMessage());
1579 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1582 OcPlatform.DEFAULT_INTERFACE,
1584 EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
1587 resourceHandleList.add(resourceHandle);
1590 OcPlatform.findResource("",
1591 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1592 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1593 resourceFoundListener);
1595 //wait for onResourceFound event
1596 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1598 //wait for OnObserveListener event to observe 20 values
1599 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1601 if (resourceList.size() > 0) {
1602 OcResource resource = resourceList.get(0);
1603 if (resource.isObservable()) {
1604 resource.cancelObserve();
1608 //wait for server to finish
1609 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1612 OcPlatform.unregisterResource(resourceHandle);
1614 } catch (OcException e) {
1615 Log.e(TAG, e.getMessage());
1620 private OcRepresentation getRepresentation(int value) {
1621 OcRepresentation rep = new OcRepresentation();
1623 rep.setValue("power", value);
1624 } catch (OcException e) {
1625 Log.e(TAG, e.toString());
1631 private OcRepresentation getRepresentation() {
1632 return getRepresentation(74);