1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 package com.oc.unittests;
8 import static org.junit.Assert.*;
9 import junit.framework.Assert;
11 import org.junit.After;
12 import org.junit.Before;
13 import org.junit.Test;
15 import com.oc.OCDiscovery;
16 import com.oc.OCObserver;
17 import com.oc.OCResource;
18 import com.oc.OCResourceResultHandler;
19 import com.oc.OCSecurityModel;
20 import com.oc.OCServer;
22 public class UnitTest {
23 private OCServer clientService = null;
24 private OCServer serverService = null;
25 private OCDiscovery ocDiscovery = null;
26 private UnitTestResource serverResource = null;
29 public void init() throws Exception {
30 clientService = new OCServer();
31 clientService.start();
32 ocDiscovery = clientService.getDiscoverySingleton();
36 public void destroy() throws Exception {
40 serverService.unregisterResource(serverResource);
42 } catch (Exception exception) {
43 // TODO: Handle exception
48 public void findMyself() {
53 @SuppressWarnings("serial")
54 public void client() {
55 assertNotNull(clientService);
58 * APPLICATION NOTE: All callbacks will come through on a new thread and will not interfere with
59 * the UI thread or the Framework Thread (i.e. Networking Thread) which means that you need to
60 * use a Handler() to post back to the UI.
62 ocDiscovery.findResourceByType(null, null, new OCResourceResultHandler() {
64 public void onResourcesFound(String serviceURL, OCResource[] resources) {
65 // NOTE: This means that a service is reporting back zero or more resources
66 // During the callback, lets check to see if the resource supports the
67 // light control interface and turn the light on and then subscribe to any changes in the
68 // light's power state.
69 for (OCResource resource : resources) {
71 UnitTestControlItf control = resource.getInterface(UnitTestControlItf.class);
72 assertNotNull(control);
73 if (control != null) {
74 control.setState(true);
77 Assert.assertEquals(true, control.isTrue());
80 * APPLICATION NOTE: All callbacks will come through on a new thread and will not
81 * interfere with the UI thread or the Framework Thread (i.e. Networking Thread) which
82 * means that you need to use a Handler() to post back to the UI.
84 control.addMyUnitTestObserver(new OCObserver<Boolean>() {
86 public void onChanged(Boolean data) {
87 // TODO: Tell the UI! The state of the Light has changed.
92 public void onFailed(Throwable throwable) {
93 // TODO: Tell the UI! We are no longer subscribed to events for the Light's power
98 } catch (Exception exception) {
106 public void onFailed(Throwable throwable) {
107 // TODO: Handle the error. The request to find resources has failed.
111 public void onCompleted() {
112 // Nothing to do here... we are just done finding resources is all.
117 public void server() {
118 // NOTE: We throw exceptions for the bad errors... so no return codes
120 // In a complete example, the MyLight class should have callback to
122 serverResource = new UnitTestResource();
123 serverService = new OCServer();
124 serverService.setSecurityModel(new OCSecurityModel());
125 // TODO: Mats, will we allow one resource to be mapped to two URLs???
126 serverService.registerResource(serverResource, "/ResourceTypeGoesHere/a");
127 // TODO: Mats, will we allow one resource to be mapped to two URLs???
128 // service.registerResource(resource, "/light/b", new OCAccessHandler());
129 serverService.start();
130 } catch (Exception exception) {
132 return; // End the application.