Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / android / service / src / androidTest / java / org / iotivity / service / TestBase.java
1 package org.iotivity.service;
2
3 import org.iotivity.service.client.RcsAddress;
4 import org.iotivity.service.client.RcsDiscoveryManager;
5 import org.iotivity.service.client.RcsDiscoveryManager.OnResourceDiscoveredListener;
6 import org.iotivity.service.client.RcsRemoteResourceObject;
7 import org.iotivity.service.server.RcsResourceObject;
8
9 import android.test.InstrumentationTestCase;
10
11 public abstract class TestBase extends InstrumentationTestCase {
12     protected static final String RESOURCEURI       = "/a/TemperatureSensor";
13     protected static final String RESOURCETYPE      = "oic.r.type";
14     protected static final String RESOURCEINTERFACE = "oic.if.baseline";
15
16     protected static final String   KEY       = "key";
17     protected static final RcsValue VALUE     = new RcsValue(100);
18     protected static final int      RAW_VALUE = 100;
19
20     private final Object mCond = new Object();
21
22     protected RcsResourceObject       mServer;
23     protected RcsRemoteResourceObject mClient;
24
25     private OnResourceDiscoveredListener mOnResourceDiscoveredListener = new OnResourceDiscoveredListener() {
26         @Override
27         public void onResourceDiscovered(
28                 RcsRemoteResourceObject remoteObject) {
29             if (mClient != null) {
30                 return;
31             }
32
33             mClient = remoteObject;
34             synchronized (mCond) {
35                 mCond.notify();
36             }
37         }
38     };
39
40     @Override
41     protected void setUp() throws Exception {
42         super.setUp();
43
44         mServer = new RcsResourceObject.Builder(RESOURCEURI, RESOURCETYPE,
45                 RESOURCEINTERFACE).build();
46         mServer.setAttribute(KEY, VALUE);
47
48         WaitUntilDiscovered();
49
50         assertNotNull(mClient);
51     }
52
53     private void WaitUntilDiscovered() throws RcsException {
54         while (mClient == null) {
55             try {
56                 RcsDiscoveryManager.DiscoveryTask discoveryTask = RcsDiscoveryManager
57                         .getInstance().discoverResourceByType(RcsAddress.multicast(),
58                                 RESOURCETYPE, mOnResourceDiscoveredListener);
59
60                 synchronized (mCond) {
61                     mCond.wait(100);
62                 }
63
64                 discoveryTask.cancel();
65             } catch (InterruptedException e) {
66                 e.printStackTrace();
67             }
68         }
69     }
70
71     protected void setServerAttrbutes(RcsResourceAttributes attrs)
72             throws RcsException {
73         RcsResourceObject.AttributesLock lock = mServer.getAttributesLock();
74
75         try {
76             lock.lock().putAll(attrs);
77             lock.apply();
78         } finally {
79             lock.unlock();
80         }
81     }
82
83     @Override
84     protected void tearDown() throws Exception {
85         super.tearDown();
86
87         mServer.destroy();
88         mClient.destroy();
89     }
90 }