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);
90 public void onFindResourceFailed(Throwable ex, String uri) {
91 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
97 OcResourceHandle resourceHandle = OcPlatform.registerResource(
100 OcPlatform.DEFAULT_INTERFACE,
102 EnumSet.of(ResourceProperty.DISCOVERABLE)
106 OcPlatform.findResource("",
107 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
108 EnumSet.of(OcConnectivityType.CT_DEFAULT),
109 resourceFoundListener);
111 //wait for onResourceFound event
112 assertTrue(signal.await(60, TimeUnit.SECONDS));
115 OcPlatform.unregisterResource(resourceHandle);
116 } catch (OcException e) {
117 Log.e(TAG, e.getMessage());
122 public void testStartStopListenForPresence() throws InterruptedException {
123 final String resourceType = "unit.test.resource" +
124 new Date().getTime();
125 final CountDownLatch signal = new CountDownLatch(1);
127 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
129 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
130 return EntityHandlerResult.OK;
134 final OcPlatform.OnPresenceListener presenceListener = new OcPlatform.OnPresenceListener() {
136 public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress) {
137 Log.i(TAG, "onPresence status " + ocPresenceStatus.toString() + " nonce " + nonce);
142 OcPlatform.OnResourceFoundListener resourceFoundListener =
143 new OcPlatform.OnResourceFoundListener() {
145 public void onResourceFound(OcResource resource) {
148 OcPresenceHandle presenceHandle = OcPlatform.subscribePresence(
150 EnumSet.of(OcConnectivityType.CT_DEFAULT),
154 //wait for onPresence event
155 assertTrue(signal.await(60, TimeUnit.SECONDS));
158 OcPlatform.unsubscribePresence(presenceHandle);
159 } catch (OcException e) {
161 } catch (InterruptedException e) {
167 public void onFindResourceFailed(Throwable ex, String uri) {
168 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
174 OcResourceHandle resourceHandle = OcPlatform.registerResource(
177 OcPlatform.DEFAULT_INTERFACE,
179 EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
183 OcPlatform.findResource("",
184 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
185 EnumSet.of(OcConnectivityType.CT_DEFAULT),
186 resourceFoundListener);
189 //wait 2 seconds for the client's resourceFoundListener to set the presenceListener.
190 //the presenceListener must be set before startPresence() is called to get notified.
193 } catch (InterruptedException e) {
194 Log.e(TAG, e.getMessage());
196 OcPlatform.startPresence(OcPlatform.DEFAULT_PRESENCE_TTL);
198 //wait for onPresence event
199 assertTrue(signal.await(60, TimeUnit.SECONDS));
202 OcPlatform.stopPresence();
205 OcPlatform.unregisterResource(resourceHandle);
207 } catch (OcException e) {
208 Log.e(TAG, e.getMessage());
213 public void testHandleGetRequest() throws InterruptedException {
214 final String someKey = "SomeKey";
215 final String someValue = "SomeValue";
216 final String resourceType = "unit.test.resource" + new Date().getTime();
217 final CountDownLatch signal1 = new CountDownLatch(1);
218 final CountDownLatch signal2 = new CountDownLatch(1);
219 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
222 final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
224 public void onGetCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
225 if (!headerOptionList.isEmpty()) {
226 for (OcHeaderOption headerOption : headerOptionList) {
227 Log.i(TAG, "Header option " +
228 headerOption.getOptionId() +
230 headerOption.getOptionData());
234 Log.i(TAG, "Power: " + ocRepresentation.getValue("power"));
235 } catch (OcException e) {
236 Log.e(TAG, e.toString());
243 public void onGetFailed(Throwable ex) {
244 if (ex instanceof OcException) {
245 OcException ocEx = (OcException) ex;
246 ErrorCode errCode = ocEx.getErrorCode();
247 if (ErrorCode.NO_RESOURCE != errCode) {
248 Log.e(TAG, ocEx.getMessage());
252 Log.e(TAG, ex.getMessage());
259 final OcPlatform.OnResourceFoundListener resourceFoundListener =
260 new OcPlatform.OnResourceFoundListener() {
262 public void onResourceFound(OcResource resource) {
263 Map<String, String> queryParamsMap = new HashMap<String, String>();
264 queryParamsMap.put(someKey, someValue);
266 ocResourceList.add(resource);
268 resource.get(queryParamsMap, onGetListener);
269 // TODO there is a bug in the stack that prevents the usage of the following APIs
270 // resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE, queryParamsMap,
273 // resource.get(queryParamsMap, onGetListener, QualityOfService.LOW);
275 // resource.get(resourceType, OcPlatform.DEFAULT_INTERFACE,queryParamsMap,
276 // onGetListener, QualityOfService.LOW);
277 } catch (OcException e) {
278 Log.e(TAG, e.toString());
285 public void onFindResourceFailed(Throwable ex, String uri) {
286 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
292 OcResourceHandle resourceHandle = OcPlatform.registerResource(
295 OcPlatform.DEFAULT_INTERFACE,
296 new OcPlatform.EntityHandler() {
298 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
299 EnumSet<RequestHandlerFlag> handlerFlagSet =
300 ocResourceRequest.getRequestHandlerFlagSet();
302 RequestType requestType = ocResourceRequest.getRequestType();
304 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
306 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
307 OcResourceResponse ocResourceResponse = new OcResourceResponse();
308 ocResourceResponse.setRequestHandle(
309 ocResourceRequest.getRequestHandle());
310 ocResourceResponse.setResourceHandle(
311 ocResourceRequest.getResourceHandle());
313 switch (requestType) {
315 Map<String, String> queryParams =
316 ocResourceRequest.getQueryParameters();
318 if (!(queryParams.containsKey(someKey) &&
319 someValue.equals(queryParams.get(someKey)))) {
323 ocResourceResponse.setErrorCode(200);
324 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
325 ocResourceResponse.setResourceRepresentation(
326 getRepresentation(74));
331 OcPlatform.sendResponse(ocResourceResponse);
332 } catch (OcException e) {
333 Log.e(TAG, e.getMessage());
334 return EntityHandlerResult.ERROR;
337 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
339 return EntityHandlerResult.OK;
342 EnumSet.of(ResourceProperty.DISCOVERABLE)
346 OcPlatform.findResource(null,
347 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
348 EnumSet.of(OcConnectivityType.CT_DEFAULT),
349 resourceFoundListener);
351 //wait for onResourceFound event
352 assertTrue(signal1.await(60, TimeUnit.SECONDS));
354 //wait for onGetCompleted event
355 assertTrue(signal2.await(60, TimeUnit.SECONDS));
358 OcPlatform.unregisterResource(resourceHandle);
360 } catch (OcException e) {
361 Log.e(TAG, e.getMessage());
366 public void testHandlePutRequest() throws InterruptedException {
367 final String resourceType = "unit.test.resource" + new Date().getTime();
368 final CountDownLatch signal1 = new CountDownLatch(1);
369 final CountDownLatch signal2 = new CountDownLatch(3);
370 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
372 final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
374 public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
375 if (!headerOptionList.isEmpty()) {
376 for (OcHeaderOption headerOption : headerOptionList) {
377 Log.i(TAG, "Header option " +
378 headerOption.getOptionId() +
380 headerOption.getOptionData());
384 Log.i(TAG, "onPutCompleted Power: " + ocRepresentation.getValue("power"));
385 } catch (OcException e) {
386 Log.e(TAG, e.getMessage());
389 Log.i(TAG, "onPutCompleted Uri: " + ocRepresentation.getUri());
394 public void onPutFailed(Throwable ex) {
395 if (ex instanceof OcException) {
396 OcException ocEx = (OcException) ex;
397 ErrorCode errCode = ocEx.getErrorCode();
398 if (ErrorCode.NO_RESOURCE != errCode) {
399 Log.e(TAG, ocEx.getMessage());
403 Log.e(TAG, ex.getMessage());
409 final OcPlatform.OnResourceFoundListener resourceFoundListener =
410 new OcPlatform.OnResourceFoundListener() {
412 public void onResourceFound(OcResource resource) {
413 ocResourceList.add(resource);
417 new HashMap<String, String>(),
422 new HashMap<String, String>(),
427 new HashMap<String, String>(),
428 new OcResource.OnPutListener() {
430 public void onPutCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
435 public void onPutFailed(Throwable ex) {
436 if (ex instanceof OcException) {
437 OcException ocEx = (OcException) ex;
438 ErrorCode errCode = ocEx.getErrorCode();
439 if (ErrorCode.NO_RESOURCE != errCode) {
440 Log.e(TAG, ocEx.getMessage());
444 Log.e(TAG, ex.getMessage());
450 } catch (OcException e) {
457 public void onFindResourceFailed(Throwable ex, String uri) {
458 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
464 OcResourceHandle resourceHandle = OcPlatform.registerResource(
467 OcPlatform.DEFAULT_INTERFACE,
468 new OcPlatform.EntityHandler() {
470 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
471 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
472 RequestType requestType = ocResourceRequest.getRequestType();
474 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
476 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
477 OcResourceResponse ocResourceResponse = new OcResourceResponse();
478 ocResourceResponse.setRequestHandle(
479 ocResourceRequest.getRequestHandle());
480 ocResourceResponse.setResourceHandle(
481 ocResourceRequest.getResourceHandle());
483 switch (requestType) {
488 OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
490 Log.i(TAG, "Put res. power: " + rep.getValue("power"));
491 } catch (OcException e) {
492 Log.e(TAG, e.toString());
495 Log.i(TAG, "URI: " + rep.getUri());
497 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
498 ocResourceResponse.setErrorCode(200);
499 ocResourceResponse.setResourceRepresentation(rep);
509 OcPlatform.sendResponse(ocResourceResponse);
510 } catch (OcException e) {
511 Log.e(TAG, e.getMessage());
512 return EntityHandlerResult.ERROR;
515 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
517 return EntityHandlerResult.OK;
520 EnumSet.of(ResourceProperty.DISCOVERABLE)
524 OcPlatform.findResource("",
525 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
526 EnumSet.of(OcConnectivityType.CT_DEFAULT),
527 resourceFoundListener);
529 //wait for onResourceFound event
530 assertTrue(signal1.await(60, TimeUnit.SECONDS));
532 //wait for onGetCompleted event
533 assertTrue(signal2.await(60, TimeUnit.SECONDS));
536 OcPlatform.unregisterResource(resourceHandle);
538 } catch (OcException e) {
539 Log.e(TAG, e.getMessage());
544 public void testHandlePostRequest() throws InterruptedException {
545 final String resourceType = "unit.test.resource" + new Date().getTime();
546 final CountDownLatch signal1 = new CountDownLatch(1);
547 final CountDownLatch signal2 = new CountDownLatch(3);
548 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
550 final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
552 public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
553 if (!headerOptionList.isEmpty()) {
554 for (OcHeaderOption headerOption : headerOptionList) {
555 Log.i(TAG, "Header option " +
556 headerOption.getOptionId() +
558 headerOption.getOptionData());
562 Log.i(TAG, "onPostCompleted Power: " + ocRepresentation.getValue("power"));
563 } catch (OcException e) {
564 Log.e(TAG, e.toString());
567 Log.i(TAG, "onPostCompleted Uri: " + ocRepresentation.getUri());
572 public void onPostFailed(Throwable ex) {
573 if (ex instanceof OcException) {
574 OcException ocEx = (OcException) ex;
575 ErrorCode errCode = ocEx.getErrorCode();
576 if (ErrorCode.NO_RESOURCE != errCode) {
577 Log.e(TAG, ocEx.getMessage());
581 Log.e(TAG, ex.getMessage());
587 final OcPlatform.OnResourceFoundListener resourceFoundListener =
588 new OcPlatform.OnResourceFoundListener() {
590 public void onResourceFound(OcResource resource) {
591 ocResourceList.add(resource);
595 new HashMap<String, String>(),
600 new HashMap<String, String>(),
605 new HashMap<String, String>(),
606 new OcResource.OnPostListener() {
608 public void onPostCompleted(List<OcHeaderOption> headerOptionList, OcRepresentation ocRepresentation) {
613 public void onPostFailed(Throwable ex) {
614 if (ex instanceof OcException) {
615 OcException ocEx = (OcException) ex;
616 ErrorCode errCode = ocEx.getErrorCode();
617 if (ErrorCode.NO_RESOURCE != errCode) {
618 Log.e(TAG, ocEx.getMessage());
622 Log.e(TAG, ex.getMessage());
628 } catch (OcException e) {
635 public void onFindResourceFailed(Throwable ex, String uri) {
636 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
642 OcResourceHandle resourceHandle = OcPlatform.registerResource(
645 OcPlatform.DEFAULT_INTERFACE,
646 new OcPlatform.EntityHandler() {
648 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
649 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
650 RequestType requestType = ocResourceRequest.getRequestType();
652 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
654 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
655 OcResourceResponse ocResourceResponse = new OcResourceResponse();
656 ocResourceResponse.setRequestHandle(
657 ocResourceRequest.getRequestHandle());
658 ocResourceResponse.setResourceHandle(
659 ocResourceRequest.getResourceHandle());
661 switch (requestType) {
669 OcRepresentation rep = ocResourceRequest.getResourceRepresentation();
671 Log.i(TAG, "Post res. power: " + rep.getValue("power"));
672 } catch (OcException e) {
673 Log.e(TAG, e.toString());
676 Log.i(TAG, "URI: " + rep.getUri());
678 ocResourceResponse.setErrorCode(200);
679 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
680 ocResourceResponse.setResourceRepresentation(
681 getRepresentation(44));
690 OcPlatform.sendResponse(ocResourceResponse);
691 } catch (OcException e) {
692 Log.e(TAG, e.getMessage());
693 return EntityHandlerResult.ERROR;
696 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
698 return EntityHandlerResult.OK;
701 EnumSet.of(ResourceProperty.DISCOVERABLE)
705 OcPlatform.findResource("",
706 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
707 EnumSet.of(OcConnectivityType.CT_DEFAULT),
708 resourceFoundListener);
710 //wait for onResourceFound event
711 assertTrue(signal1.await(60, TimeUnit.SECONDS));
713 //wait for onPostCompleted event
714 assertTrue(signal2.await(60, TimeUnit.SECONDS));
717 OcPlatform.unregisterResource(resourceHandle);
719 } catch (OcException e) {
720 Log.e(TAG, e.getMessage());
726 public void testHandleDeleteRequest() throws InterruptedException {
727 final String resourceType = "unit.test.resource" + new Date().getTime();
728 final CountDownLatch signal1 = new CountDownLatch(1);
729 final CountDownLatch signal2 = new CountDownLatch(1);
730 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
732 final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
734 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
739 public void onDeleteFailed(Throwable ex) {
740 if (ex instanceof OcException) {
741 OcException ocEx = (OcException) ex;
742 ErrorCode errCode = ocEx.getErrorCode();
743 if (ErrorCode.NO_RESOURCE != errCode) {
744 Log.e(TAG, ocEx.getMessage());
748 Log.e(TAG, ex.getMessage());
754 final OcPlatform.OnResourceFoundListener resourceFoundListener =
755 new OcPlatform.OnResourceFoundListener() {
757 public void onResourceFound(OcResource resource) {
758 ocResourceList.add(resource);
760 resource.deleteResource(onDeleteListener);
761 } catch (OcException e) {
768 public void onFindResourceFailed(Throwable ex, String uri) {
769 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
775 OcResourceHandle resourceHandle = OcPlatform.registerResource(
778 OcPlatform.DEFAULT_INTERFACE,
779 new OcPlatform.EntityHandler() {
781 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
782 EnumSet<RequestHandlerFlag> handlerFlagSet =
783 ocResourceRequest.getRequestHandlerFlagSet();
784 RequestType requestType = ocResourceRequest.getRequestType();
786 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
788 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
789 OcResourceResponse ocResourceResponse = new OcResourceResponse();
790 ocResourceResponse.setRequestHandle(
791 ocResourceRequest.getRequestHandle());
792 ocResourceResponse.setResourceHandle(
793 ocResourceRequest.getResourceHandle());
795 switch (requestType) {
797 ocResourceResponse.setErrorCode(200);
798 ocResourceResponse.setResponseResult(
799 EntityHandlerResult.RESOURCE_DELETED);
804 OcPlatform.sendResponse(ocResourceResponse);
805 } catch (OcException e) {
806 Log.e(TAG, e.getMessage());
807 return EntityHandlerResult.ERROR;
810 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
812 return EntityHandlerResult.OK;
815 EnumSet.of(ResourceProperty.DISCOVERABLE)
819 OcPlatform.findResource("",
820 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
821 EnumSet.of(OcConnectivityType.CT_DEFAULT),
822 resourceFoundListener);
824 //wait for onResourceFound event
825 assertTrue(signal1.await(60, TimeUnit.SECONDS));
827 //wait for onDeleteCompleted event
828 assertTrue(signal2.await(60, TimeUnit.SECONDS));
831 OcPlatform.unregisterResource(resourceHandle);
833 } catch (OcException e) {
834 Log.e(TAG, e.getMessage());
839 public void testHandleGetPutPostDeleteFailures() throws InterruptedException {
840 final String resourceType = "unit.test.resource" + new Date().getTime();
841 final CountDownLatch signal1 = new CountDownLatch(1);
842 final CountDownLatch signal2 = new CountDownLatch(1);
843 final CountDownLatch signal3 = new CountDownLatch(1);
844 final CountDownLatch signal4 = new CountDownLatch(1);
845 final CountDownLatch signal5 = new CountDownLatch(1);
847 final List<OcResource> ocResourceList = new LinkedList<OcResource>();
848 final OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() {
850 public void onGetCompleted(List<OcHeaderOption> headerOptionList,
851 OcRepresentation ocRepresentation) {
856 public void onGetFailed(Throwable ex) {
857 if (ex instanceof OcException) {
858 OcException ocEx = (OcException) ex;
859 ErrorCode errCode = ocEx.getErrorCode();
861 Log.i(TAG, ex.toString());
866 final OcResource.OnPutListener onPutListener = new OcResource.OnPutListener() {
868 public void onPutCompleted(List<OcHeaderOption> headerOptionList,
869 OcRepresentation ocRepresentation) {
874 public void onPutFailed(Throwable ex) {
875 if (ex instanceof OcException) {
876 OcException ocEx = (OcException) ex;
877 ErrorCode errCode = ocEx.getErrorCode();
879 Log.i(TAG, ex.toString());
883 final OcResource.OnPostListener onPostListener = new OcResource.OnPostListener() {
885 public void onPostCompleted(List<OcHeaderOption> headerOptionList,
886 OcRepresentation ocRepresentation) {
891 public void onPostFailed(Throwable ex) {
892 if (ex instanceof OcException) {
893 OcException ocEx = (OcException) ex;
894 ErrorCode errCode = ocEx.getErrorCode();
896 Log.i(TAG, ex.toString());
901 final OcResource.OnDeleteListener onDeleteListener = new OcResource.OnDeleteListener() {
903 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList) {
908 public void onDeleteFailed(Throwable ex) {
909 if (ex instanceof OcException) {
910 OcException ocEx = (OcException) ex;
911 ErrorCode errCode = ocEx.getErrorCode();
913 Log.i(TAG, ex.toString());
918 final OcPlatform.OnResourceFoundListener resourceFoundListener =
919 new OcPlatform.OnResourceFoundListener() {
921 public void onResourceFound(OcResource resource) {
922 ocResourceList.add(resource);
924 resource.get(new HashMap<String, String>(), onGetListener);
925 resource.put(new OcRepresentation(), new HashMap<String, String>(),
927 resource.post(new OcRepresentation(), new HashMap<String, String>(),
929 resource.deleteResource(onDeleteListener);
930 } catch (OcException e) {
937 public void onFindResourceFailed(Throwable ex, String uri) {
938 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
944 OcResourceHandle resourceHandle = OcPlatform.registerResource(
947 OcPlatform.DEFAULT_INTERFACE,
948 new OcPlatform.EntityHandler() {
950 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
951 EnumSet<RequestHandlerFlag> handlerFlagSet =
952 ocResourceRequest.getRequestHandlerFlagSet();
953 RequestType requestType = ocResourceRequest.getRequestType();
955 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
957 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
958 OcResourceResponse ocResourceResponse = new OcResourceResponse();
959 ocResourceResponse.setRequestHandle(
960 ocResourceRequest.getRequestHandle());
961 ocResourceResponse.setResourceHandle(
962 ocResourceRequest.getResourceHandle());
964 switch (requestType) {
966 ocResourceResponse.setErrorCode(200);
967 ocResourceResponse.setResponseResult(
968 EntityHandlerResult.ERROR);
971 ocResourceResponse.setErrorCode(200);
972 ocResourceResponse.setResponseResult(
973 EntityHandlerResult.ERROR);
976 ocResourceResponse.setErrorCode(200);
977 ocResourceResponse.setResponseResult(
978 EntityHandlerResult.ERROR);
981 ocResourceResponse.setErrorCode(200);
982 ocResourceResponse.setResponseResult(
983 EntityHandlerResult.ERROR);
987 OcPlatform.sendResponse(ocResourceResponse);
988 } catch (OcException e) {
989 Log.e(TAG, e.getMessage());
990 return EntityHandlerResult.ERROR;
993 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
995 return EntityHandlerResult.OK;
998 EnumSet.of(ResourceProperty.DISCOVERABLE)
1002 OcPlatform.findResource("",
1003 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1004 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1005 resourceFoundListener);
1007 //wait for onResourceFound event
1008 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1009 //wait for onGetCompleted event
1010 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1011 //wait for onPutCompleted event
1012 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1013 //wait for onPostCompleted event
1014 assertTrue(signal4.await(60, TimeUnit.SECONDS));
1015 //wait for onDeleteCompleted event
1016 assertTrue(signal5.await(60, TimeUnit.SECONDS));
1019 OcPlatform.unregisterResource(resourceHandle);
1021 } catch (OcException e) {
1022 Log.e(TAG, e.getMessage());
1027 public void testPlatformInfo() throws InterruptedException {
1028 final String resourceType = "unit.test.resource" + new Date().getTime();
1029 final CountDownLatch signal = new CountDownLatch(1);
1031 OcPlatform.OnPlatformFoundListener platformFoundListener = new OcPlatform.OnPlatformFoundListener() {
1033 public void onPlatformFound(OcRepresentation ocRepresentation) {
1034 Log.i(TAG, "Platform Info Received: ");
1035 Log.i(TAG, "URI: " + ocRepresentation.getUri());
1040 OcPlatformInfo platformInfo = null;
1042 platformInfo = new OcPlatformInfo("myPlatformID", "myManuName", "myManuUrl");
1044 platformInfo.setModelNumber("myModelNumber");
1045 platformInfo.setDateOfManufacture("myDateOfManufacture");
1046 platformInfo.setPlatformVersion("myPlatformVersion");
1047 platformInfo.setOperatingSystemVersion("myOperatingSystemVersion");
1048 platformInfo.setHardwareVersion("myHardwareVersion");
1049 platformInfo.setFirmwareVersion("myFirmwareVersion");
1050 platformInfo.setSupportUrl("mySupportUrl");
1051 platformInfo.setSystemTime("mySystemTime");
1055 OcPlatform.registerPlatformInfo(platformInfo);
1058 OcPlatform.getPlatformInfo(
1060 OcPlatform.WELL_KNOWN_PLATFORM_QUERY,
1061 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1062 platformFoundListener);
1064 //wait for onPlatformFound event
1065 assertTrue(signal.await(60, TimeUnit.SECONDS));
1066 } catch (OcException e) {
1067 Log.e(TAG, e.getMessage() + platformInfo.toString());
1072 public void testRegisterDeviceInfoGetDeviceInfo() throws InterruptedException {
1073 final String resourceType = "unit.test.resource" + new Date().getTime();
1074 final CountDownLatch signal = new CountDownLatch(1);
1076 OcPlatform.OnDeviceFoundListener deviceFoundListener = new OcPlatform.OnDeviceFoundListener() {
1078 public void onDeviceFound(OcRepresentation ocRepresentation) {
1080 Log.i(TAG, "Device Name: " + ocRepresentation.getValue("n"));
1081 } catch (OcException e) {
1082 Log.e(TAG, e.toString());
1085 boolean hasDeviceNameAtr = ocRepresentation.hasAttribute("n");
1086 assertTrue(hasDeviceNameAtr);
1087 boolean hasNonExistingAtr = ocRepresentation.hasAttribute("NonExisting");
1088 assertFalse(hasNonExistingAtr);
1089 Log.i(TAG, "URI: " + ocRepresentation.getUri());
1094 OcDeviceInfo devInfo = new OcDeviceInfo(
1096 Arrays.asList(new String[]{"oic.d.test"})
1101 OcPlatform.registerDeviceInfo(devInfo);
1103 OcPlatform.getDeviceInfo(
1105 OcPlatform.WELL_KNOWN_DEVICE_QUERY,
1106 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1107 deviceFoundListener);
1109 //wait for onDeviceFound event
1110 assertTrue(signal.await(60, TimeUnit.SECONDS));
1112 } catch (OcException e) {
1113 Log.e(TAG, e.getMessage());
1118 public void testBindUnbindResources() throws InterruptedException {
1119 final String resourceType = "unit.test.resource" + new Date().getTime();
1120 final CountDownLatch signal1 = new CountDownLatch(3);
1121 final CountDownLatch signal2 = new CountDownLatch(2);
1122 final CountDownLatch signal3 = new CountDownLatch(1);
1123 final CountDownLatch signal4 = new CountDownLatch(3);
1124 final CountDownLatch signal5 = new CountDownLatch(3);
1125 final CountDownLatch signal6 = new CountDownLatch(1);
1127 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1129 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1130 return EntityHandlerResult.OK;
1134 OcPlatform.OnResourceFoundListener resourceFoundListener1 = new OcPlatform.OnResourceFoundListener() {
1136 public void onResourceFound(OcResource resource) {
1137 signal1.countDown();
1141 public void onFindResourceFailed(Throwable ex, String uri) {
1142 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1146 OcPlatform.OnResourceFoundListener resourceFoundListener2 = new OcPlatform.OnResourceFoundListener() {
1148 public void onResourceFound(OcResource resource) {
1149 signal2.countDown();
1153 public void onFindResourceFailed(Throwable ex, String uri) {
1154 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1158 OcPlatform.OnResourceFoundListener resourceFoundListener3 = new OcPlatform.OnResourceFoundListener() {
1160 public void onResourceFound(OcResource resource) {
1161 signal3.countDown();
1165 public void onFindResourceFailed(Throwable ex, String uri) {
1166 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1170 OcPlatform.OnResourceFoundListener resourceFoundListener4 = new OcPlatform.OnResourceFoundListener() {
1172 public void onResourceFound(OcResource resource) {
1173 signal4.countDown();
1177 public void onFindResourceFailed(Throwable ex, String uri) {
1178 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1182 OcPlatform.OnResourceFoundListener resourceFoundListener5 = new OcPlatform.OnResourceFoundListener() {
1184 public void onResourceFound(OcResource resource) {
1185 signal5.countDown();
1189 public void onFindResourceFailed(Throwable ex, String uri) {
1190 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1194 OcPlatform.OnResourceFoundListener resourceFoundListener6 = new OcPlatform.OnResourceFoundListener() {
1196 public void onResourceFound(OcResource resource) {
1197 signal6.countDown();
1201 public void onFindResourceFailed(Throwable ex, String uri) {
1202 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1209 OcResourceHandle resourceHandleCollection = OcPlatform.registerResource(
1212 OcPlatform.DEFAULT_INTERFACE,
1214 EnumSet.of(ResourceProperty.DISCOVERABLE)
1217 OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1220 OcPlatform.DEFAULT_INTERFACE,
1222 EnumSet.of(ResourceProperty.DISCOVERABLE)
1225 OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1228 OcPlatform.DEFAULT_INTERFACE,
1230 EnumSet.of(ResourceProperty.DISCOVERABLE)
1234 OcPlatform.findResource("",
1235 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1236 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1237 resourceFoundListener1);
1239 //wait for onResourceFound event to find 3 registered resources
1240 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1243 OcPlatform.bindResource(resourceHandleCollection, resourceHandle1);
1244 OcPlatform.bindResource(resourceHandleCollection, resourceHandle2);
1247 OcPlatform.findResource("",
1248 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1249 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1250 resourceFoundListener3);
1252 //wait for onResourceFound event to find 1 collection resources
1253 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1256 OcPlatform.unbindResource(resourceHandleCollection, resourceHandle1);
1259 OcPlatform.findResource("",
1260 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1261 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1262 resourceFoundListener2);
1264 //wait for onResourceFound event to find 2 resources
1265 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1268 OcPlatform.unbindResource(resourceHandleCollection, resourceHandle2);
1271 OcPlatform.findResource("",
1272 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1273 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1274 resourceFoundListener4);
1276 //wait for onResourceFound event to find 3 registered resources
1277 assertTrue(signal4.await(60, TimeUnit.SECONDS));
1279 //Bind/unbind a list of resource handles
1280 List<OcResourceHandle> resourceHandleList = new LinkedList<OcResourceHandle>();
1281 resourceHandleList.add(resourceHandle1);
1282 resourceHandleList.add(resourceHandle2);
1283 OcPlatform.bindResources(resourceHandleCollection, resourceHandleList);
1286 OcPlatform.findResource("",
1287 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1288 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1289 resourceFoundListener6);
1291 //wait for onResourceFound event to find 1 collection resources
1292 assertTrue(signal6.await(60, TimeUnit.SECONDS));
1294 OcPlatform.unbindResources(resourceHandleCollection, resourceHandleList);
1297 OcPlatform.findResource("",
1298 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1299 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1300 resourceFoundListener5);
1302 //wait for onResourceFound event to find 1 collection resources
1303 assertTrue(signal5.await(60, TimeUnit.SECONDS));
1306 OcPlatform.unregisterResource(resourceHandleCollection);
1307 OcPlatform.unregisterResource(resourceHandle1);
1308 OcPlatform.unregisterResource(resourceHandle2);
1309 } catch (OcException e) {
1310 Log.e(TAG, e.getMessage());
1315 public void testResourceMethods() throws InterruptedException {
1316 final String resourceType1 = "unit.test.resource" + new Date().getTime();
1317 final String resourceType2 = "unit.test.resource" + new Date().getTime();
1319 final CountDownLatch signal = new CountDownLatch(2);
1320 final List<OcResource> resourceList = new LinkedList<>();
1322 OcPlatform.OnResourceFoundListener resourceFoundListener =
1323 new OcPlatform.OnResourceFoundListener() {
1325 public synchronized void onResourceFound(OcResource resource) {
1326 resourceList.add(resource);
1327 Log.i(TAG, "Host: " + resource.getHost());
1328 Log.i(TAG, "Uri: " + resource.getUri());
1329 Log.i(TAG, "Observable: " + resource.isObservable());
1331 assertFalse(resource.getResourceTypes().isEmpty());
1332 for (String resourceType : resource.getResourceTypes()) {
1333 Log.i(TAG, "Type: " + resourceType);
1336 assertFalse(resource.getResourceTypes().isEmpty());
1337 for (String resourceInterface : resource.getResourceInterfaces()) {
1338 Log.i(TAG, "Interface: " + resourceInterface);
1341 List<OcHeaderOption> headerOptionList = new LinkedList<OcHeaderOption>();
1342 headerOptionList.add(new OcHeaderOption(2885, "OptionData1"));
1343 headerOptionList.add(new OcHeaderOption(2886, "OptionData2"));
1346 resource.setHeaderOptions(headerOptionList);
1347 } catch (OcException e) {
1348 Log.e(TAG, "onResourceFound, error in setHeaderOptions -- " + e.getMessage());
1351 resource.unsetHeaderOptions();
1353 OcResourceIdentifier resourceIdentifier = resource.getUniqueIdentifier();
1354 OcResourceIdentifier resourceIdentifier2 = resource.getUniqueIdentifier();
1355 assertTrue(resourceIdentifier.equals(resourceIdentifier2));
1361 public void onFindResourceFailed(Throwable ex, String uri) {
1362 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1368 OcResourceHandle resourceHandle1 = OcPlatform.registerResource(
1371 OcPlatform.DEFAULT_INTERFACE,
1373 EnumSet.of(ResourceProperty.DISCOVERABLE)
1376 OcResourceHandle resourceHandle2 = OcPlatform.registerResource(
1379 OcPlatform.DEFAULT_INTERFACE,
1381 EnumSet.of(ResourceProperty.DISCOVERABLE)
1385 OcPlatform.findResource("",
1386 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType1,
1387 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1388 resourceFoundListener);
1390 OcPlatform.findResource("",
1391 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType2,
1392 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1393 resourceFoundListener);
1395 //wait for onResourceFound event
1396 assertTrue(signal.await(60, TimeUnit.SECONDS));
1398 assertTrue(2 == resourceList.size());
1400 OcResource res0 = resourceList.get(0);
1401 OcResource res1 = resourceList.get(1);
1402 assertFalse(res0.getUniqueIdentifier().equals(res1.getUniqueIdentifier()));
1403 assertTrue(res0.getUniqueIdentifier().equals(res0.getUniqueIdentifier()));
1406 OcPlatform.unregisterResource(resourceHandle1);
1407 OcPlatform.unregisterResource(resourceHandle2);
1409 } catch (OcException e) {
1410 Log.e(TAG, e.getMessage());
1415 public void testCreateResourceProxy() throws InterruptedException {
1416 final String resourceType = "unit.test.resource" + new Date().getTime();
1417 final CountDownLatch signal = new CountDownLatch(1);
1419 OcPlatform.OnResourceFoundListener resourceFoundListener =
1420 new OcPlatform.OnResourceFoundListener() {
1422 public void onResourceFound(OcResource resource) {
1425 //client: construct resource proxy
1426 OcResource resourceProxy = OcPlatform.constructResourceObject(
1429 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1430 resource.isObservable(),
1431 resource.getResourceTypes(),
1432 resource.getResourceInterfaces());
1434 //client: register resource proxy
1435 OcResourceHandle resourceProxyHandle =
1436 OcPlatform.registerResource(resourceProxy);
1438 OcPlatform.unregisterResource(resourceProxyHandle);
1439 } catch (OcException e) {
1446 public void onFindResourceFailed(Throwable ex, String uri) {
1447 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1453 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1456 OcPlatform.DEFAULT_INTERFACE,
1458 EnumSet.of(ResourceProperty.DISCOVERABLE)
1462 OcPlatform.findResource("",
1463 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1464 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1465 resourceFoundListener);
1467 //wait for onResourceFound event
1468 assertTrue(signal.await(60, TimeUnit.SECONDS));
1471 OcPlatform.unregisterResource(resourceHandle);
1473 } catch (OcException e) {
1474 Log.e(TAG, e.getMessage());
1480 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1483 OcPlatform.DEFAULT_INTERFACE,
1485 EnumSet.of(ResourceProperty.DISCOVERABLE)
1489 OcPlatform.findResource("",
1490 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1491 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1492 resourceFoundListener);
1494 //wait for onResourceFound event
1495 assertTrue(signal.await(60, TimeUnit.SECONDS));
1498 OcPlatform.unregisterResource(resourceHandle);
1500 } catch (OcException e) {
1501 Log.e(TAG, e.getMessage());
1506 public void testObserveClientServer() throws InterruptedException {
1507 final int NUM_OBSERVES = 20;
1508 final Timer timer = new Timer();
1509 final List<OcResource> resourceList = new LinkedList<OcResource>();
1510 final List<OcResourceHandle> resourceHandleList = new LinkedList<OcResourceHandle>();
1511 final CountDownLatch signal1 = new CountDownLatch(1);
1512 final CountDownLatch signal2 = new CountDownLatch(NUM_OBSERVES);
1513 final CountDownLatch signal3 = new CountDownLatch(1);
1515 final String resourceType = "unit.test.resource" + new Date().getTime();
1517 final OcResource.OnObserveListener observeListener = new OcResource.OnObserveListener() {
1519 public void onObserveCompleted(
1520 List<OcHeaderOption> headerOptionList,
1521 OcRepresentation ocRepresentation,
1522 int sequenceNumber) {
1525 Log.i(TAG, "Observe #" + sequenceNumber + " power: " +
1526 ocRepresentation.getValue("power"));
1527 } catch (OcException e) {
1528 Log.e(TAG, e.toString());
1531 signal2.countDown();
1535 public void onObserveFailed(Throwable ex) {
1536 if (ex instanceof OcException) {
1537 OcException ocEx = (OcException) ex;
1538 ErrorCode errCode = ocEx.getErrorCode();
1540 Log.e(TAG, ex.toString());
1545 final List<Byte> observationIdList = new LinkedList<Byte>();
1546 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
1548 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
1549 EnumSet<RequestHandlerFlag> handlerFlagSet = ocResourceRequest.getRequestHandlerFlagSet();
1550 RequestType requestType = ocResourceRequest.getRequestType();
1552 if (handlerFlagSet.contains(RequestHandlerFlag.INIT)) {
1555 if (handlerFlagSet.contains(RequestHandlerFlag.REQUEST)) {
1556 OcResourceResponse ocResourceResponse = new OcResourceResponse();
1557 ocResourceResponse.setRequestHandle(ocResourceRequest.getRequestHandle());
1558 ocResourceResponse.setResourceHandle(ocResourceRequest.getResourceHandle());
1560 switch (requestType) {
1562 ocResourceResponse.setErrorCode(200);
1563 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
1564 ocResourceResponse.setResourceRepresentation(
1568 OcPlatform.sendResponse(ocResourceResponse);
1569 } catch (OcException e) {
1570 Log.e(TAG, e.getMessage());
1571 return EntityHandlerResult.ERROR;
1577 if (handlerFlagSet.contains(RequestHandlerFlag.OBSERVER)) {
1578 ObservationInfo observationInfo = ocResourceRequest.getObservationInfo();
1580 switch (observationInfo.getObserveAction()) {
1582 synchronized (observationIdList) {
1583 observationIdList.add(observationInfo.getOcObservationId());
1584 timer.schedule(new TimerTask() {
1585 int numNotified = 1;
1589 if (0 < resourceHandleList.size()) {
1590 synchronized (observationIdList) {
1591 if (numNotified > NUM_OBSERVES) {
1594 signal3.countDown();
1597 OcPlatform.notifyAllObservers(
1598 resourceHandleList.get(0));
1599 } catch (OcException e) {
1600 if (ErrorCode.NO_OBSERVERS == e.getErrorCode()) {
1603 signal3.countDown();
1605 Log.e(TAG, e.getMessage());
1617 //TODO unregister isn't implemented in C++ API, yet
1618 synchronized (observationIdList) {
1624 return EntityHandlerResult.OK;
1628 OcPlatform.OnResourceFoundListener resourceFoundListener =
1629 new OcPlatform.OnResourceFoundListener() {
1631 public void onResourceFound(OcResource resource) {
1632 resourceList.add(resource);
1633 if (resource.isObservable()) {
1636 ObserveType.OBSERVE,
1637 new HashMap<String, String>(),
1640 signal1.countDown();
1641 } catch (OcException e) {
1642 Log.e(TAG, e.getMessage());
1649 public void onFindResourceFailed(Throwable ex, String uri) {
1650 Log.i(TAG, "Find Resource Failed for Uri: " + uri + " Error: " + ex.getMessage());
1655 OcResourceHandle resourceHandle = OcPlatform.registerResource(
1658 OcPlatform.DEFAULT_INTERFACE,
1660 EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
1663 resourceHandleList.add(resourceHandle);
1666 OcPlatform.findResource("",
1667 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + resourceType,
1668 EnumSet.of(OcConnectivityType.CT_DEFAULT),
1669 resourceFoundListener);
1671 //wait for onResourceFound event
1672 assertTrue(signal1.await(60, TimeUnit.SECONDS));
1674 //wait for OnObserveListener event to observe 20 values
1675 assertTrue(signal2.await(60, TimeUnit.SECONDS));
1677 if (resourceList.size() > 0) {
1678 OcResource resource = resourceList.get(0);
1679 if (resource.isObservable()) {
1680 resource.cancelObserve();
1684 //wait for server to finish
1685 assertTrue(signal3.await(60, TimeUnit.SECONDS));
1688 OcPlatform.unregisterResource(resourceHandle);
1690 } catch (OcException e) {
1691 Log.e(TAG, e.getMessage());
1696 private OcRepresentation getRepresentation(int value) {
1697 OcRepresentation rep = new OcRepresentation();
1699 rep.setValue("power", value);
1700 } catch (OcException e) {
1701 Log.e(TAG, e.toString());
1707 private OcRepresentation getRepresentation() {
1708 return getRepresentation(74);