class InstancesDaoTests(TestCase):
    def setUp(self):
        self.class_ = {
            '_id': 'test',
            'description': 'Descripcion test',
            'default_version': "1.0"
        }
        self.instance = {
            'uri': 'http://test',
            'version': '1.0',
            'class_name': 'test'
        }
        self.instance_new = {
            'uri': 'http://testNew',
            'version': '1.0',
            'class_name': 'test'
        }
        self.dao = ServiceInstanceDao()
        self.classes_dao = ServiceClassDao()
        self.classes_dao.create(self.class_)
        self.dao.create(self.instance)
        super(InstancesDaoTests, self).setUp()

    def tearDown(self):
        self.dao.delete(self.instance['_id'])
        self.classes_dao.delete(self.class_['_id'])

    def test_add_new_instance_should_work(self):
        self.dao.create(self.instance_new)
        created_instance = self.dao.find(self.instance_new['_id'])
        self.assertEquals(created_instance, self.instance_new)
        # Removing created instance
        self.dao.delete(created_instance['_id'])

    def test_find_existing_instance_should_work(self):
        # _id is an objectId but we convert to str representation
        existing_instance = self.dao.find(str(self.instance['_id']))
        self.assertEquals(existing_instance, self.instance)

    def test_find_instance_by_class_name_and_id_invalid_id_should_return_none(
            self):
        # _id is an objectId but we convert to str representation
        res = self.dao.find_by_class_name_and_id('test', 'invalid_id')
        self.assertEquals(None, res)

    def test_find_all_existing_instance_should_work(self):
        # _id is an objectId but we convert to str representation
        instances_list = self.dao.find_all('test')
        self.assertEquals(1, len(list(instances_list)))

    def test_add_existing_instance_should_raiseException(self):
        """
        We check than uri, version and class_name are unique for instances collection
        """
        # We don't use self.instance as _id will be populated by setUp method
        # We want ton ensure unique index is being managed
        self.assertRaises(DuplicateKeyError, self.dao.create, {
            'uri': 'http://test',
            'version': '1.0',
            'class_name': 'test'
        })

    def test_modify_existing_instance_should_return_true(self):
        self.instance['ob'] = 'oba'
        res = self.dao.update(self.instance)
        self.assertEquals(True, res, 'ob param not added')

    def test_modify_invalid_instance_id_should_return_false(self):
        self.instance_new['_id'] = '8989_nonExisting'
        res = self.dao.update(self.instance_new)
        self.assertEquals(
            False, res, 'Non existing instance was modified and it shouldnt')

    def test_modify_non_existing_instance_should_return_false(self):
        self.instance_new['_id'] = '5555b1d2f26ba3088459ffff'
        res = self.dao.update(self.instance_new)
        self.assertEquals(
            False, res, 'Non existing instance was modified and it shouldnt')

    def test_modify_existing_instance_matching_another_should_return_DuplicateKeyException(
            self):
        instance_new = self.dao.create(self.instance_new)
        instance_new['uri'] = self.instance['uri']
        instance_new['version'] = self.instance['version']
        self.assertRaises(DuplicateKeyError, self.dao.update, instance_new)

    def test_delete_invalid_instance_id_should_return_false(self):
        res = self.dao.delete('non_exitings_id')
        self.assertEquals(res, False,
                          'Delete not existing instance didnt return false')

    def test_delete_unexisting_instance_id_should_return_false(self):
        res = self.dao.delete('5555b1d2f26ba3088459ffff')
        self.assertEquals(res, False,
                          'Delete not existing instance didnt return false')
Exemple #2
0
class ServiceInstanceService(BaseService):
    """
    Service to handle ServiceInstance objects with mongodb and provide
    all business logic needed to views layer
    """
    def __init__(self):
        self.instance_dao = ServiceInstanceDao()
        self.class_dao = ServiceClassDao()
        self.binding_dao = BindingDao()

    def create(self, obj):
        if not self.class_dao.find(obj['class_name']):
            raise NotFoundException(obj['class_name'])
        try:
            return self.instance_dao.create(obj)
        except DuplicateKeyError:
            raise UnsupportedParameterValueException(
                '{0}-{1}-{2}'.format(obj['class_name'], obj['uri'],
                                     obj['version']),
                'non-duplicated-instance')

    def get_service_instance(self, class_name, instance_id):
        class_obj = self.class_dao.find(class_name)
        if class_obj:
            instance = self.instance_dao.find(instance_id)
            if instance:
                if instance['class_name'] == class_name:
                    return instance
                else:
                    raise UnsupportedParameterValueException(
                        '{0}-{1}'.format(class_name, instance_id),
                        'instances-of-class')
            else:
                raise NotFoundException(instance_id)
        else:
            raise NotFoundException(class_name)

    def update(self, obj):
        # if not instance is found, not found would be raised in serializers
        try:
            if self.instance_dao.update(obj):
                return obj
            else:
                raise GenericServiceError(
                    'The instance update process failed.')
        except DuplicateKeyError:
            raise UnsupportedParameterValueException(
                '{0}-{1}-{2}'.format(obj['class_name'], obj['uri'],
                                     obj['version']),
                'non-duplicated-instance')

    def delete(self, class_name, instance_id):
        class_obj = self.class_dao.find(class_name)
        if class_obj:
            instance = self.instance_dao.find(instance_id)
            if instance:
                if instance['class_name'] == class_name:
                    if self.instance_dao.delete(instance_id):
                        return
                    else:
                        raise GenericServiceError(
                            'The instance delete process failed.')
                else:
                    raise UnsupportedParameterValueException(
                        '{0}-{1}'.format(class_name, instance_id),
                        'instances-of-class')
            else:
                raise NotFoundException(instance_id)
        raise NotFoundException(class_name)

    def discover_service_instances(self, class_name, params):
        if 'class_name' in params:
            raise BadParameterException('class_name')
        class_obj = self.class_dao.find(class_name)
        if not class_obj:
            raise NotFoundException(class_name)
        params['class_name'] = class_name
        return self.instance_dao.find_instances(params)