JUnit test cases for updated APIs.
[platform/upstream/iotivity.git] / service / simulator / unittests / SimulatorTest / src / org / oic / simulator / server / test / SimulatorResourceTest.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.server.test;
18
19 import java.util.Vector;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
22
23 import org.oic.simulator.InvalidArgsException;
24 import org.oic.simulator.SimulatorException;
25 import org.oic.simulator.SimulatorManager;
26 import org.oic.simulator.client.FindResourceListener;
27 import org.oic.simulator.client.SimulatorRemoteResource;
28 import org.oic.simulator.server.SimulatorResource;
29 import org.oic.simulator.test.ExceptionType;
30 import org.oic.simulator.utils.ObjectHolder;
31
32 import junit.framework.TestCase;
33
34 /**
35  * This class tests the APIs of SimulatorResource class
36  */
37 public class SimulatorResourceTest extends TestCase {
38     private static final String RES_NAME = "test-resource";
39     private static final String RES_URI  = "/test/resource";
40     private static final String RES_TYPE = "test.resource";
41     private SimulatorResource   resource = null;
42
43     static {
44         System.loadLibrary("SimulatorManager");
45         System.loadLibrary("RamlParser");
46         System.loadLibrary("oc");
47         System.loadLibrary("oc_logger");
48         System.loadLibrary("octbstack");
49     }
50
51     protected void setUp() throws Exception {
52         super.setUp();
53         resource = SimulatorManager.createResource(
54                 SimulatorResource.Type.SINGLE, RES_NAME, RES_URI, RES_TYPE);
55     }
56
57     protected void tearDown() throws Exception {
58         super.tearDown();
59         resource = null;
60     }
61
62     public void testGetName_P01() {
63         String name = null;
64
65         try {
66             name = resource.getName();
67         } catch (SimulatorException e) {
68             e.printStackTrace();
69         }
70
71         assertEquals(RES_NAME, name);
72     }
73
74     public void testGetType_P01() {
75         SimulatorResource.Type type = SimulatorResource.Type.SINGLE;
76
77         try {
78             type = resource.getType();
79         } catch (SimulatorException e) {
80             e.printStackTrace();
81         }
82
83         assertEquals(SimulatorResource.Type.SINGLE, type);
84     }
85
86     public void testGetURI_P01() {
87         String uri = null;
88
89         try {
90             uri = resource.getURI();
91         } catch (SimulatorException e) {
92             e.printStackTrace();
93         }
94
95         assertEquals(RES_URI, uri);
96     }
97
98     public void testGetResourceType_P01() {
99         String resType = null;
100
101         try {
102             resType = resource.getResourceType();
103         } catch (SimulatorException e) {
104             e.printStackTrace();
105         }
106
107         assertEquals(RES_TYPE, resType);
108     }
109
110     public void testGetInterface_P01() {
111         Vector<String> interfaces = null;
112
113         try {
114             interfaces = resource.getInterface();
115         } catch (SimulatorException e) {
116             e.printStackTrace();
117         }
118
119         assertNotNull(interfaces);
120         assertTrue(interfaces.size() > 0);
121     }
122
123     public void testSetName_P01() {
124         String name = "new-name";
125         String newName = null;
126
127         try {
128             resource.setName(name);
129             newName = resource.getName();
130         } catch (InvalidArgsException e) {
131             e.printStackTrace();
132         } catch (SimulatorException e) {
133             e.printStackTrace();
134         }
135
136         assertEquals(name, newName);
137     }
138
139     public void testSetName_N01() {
140         String newName = "";
141         ExceptionType exType = ExceptionType.UNKNOWN;
142
143         try {
144             resource.setName(newName);
145         } catch (InvalidArgsException e) {
146             exType = ExceptionType.INVALID_ARGS;
147         } catch (SimulatorException e) {
148             exType = ExceptionType.SIMULATOR;
149         }
150
151         assertEquals(ExceptionType.INVALID_ARGS, exType);
152     }
153
154     public void testSetName_N02() {
155         String newName = null;
156         ExceptionType exType = ExceptionType.UNKNOWN;
157
158         try {
159             resource.setName(newName);
160         } catch (InvalidArgsException e) {
161             exType = ExceptionType.INVALID_ARGS;
162         } catch (SimulatorException e) {
163             exType = ExceptionType.SIMULATOR;
164         }
165
166         assertEquals(ExceptionType.INVALID_ARGS, exType);
167     }
168
169     public void testSetURI_P01() {
170         String newUri = "/test/newuri/1";
171         String uri = null;
172
173         try {
174             resource.setURI(newUri);
175             uri = resource.getURI();
176         } catch (InvalidArgsException e) {
177             e.printStackTrace();
178         } catch (SimulatorException e) {
179             e.printStackTrace();
180         }
181
182         assertEquals(newUri, uri);
183     }
184
185     public void testSetURI_N01() {
186         String newUri = "";
187         ExceptionType exType = ExceptionType.UNKNOWN;
188
189         try {
190             resource.setURI(newUri);
191         } catch (InvalidArgsException e) {
192             exType = ExceptionType.INVALID_ARGS;
193         } catch (SimulatorException e) {
194             exType = ExceptionType.SIMULATOR;
195         }
196
197         assertEquals(ExceptionType.INVALID_ARGS, exType);
198     }
199
200     public void testSetURI_N02() {
201         String newUri = null;
202         ExceptionType exType = ExceptionType.UNKNOWN;
203
204         try {
205             resource.setURI(newUri);
206         } catch (InvalidArgsException e) {
207             exType = ExceptionType.INVALID_ARGS;
208         } catch (SimulatorException e) {
209             exType = ExceptionType.SIMULATOR;
210         }
211
212         assertEquals(ExceptionType.INVALID_ARGS, exType);
213     }
214
215     public void testSetResourceType_P01() {
216         String newResType = "test.newresource";
217         String resType = null;
218
219         try {
220             resource.setResourceType(newResType);
221             resType = resource.getResourceType();
222         } catch (InvalidArgsException e) {
223             e.printStackTrace();
224         } catch (SimulatorException e) {
225             e.printStackTrace();
226         }
227
228         assertEquals(newResType, resType);
229     }
230
231     public void testSetResourceType_N01() {
232         String newResType = "";
233         ExceptionType exType = ExceptionType.UNKNOWN;
234
235         try {
236             resource.setResourceType(newResType);
237         } catch (InvalidArgsException e) {
238             exType = ExceptionType.INVALID_ARGS;
239         } catch (SimulatorException e) {
240             exType = ExceptionType.SIMULATOR;
241         }
242
243         assertEquals(ExceptionType.INVALID_ARGS, exType);
244     }
245
246     public void testSetResourceType_N02() {
247         String newResType = null;
248         ExceptionType exType = ExceptionType.UNKNOWN;
249
250         try {
251             resource.setResourceType(newResType);
252         } catch (InvalidArgsException e) {
253             exType = ExceptionType.INVALID_ARGS;
254         } catch (SimulatorException e) {
255             exType = ExceptionType.SIMULATOR;
256         }
257
258         assertEquals(ExceptionType.INVALID_ARGS, exType);
259     }
260
261     public void testSetObservable_P01() {
262         boolean newState = true;
263         boolean state = false;
264         try {
265             resource.setObservable(newState);
266             state = resource.isObservable();
267         } catch (SimulatorException e) {
268             e.printStackTrace();
269         }
270
271         assertEquals(newState, state);
272     }
273
274     public void testSetObservable_P02() {
275         boolean newState = false;
276         boolean state = true;
277         try {
278             resource.setObservable(newState);
279             state = resource.isObservable();
280         } catch (SimulatorException e) {
281             e.printStackTrace();
282         }
283
284         assertEquals(newState, state);
285     }
286
287     public void testIsObservable_P01() {
288         boolean state = false;
289
290         try {
291             state = resource.isObservable();
292         } catch (SimulatorException e) {
293             e.printStackTrace();
294         }
295
296         assertEquals(true, state);
297     }
298
299     public void testIsStarted_P01() {
300         boolean state = true;
301
302         try {
303             state = resource.isStarted();
304         } catch (SimulatorException e) {
305             e.printStackTrace();
306         }
307
308         assertEquals(false, state);
309     }
310
311     public void testIsStarted_P02() {
312         boolean state = false;
313
314         try {
315             resource.start();
316             state = resource.isStarted();
317             resource.stop();
318         } catch (SimulatorException e) {
319             e.printStackTrace();
320         }
321
322         assertEquals(true, state);
323     }
324
325     public void testIsStarted_P03() {
326         boolean state = true;
327
328         try {
329             resource.start();
330             resource.stop();
331             state = resource.isStarted();
332         } catch (SimulatorException e) {
333             e.printStackTrace();
334         }
335
336         assertEquals(false, state);
337     }
338
339     public void testStart_P01() {
340         CountDownLatch lockObject = new CountDownLatch(1);
341         ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
342         FindResourceCallbackListener listener = new FindResourceCallbackListener(
343                 lockObject, resourceHolder);
344
345         try {
346             resource.start();
347             SimulatorManager.findResource(resource.getResourceType(), listener);
348
349             // Wait for the resource to found
350             try {
351                 lockObject.await(10, TimeUnit.SECONDS);
352             } catch (InterruptedException e) {
353                 e.printStackTrace();
354             }
355         } catch (InvalidArgsException e) {
356             e.printStackTrace();
357         } catch (SimulatorException e) {
358             e.printStackTrace();
359         }
360
361         try {
362             resource.stop();
363         } catch (SimulatorException e) {
364             e.printStackTrace();
365         }
366
367         assertNotNull(resourceHolder.get());
368     }
369
370     public void testStop_P01() {
371         CountDownLatch lockObject = new CountDownLatch(1);
372         ObjectHolder<SimulatorRemoteResource> resourceHolder = new ObjectHolder<>();
373         FindResourceCallbackListener listener = new FindResourceCallbackListener(
374                 lockObject, resourceHolder);
375
376         try {
377             resource.start();
378             resource.stop();
379             SimulatorManager.findResource(resource.getResourceType(), listener);
380
381             // Wait for the resource to found
382             try {
383                 lockObject.await(10, TimeUnit.SECONDS);
384             } catch (InterruptedException e) {
385                 e.printStackTrace();
386             }
387         } catch (InvalidArgsException e) {
388             e.printStackTrace();
389         } catch (SimulatorException e) {
390             e.printStackTrace();
391         }
392
393         try {
394             resource.stop();
395         } catch (SimulatorException e) {
396             e.printStackTrace();
397         }
398
399         assertNull(resourceHolder.get());
400     }
401 }
402
403 class FindResourceCallbackListener implements FindResourceListener {
404
405     private CountDownLatch                        mLockObject;
406     private ObjectHolder<SimulatorRemoteResource> mResourceHolder;
407
408     public FindResourceCallbackListener(CountDownLatch lockObject,
409             ObjectHolder<SimulatorRemoteResource> resourceHolder) {
410         mLockObject = lockObject;
411         mResourceHolder = resourceHolder;
412     }
413
414     @Override
415     public void onResourceFound(SimulatorRemoteResource resource) {
416         mResourceHolder.set(resource);
417         mLockObject.countDown();
418     }
419 }