示例#1
0
class Module07Test(unittest.TestCase):
    """
	Use this to setup your tests. This is where you may want to load configuration
	information (if needed), initialize class-scoped variables, create class-scoped
	instances of complex objects, initialize any requisite connections, etc.
	"""
    def setUp(self):
        host = '127.0.0.1'
        #port number to connect
        port = 5683
        #resource uri
        path = 'temperature'
        self.coap_client = CoapClientConnector(host, port, path)

    """
	Use this to tear down any allocated resources after your tests are complete. This
	is where you may want to release connections, zero out any long-term data, etc.
	"""

    def tearDown(self):
        #setting to none to destroy
        self.coap_client = None

    """
 Method to check ping 
	"""

    def test_pinging(self):
        """start ping test"""
        assert (self.coap_client.ping())

    """	Method to check GET	"""

    def test_get_method(self):
        """start get test"""
        assert (self.coap_client.get())

    """
 Method to check PUT
	"""

    def test_put_method(self):
        #demo json data
        jsonData = "{'name': 'temperature'}"
        assert (self.coap_client.put(jsonData))

    """
 Method to check POST
	"""

    def test_post_method(self):
        jsonData = "{'name': 'temperature'}"
        assert (self.coap_client.post(jsonData))

    """
 Method to check DELETE	
	"""

    def test_delete_method(self):
        assert (self.coap_client.delete())
class Module07Test(unittest.TestCase):
    """
	Use this to setup your tests. This is where you may want to load configuration
	information (if needed), initialize class-scoped variables, create class-scoped
	instances of complex objects, initialize any requisite connections, etc.
	"""
    def setUp(self):
        self.ccc = CoapClientConnector()
        pass

    """
	Use this to tear down any allocated resources after your tests are complete. This
	is where you may want to release connections, zero out any long-term data, etc.
	"""

    def tearDown(self):
        pass

    """
	Place your comments describing the test here.
	"""

    def testSend(self):
        message = "Test"
        self.assertTrue(self.ccc.send(message), "Message issues")

    def testPut(self):
        message = "Test"
        self.assertTrue(self.ccc.put(message), "put issues")

    def testGet(self):
        message = "Test"
        self.assertTrue(self.ccc.get(), "get issues")

    def testPost(self):
        message = "Test"
        self.assertTrue(self.ccc.put(message), "put issues")

    def testDelete(self):
        message = "Test"
        self.assertTrue(self.ccc.delete(), "delete issues")
示例#3
0
class Module07Test(unittest.TestCase):
    """
	Use this to setup your tests. This is where you may want to load configuration
	information (if needed), initialize class-scoped variables, create class-scoped
	instances of complex objects, initialize any requisite connections, etc.
	"""
    def setUp(self):

        #getting the host data
        host = ConfigUtil().getProperty(ConfigConst.COAP_DEVICE_SECTION,
                                        ConfigConst.HOST_KEY)

        #port number to connect
        port = int(ConfigUtil().getProperty(ConfigConst.COAP_DEVICE_SECTION,
                                            ConfigConst.PORT_KEY))

        #resource uri
        path = 'Temperature Resource'

        #connecting to my CoAp client connector
        self.coap_client = CoapClientConnector(host, port, path)

    """
	Use this to tear down any allocated resources after your tests are complete. This
	is where you may want to release connections, zero out any long-term data, etc.
	"""

    def tearDown(self):
        self.coap_client = None

    """
	Unit Test Method to check my ping functionality
	"""

    def test_ping(self):
        assert (self.coap_client.ping())

    """
	Unit Test Method to check my GET functionality
	"""

    def test_get(self):
        assert (self.coap_client.get())

    """
	Unit Test Method to check my PUT functionality
	"""

    def test_put(self):
        jsonData = "{'name': 'temperature'}"
        assert (self.coap_client.put(jsonData))

    """
	Unit Test Method to check my POST functionality
	"""

    def test_post(self):
        jsonData = "{'name': 'temperature'}"
        assert (self.coap_client.post(jsonData))

    """
	Unit Test Method to check my DELETE functionality
	"""

    def test_delete(self):
        assert (self.coap_client.delete())
config = ConfigUtil('../../../config/ConnectedDevicesConfig.props')
config.loadConfig()

data = DataUtil()

host = config.getProperty(ConfigConst.ConfigConst.COAP_DEVICE_SECTION, ConfigConst.ConfigConst.HOST_KEY)
port = int(config.getProperty(ConfigConst.ConfigConst.COAP_DEVICE_SECTION, ConfigConst.ConfigConst.PORT_KEY))
path = 'temperature'

sensor = SensorData("Temperature",0.0,30.0)
sensor.addValue(4.0)
print(str(sensor))

coapClient = CoapClientConnector(host, port, path)

coapClient.ping()
json_data = data.SensorDataToJson(sensor)
print("json"+json_data)

#coapClient.get() #Get will respond with NOT_FOUND since data object on server is not initialized
coapClient.post((json_data)) #Post JSON to server
coapClient.get()

sensor.addValue(5.00)
coapClient.put(data.SensorDataToJson(sensor)) #Update resource on the server
coapClient.get()

coapClient.delete() #Delete resource
coapClient.get()

coapClient.stop()
'''initialise variable for DataUtil class'''

data = DataUtil()
#ip/ or hostname direct
host = '127.0.0.1'  #config.getProperty(ConfigConst.COAP_DEVICE_SECTION, ConfigConst.HOST_KEY)
#port number initialized
port = 5683  #int(config.getProperty(ConfigConst.COAP_DEVICE_SECTION, ConfigConst.PORT_KEY))
#equivalent to channel in mqtt
path = 'temperature'
#instance variable for SensorData class
sensorData = SensorData()

#add new sensordata value
sensorData.addvalue(20.00)
#call the Coapclient connector
coapClient_obj = CoapClientConnector(host, port, path)
'''ping request'''
coapClient_obj.ping()
'''get request'''
coapClient_obj.get()
'''post request'''
coapClient_obj.post(data.tojsonfromSensorData(sensorData))
'''add new value to sensor data'''
sensorData.addvalue(15.00)
'''put request'''
coapClient_obj.put(data.tojsonfromSensorData(sensorData))
'''delete request'''
coapClient_obj.delete()
'''stop the Coap client'''
coapClient_obj.stop()
#creating my instance variable for SensorData class
sensorData = SensorData()

#adding a new sensordata value
sensorData.addValue(10.00)

#calling the Coapclient connector
coapClient = CoapClientConnector(host, port, path)

#ping request
coapClient.ping()

#get request
coapClient.get()  

#post request
coapClient.post(data.toJsonFromSensorData(sensorData))  

#add new value to sensor data
sensorData.addValue(5.00)

#put request
coapClient.put(data.toJsonFromSensorData(sensorData))  

#delete request
coapClient.delete()  

#stop the Coap client
coapClient.stop()