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);
355 public void testPost_P01()
357 boolean result = true;
358 ListenerObject listenerObject = null;
359 lockObject = new CountDownLatch(1);
361 SimulatorResourceModel model = new SimulatorResourceModel();
364 model.addAttributeInt("intensity", 8);
365 //model.addAttributeString("power", "off");
367 listenerObject = new ListenerObject();
368 PostListener postListener = new PostListener(lockObject, listenerObject);
370 simulatorRemoteResource.post(model, null, postListener);
379 lockObject.await(10, TimeUnit.SECONDS);
381 catch (InterruptedException e)
385 assertTrue(result && listenerObject != null && listenerObject.getRepresentation() != null && listenerObject.getuId() != null);
388 public void testGet_P01()
390 boolean result = true;
391 ListenerObject listenerObject = null;
392 lockObject = new CountDownLatch(1);
396 listenerObject = new ListenerObject();
397 GetListener onGetListener = new GetListener(lockObject, listenerObject);
399 String resInterface = simulatorRemoteResource.getResourceInterfaces().get(0);
401 if(resInterface == null)
402 simulatorRemoteResource.get(resInterface, null, onGetListener);
413 lockObject.await(10, TimeUnit.SECONDS);
415 catch (InterruptedException e)
419 assertTrue(result && listenerObject != null && listenerObject.getRepresentation() != null && listenerObject.getuId() != null);
425 public void testGet_N01()
427 boolean result = false;
428 ListenerObject listenerObject = null;
429 lockObject = new CountDownLatch(1);
433 listenerObject = new ListenerObject();
434 GetListener onGetListener = new GetListener(lockObject, listenerObject);
436 simulatorRemoteResource.get(null, null, onGetListener);
446 lockObject.await(10, TimeUnit.SECONDS);
448 catch (InterruptedException e)
458 public void testGet_N02()
460 boolean result = false;
463 String resInterface = simulatorRemoteResource.getResourceInterfaces().get(0);
465 if(resInterface == null)
466 simulatorRemoteResource.get(resInterface, null, null);
481 public void testGet_N03()
483 boolean result = false;
486 simulatorRemoteResource.get(null, null, null);
497 public void testGetWithoutResInterface_P01()
499 boolean result = true;
500 ListenerObject listenerObject = null;
501 lockObject = new CountDownLatch(1);
505 listenerObject = new ListenerObject();
506 GetListener onGetListener = new GetListener(lockObject, listenerObject);
508 simulatorRemoteResource.get(null, onGetListener);
517 lockObject.await(10, TimeUnit.SECONDS);
519 catch (InterruptedException e)
523 assertTrue(result && listenerObject != null && listenerObject.getRepresentation() != null && listenerObject.getuId() != null);
529 public void testGetWithoutResInterface_N01()
531 boolean result = false;
534 simulatorRemoteResource.get(null, null);
545 public void testSetConfigInfo_P01()
547 boolean result = true;
550 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
554 e2.printStackTrace();
558 lockObject = new CountDownLatch(1);
559 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
560 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
564 simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, verifyListener);
568 e1.printStackTrace();
574 lockObject.await(10, TimeUnit.MILLISECONDS);
576 catch (InterruptedException e)
580 assertTrue(result && verifyListenerObject.getWhichOne().equals("started")&&
581 verifyListenerObject.getuId() != null &&
582 verifyListenerObject.getId() != -1);
588 public void testSetConfigInfo_N01()
590 boolean result = true;
593 simulatorRemoteResource.setConfigInfo("");
604 public void testStartVerification_P01()
606 boolean result = true;
607 lockObject = new CountDownLatch(1);
610 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
614 e1.printStackTrace();
618 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
619 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
622 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, verifyListener) != -1;
626 e1.printStackTrace();
632 lockObject.await(10, TimeUnit.MILLISECONDS);
634 catch (InterruptedException e)
638 assertTrue(result && verifyListenerObject.getWhichOne().equals("started") &&
639 verifyListenerObject.getuId() != null &&
640 verifyListenerObject.getId() != -1);
643 public void testStartVerification_P02()
645 boolean result = true;
646 lockObject = new CountDownLatch(1);
649 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
653 e1.printStackTrace();
657 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
658 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
661 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_PUT, verifyListener) != -1;
665 e1.printStackTrace();
671 lockObject.await(10, TimeUnit.MILLISECONDS);
673 catch (InterruptedException e)
677 assertTrue(result && verifyListenerObject.getWhichOne().equals("started") &&
678 verifyListenerObject.getuId() != null &&
679 verifyListenerObject.getId() != -1);
682 public void testStartVerification_P03()
684 boolean result = true;
685 lockObject = new CountDownLatch(1);
688 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
692 e1.printStackTrace();
696 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
697 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
700 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_GET, verifyListener) != -1;
704 e1.printStackTrace();
710 lockObject.await(10, TimeUnit.MILLISECONDS);
712 catch (InterruptedException e)
716 assertTrue(result && verifyListenerObject.getWhichOne().equals("started") &&
717 verifyListenerObject.getuId() != null &&
718 verifyListenerObject.getId() != -1);
722 * setting listener to null
724 public void testStartVerification_N01()
726 boolean result = true;
729 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
739 result = result && (simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, null) == -1);
749 public void testStopVerification_P01()
751 boolean result = true;
752 lockObject = new CountDownLatch(2);
755 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
759 e1.printStackTrace();
763 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
764 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
767 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, verifyListener) != -1;
771 e1.printStackTrace();
777 lockObject.await(10, TimeUnit.MILLISECONDS);
779 catch (InterruptedException e)
783 result = result && verifyListenerObject.getWhichOne().equals("started") &&
784 verifyListenerObject.getuId() != null &&
785 verifyListenerObject.getId() != -1;
789 simulatorRemoteResource.stopVerification(verifyListenerObject.getId());
793 e1.printStackTrace();
800 lockObject.await(100, TimeUnit.MILLISECONDS);
802 catch (InterruptedException e)
806 assertTrue(result && verifyListenerObject.getWhichOne().equals("aborted") &&
807 verifyListenerObject.getuId() != null &&
808 verifyListenerObject.getId() != -1);
812 * Random id. This is just to check the crash
814 public void testStopVerification_N01()
816 boolean result = true;
817 lockObject = new CountDownLatch(1);
820 simulatorRemoteResource.setConfigInfo(CONFIG_PATH);
825 e2.printStackTrace();
828 VerifyListenerObject verifyListenerObject = new VerifyListenerObject();
829 VerifyListener verifyListener = new VerifyListener(lockObject, verifyListenerObject);
832 result = result && simulatorRemoteResource.startVerification(SimulatorVerificationType.RQ_TYPE_POST, verifyListener) != -1;
836 e1.printStackTrace();
842 lockObject.await(10, TimeUnit.SECONDS);
844 catch (InterruptedException e)
848 result = result && verifyListenerObject.getWhichOne().equals("started") &&
849 verifyListenerObject.getuId() != null &&
850 verifyListenerObject.getId() != -1;
854 simulatorRemoteResource.stopVerification(123435);