20b668992693569c83b33c3724a3b8c073ed423c
[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 org.oic.simulator.AttributeValue;
22 import org.oic.simulator.InvalidArgsException;
23 import org.oic.simulator.SimulatorResourceAttribute;
24 import org.oic.simulator.SimulatorResourceModel;
25
26 import junit.framework.TestCase;
27
28 /**
29  * This class tests the APIs of SimulatorResourceModel class.
30  */
31 public class SimulatorResourceModelTest extends TestCase {
32     private static final String INT_KEY    = "Interger";
33     private static final String DOUBLE_KEY = "Double";
34     private static final String BOOL_KEY   = "Boolean";
35     private static final String STRING_KEY = "String";
36
37     @Override
38     protected void setUp() throws Exception {
39         super.setUp();
40     }
41
42     @Override
43     protected void tearDown() throws Exception {
44         super.tearDown();
45     }
46
47     public void testSimulatorResourceModel_P01() {
48         SimulatorResourceModel resModel = new SimulatorResourceModel();
49         assertNotNull(resModel);
50     }
51
52     public void testSetInt_P01() {
53         int result = -1;
54
55         try {
56             SimulatorResourceModel resModel = new SimulatorResourceModel();
57             resModel.set(INT_KEY, new AttributeValue(10));
58             result = ((Integer) resModel.get(INT_KEY).get()).intValue();
59         } catch (InvalidArgsException e) {
60             e.printStackTrace();
61         }
62
63         assertEquals(10, result);
64     }
65
66     public void testSetInt_P02() {
67         int result = -1;
68
69         try {
70             SimulatorResourceModel resModel = new SimulatorResourceModel();
71             resModel.set(INT_KEY, new AttributeValue(-10));
72             result = ((Integer) resModel.get(INT_KEY).get()).intValue();
73         } catch (InvalidArgsException e) {
74             e.printStackTrace();
75         }
76
77         assertEquals(-10, result);
78     }
79
80     public void testSetDouble_P01() {
81         double result = 0.0;
82
83         try {
84             SimulatorResourceModel resModel = new SimulatorResourceModel();
85             resModel.set(DOUBLE_KEY, new AttributeValue(4.0));
86             result = ((Double) resModel.get(DOUBLE_KEY).get()).doubleValue();
87         } catch (InvalidArgsException e) {
88             e.printStackTrace();
89         }
90
91         assertEquals(4.0, result);
92     }
93
94     public void testSetDouble_P02() {
95         double result = 0.0;
96
97         try {
98             SimulatorResourceModel resModel = new SimulatorResourceModel();
99             resModel.set(DOUBLE_KEY, new AttributeValue(-4.0));
100             result = ((Double) resModel.get(DOUBLE_KEY).get()).doubleValue();
101         } catch (InvalidArgsException e) {
102             e.printStackTrace();
103         }
104
105         assertEquals(-4.0, result);
106     }
107
108     public void testSetBoolean_P01() {
109         boolean result = false;
110
111         try {
112             SimulatorResourceModel resModel = new SimulatorResourceModel();
113             resModel.set(BOOL_KEY, new AttributeValue(true));
114             result = ((Boolean) resModel.get(BOOL_KEY).get()).booleanValue();
115         } catch (InvalidArgsException e) {
116             e.printStackTrace();
117         }
118
119         assertEquals(true, result);
120     }
121
122     public void testSetString_P01() {
123         String result = null;
124
125         try {
126             SimulatorResourceModel resModel = new SimulatorResourceModel();
127             resModel.set(STRING_KEY, new AttributeValue("string-value"));
128             result = (String) resModel.get(STRING_KEY).get();
129         } catch (InvalidArgsException e) {
130             e.printStackTrace();
131         }
132
133         assertEquals("string-value", result);
134     }
135
136     public void testSetString_P02() {
137         String result = null;
138
139         try {
140             SimulatorResourceModel resModel = new SimulatorResourceModel();
141             resModel.set(STRING_KEY, new AttributeValue(""));
142             result = (String) resModel.get(STRING_KEY).get();
143         } catch (InvalidArgsException e) {
144             e.printStackTrace();
145         }
146
147         assertEquals("", result);
148     }
149
150     public void testSetString_P03() {
151         String result = null;
152
153         try {
154             SimulatorResourceModel resModel = new SimulatorResourceModel();
155             resModel.set(STRING_KEY, new AttributeValue("@#$$&^*^(*^&"));
156             result = (String) resModel.get(STRING_KEY).get();
157         } catch (InvalidArgsException e) {
158             e.printStackTrace();
159         }
160
161         assertEquals("@#$$&^*^(*^&", result);
162     }
163
164     public void testSize_P01() {
165         int result = -1;
166
167         SimulatorResourceModel resModel = new SimulatorResourceModel();
168         result = resModel.size();
169
170         assertEquals(0, result);
171     }
172
173     public void testSize_P02() {
174         int result = -1;
175
176         SimulatorResourceModel resModel = new SimulatorResourceModel();
177         try {
178             resModel.set(INT_KEY, new AttributeValue(1234));
179             resModel.set(DOUBLE_KEY, new AttributeValue(1.234));
180             resModel.set(BOOL_KEY, new AttributeValue(true));
181             resModel.set(STRING_KEY, new AttributeValue("string-value"));
182             result = resModel.size();
183         } catch (InvalidArgsException e) {
184             e.printStackTrace();
185         }
186
187         assertEquals(4, result);
188     }
189
190     public void testGet_P01() {
191         AttributeValue result = null;
192
193         SimulatorResourceModel resModel = new SimulatorResourceModel();
194         try {
195             resModel.set(INT_KEY, new AttributeValue(10));
196             result = resModel.get(INT_KEY);
197         } catch (InvalidArgsException e) {
198             e.printStackTrace();
199         }
200
201         assertNotNull(result);
202     }
203
204     public void testGet_P02() {
205         Map<String, AttributeValue> result = null;
206
207         SimulatorResourceModel resModel = new SimulatorResourceModel();
208         try {
209             resModel.set(INT_KEY, new AttributeValue(1234));
210             resModel.set(DOUBLE_KEY, new AttributeValue(1.234));
211             resModel.set(BOOL_KEY, new AttributeValue(true));
212             resModel.set(STRING_KEY, new AttributeValue("string-value"));
213             result = resModel.get();
214         } catch (InvalidArgsException e) {
215             e.printStackTrace();
216         }
217
218         assertNotNull(result);
219         assertEquals(4, result.size());
220     }
221 }