# Path: scar/providers/oscar/controller.py
# class OSCAR():
# 
#     def __init__(self, func_call: Dict):
#         self.raw_args = FileUtils.load_tmp_config_file()
#         # Flatten the list of services
#         self.oscar_resources = []
#         nested_resources = self.raw_args.get('functions', {}).get('oscar', [])
#         for resources in nested_resources:
#             for cluster_id, resources_info in resources.items():
#                 if ('name' in resources[cluster_id] and
#                         resources[cluster_id]['name']):
#                     resources_info['cluster_id'] = cluster_id
#                     self.oscar_resources.append(resources_info)
#         # Store the storage_providers dict independently
#         self.storage_providers = self.raw_args.get('storage_providers', {})
#         self.scar_info = self.raw_args.get('scar', {})
#         add_output(self.scar_info)
#         # Call the user's command
#         getattr(self, func_call)()
# 
#     @excp.exception(logger)
#     def init(self):
#         for resources_info in self.oscar_resources:
#             resources_info = deepcopy(resources_info)
#             self._create_oscar_service(resources_info)
#             response_parser.parse_service_creation(resources_info, self.scar_info.get('cli_output'))
# 
#     @excp.exception(logger)
#     def rm(self):
#         for resources_info in self.oscar_resources:
#             resources_info = deepcopy(resources_info)
#             credentials_info = _get_credentials_info(resources_info)
#             OSCARClient(credentials_info, resources_info.get('cluster_id', '')).delete_service(resources_info['name'])
#             response_parser.parse_service_deletion(resources_info, self.scar_info.get('cli_output'))
# 
#     def _create_oscar_service(self, resources_info: Dict):
#         credentials_info = _get_credentials_info(resources_info)
#         creation_args = _get_creation_args(resources_info, self.storage_providers)
#         OSCARClient(credentials_info, resources_info.get('cluster_id', '')).create_service(**creation_args)
# 
#     @excp.exception(logger)
#     def ls(self):
#         clusters = self.raw_args.get('functions', {}).get('oscar', [{}])[0]
#         for cluster_id, resources_info in clusters.items():
#             credentials_info = _get_credentials_info(resources_info)
#             if _are_credentials_defined(credentials_info):
#                 oscar_resources = OSCARClient(credentials_info, cluster_id).list_services()
#                 response_parser.parse_ls_response(oscar_resources,
#                                                   credentials_info['endpoint'],
#                                                   cluster_id,
#                                                   self.scar_info.get('cli_output'))


import unittest
import sys
import os
import tempfile
from mock import MagicMock
from mock import patch
from scar.providers.oscar.controller import OSCAR

    def __init__(self, *args):
        unittest.TestCase.__init__(self, *args)

    @patch('scar.providers.oscar.controller.OSCARClient')
    @patch('scar.providers.aws.controller.FileUtils.load_tmp_config_file')
    def test_init(self, load_tmp_config_file, oscar_client):
        tmpfile = tempfile.NamedTemporaryFile(delete=False)
        tmpfile.write(b'Hello world!')
        tmpfile.close()
        load_tmp_config_file.return_value = {"functions": {"oscar": [{"my_oscar": {"name": "oname",
                                                                                   "script": tmpfile.name}}]}}
        ocli = MagicMock(['create_service'])
        oscar_client.return_value = ocli

        OSCAR('init')

        os.unlink(tmpfile.name)
        res = {'name': 'oname', 'script': 'Hello world!',
               'cluster_id': 'my_oscar', 'storage_providers': {}}
        self.assertEqual(ocli.create_service.call_args_list[0][1], res)

    @patch('scar.providers.oscar.controller.OSCARClient')
    @patch('scar.providers.aws.controller.FileUtils.load_tmp_config_file')
    def test_rm(self, load_tmp_config_file, oscar_client):
        load_tmp_config_file.return_value = {"functions": {"oscar": [{"my_oscar": {"name": "oname",
                                                                                   "script": "some.sh"}}]}}
        ocli = MagicMock(['delete_service'])
        oscar_client.return_value = ocli


# Path: scar/providers/oscar/controller.py
# class OSCAR():
# 
#     def __init__(self, func_call: Dict):
#         self.raw_args = FileUtils.load_tmp_config_file()
#         # Flatten the list of services
#         self.oscar_resources = []
#         nested_resources = self.raw_args.get('functions', {}).get('oscar', [])
#         for resources in nested_resources:
#             for cluster_id, resources_info in resources.items():
#                 if ('name' in resources[cluster_id] and
#                         resources[cluster_id]['name']):
#                     resources_info['cluster_id'] = cluster_id
#                     self.oscar_resources.append(resources_info)
#         # Store the storage_providers dict independently
#         self.storage_providers = self.raw_args.get('storage_providers', {})
#         self.scar_info = self.raw_args.get('scar', {})
#         add_output(self.scar_info)
#         # Call the user's command
#         getattr(self, func_call)()
# 
#     @excp.exception(logger)
#     def init(self):
#         for resources_info in self.oscar_resources:
#             resources_info = deepcopy(resources_info)
#             self._create_oscar_service(resources_info)
#             response_parser.parse_service_creation(resources_info, self.scar_info.get('cli_output'))
# 
#     @excp.exception(logger)
#     def rm(self):
#         for resources_info in self.oscar_resources:
#             resources_info = deepcopy(resources_info)
#             credentials_info = _get_credentials_info(resources_info)
#             OSCARClient(credentials_info, resources_info.get('cluster_id', '')).delete_service(resources_info['name'])
#             response_parser.parse_service_deletion(resources_info, self.scar_info.get('cli_output'))
# 
#     def _create_oscar_service(self, resources_info: Dict):
#         credentials_info = _get_credentials_info(resources_info)
#         creation_args = _get_creation_args(resources_info, self.storage_providers)
#         OSCARClient(credentials_info, resources_info.get('cluster_id', '')).create_service(**creation_args)
# 
#     @excp.exception(logger)
#     def ls(self):
#         clusters = self.raw_args.get('functions', {}).get('oscar', [{}])[0]
#         for cluster_id, resources_info in clusters.items():
#             credentials_info = _get_credentials_info(resources_info)
#             if _are_credentials_defined(credentials_info):
#                 oscar_resources = OSCARClient(credentials_info, cluster_id).list_services()
#                 response_parser.parse_ls_response(oscar_resources,
#                                                   credentials_info['endpoint'],
#                                                   cluster_id,
#                                                   self.scar_info.get('cli_output'))


# Path: test/unit/oscar/test_controller.py
import unittest
import sys
import os
import tempfile
from mock import MagicMock
from mock import patch
from scar.providers.oscar.controller import OSCAR


    def __init__(self, *args):
        unittest.TestCase.__init__(self, *args)

    @patch('scar.providers.oscar.controller.OSCARClient')
    @patch('scar.providers.aws.controller.FileUtils.load_tmp_config_file')
    def test_init(self, load_tmp_config_file, oscar_client):
        tmpfile = tempfile.NamedTemporaryFile(delete=False)
        tmpfile.write(b'Hello world!')
        tmpfile.close()
        load_tmp_config_file.return_value = {"functions": {"oscar": [{"my_oscar": {"name": "oname",
                                                                                   "script": tmpfile.name}}]}}
        ocli = MagicMock(['create_service'])
        oscar_client.return_value = ocli

        OSCAR('init')

        os.unlink(tmpfile.name)
        res = {'name': 'oname', 'script': 'Hello world!',
               'cluster_id': 'my_oscar', 'storage_providers': {}}
        self.assertEqual(ocli.create_service.call_args_list[0][1], res)

    @patch('scar.providers.oscar.controller.OSCARClient')
    @patch('scar.providers.aws.controller.FileUtils.load_tmp_config_file')
    def test_rm(self, load_tmp_config_file, oscar_client):
        load_tmp_config_file.return_value = {"functions": {"oscar": [{"my_oscar": {"name": "oname",
                                                                                   "script": "some.sh"}}]}}
        ocli = MagicMock(['delete_service'])
        oscar_client.return_value = ocli


        OSCAR('rm')