# Path: data_catalog/configuration.py
# class DCConfig(object):
# 
#     """
#     Contains a configuration of the Data Catalog.
#     No fields should  be written to be the user.
#     """
# 
#     def __init__(self):
#         """
#         Reads the configuration from environment variables set by Cloud Foundry (CF)
#         or by the developer when running locally.
#         Normally, CF sets the full configuration in environment variables,
#         e.g. VCAP_SERVICES (which is most important for us).
#         When working on a local machine, VCAP_SERVICES also needs to be set and contain some values.
#         Without them, the app couldn't work.
#         Most of configuration values have some defaults that will be set when they aren't configured
#         in the environment, but some of them, like the address of the authorization server can't
#         have a default. They need to be set manually by the developer to enable local work.
#         """
#         self._fail_if_no_configuration()
# 
#         self.app_base_path = '/rest/datasets'
# 
#         # if values aren't found we set defaults that are meant for local operations
#         self.app_port = int(os.getenv(VCAP_APP_PORT, '5000'))
#         self.log_level = os.getenv(LOG_LEVEL, 'DEBUG')
# 
#         services_config = json.loads(os.environ[VCAP_SERVICES])
#         self.elastic = ElasticConfig(services_config)
#         self.services_url = ServiceUrlsConfig(services_config)
# 
#     @staticmethod
#     def _fail_if_no_configuration():
#         """
#         Checks the environment variables to decide if the app can run.
#         :rtype: None
#         :raises NoConfigEnvError: When VCAP_SERVICES is not present and the app can't run.
#         """
#         if not VCAP_SERVICES in os.environ:
#             raise NoConfigEnvError(
#                 'VCAP_SERVICES environment variable needs to be set to run Data Catalog.')
# 
# class NoConfigEnvError(Exception):
#     """
#     Environment variable that should contain configuration is missing.
#     """
#     pass
# 
# VCAP_SERVICES = 'VCAP_SERVICES'
# 
# Path: tests/conftest.py
# @contextmanager
# def fake_env():
#     """
#     Sets a fake environment and gets rid of it after the function.
#     """
#     setup_fake_env()
#     try:
#         yield
#     finally:
#         clean_fake_env()
# 
# def clean_fake_env():
#     os.environ.pop(VCAP_SERVICES, None)
#     os.environ.pop(VCAP_APP_PORT, None)
#     os.environ.pop(LOG_LEVEL, None)
#     os.environ.pop(VCAP_APPLICATION, None)


import json
import os
import unittest
from data_catalog.configuration import DCConfig, NoConfigEnvError, VCAP_SERVICES
from .conftest import fake_env, clean_fake_env

        clean_fake_env()

    def test_getConfig_withCloudEnv_returnCloudConfig(self):
        with fake_env():
            config = DCConfig()
            self.assertEqual(5555, config.app_port)
            self.assertEqual('INFO', config.log_level)

            self.assertEqual('10.10.2.7', config.elastic.elastic_hostname)
            self.assertEqual(49237, config.elastic.elastic_port)

            self.assertEqual(
                'http://uaa.run.example.com/token_key',
                config.services_url.uaa_token_uri)
            self.assertEqual(
                'http://hive.apps.example.com/rest/tables',
                config.services_url.dataset_publisher_url)
            self.assertEqual(
                'http://user-management.apps.example.com/rest/orgs/permissions',
                config.services_url.user_management_uri)


    #TODO we should make downloader config not in user-provided services obsolete soon
    def test_getConfig_alternativeDownloaderSetup_downloaderUrlSet(self):
        def set_alternative_downloader_conf():
            vcap_services = json.loads(os.environ[VCAP_SERVICES])
            del vcap_services['downloader']
            os.environ[VCAP_SERVICES] = json.dumps(vcap_services)


# Path: data_catalog/configuration.py
# class DCConfig(object):
# 
#     """
#     Contains a configuration of the Data Catalog.
#     No fields should  be written to be the user.
#     """
# 
#     def __init__(self):
#         """
#         Reads the configuration from environment variables set by Cloud Foundry (CF)
#         or by the developer when running locally.
#         Normally, CF sets the full configuration in environment variables,
#         e.g. VCAP_SERVICES (which is most important for us).
#         When working on a local machine, VCAP_SERVICES also needs to be set and contain some values.
#         Without them, the app couldn't work.
#         Most of configuration values have some defaults that will be set when they aren't configured
#         in the environment, but some of them, like the address of the authorization server can't
#         have a default. They need to be set manually by the developer to enable local work.
#         """
#         self._fail_if_no_configuration()
# 
#         self.app_base_path = '/rest/datasets'
# 
#         # if values aren't found we set defaults that are meant for local operations
#         self.app_port = int(os.getenv(VCAP_APP_PORT, '5000'))
#         self.log_level = os.getenv(LOG_LEVEL, 'DEBUG')
# 
#         services_config = json.loads(os.environ[VCAP_SERVICES])
#         self.elastic = ElasticConfig(services_config)
#         self.services_url = ServiceUrlsConfig(services_config)
# 
#     @staticmethod
#     def _fail_if_no_configuration():
#         """
#         Checks the environment variables to decide if the app can run.
#         :rtype: None
#         :raises NoConfigEnvError: When VCAP_SERVICES is not present and the app can't run.
#         """
#         if not VCAP_SERVICES in os.environ:
#             raise NoConfigEnvError(
#                 'VCAP_SERVICES environment variable needs to be set to run Data Catalog.')
# 
# class NoConfigEnvError(Exception):
#     """
#     Environment variable that should contain configuration is missing.
#     """
#     pass
# 
# VCAP_SERVICES = 'VCAP_SERVICES'
# 
# Path: tests/conftest.py
# @contextmanager
# def fake_env():
#     """
#     Sets a fake environment and gets rid of it after the function.
#     """
#     setup_fake_env()
#     try:
#         yield
#     finally:
#         clean_fake_env()
# 
# def clean_fake_env():
#     os.environ.pop(VCAP_SERVICES, None)
#     os.environ.pop(VCAP_APP_PORT, None)
#     os.environ.pop(LOG_LEVEL, None)
#     os.environ.pop(VCAP_APPLICATION, None)


# Path: tests/test_configuration.py
import json
import os
import unittest
from data_catalog.configuration import DCConfig, NoConfigEnvError, VCAP_SERVICES
from .conftest import fake_env, clean_fake_env


        clean_fake_env()

    def test_getConfig_withCloudEnv_returnCloudConfig(self):
        with fake_env():
            config = DCConfig()
            self.assertEqual(5555, config.app_port)
            self.assertEqual('INFO', config.log_level)

            self.assertEqual('10.10.2.7', config.elastic.elastic_hostname)
            self.assertEqual(49237, config.elastic.elastic_port)

            self.assertEqual(
                'http://uaa.run.example.com/token_key',
                config.services_url.uaa_token_uri)
            self.assertEqual(
                'http://hive.apps.example.com/rest/tables',
                config.services_url.dataset_publisher_url)
            self.assertEqual(
                'http://user-management.apps.example.com/rest/orgs/permissions',
                config.services_url.user_management_uri)


    #TODO we should make downloader config not in user-provided services obsolete soon
    def test_getConfig_alternativeDownloaderSetup_downloaderUrlSet(self):
        def set_alternative_downloader_conf():
            vcap_services = json.loads(os.environ[VCAP_SERVICES])
            del vcap_services['downloader']
            os.environ[VCAP_SERVICES] = json.dumps(vcap_services)


        with fake_env():