import random

class AlmPluginTestHelper(object):
    def setUp(self):
        self.sde_tasks = None
        self.alm_tasks = None
        # Connect to SDE / ALM
        self.assertTrue(self.tac)
        self.tac.sde_connect()
        self.tac.alm_connect()
        self.assertTrue(self.tac.is_sde_connected())

    def _create_test_task(self):
        random_id = 'T%d' % random.randint(1, 999999999)
        random_title = '%s: Test task' % (random_id)
        return  {
            'status': 'TODO',
            'contextrulesets': [],
            'timestamp': 1343386079,
            'note_count': 0,
            'implementations': [],
            'phase': 'requirements',
            'id': random_id,
            'categories': ['Authentication'],
            'priority': 10,
            'weakness': {
                'content': 'This is a test problem generated' +
                ' by a test script',
                'title': 'W9999: Test Weakness',
                'id': 'W9999',
                'cwe_id': 0
            },
            'title': random_title,
            'url': 'https://example.sdelements.com/library/tasks/T99999/',
            'age': 'current',
            'project': 0,
            'assigned_to': ['bob@example.com'],
            'content': 'This is a test task generated by a test script'
        }

    def test_get_task(self):
        """ Verify we can get all SD Elements tasks """
        self.sde_tasks = self.tac.sde_get_tasks()
        self.assertTrue(self.sde_tasks)
        #Check to see that all of the expected fields are there
        for task in self.sde_tasks:
            self.assertTrue(task.has_key('status'))
            self.assertTrue(task.has_key('timestamp'))
            self.assertTrue(task.has_key('phase'))
            self.assertTrue(task.has_key('id'))
            self.assertTrue(task.has_key('priority'))
            self.assertTrue(task.has_key('note_count'))

    def test_add_task(self):
        """ Verify we can add the task to the ALM """
        test_task = self._create_test_task()
        self.tac.alm_add_task(test_task)
        test_task_result = self.tac.alm_get_task(test_task)
        self.assertTrue(test_task_result)

    def test_update_task_status(self):
        test_task = self._create_test_task()
        self.tac.alm_add_task(test_task)
        alm_task = self.tac.alm_get_task(test_task)
        self.tac.alm_update_task_status(alm_task,'DONE')
        test_task_result = self.tac.alm_get_task(test_task)
        self.assertTrue(test_task_result.get_status() == 'DONE')
        test_task2 = self._create_test_task()
        self.tac.alm_add_task(test_task2)
        alm_task2 = self.tac.alm_get_task(test_task2)
        self.tac.alm_update_task_status(alm_task2,'NA')
        test_task2_result = self.tac.alm_get_task(test_task2)
        self.assertTrue((test_task2_result.get_status() == 'DONE') or
                        (test_task2_result.get_status() == 'NA'))
        self.tac.alm_update_task_status(alm_task2,'TODO')
        test_task2_result = self.tac.alm_get_task(test_task2)
        self.assertTrue(test_task2_result.get_status() == 'TODO')

    def test_synchronize(self):
        """ Tests if full-fledged synchronization worked. """
        # TODO: This isn't really a test. We aren't verifying anything
        #       besides the fact it doesn't raise exceptions.
        self.tac.synchronize()

