Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / unittests / SimulatorTest / src / org / oic / simulator / test / SimulatorResourceModelTest.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.oic.simulator.test;
18
19 import java.util.Map;
20
21 import junit.framework.TestCase;
22
23 import org.oic.simulator.ResourceAttribute;
24 import org.oic.simulator.SimulatorResourceModel;
25
26 /**
27  * This class tests the functionality of Simulator Resource Model
28  * class APIs.
29  */
30 public class SimulatorResourceModelTest extends TestCase
31 {
32
33     private SimulatorResourceModel simulatorResourceModel;
34
35     private static final String KEY = "test";
36
37     static
38     {
39         System.loadLibrary("SimulatorManager");
40         System.loadLibrary("RamlParser");
41         System.loadLibrary("oc");
42         System.loadLibrary("oc_logger");
43         System.loadLibrary("octbstack");
44     }
45
46     @Override
47     protected void setUp() throws Exception
48     {
49         super.setUp();
50
51         simulatorResourceModel = new SimulatorResourceModel();
52     }
53
54     @Override
55     protected void tearDown() throws Exception
56     {
57         super.tearDown();
58
59         simulatorResourceModel = null;
60     }
61
62     public void testAddAttributeInt_P01()
63     {
64         int val = 100;
65
66         boolean result = true;
67         try
68         {
69             simulatorResourceModel.addAttributeInt(KEY, val);
70             result = result && Integer.parseInt(simulatorResourceModel.getAttribute(KEY).getValue().toString()) == val;
71         }
72         catch(Exception e)
73         {
74             result = false;
75         }
76
77         assertTrue(result);
78     }
79
80     public void testAddAttributeInt_N01()
81     {
82         int val = -10;
83
84         boolean result = true;
85         try
86         {
87             simulatorResourceModel.addAttributeInt(KEY, val);
88             result = result && Integer.parseInt(simulatorResourceModel.getAttribute(KEY).getValue().toString()) == val;
89         }
90         catch(Exception e)
91         {
92             result = false;
93         }
94
95         assertTrue(result);
96     }
97
98     public void testAddAttributeInt_N02()
99     {
100         int val = 666666;
101
102         boolean result = true;
103         try
104         {
105             simulatorResourceModel.addAttributeInt(KEY, val);
106             result = result && Integer.parseInt(simulatorResourceModel.getAttribute(KEY).getValue().toString()) == val;
107         }
108         catch(Exception e)
109         {
110             result = false;
111         }
112
113         assertTrue(result);
114     }
115
116     public void testAddAttributeDouble_P01()
117     {
118         double val = 10.11;
119
120         boolean result = true;
121         try
122         {
123             simulatorResourceModel.addAttributeDouble(KEY, val);
124             result = result && Double.parseDouble(simulatorResourceModel.getAttribute(KEY).getValue().toString()) == val;
125         }
126         catch(Exception e)
127         {
128             result = false;
129         }
130
131         assertTrue(result);
132     }
133
134     public void testAddAttributeDouble_N01()
135     {
136         double val = -11.12;
137
138         boolean result = true;
139         try
140         {
141             simulatorResourceModel.addAttributeDouble(KEY, val);
142             result = result && Double.parseDouble(simulatorResourceModel.getAttribute(KEY).getValue().toString()) == val;
143         }
144         catch(Exception e)
145         {
146             result = false;
147         }
148
149         assertTrue(result);
150     }
151
152     public void testAddAttributeDouble_N02()
153     {
154         double val = 0.0044444444444;
155
156         boolean result = true;
157         try
158         {
159             simulatorResourceModel.addAttributeDouble(KEY, val);
160             result = result && Double.parseDouble(simulatorResourceModel.getAttribute(KEY).getValue().toString()) == val;
161         }
162         catch(Exception e)
163         {
164             result = false;
165         }
166
167         assertTrue(result);
168     }
169
170     public void testAddAttributeString_P01()
171     {
172         String val = "asdf";
173
174         boolean result = true;
175         try
176         {
177             simulatorResourceModel.addAttributeString(KEY, val);
178             result = result && simulatorResourceModel.getAttribute(KEY).getValue().toString().equals(val);
179         }
180         catch(Exception e)
181         {
182             result = false;
183         }
184
185         assertTrue(result);
186     }
187
188     public void testAddAttributeString_N01()
189     {
190         String val = "";
191
192         boolean result = true;
193         try
194         {
195             simulatorResourceModel.addAttributeString(KEY, val);
196             result = result && simulatorResourceModel.getAttribute(KEY).getValue().toString().equals(val);
197         }
198         catch(Exception e)
199         {
200             result = false;
201         }
202
203         assertTrue(result);
204     }
205
206    public void testAddAttributeString_N02() {
207         String val = null;
208
209         boolean result = false;
210         try {
211
212             simulatorResourceModel.addAttributeString(KEY, val);
213
214             result = result && simulatorResourceModel.getAttribute(KEY).getValue().toString().equals(val);
215
216         } catch(Exception e) {
217             result = true;
218         }
219
220         assertTrue(result);
221     }
222
223     public void testAddAttributeString_N03()
224     {
225         String val = "@#$$&^*^(*^&";
226
227         boolean result = true;
228         try
229         {
230             simulatorResourceModel.addAttributeString(KEY, val);
231             result = result && simulatorResourceModel.getAttribute(KEY).getValue().toString().equals(val);
232         }
233         catch(Exception e)
234         {
235             result = false;
236         }
237
238         assertTrue(result);
239     }
240
241     public void testAddAttributeBoolean_P01()
242     {
243         boolean result = true;
244
245         boolean val;
246
247         try
248         {
249             val=Boolean.parseBoolean("true");
250             simulatorResourceModel.addAttributeBoolean(KEY, val);
251
252             result = result && ((Boolean.parseBoolean(simulatorResourceModel.getAttribute(KEY).getValue().toString()+"")));
253
254             val = Boolean.parseBoolean("false");
255
256             simulatorResourceModel.addAttributeBoolean(KEY, val);
257
258             result = result && !((Boolean.parseBoolean(simulatorResourceModel.getAttribute(KEY).getValue().toString()+"")));
259         }
260         catch (Exception e)
261         {
262             result = false;
263         }
264
265         assertTrue(result);
266     }
267
268     public void testSize_P01()
269     {
270         boolean result = true;
271
272         try
273         {
274             simulatorResourceModel.addAttributeInt("test1", 1234);
275
276             result = result && (simulatorResourceModel.size() == 1);
277
278             simulatorResourceModel.addAttributeString("test2", "asdf");
279             simulatorResourceModel.addAttributeBoolean("test3", Boolean.parseBoolean("true"));
280             simulatorResourceModel.addAttributeDouble("test4", 22.435234);
281
282             result = result && (simulatorResourceModel.size() == 4);
283         }
284         catch(Exception e)
285         {
286             result = false;
287         }
288         assertTrue(result);
289     }
290
291     public void testGetAttributes_P01()
292     {
293         boolean result = true;
294
295         try
296         {
297             simulatorResourceModel.addAttributeInt("test1", 1234);
298             simulatorResourceModel.addAttributeString("test2", "asdf");
299             simulatorResourceModel.addAttributeBoolean("test3", Boolean.parseBoolean("true"));
300             simulatorResourceModel.addAttributeDouble("test4", 22.435234);
301
302             Map<String, ResourceAttribute> attributes = simulatorResourceModel.getAttributes();
303
304             result = result && (((Integer)attributes.get("test1").getValue()) == 1234) &&
305                      (((String)attributes.get("test2").getValue()).equals("asdf")) &&
306                      ((Boolean.parseBoolean(attributes.get("test3").getValue().toString() + "")==true)) &&
307                      (((Double)attributes.get("test4").getValue()) == 22.435234);
308         }
309         catch(Exception e)
310         {
311             result = false;
312         }
313         assertTrue(result);
314     }
315
316     public void testGetAttribute_P01()
317     {
318         int val = 100;
319
320         boolean result = true;
321
322         try
323         {
324             simulatorResourceModel.addAttributeInt(KEY, val);
325
326             result = result && Integer.parseInt(simulatorResourceModel.getAttribute(KEY).getValue().toString()) == val;
327         }
328         catch(Exception e)
329         {
330             result = false;
331         }
332         assertTrue(result);
333     }
334 }