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