Merge "Partial Implementation of US1574:"
[platform/upstream/iotivity.git] / csdk / android / UnitTests / src / com / oc / unittests / UnitTest.java
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 package com.oc.unittests;
22
23 import static org.junit.Assert.*;
24 import junit.framework.Assert;
25
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29
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;
36
37 public class UnitTest {
38   private OCServer clientService = null;
39   private OCServer serverService = null;
40   private OCDiscovery ocDiscovery = null;
41   private UnitTestResource serverResource = null;
42
43   @Before
44   public void init() throws Exception {
45     clientService = new OCServer();
46     clientService.start();
47     ocDiscovery = clientService.getDiscoverySingleton();
48   }
49
50   @After
51   public void destroy() throws Exception {
52     try {
53       clientService.stop();
54       clientService = null;
55       serverService.unregisterResource(serverResource);
56       serverService.stop();
57     } catch (Exception exception) {
58       // TODO: Handle exception
59     }
60   }
61
62   @Test
63   public void findMyself() {
64     client();
65     server();
66   }
67
68   @SuppressWarnings("serial")
69   public void client() {
70     assertNotNull(clientService);
71
72     /*
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.
76      */
77     ocDiscovery.findResourceByType(null, null, new OCResourceResultHandler() {
78       @Override
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) {
85           try {
86             UnitTestControlItf control = resource.getInterface(UnitTestControlItf.class);
87             assertNotNull(control);
88             if (control != null) {
89               control.setState(true);
90               control.isTrue();
91
92               Assert.assertEquals(true, control.isTrue());
93
94               /*
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.
98                */
99               control.addMyUnitTestObserver(new OCObserver<Boolean>() {
100                 @Override
101                 public void onChanged(Boolean data) {
102                   // TODO: Tell the UI! The state of the Light has changed.
103
104                 }
105
106                 @Override
107                 public void onFailed(Throwable throwable) {
108                   // TODO: Tell the UI! We are no longer subscribed to events for the Light's power
109                   // state.
110                 }
111               });
112             }
113           } catch (Exception exception) {
114             // TODO: Ooops
115           }
116         }
117
118       }
119
120       @Override
121       public void onFailed(Throwable throwable) {
122         // TODO: Handle the error. The request to find resources has failed.
123       }
124
125       @Override
126       public void onCompleted() {
127         // Nothing to do here... we are just done finding resources is all.
128       }
129     });
130   }
131
132   public void server() {
133     // NOTE: We throw exceptions for the bad errors... so no return codes
134     try {
135       // In a complete example, the MyLight class should have callback to
136       // update the UI
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) {
146       // finish();
147       return; // End the application.
148     }
149   }
150 }