1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 package com.oc.unittests;
23 import static org.junit.Assert.*;
24 import junit.framework.Assert;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
30 import com.oc.OCDiscovery;
31 import com.oc.OCObserver;
32 import com.oc.OCResource;
33 import com.oc.OCResourceResultHandler;
34 import com.oc.OCSecurityModel;
35 import com.oc.OCServer;
37 public class UnitTest {
38 private OCServer clientService = null;
39 private OCServer serverService = null;
40 private OCDiscovery ocDiscovery = null;
41 private UnitTestResource serverResource = null;
44 public void init() throws Exception {
45 clientService = new OCServer();
46 clientService.start();
47 ocDiscovery = clientService.getDiscoverySingleton();
51 public void destroy() throws Exception {
55 serverService.unregisterResource(serverResource);
57 } catch (Exception exception) {
58 // TODO: Handle exception
63 public void findMyself() {
68 @SuppressWarnings("serial")
69 public void client() {
70 assertNotNull(clientService);
73 * APPLICATION NOTE: All callbacks will come through on a new thread and will not interfere with
74 * the UI thread or the Framework Thread (i.e. Networking Thread) which means that you need to
75 * use a Handler() to post back to the UI.
77 ocDiscovery.findResourceByType(null, null, new OCResourceResultHandler() {
79 public void onResourcesFound(String serviceURL, OCResource[] resources) {
80 // NOTE: This means that a service is reporting back zero or more resources
81 // During the callback, lets check to see if the resource supports the
82 // light control interface and turn the light on and then subscribe to any changes in the
83 // light's power state.
84 for (OCResource resource : resources) {
86 UnitTestControlItf control = resource.getInterface(UnitTestControlItf.class);
87 assertNotNull(control);
88 if (control != null) {
89 control.setState(true);
92 Assert.assertEquals(true, control.isTrue());
95 * APPLICATION NOTE: All callbacks will come through on a new thread and will not
96 * interfere with the UI thread or the Framework Thread (i.e. Networking Thread) which
97 * means that you need to use a Handler() to post back to the UI.
99 control.addMyUnitTestObserver(new OCObserver<Boolean>() {
101 public void onChanged(Boolean data) {
102 // TODO: Tell the UI! The state of the Light has changed.
107 public void onFailed(Throwable throwable) {
108 // TODO: Tell the UI! We are no longer subscribed to events for the Light's power
113 } catch (Exception exception) {
121 public void onFailed(Throwable throwable) {
122 // TODO: Handle the error. The request to find resources has failed.
126 public void onCompleted() {
127 // Nothing to do here... we are just done finding resources is all.
132 public void server() {
133 // NOTE: We throw exceptions for the bad errors... so no return codes
135 // In a complete example, the MyLight class should have callback to
137 serverResource = new UnitTestResource();
138 serverService = new OCServer();
139 serverService.setSecurityModel(new OCSecurityModel());
140 // TODO: Mats, will we allow one resource to be mapped to two URLs???
141 serverService.registerResource(serverResource, "/ResourceTypeGoesHere/a");
142 // TODO: Mats, will we allow one resource to be mapped to two URLs???
143 // service.registerResource(resource, "/light/b", new OCAccessHandler());
144 serverService.start();
145 } catch (Exception exception) {
147 return; // End the application.