def run(self):
     logging.info("Starting application")
     tempTask = TempSensorEmulatorTask()
     i = 0
     #Creating a while loop for taking 10 reading
     while (i < 11):
         tempTask.sendNotification()
         i += 1
         sleep(5)
示例#2
0
 def setUp(self):
     self.config = ConfigUtil()
     self.config.loadConfig('../../../config/ConnectedDevicesConfig.props')
     self.tempsensor = TempSensorEmulatorTask()
     self.sensordata = SensorData()
     self.sensordata.addValue(10)
     self.sensordata.addValue(15)
     self.sensordata.addValue(20)
     self.sensordata.setName('Temperature')
示例#3
0
class TempEmulatorAdaptor(object):
    '''
    classdocs
    '''

    #instantiate the emulator task
    tempSensorEmulator = TempSensorEmulatorTask()
    
    #empty constructor because no extra params need to be passed during instantiation
    def __init__(self):
        '''
        Constructor
        '''
        pass
    
    #method for creating and running the emulator thread
    def run(self):
        
        #enable the emulator 
        self.tempSensorEmulator.enableEmulator = True
        
        #call the generateRandomTemperature
        self.tempSensorEmulator.generateRandomTemperature()
        
        #return true for running successfully
        return True    
示例#4
0
class Module02Test(unittest.TestCase):
    """
	class scoped variables and objects are created
	"""
    confU = ConfigUtil()
    sensorD = SensorData()
    tempSensor = TempSensorEmulatorTask()
    curVal = 0.0
    avgVal = 0.0
    hasConf = ""
    data = None
    '''
	values are setup into the variables
	 '''
    def setUp(self):
        self.data = self.tempSensor.getSensorData()
        self.hasConf = self.confU.hasConfig()
        self.curVal = self.sensorD.getCurrentValue()
        self.avgVal = self.sensorD.getAverageValue()

    """
	All the resources are tear down here.
	"""

    def tearDown(self):
        del self.data
        del self.hasConf
        del self.curVal
        del self.avgVal

    """
	it checks for the datatype of hasConf to be boolean, If true test case is passed.
	"""

    def testConfigUtil(self):
        self.assertTrue(isinstance(self.hasConf, bool), "Boolean Value")

    """
	it checks for the datatype of curVal to be float, If true test case is passed. 
	Second checks for the avgVal to be within the specified range.If true test case is passed. 
	"""

    def testSensorData(self):
        self.assertTrue(isinstance(self.curVal, float), "Float Value")
        self.assertTrue(self.avgVal >= 0.0 and self.avgVal <= 30.0,
                        "Average Temperature within the range")

    '''
	It checks for the datatype of data to be of class type SensorData, If true test case is passed.
	'''

    def testTemperatureSensorEmulatorTask(self):
        self.assertTrue(isinstance(self.data, SensorData),
                        "Sensor Data Type Value")
示例#5
0
    def setUp(self):

        #instantiate the variables required
        self.smtpClientConnector = SmtpClientConnector()
        self.tempSensorEmulatorTask = TempSensorEmulatorTask()
        self.tempEmulatorAdaptor = TempEmulatorAdaptor()
示例#6
0
class Module02Test(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-scopedsou
	instances of complex objects, initialize any requisite connections, etc.
	"""
    def setUp(self):

        #instantiate the variables required
        self.smtpClientConnector = SmtpClientConnector()
        self.tempSensorEmulatorTask = TempSensorEmulatorTask()
        self.tempEmulatorAdaptor = TempEmulatorAdaptor()

    """
	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):

        #set the reference to the variables as none to release the resources they're holding
        self.smtpClientConnector = None
        self.tempSensorEmulatorTask = None
        self.tempEmulatorAdaptor = None

    """
	 This method tests the publishMessage() of SmtpClientConnector module. It checks whether the SmtpClientConnector is able to successfully 
	 send an email or not by testing it in various scenarios like when the configurations are correct, wrong configurations, wrong values 
	 in configurations, connection failure
	"""

    def testPublishMessage(self):

        #when testing on the pipeline on cloud, the config file has been ignored. hence, load sample config file
        if self.smtpClientConnector.config.configFileLoaded == False:

            self.smtpClientConnector.config.loadConfig(
                '../sample/ConnectedDevicesConfig_NO_EDIT_TEMPLATE_ONLY.props')

        #when default config file is loaded properly (testing on the computer)
        if self.smtpClientConnector.config.configFileLoaded == '../../../config/ConnectedDevicesConfig.props':

            #when configurations are correct
            success = self.smtpClientConnector.publishMessage(
                "Testing Message", "This is a test")

            #test true on success of mail
            self.assertEqual(
                success, True,
                "Message was sent successfully, test case showing false")

            #load the sample config file where the fromAddr and toAddr, authToken are not valid
            self.smtpClientConnector.config.loadConfig(
                '../../../sample/ConnectedDevicesConfig_NO_EDIT_TEMPLATE_ONLY.props'
            )

            #test false on success of mail
            success = self.smtpClientConnector.publishMessage(
                "Testing Message", "This is a test")
            self.assertEqual(
                success, False,
                "Message was not sent successfully, test case showing true")

    """
	This method tests the generateRandomTemperature() method of the TempSensorEmulatorTask module. It checks whether the 
	generator runs in various scenarios such as disabling the emulator, setting the number of readings to 0
	"""

    def testGenerateRandomTemperature(self):

        #enable the emulator
        self.tempSensorEmulatorTask.enableEmulator = True

        #change numReadings to a small finite value to check
        self.tempSensorEmulatorTask.numReadings = 1

        #change sleep time (rateInSec) to a small amount
        self.tempSensorEmulatorTask.rateInSec = 1

        #run when numReadings > 0 and adaptor is enabled
        self.assertEqual(
            True, self.tempSensorEmulatorTask.generateRandomTemperature())

        #change numReadings to 0
        self.tempSensorEmulatorTask.numReadings = 0

        #run when numReadings = 0 and emulator is enabled, should return false because generator didn't run
        self.assertEqual(
            False, self.tempSensorEmulatorTask.generateRandomTemperature())

        #disable the emulator
        self.tempSensorEmulatorTask.enableEmulator = False

        #change readings to > 0
        self.tempSensorEmulatorTask.numReadings = 1

        #run when numReadings > 0 and emulator is disabled, should return false because generator didn't run
        self.assertEqual(
            False, self.tempSensorEmulatorTask.generateRandomTemperature())

    """
	This method tests the getSensorData() method of the TempSensorEmulatorTask module. It simply checks
	whether the reference to the sensorData of tempSensorEmulatorTask is valid or not
	"""

    def testGetSensorData(self):

        #check type of the return of the method, should be of type SensorData
        self.assertEqual(type(self.tempSensorEmulatorTask.getSensorData()),
                         SensorData)

        #check if the returned sensorData reference belongs to the tempSensorEmulatorTask's sensorData
        sensorData = self.tempSensorEmulatorTask.getSensorData()
        sensorData.addValue(30)
        self.assertEqual(
            30,
            self.tempSensorEmulatorTask.getSensorData().getCurrentValue())

    """
	 This method tests the sendNotification method of the TempSensorEmulatorTask module. It simply whether
	 the notification is being sent or not. This has been shown in documentation using screenshot of the
	 email
	"""

    def testSendNotification(self):

        #if the config file is loaded: while testing on system
        if self.tempSensorEmulatorTask.smtpConnector.config.fileName == '../sample/ConnectedDevicesConfig_NO_EDIT_TEMPLATE_ONLY.props':

            #returns true, notification is being sent
            self.assertEqual(
                True, self.tempSensorEmulatorTask.sendNotification("Hello"))

    """
	This method simply checks whether the adaptor is running successfully
	"""

    def testTempEmulatorAdaptor(self):

        #get the reference to the tempSensorEmulatorTask
        tempSensTask = self.tempEmulatorAdaptor.tempSensorEmulator

        #change numReadings to a small finite value to check
        tempSensTask.numReadings = 1

        #change sleep time (rateInSec) to a small amount
        tempSensTask.rateInSec = 1

        #enable the tempEmulatorTask's emulator
        tempSensTask.enableEmulator = True

        #run the run function of tempEmulatorAdaptor and get the value of success of the adaptor
        self.assertEqual(True, self.tempEmulatorAdaptor.run())
'''
Created on Jan 28, 2020

@author: sk199
'''
import logging
from labs.module02.TempSensorEmulatorTask import TempSensorEmulatorTask
from time import sleep

logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',
                    level=logging.DEBUG)
logging.info("Starting temperature emulator adaptor  daemon thread...")

tempsensoremulator = TempSensorEmulatorTask()
tempsensoremulator.daemon = True
tempsensoremulator.enableEmulator = True
tempsensoremulator.start()

while (True):
    sleep(10)
    pass
示例#8
0
class Module02Test(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.config = ConfigUtil()
        self.config.loadConfig('../../../config/ConnectedDevicesConfig.props')
        self.tempsensor = TempSensorEmulatorTask()
        self.sensordata = SensorData()
        self.sensordata.addValue(10)
        self.sensordata.addValue(15)
        self.sensordata.addValue(20)
        self.sensordata.setName('Temperature')

    """
	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 testloadConfig(self):
        self.assertTrue(
            self.config.loadConfig(
                '../../../config/ConnectedDevicesConfig.props'))

    def testhasConfigData(self):
        self.assertTrue(self.config.hasConfigData())

    def testgetValue(self):
        self.assertEqual(self.config.getValue("smtp.cloud", "port"), '465')

    def testgetSensorData(self):
        assert self.tempsensor.getSensorData(
        ) > 0 and self.tempsensor.getSensorData() < 30

    def testgetAverageValue(self):
        assert self.sensordata.getAverageValue(
        ) > 0 and self.sensordata.getAverageValue() < 30

    def testgetCount(self):
        self.assertEqual(self.sensordata.getCount(), 3)

    def testgetCurrentValue(self):
        assert self.sensordata.getCurrentValue(
        ) > 0 and self.sensordata.getCurrentValue() < 30

    def testMinValue(self):
        assert self.sensordata.getMinValue(
        ) >= 0 and self.sensordata.getMinValue() < 30

    def testMaxValue(self):
        assert self.sensordata.getMaxValue(
        ) > 0 and self.sensordata.getMaxValue() < 30

    def testName(self):
        self.assertEqual(self.sensordata.getName(), 'Temperature')
 def __init__(self):  #constructor of TempEmulatorAdaptor
     tempSensor = TempSensorEmulatorTask(
     )  #object of TempSensorEmulatorTask is created
     tempSensor.temperatureGenerator(
     )  #temperatureGenerator function is called
 def setUp(self):
     self.emu = TempSensorEmulatorTask()
     self.smtp = SmtpClientConnector()
     self.config = "../../../config/ConnectedDevicesConfig.props"
     self.tempSensor = TempSensorEmulatorTask()
     pass
class Module02Test(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.emu = TempSensorEmulatorTask()
        self.smtp = SmtpClientConnector()
        self.config = "../../../config/ConnectedDevicesConfig.props"
        self.tempSensor = TempSensorEmulatorTask()
        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 testSomething(self):
		pass
	"""
    '@Test'

    #Testing Lower bound of sensor data
    def testgetData(self):
        self.assertGreaterEqual(self.emu.getSensorData(), 0,
                                "Sensor temp less than 0")
        pass

    # Testing SMTP connection and test mail
    def testSmtpData(self):
        self.assertTrue(
            self.smtp.publishMessage(
                "Python Unit Test check",
                "If you are receiving this mail then the python smtp test successful"
            ), "Issue in mail server")
        pass

    # Testing data generation from emulator task
    def testSensorEmulatorTask(self):
        self.assertIsNotNone(self.tempSensor.getSensorData(),
                             "Generated data from getSensordata()")
        pass

    # Testing send notification method
    def testSendNotification(self):
        self.assertIsNotNone(self.tempSensor.sendNotification(),
                             "Generated data from getdata()")
        pass

    # Testing sensor emulator upper bounds
    def testSensorEmulatorTaskUpperBound(self):
        self.assertGreaterEqual(
            30, self.tempSensor.getSensorData(),
            "Generated data from Sensor if greater than 30")
        pass