Merge test folder of test branch
[iotivity.git] / test / src / tc / rc / junit / src / org / iotivity / test / rc / tc / helper / RCHelper.java
1 /******************************************************************
2 *
3 * Copyright 2016 Samsung Electronics 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 org.iotivity.test.rc.tc.helper;
22
23 import static org.iotivity.test.rc.tc.helper.RCCommonUtil.*;
24
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.iotivity.service.resourcecontainer.*;
34
35 import android.content.Context;
36 import android.content.res.AssetManager;
37 import android.util.Log;
38
39 public class RCHelper {
40         private static final String LOG_TAG = "RCTest : RCHelper";
41         private static RCHelper g_RCHelperInstance = null;
42
43         public static final String s_LIB = "lib";
44         public static final String s_VERSION = "Version";
45         public static final String s_TEST_KEY = "testKey";
46
47         public static final int n_VALUE = 30;
48         public static final int n_ZERO = 0;
49         public static final int n_MAX_WAIT = 2;
50         public static final int n_MAX_TIMER = 1000;
51
52         public static final String RCS_KEY = "testKey";
53         public static final int RCS_VALUE = 30;
54         public static final String ASSET_MANAGER_LIB = "lib";
55
56         private RCHelper() {
57         }
58
59         public static RCHelper getInstance() {
60                 if (g_RCHelperInstance == null) {
61                         g_RCHelperInstance = new RCHelper();
62                 }
63
64                 return g_RCHelperInstance;
65         }
66
67         public boolean copyFilesToDevice(Context context) {
68                 boolean isSuccess = true;
69                 AssetManager assetManager = context.getAssets();
70                 String[] files = null;
71                 try {
72                         files = assetManager.list(s_LIB);
73                 } catch (IOException e) {
74                         Log.e(LOG_TAG, e.getMessage());
75                         isSuccess = false;
76                 }
77
78                 for (String filename : files) {
79                         InputStream in = null;
80                         OutputStream out = null;
81                         try {
82                                 in = assetManager.open("lib/" + filename);
83                                 out = new FileOutputStream(context.getFilesDir().getParent()
84                                                 + "/files/" + filename);
85                                 copyIndividualFile(in, out);
86                                 in.close();
87                                 in = null;
88                                 out.flush();
89                                 out.close();
90                                 out = null;
91                         } catch (Exception e) {
92                                 Log.e(LOG_TAG, e.getMessage());
93                                 isSuccess = false;
94                         }
95                 }
96
97                 return isSuccess;
98         }
99
100         private void copyIndividualFile(InputStream in, OutputStream out)
101                         throws IOException {
102
103                 Log.d(LOG_TAG, "copyIndividualFile");
104                 byte[] buffer = new byte[2048];
105                 int read;
106                 while ((read = in.read(buffer)) != -1) {
107                         out.write(buffer, n_ZERO, read);
108                 }
109         }
110
111         public void waitInSecond(int time) {
112                 int timeInSec = time * n_MAX_TIMER;
113                 try {
114                         Thread.sleep(timeInSec);
115                 } catch (InterruptedException ex) {
116                         Thread.currentThread().interrupt();
117                 }
118         }
119
120         public RcsBundleInfo getBundle(RcsResourceContainer container,
121                         String bundleID) {
122                 waitInSecond(n_MAX_WAIT);
123                 List<RcsBundleInfo> info = container.listBundles();
124                 for (int i = n_ZERO; i < info.size(); i++) {
125                         Log.i(LOG_TAG, "BundleID: " + info.get(i).getID());
126                         if (bundleID.equals(info.get(i).getID()))
127                                 return info.get(i);
128                 }
129                 return null;
130         }
131
132         public boolean isBundleInAddedList(RcsResourceContainer container,
133                         String bundleID) {
134                 if (getBundle(container, bundleID) == null) {
135                         Log.i(LOG_TAG, "Bundle is not in Added Bundle List. The ID is "
136                                         + bundleID);
137                         return false;
138                 }
139                 return true;
140         }
141
142         public boolean startBundle(RcsResourceContainer container, String bundleID) {
143                 container.startBundle(bundleID);
144
145                 RcsBundleInfo info = getBundle(container, bundleID);
146
147                 if (info == null) {
148                         Log.i(LOG_TAG, "Bundle is not created. ID : " + bundleID);
149                         return false;
150                 }
151
152                 return true;
153         }
154
155         public boolean stopBundle(RcsResourceContainer container, String bundleID) {
156                 container.stopBundle(bundleID);
157
158                 return true;
159         }
160
161         public boolean removeBundle(RcsResourceContainer container, String bundleID) {
162                 try {
163                         container.removeBundle(bundleID);
164                 } catch (Exception e) {
165                         return false;
166                 }
167
168                 return true;
169         }
170
171         public boolean addResourceConfigVersion(RcsResourceContainer container,
172                         String bundleID, String value) {
173                 Map<String, String> params = new HashMap<String, String>();
174                 params.put(s_VERSION, value);
175                 container.addResourceConfig(bundleID, EMPTY_STRING, params);
176
177                 RcsBundleInfo info = getBundle(container, bundleID);
178                 if (info == null) {
179                         Log.i(LOG_TAG, "Bundle is not created. ID : " + bundleID);
180                         return false;
181                 }
182
183                 if (!info.getVersion().equals(value)) {
184                         Log.i(LOG_TAG, "Bundle version is not matched. ID : " + bundleID
185                                         + ", value : " + value);
186                         return false;
187                 }
188                 return true;
189         }
190
191         public String getFilesPath(Context context) {
192                 try {
193                         return context.getFilesDir().getPath();
194                 } catch (Exception e) {
195                         return EMPTY_STRING;
196                 }
197         }
198
199         /**
200          * Create BundleResource object reference and send back to the user.
201          * 
202          * @param context
203          *            Application context
204          * @return BundleResource class reference
205          **/
206         public BundleResource getBundleResource(Context context) {
207                 BundleResource bundleResource = new BundleResource(context) {
208
209                         @Override
210                         protected void initAttributes() {
211                                 this.m_name = DI_SENSOR_NAME;
212                                 this.m_resourceType = DI_RESOURCE_TYPE;
213                                 this.m_attributes.put(DI_SENSOR_KEY, n_ZERO);
214                                 this.m_attributes.put(HUMIDITY_SENSOR_KEY, n_ZERO);
215                                 this.m_attributes.put(TEMPERATURE_SENSOR_KEY, n_ZERO);
216                                 this.m_attributes.put(LIGHT_SENSOR_KEY, n_ZERO);
217                         }
218
219                         @Override
220                         public void handleSetAttributesRequest(RcsResourceAttributes attrs) {
221                                 this.setAttributes(attrs);
222                         }
223
224                         @Override
225                         public RcsResourceAttributes handleGetAttributesRequest() {
226                                 return this.getAttributes();
227                         }
228
229                         @Override
230                         public void deactivateResource() {
231                         }
232                 };
233
234                 return bundleResource;
235         }
236
237         public RcsResourceAttributes getResourceAttributes() {
238                 RcsResourceAttributes resourceAttributes = new RcsResourceAttributes();
239
240                 resourceAttributes.put(s_TEST_KEY, n_VALUE);
241
242                 return resourceAttributes;
243         }
244
245 }