23448e43a17cdaae4357410ed838fa5ef5004e26
[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_N03()
207     {
208         String val = "@#$$&^*^(*^&";
209
210         boolean result = true;
211         try
212         {
213             simulatorResourceModel.addAttributeString(KEY, val);
214             result = result && simulatorResourceModel.getAttribute(KEY).getValue().toString().equals(val);
215         }
216         catch(Exception e)
217         {
218             result = false;
219         }
220
221         assertTrue(result);
222     }
223
224     public void testAddAttributeBoolean_P01()
225     {
226         boolean result = true;
227
228         boolean val = true;
229
230         try
231         {
232             simulatorResourceModel.addAttributeBoolean(KEY, val);
233
234             result = result && ((Boolean.parseBoolean(simulatorResourceModel.getAttribute(KEY).getValue() + "")));
235
236             val = false;
237
238             simulatorResourceModel.addAttributeBoolean(KEY, val);
239
240             result = result && !((Boolean.parseBoolean(simulatorResourceModel.getAttribute(KEY).getValue() + "")));
241         }
242         catch (Exception e)
243         {
244             result = false;
245         }
246
247         assertTrue(result);
248     }
249
250     public void testSize_P01()
251     {
252         boolean result = true;
253
254         try
255         {
256             simulatorResourceModel.addAttributeInt("test1", 1234);
257
258             result = result && (simulatorResourceModel.size() == 1);
259
260             simulatorResourceModel.addAttributeString("test2", "asdf");
261             simulatorResourceModel.addAttributeBoolean("test3", true);
262             simulatorResourceModel.addAttributeDouble("test4", 22.435234);
263
264             result = result && (simulatorResourceModel.size() == 4);
265         }
266         catch(Exception e)
267         {
268             result = false;
269         }
270         assertTrue(result);
271     }
272
273     public void testGetAttributes_P01()
274     {
275         boolean result = true;
276
277         try
278         {
279             simulatorResourceModel.addAttributeInt("test1", 1234);
280             simulatorResourceModel.addAttributeString("test2", "asdf");
281             simulatorResourceModel.addAttributeBoolean("test3", true);
282             simulatorResourceModel.addAttributeDouble("test4", 22.435234);
283
284             Map<String, ResourceAttribute> attributes = simulatorResourceModel.getAttributes();
285
286             result = result && (((Integer)attributes.get("test1").getValue()) == 1234) &&
287                      (((String)attributes.get("test2").getValue()).equals("asdf")) &&
288                      ((Boolean.parseBoolean(attributes.get("test3").getValue() + "")==true)) &&
289                      (((Double)attributes.get("test4").getValue()) == 22.435234);
290         }
291         catch(Exception e)
292         {
293             result = false;
294         }
295         assertTrue(result);
296     }
297
298     public void testGetAttribute_P01()
299     {
300         int val = 100;
301
302         boolean result = true;
303
304         try
305         {
306             simulatorResourceModel.addAttributeInt(KEY, val);
307
308             result = result && Integer.parseInt(simulatorResourceModel.getAttribute(KEY).getValue().toString()) == val;
309         }
310         catch(Exception e)
311         {
312             result = false;
313         }
314         assertTrue(result);
315     }
316 }