<commit_before># *****************************************************************************
# Copyright (c) 2016 IBM Corporation and other Contributors.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#   <NAME>  - Initial Contribution
# *****************************************************************************

import ibmiotf.device
import ibmiotf.application
import uuid
import os
from ibmiotf import *
from nose.tools import *
from nose import SkipTest
import logging
import testUtils

class TestDevice(testUtils.AbstractTest):
    registeredDevice = None
    managedClient = None

    DEVICE_TYPE = "test_device"
    DEVICE_ID = str(uuid.uuid4())
    
    @classmethod
    def setup_class(self):
        try: 
            deviceType = self.setupAppClient.api.getDeviceType(self.DEVICE_TYPE)
        except APIException as e:
            if e.httpCode == 404:
                deviceType = self.setupAppClient.api.addDeviceType(self.DEVICE_TYPE)
            else: 
                raise e
        
        self.registeredDevice = self.setupAppClient.api.registerDevice(self.DEVICE_TYPE, self.DEVICE_ID)
        
        self.options={
            "org": self.ORG_ID,
            "type": self.registeredDevice["typeId"],
            "id": self.registeredDevice["deviceId"],
            "auth-method": "token",
            "auth-token": self.registeredDevice["authToken"]
        }
        
        #Create default DeviceInfo Instance and associate with ManagedClient Instance
        deviceInfoObj = ibmiotf.device.DeviceInfo()
        deviceInfoObj.fwVersion = 0.0
        self.managedClient = ibmiotf.device.ManagedClient(self.options, deviceInfo=deviceInfoObj)    

    @classmethod
    def teardown_class(self):
        del self.managedClient
        self.setupAppClient.api.deleteDevice(self.DEVICE_TYPE, self.DEVICE_ID)
    
    
    @raises(Exception)
    def testManagedClientQSException(self):
        with assert_raises(Exception)as e:
            options={"org": "quickstart", "type": self.registeredDevice["typeId"], "id": self.registeredDevice["deviceId"],
                                        "auth-method":"None", "auth-token":"<PASSWORD>" }
            ibmiotf.device.ManagedClient(options)
        assert_equals(e.exception, Exception)

    @SkipTest
    def testManagedClientSetMethods(self):
        self.managedClient.connect()
        #Define device properties to be notified whenever reset
        self.managedClient._deviceMgmtObservations = ["deviceInfo.manufacturer", "deviceInfo.descriptiveLocation",
                                                "deviceInfo.fwVersion", "deviceInfo.model", "deviceInfo.description",
                                                "deviceInfo.deviceClass", "deviceInfo.hwVersion", "deviceInfo.serialNumber"]

        #Reset managedClient properties  and validate the returned is instance of threading.Event
	    if sys.version_info[0] < 3:
             assert_is_instance(self.managedClient.setErrorCode(1),threading._Event)
             assert_is_instance(self.managedClient.setLocation(longitude=200, latitude=278),threading._Event)
             assert_is_instance(self.managedClient.setSerialNumber('iot-device-12345'),threading._Event)
             assert_is_instance(self.managedClient.setManufacturer("IBM India Pvt Ltd"),threading._Event)
             assert_is_instance(self.managedClient.setModel("2016"),threading._Event)
             assert_is_instance(self.managedClient.setdeviceClass("Smart Device"),threading._Event)
             assert_is_instance(self.managedClient.setDescription("Sample Smart IoT Device"),threading._Event)
             assert_is_instance(self.managedClient.setFwVersion("1.0"),threading._Event)
             assert_is_instance(self.managedClient.setHwVersion("2.0"),threading._Event)
             assert_is_instance(self.managedClient.setDescriptiveLocation("ISL Lab Bangalore"),threading._Event)
             assert_is_instance(self.managedClient.clearErrorCodes(),threading._Event)
             assert_is_instance(self.managedClient.addLog(),threading._Event)
             assert_is_instance(self.managedClient.clearLog(),threading._Event)
             assert_is_instance(self.managedClient.unmanage(),threading._Event)
 	    else:
             assert_is_instance(self.managedClient.setErrorCode(1),threading.Event)
             assert_is_instance(self.managedClient.setLocation(longitude=200, latitude=278),threading.Event)
             assert_is_instance(self.managedClient.setSerialNumber('iot-device-12345'),threading.Event)
             assert_is_instance(self.managedClient.setManufacturer("IBM India Pvt Ltd"),threading.Event)
             assert_is_instance(self.managedClient.setModel("2016"),threading.Event)
             assert_is_instance(self.managedClient.setdeviceClass("Smart Device"),threading.Event)
             assert_is_instance(self.managedClient.setDescription("Sample Smart IoT Device"),threading.Event)
             assert_is_instance(self.managedClient.setFwVersion("1.0"),threading.Event)
             assert_is_instance(self.managedClient.setHwVersion("2.0"),threading.Event)
             assert_is_instance(self.managedClient.setDescriptiveLocation("ISL Lab Bangalore"),threading.Event)
             assert_is_instance(self.managedClient.clearErrorCodes(),threading.Event)
             assert_is_instance(self.managedClient.addLog(),threading.Event)
             assert_is_instance(self.managedClient.clearLog(),threading.Event)
             assert_is_instance(self.managedClient.unmanage(),threading.Event)

        self.managedClient.disconnect()

    @SkipTest
    def testDeviceRebootAction(self):
        def rebootActionCB(reqId,action):
            print("Device rebootActionCB called")

        mgmtRequest = {"action": "device/reboot", "parameters": [{"name": "action","value": "reboot" }],
                       "devices": [{ "typeId": self.registeredDevice["typeId"], "deviceId": self.registeredDevice["deviceId"] }]}

        #Setup user defined reboot call back
        self.managedClient.deviceActionCallback = rebootActionCB
        self.managedClient.connect()


        #Initialize device management request to reboot<commit_msg>Updated testManagedClientSetMethods() to suit Python 2.7 and 3.x<commit_after># *****************************************************************************
# Copyright (c) 2016 IBM Corporation and other Contributors.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#   <NAME>  - Initial Contribution
# *****************************************************************************

import ibmiotf.device
import ibmiotf.application
import uuid
import os
from ibmiotf import *
from nose.tools import *
from nose import SkipTest
import logging
import testUtils

class TestDevice(testUtils.AbstractTest):
    registeredDevice = None
    managedClient = None

    DEVICE_TYPE = "test_device"
    DEVICE_ID = str(uuid.uuid4())
    
    @classmethod
    def setup_class(self):
        try: 
            deviceType = self.setupAppClient.api.getDeviceType(self.DEVICE_TYPE)
        except APIException as e:
            if e.httpCode == 404:
                deviceType = self.setupAppClient.api.addDeviceType(self.DEVICE_TYPE)
            else: 
                raise e
        
        self.registeredDevice = self.setupAppClient.api.registerDevice(self.DEVICE_TYPE, self.DEVICE_ID)
        
        self.options={
            "org": self.ORG_ID,
            "type": self.registeredDevice["typeId"],
            "id": self.registeredDevice["deviceId"],
            "auth-method": "token",
            "auth-token": self.registeredDevice["authToken"]
        }
        
        #Create default DeviceInfo Instance and associate with ManagedClient Instance
        deviceInfoObj = ibmiotf.device.DeviceInfo()
        deviceInfoObj.fwVersion = 0.0
        self.managedClient = ibmiotf.device.ManagedClient(self.options, deviceInfo=deviceInfoObj)    

    @classmethod
    def teardown_class(self):
        del self.managedClient
        self.setupAppClient.api.deleteDevice(self.DEVICE_TYPE, self.DEVICE_ID)
    
    
    @raises(Exception)
    def testManagedClientQSException(self):
        with assert_raises(Exception)as e:
            options={"org": "quickstart", "type": self.registeredDevice["typeId"], "id": self.registeredDevice["deviceId"],
                                        "auth-method":"None", "auth-token":"<PASSWORD>" }
            ibmiotf.device.ManagedClient(options)
        assert_equals(e.exception, Exception)

    @SkipTest
    def testManagedClientSetMethods(self):
        self.managedClient.connect()
        #Define device properties to be notified whenever reset
        self.managedClient._deviceMgmtObservations = ["deviceInfo.manufacturer", "deviceInfo.descriptiveLocation",
                                                "deviceInfo.fwVersion", "deviceInfo.model", "deviceInfo.description",
                                                "deviceInfo.deviceClass", "deviceInfo.hwVersion", "deviceInfo.serialNumber"]

        #Reset managedClient properties  and validate the returned is instance of threading.Event
	if sys.version_info[0] < 3:
            assert_is_instance(self.managedClient.setErrorCode(1),threading._Event)
            assert_is_instance(self.managedClient.setLocation(longitude=200, latitude=278),threading._Event)
            assert_is_instance(self.managedClient.setSerialNumber('iot-device-12345'),threading._Event)
            assert_is_instance(self.managedClient.setManufacturer("IBM India Pvt Ltd"),threading._Event)
            assert_is_instance(self.managedClient.setModel("2016"),threading._Event)
            assert_is_instance(self.managedClient.setdeviceClass("Smart Device"),threading._Event)
            assert_is_instance(self.managedClient.setDescription("Sample Smart IoT Device"),threading._Event)
            assert_is_instance(self.managedClient.setFwVersion("1.0"),threading._Event)
            assert_is_instance(self.managedClient.setHwVersion("2.0"),threading._Event)
            assert_is_instance(self.managedClient.setDescriptiveLocation("ISL Lab Bangalore"),threading._Event)
            assert_is_instance(self.managedClient.clearErrorCodes(),threading._Event)
            assert_is_instance(self.managedClient.addLog(),threading._Event)
            assert_is_instance(self.managedClient.clearLog(),threading._Event)
            assert_is_instance(self.managedClient.unmanage(),threading._Event)
 	else:
            assert_is_instance(self.managedClient.setErrorCode(1),threading.Event)
            assert_is_instance(self.managedClient.setLocation(longitude=200, latitude=278),threading.Event)
            assert_is_instance(self.managedClient.setSerialNumber('iot-device-12345'),threading.Event)
            assert_is_instance(self.managedClient.setManufacturer("IBM India Pvt Ltd"),threading.Event)
            assert_is_instance(self.managedClient.setModel("2016"),threading.Event)
            assert_is_instance(self.managedClient.setdeviceClass("Smart Device"),threading.Event)
            assert_is_instance(self.managedClient.setDescription("Sample Smart IoT Device"),threading.Event)
            assert_is_instance(self.managedClient.setFwVersion("1.0"),threading.Event)
            assert_is_instance(self.managedClient.setHwVersion("2.0"),threading.Event)
            assert_is_instance(self.managedClient.setDescriptiveLocation("ISL Lab Bangalore"),threading.Event)
            assert_is_instance(self.managedClient.clearErrorCodes(),threading.Event)
            assert_is_instance(self.managedClient.addLog(),threading.Event)
            assert_is_instance(self.managedClient.clearLog(),threading.Event)
            assert_is_instance(self.managedClient.unmanage(),threading.Event)

        self.managedClient.disconnect()

    @SkipTest
    def testDeviceRebootAction(self):
        def rebootActionCB(reqId,action):
            print("Device rebootActionCB called")

        mgmtRequest = {"action": "device/reboot", "parameters": [{"name": "action","value": "reboot" }],
                       "devices": [{ "typeId": self.registeredDevice["typeId"], "deviceId": self.registeredDevice["deviceId"] }]}

        #Setup user defined reboot call back
        self.managedClient.deviceActionCallback = rebootActionCB
        self.managedClient.connect()


        #Initialize device management request to reboot
{'fraction_non_alphanumeric': 0.12136338819124873, 'fraction_numerical': 0.008969659152952187, 'mean_word_length': 3.3523421588594706, 'pattern_counts': {'":': 34, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6476656', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3319, 'n_words': 540}