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.serviceprovider.test;
19 import java.util.Vector;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
23 import junit.framework.TestCase;
25 import org.oic.simulator.InvalidArgsException;
26 import org.oic.simulator.ResourceAttribute;
27 import org.oic.simulator.SimulatorException;
28 import org.oic.simulator.SimulatorManager;
29 import org.oic.simulator.SimulatorResourceModel;
30 import org.oic.simulator.serviceprovider.AutomationType;
31 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
32 import org.oic.simulator.test.ResourceModelChangeListener;
33 import org.oic.simulator.test.ResourceModelObject;
36 * This class tests the functionality of Simulator Resource Server
39 public class SimlatorResourceServerTest extends TestCase
42 private static final String CONFIG_PATH = "./ramls/simple-light.raml";
44 private static final String KEY = "testkey";
46 private CountDownLatch lockObject;
47 private ResourceModelObject resourceModelObject;
48 private ResourceModelChangeListener resourceModelChangeListener;
50 private SimulatorResourceServer simulatorResourceServer;
54 System.loadLibrary("SimulatorManager");
55 System.loadLibrary("RamlParser");
56 System.loadLibrary("oc");
57 System.loadLibrary("oc_logger");
58 System.loadLibrary("octbstack");
61 protected void setUp() throws Exception
64 lockObject= new CountDownLatch(1);
65 resourceModelObject = new ResourceModelObject();
66 resourceModelChangeListener = new ResourceModelChangeListener(resourceModelObject);
67 simulatorResourceServer = SimulatorManager.createResource(CONFIG_PATH, resourceModelChangeListener);
70 protected void tearDown() throws Exception
74 SimulatorManager.deleteResource(simulatorResourceServer);
77 resourceModelObject = null;
78 resourceModelChangeListener = null;
80 simulatorResourceServer = null;
83 public void testGetURI_P01()
85 assertNotNull(simulatorResourceServer.getURI());
88 public void testGetResourceType_P01()
90 assertNotNull(simulatorResourceServer.getResourceType());
93 public void testGetInterfaceType_P01()
95 assertNotNull(simulatorResourceServer.getInterfaceType());
98 public void testGetModel_P01()
100 boolean result = false;
104 if(simulatorResourceServer.getModel() != null && simulatorResourceServer.getModel().size() > 0)
107 catch (InvalidArgsException e)
111 catch (SimulatorException e)
119 public void testAddAttributeInteger_P01()
123 simulatorResourceServer.addAttributeInteger(KEY, 2);
129 assertEquals(Integer.parseInt(getValue(KEY) + ""), 2);
132 public void testAddAttributeDouble_P01()
136 simulatorResourceServer.addAttributeDouble(KEY, 4.0);
142 assertEquals(Double.parseDouble(getValue(KEY) + ""), 4.0);
145 public void testAddAttributeBoolean_P01()
149 simulatorResourceServer.addAttributeBoolean(KEY, Boolean.parseBoolean("true"));
155 assertEquals(Boolean.parseBoolean(getValue(KEY).toString() + ""), true);
158 public void testaddAttributeString_P01()
162 simulatorResourceServer.addAttributeString(KEY, "test");
168 assertEquals(getValue(KEY) + "", "test");
171 public void testUpdateAttributeInteger_P01()
173 boolean result = true;
177 simulatorResourceServer.addAttributeInteger(KEY, 10);
185 result = result && Integer.parseInt(getValue(KEY) + "") == 10;
189 simulatorResourceServer.updateAttributeInteger(KEY, 12);
196 result = result && Integer.parseInt(getValue(KEY) + "") == 12;
201 public void testUpdateAttributeDouble_P01()
203 boolean result = true;
207 simulatorResourceServer.addAttributeDouble(KEY, 22.0);
215 result = result && Double.parseDouble(getValue(KEY) + "") == 22.0;
219 simulatorResourceServer.updateAttributeDouble(KEY, 25.3);
227 result = result && Double.parseDouble(getValue(KEY) + "") == 25.3;
232 public void testUpdateAttributeBoolean_P01()
234 boolean result = true;
238 simulatorResourceServer.addAttributeBoolean(KEY, Boolean.parseBoolean("true"));
246 result = result && Boolean.parseBoolean(getValue(KEY) + "")==true;
250 simulatorResourceServer.updateAttributeBoolean(KEY, Boolean.parseBoolean("false"));
258 result = result && !Boolean.parseBoolean(getValue(KEY) + "");
263 public void testupdateAttributeString_P01()
265 boolean result = true;
269 simulatorResourceServer.addAttributeString(KEY, "old");
277 result = result && getValue(KEY).equals("old");
281 simulatorResourceServer.updateAttributeString(KEY, "new");
289 result = result && getValue(KEY).equals("new");
294 public void testSetRange_P01()
296 boolean result = true;
300 simulatorResourceServer.addAttributeInteger(KEY, 10);
301 simulatorResourceServer.setRange(KEY, 1, 12);
309 result = result && Integer.parseInt(getValue(KEY) + "") == 10;
313 simulatorResourceServer.updateAttributeInteger(KEY, 3);
321 result = result && Integer.parseInt(getValue(KEY) + "") == 3;
327 * Try to set the value out of range
329 public void testSetRange_N01()
331 boolean result = true;
335 simulatorResourceServer.addAttributeInteger(KEY, 10);
336 simulatorResourceServer.setRange(KEY, 1, 12);
344 result = result && Integer.parseInt(getValue(KEY) + "") == 10;
348 simulatorResourceServer.updateAttributeInteger(KEY, 13);
356 result = result && Integer.parseInt(getValue(KEY) + "") == 10;
361 public void testSetAllowedValuesInteger_P01()
363 boolean result = true;
367 simulatorResourceServer.addAttributeInteger(KEY, 10);
375 Vector<Integer> values = new Vector<Integer>();
383 simulatorResourceServer.setAllowedValuesInteger(KEY, values);
391 result = result && Integer.parseInt(getValue(KEY) + "") == 10;
395 simulatorResourceServer.updateAttributeInteger(KEY, 20);
403 result = result && Integer.parseInt(getValue(KEY) + "") == 20;
409 * Try setting with out of range
411 public void testSetAllowedValuesInteger_N01()
413 boolean result = true;
417 simulatorResourceServer.addAttributeInteger(KEY, 10);
425 Vector<Integer> values = new Vector<Integer>();
433 simulatorResourceServer.setAllowedValuesInteger(KEY, values);
441 result = result && Integer.parseInt(getValue(KEY) + "") == 10;
445 simulatorResourceServer.updateAttributeInteger(KEY, 2);
453 result = result && Integer.parseInt(getValue(KEY) + "") == 10;
459 * Try setting values multiple times
461 public void testSetAllowedValuesDouble_P01()
463 boolean result = true;
467 simulatorResourceServer.addAttributeDouble(KEY, 11.5);
475 Vector<Double> values = new Vector<Double>();
483 simulatorResourceServer.setAllowedValuesDouble(KEY, values);
491 result = result && Double.parseDouble(getValue(KEY) + "") == 11.5;
495 simulatorResourceServer.updateAttributeDouble(KEY, 10.5);
503 result = result && Double.parseDouble(getValue(KEY) + "") == 10.5;
509 * Try setting with out of range
511 public void testSetAllowedValuesDouble_N01()
513 boolean result = true;
517 simulatorResourceServer.addAttributeDouble(KEY, 10.5);
525 Vector<Double> values = new Vector<Double>();
533 simulatorResourceServer.setAllowedValuesDouble(KEY, values);
541 result = result && Double.parseDouble(getValue(KEY) + "") == 10.5;
545 simulatorResourceServer.updateAttributeDouble(KEY, 2.2);
553 result = result && Double.parseDouble(getValue(KEY) + "") == 10.5;
558 public void testsetAllowedValuesString_P01()
560 boolean result = true;
564 simulatorResourceServer.addAttributeString(KEY, "mon");
572 Vector<String> values = new Vector<String>();
579 simulatorResourceServer.setAllowedValuesString(KEY, values);
587 result = result && getValue(KEY).equals("mon");
591 simulatorResourceServer.updateAttributeString(KEY, "tue");
599 result = result && getValue(KEY).equals("tue");
605 * Set the value that is not in allowed values
607 public void testsetAllowedValuesString_N01()
609 boolean result = true;
613 simulatorResourceServer.addAttributeString(KEY, "mon");
621 Vector<String> values = new Vector<String>();
628 simulatorResourceServer.setAllowedValuesString(KEY, values);
636 result = result && getValue(KEY).equals("mon");
640 simulatorResourceServer.updateAttributeString(KEY, "thu");
648 result = result && getValue(KEY).equals("mon");
653 public void testRemoveAttribute_P01()
655 boolean result = true;
659 simulatorResourceServer.addAttributeString(KEY, "fri");
667 result = result && getValue(KEY).equals("fri");
671 simulatorResourceServer.removeAttribute(KEY);
672 result = result && !simulatorResourceServer.getModel().getAttributes().containsKey(KEY);
684 * Try removing the attribute thats not present
686 public void testRemoveAttribute_N01()
688 boolean result = false;
692 simulatorResourceServer.removeAttribute("something");
703 * Try removing when null is passed
705 public void testRemoveAttribute_N02()
707 boolean result = false;
711 simulatorResourceServer.removeAttribute(null);
722 * Try removing when attribute is empty
724 public void testRemoveAttribute_N03()
726 boolean result = false;
730 simulatorResourceServer.removeAttribute("");
740 public void testStartResourceAutomation_P01()
742 boolean result = true;
743 lockObject = new CountDownLatch(1);
744 AutomationObject automationObject = new AutomationObject();
745 AutomationListener automationListener = new AutomationListener(lockObject, automationObject);
749 id = simulatorResourceServer.startResourceAutomation(AutomationType.NORMAL, automationListener);
759 lockObject.await(15,TimeUnit.SECONDS);
761 catch (InterruptedException e)
766 assertTrue(result && automationObject.getResourceURI() != null && automationObject.getAutomationId() != -1 && id != -1);
770 simulatorResourceServer.stopAutomation(id);
781 public void testStartResourceAutomation_N01()
783 boolean result = true;
787 id = simulatorResourceServer.startResourceAutomation(AutomationType.NORMAL, null);
793 assertTrue(!result && id == 0);
796 public void testStartAttributeAutomation_P01()
798 boolean result = true;
799 lockObject = new CountDownLatch(1);
800 AutomationObject automationObject = new AutomationObject();
801 AutomationListener automationListener = new AutomationListener(lockObject, automationObject);
805 simulatorResourceServer.addAttributeInteger(KEY, 10);
806 id = simulatorResourceServer.startAttributeAutomation(KEY,
807 AutomationType.NORMAL, automationListener);
816 lockObject.await(10, TimeUnit.SECONDS);
818 catch (InterruptedException e)
822 assertTrue(result && automationObject.getResourceURI() != null && automationObject.getAutomationId() != -1 && id != -1);
826 simulatorResourceServer.stopAutomation(id);
837 public void testStartAttributeAutomation_N01()
839 boolean result = false;
843 id = simulatorResourceServer.startAttributeAutomation(simulatorResourceServer.getModel().getAttributes().get(0).getName(),
844 AutomationType.NORMAL, null);
852 assertTrue(result && id != -1);
855 public void testStopAutomation_P01()
857 boolean result = true;
859 lockObject = new CountDownLatch(1);
861 AutomationObject automationObject = new AutomationObject();
862 AutomationListener automationListener = new AutomationListener(lockObject, automationObject);
867 id = simulatorResourceServer.startResourceAutomation(AutomationType.NORMAL, automationListener);
876 lockObject.await(05, TimeUnit.SECONDS);
878 catch (InterruptedException e)
884 simulatorResourceServer.stopAutomation(id);
898 public void testStopAutomation_N01()
900 boolean result = false;
903 simulatorResourceServer.stopAutomation(144353544);
913 private Object getValue(Object key)
915 SimulatorResourceModel simulatorResourceModel = null;
918 simulatorResourceModel = simulatorResourceServer.getModel();
925 ResourceAttribute resourceAttribute = null;
928 resourceAttribute = simulatorResourceModel.getAttributes().get(key);
930 catch (SimulatorException e)
934 return resourceAttribute.getValue();