2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.oic.simulator.clientcontroller.test;
19 import java.util.HashMap;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
23 import junit.framework.TestCase;
25 import org.oic.simulator.SimulatorException;
26 import org.oic.simulator.SimulatorManager;
27 import org.oic.simulator.SimulatorResourceModel;
28 import org.oic.simulator.clientcontroller.SimulatorObserveType;
29 import org.oic.simulator.clientcontroller.SimulatorRemoteResource;
30 import org.oic.simulator.clientcontroller.SimulatorVerificationType;
31 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
32 import org.oic.simulator.test.FindResourceListener;
33 import org.oic.simulator.test.ResourceModelChangeListener;
34 import org.oic.simulator.test.ResourceModelObject;
35 import org.oic.simulator.test.SimulatorRemoteResourceObject;
38 * This class tests the functionality of Simulator Remote Resource
41 public class SimulatorRemoteResourceTest extends TestCase
43 private static final String CONFIG_PATH = "./ramls/simple-light.raml";
44 // private static final String RESOURCE_TYPE = "oic.light";
46 private CountDownLatch lockObject;
47 private ResourceModelObject resourceModelObject;
48 private ResourceModelChangeListener resourceModelChangeListener;
50 private SimulatorRemoteResourceObject simulatorRemoteResourceObject;
51 private SimulatorRemoteResource simulatorRemoteResource;
53 private SimulatorResourceServer simulatorResourceServer;
55 private FindResourceListener findResourceListener;
59 System.loadLibrary("SimulatorManager");
60 System.loadLibrary("RamlParser");
61 System.loadLibrary("oc");
62 System.loadLibrary("oc_logger");
63 System.loadLibrary("octbstack");
66 protected void setUp() throws Exception
70 lockObject = new CountDownLatch(1);
71 resourceModelObject = new ResourceModelObject();
72 resourceModelChangeListener = new ResourceModelChangeListener(resourceModelObject);
74 simulatorResourceServer = SimulatorManager.createResource(CONFIG_PATH, resourceModelChangeListener);
76 simulatorRemoteResourceObject = new SimulatorRemoteResourceObject();
78 findResourceListener = new FindResourceListener(lockObject, simulatorRemoteResourceObject);
80 SimulatorManager.findResource(findResourceListener);
84 lockObject.await(10, TimeUnit.SECONDS);
86 catch (InterruptedException e)
90 simulatorRemoteResource = simulatorRemoteResourceObject.getSimulatorRemoteResource();
93 protected void tearDown() throws Exception
97 SimulatorManager.deleteResource(simulatorResourceServer);
100 resourceModelObject = null;
101 resourceModelChangeListener = null;
103 simulatorRemoteResourceObject = null;
104 findResourceListener = null;
106 simulatorRemoteResource = null;
109 public void testGetUri_P01()
111 assertNotNull(simulatorRemoteResource.getUri());
114 public void testGetIsObservable_P01()
116 assertTrue(simulatorRemoteResource.getIsObservable());
119 public void testGetConnectivityType_P01()
121 assertNotNull(simulatorRemoteResource.getConnectivityType());
124 public void testGetResourceTypes_P01()
126 assertTrue(simulatorRemoteResource.getResourceTypes() != null && simulatorRemoteResource.getResourceTypes().size() > 0);
129 public void testGetResourceInterfaces_P01()
131 assertTrue(simulatorRemoteResource.getResourceInterfaces() != null && simulatorRemoteResource.getResourceInterfaces().size() > 0);
134 public void testGetId_P01()
136 assertNotNull(simulatorRemoteResource.getId());
139 public void testStartObserve_P01()
141 boolean result = true;
142 HashMap<String, String> queryParamMap = new HashMap<String, String>();
144 lockObject = new CountDownLatch(1);
146 ObserveListenerObject observeListenerObject = new ObserveListenerObject();
147 ObserveListener observeListener = new ObserveListener(lockObject, observeListenerObject);
151 simulatorRemoteResource.startObserve(SimulatorObserveType.OBSERVE, queryParamMap, observeListener);
152 simulatorResourceServer.addAttributeString("test", "test");
161 lockObject.await(10, TimeUnit.SECONDS);
163 catch (InterruptedException e)
167 assertTrue(observeListenerObject.getRepresentation() != null && result);
171 simulatorRemoteResource.stopObserve();
178 observeListenerObject = null;
179 observeListener = null;
182 public void testStopObserve_P01()
184 boolean result = true;
186 HashMap<String, String> queryParamMap = new HashMap<String, String>();
187 lockObject = new CountDownLatch(1);
188 ObserveListenerObject observeListenerObject = new ObserveListenerObject();
189 ObserveListener observeListener = new ObserveListener(lockObject, observeListenerObject);
193 simulatorRemoteResource.startObserve(SimulatorObserveType.OBSERVE, queryParamMap, observeListener);
194 simulatorResourceServer.addAttributeString("test", "test");
198 e1.printStackTrace();
204 lockObject.await(10, TimeUnit.SECONDS);
206 catch (InterruptedException e)
210 result = result && observeListenerObject.getRepresentation() != null;
214 simulatorRemoteResource.stopObserve();
224 observeListenerObject = null;
225 observeListener = null;
228 public void testGetQueryParamGetListener_P01()
230 boolean result = true;
231 lockObject = new CountDownLatch(1);
232 HashMap<String, String> queryParamMap = new HashMap<String, String>();
234 ListenerObject getListenerObject = new ListenerObject();
235 GetListener getListener = new GetListener(lockObject, getListenerObject);
239 simulatorRemoteResource.get(queryParamMap, getListener);
243 e1.printStackTrace();
249 lockObject.await(10, TimeUnit.SECONDS);
251 catch (InterruptedException e)
255 if(getListenerObject.getEx() == null)
259 result = result && getListenerObject.getRepresentation() != null && getListenerObject.getRepresentation().size() > 0;
261 catch (SimulatorException e)
273 public void testGetStringMapOfStringStringIGetListener_P01()
275 boolean result = true;
276 lockObject = new CountDownLatch(1);
277 HashMap<String, String> queryParamMap = new HashMap<String, String>();
279 String resourceInterface = "oic.if.baseline";
281 ListenerObject getListenerObject = new ListenerObject();
282 GetListener getListener = new GetListener(lockObject, getListenerObject);
286 simulatorRemoteResource.get(resourceInterface, queryParamMap, getListener);
290 e1.printStackTrace();
296 lockObject.await(10, TimeUnit.SECONDS);
298 catch (InterruptedException e)
302 if(getListenerObject.getEx() == null)
306 result = result && getListenerObject.getRepresentation() != null && getListenerObject.getRepresentation().size() > 0;
308 catch (SimulatorException e)
320 public void testPut_P01()
322 boolean result = true;
323 SimulatorResourceModel model = new SimulatorResourceModel();
325 lockObject = new CountDownLatch(1);
327 ListenerObject listenerObject = null;
331 listenerObject = new ListenerObject();
332 PutListener putListener = new PutListener(lockObject, listenerObject);
334 model.addAttributeInt("intensity", 5);
335 model.addAttributeString("power", "off");
337 simulatorRemoteResource.put(model, null, putListener);
346 lockObject.await(10, TimeUnit.SECONDS);
348 catch (InterruptedException e)
352 assertTrue(result && listenerObject != null && listenerObject.getRepresentation() != null && listenerObject.getuId() != null);
359 public void testPut_N01() {
360 boolean result = true;
361 ListenerObject listenerObject = new ListenerObject();
362 PutListener putListener = new PutListener(lockObject, listenerObject);
365 simulatorRemoteResource.put(null, null, putListener);
367 } catch (Exception e1) {
372 lockObject.await(10, TimeUnit.SECONDS);
373 } catch (InterruptedException e) {
376 assertTrue(result && listenerObject.getRepresentation() == null && listenerObject.getuId() == null);
379 public void testPost_P01()
381 boolean result = true;
382 ListenerObject listenerObject = null;
383 lockObject = new CountDownLatch(1);
385 SimulatorResourceModel model = new SimulatorResourceModel();
388 model.addAttributeInt("intensity", 8);
390 listenerObject = new ListenerObject();
391 PostListener postListener = new PostListener(lockObject, listenerObject);
393 simulatorRemoteResource.post(model, null, postListener);
402 lockObject.await(10, TimeUnit.SECONDS);
404 catch (InterruptedException e)
408 assertTrue(result && listenerObject != null && listenerObject.getRepresentation() != null && listenerObject.getuId() != null);
412 * Model is set to null
415 public void testPost_N01() {
416 boolean result = true;
418 lockObject = new CountDownLatch(1);
420 ListenerObject listenerObject = new ListenerObject();
421 PostListener postListener = new PostListener(lockObject, listenerObject);
424 simulatorRemoteResource.post(null, null, postListener);
426 } catch (Exception e1) {
431 lockObject.await(10, TimeUnit.SECONDS);
432 } catch (InterruptedException e) {
435 assertTrue(result && listenerObject.getRepresentation() == null && listenerObject.getuId() == null);
438 public void testGet_P01()
440 boolean result = true;
441 ListenerObject listenerObject = null;
442 lockObject = new CountDownLatch(1);
446 listenerObject = new ListenerObject();
447 GetListener onGetListener = new GetListener(lockObject, listenerObject);
449 String resInterface = simulatorRemoteResource.getResourceInterfaces().get(0);
451 if(resInterface != null)
452 simulatorRemoteResource.get(resInterface,null, onGetListener);
463 lockObject.await(10, TimeUnit.SECONDS);
465 catch (InterruptedException e)
469 assertTrue(result && listenerObject != null && listenerObject.getRepresentation() != null && listenerObject.getuId() != null);
475 public void testGet_N01()
477 boolean result = false;
478 ListenerObject listenerObject = null;
479 lockObject = new CountDownLatch(1);
483 listenerObject = new ListenerObject();
484 GetListener onGetListener = new GetListener(lockObject, listenerObject);
486 simulatorRemoteResource.get(null, null, onGetListener);
496 lockObject.await(10, TimeUnit.SECONDS);
498 catch (InterruptedException e)
508 public void testGet_N02()
510 boolean result = false;
513 String resInterface = simulatorRemoteResource.getResourceInterfaces().get(0);
515 if(resInterface != null)
517 simulatorRemoteResource.get( resInterface,null, null);
533 public void testGet_N03()
535 boolean result = false;
538 simulatorRemoteResource.get(null, null, null);
549 public void testGetWithoutResInterface_P01()
551 boolean result = true;
552 ListenerObject listenerObject = null;
553 lockObject = new CountDownLatch(1);
557 listenerObject = new ListenerObject();
558 GetListener onGetListener = new GetListener(lockObject, listenerObject);
560 simulatorRemoteResource.get(null, onGetListener);
569 lockObject.await(10, TimeUnit.SECONDS);
571 catch (InterruptedException e)
575 assertTrue(result && listenerObject != null && listenerObject.getRepresentation() != null && listenerObject.getuId() != null);
581 public void testGetWithoutResInterface_N01()
583 boolean result = false;
586 simulatorRemoteResource.get(null, null);
597 public void testSetConfigInfo_P01()
599 boolean result = true;
602 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
606 e2.printStackTrace();
610 lockObject = new CountDownLatch(1);
611 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
612 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
616 simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, verifyListener);
620 e1.printStackTrace();
626 lockObject.await(10, TimeUnit.MILLISECONDS);
628 catch (InterruptedException e)
632 assertTrue(result && verifyListenerObject.getWhichOne().equals("started")&&
633 verifyListenerObject.getuId() != null &&
634 verifyListenerObject.getId() != -1);
640 public void testSetConfigInfo_N01()
642 boolean result = true;
645 simulatorRemoteResource.setConfigInfo("");
656 public void testStartVerification_P01()
658 boolean result = true;
659 lockObject = new CountDownLatch(1);
662 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
666 e1.printStackTrace();
670 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
671 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
674 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, verifyListener) != -1;
678 e1.printStackTrace();
684 lockObject.await(10, TimeUnit.MILLISECONDS);
686 catch (InterruptedException e)
690 assertTrue(result && verifyListenerObject.getWhichOne().equals("started") &&
691 verifyListenerObject.getuId() != null &&
692 verifyListenerObject.getId() != -1);
695 public void testStartVerification_P02()
697 boolean result = true;
698 lockObject = new CountDownLatch(1);
701 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
705 e1.printStackTrace();
709 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
710 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
713 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_PUT, verifyListener) != -1;
717 e1.printStackTrace();
723 lockObject.await(10, TimeUnit.MILLISECONDS);
725 catch (InterruptedException e)
729 assertTrue(result && verifyListenerObject.getWhichOne().equals("started") &&
730 verifyListenerObject.getuId() != null &&
731 verifyListenerObject.getId() != -1);
734 public void testStartVerification_P03()
736 boolean result = true;
737 lockObject = new CountDownLatch(1);
740 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
744 e1.printStackTrace();
748 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
749 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
752 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_GET, verifyListener) != -1;
756 e1.printStackTrace();
762 lockObject.await(10, TimeUnit.MILLISECONDS);
764 catch (InterruptedException e)
768 assertTrue(result && verifyListenerObject.getWhichOne().equals("started") &&
769 verifyListenerObject.getuId() != null &&
770 verifyListenerObject.getId() != -1);
774 * setting listener to null
776 public void testStartVerification_N01()
778 boolean result = true;
781 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
791 result = result && (simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, null) == -1);
801 public void testStopVerification_P01()
803 boolean result = true;
804 lockObject = new CountDownLatch(2);
807 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
811 e1.printStackTrace();
815 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
816 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
819 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, verifyListener) != -1;
823 e1.printStackTrace();
829 lockObject.await(10, TimeUnit.MILLISECONDS);
831 catch (InterruptedException e)
835 result = result && verifyListenerObject.getWhichOne().equals("started") &&
836 verifyListenerObject.getuId() != null &&
837 verifyListenerObject.getId() != -1;
841 simulatorRemoteResource.stopVerification(verifyListenerObject.getId());
845 e1.printStackTrace();
851 lockObject.await(100, TimeUnit.MILLISECONDS);
853 catch (InterruptedException e)
857 assertTrue(result && verifyListenerObject.getWhichOne().equals("aborted") &&
858 verifyListenerObject.getuId() != null &&
859 verifyListenerObject.getId() != -1);
863 * Random id. This is just to check the crash
865 public void testStopVerification_N01()
867 boolean result = true;
868 lockObject = new CountDownLatch(1);
871 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
876 e2.printStackTrace();
879 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
880 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
883 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, verifyListener) != -1;
887 e1.printStackTrace();
893 lockObject.await(10, TimeUnit.SECONDS);
895 catch (InterruptedException e)
899 result = result && verifyListenerObject.getWhichOne().equals("started") &&
900 verifyListenerObject.getuId() != null &&
901 verifyListenerObject.getId() != -1;
905 simulatorRemoteResource.stopVerification(123435);