class TestMetricList(unittest.TestCase):

    def setUp(self):
        self.metric_list = MetricList()
        self.text = '''
        {
        "result": [
                    {
                     "name": "BOUNDARY_MOCK_METRIC",
                     "defaultAggregate": "AVG",
                     "defaultResolutionMS": 1000,
                     "description": "BOUNDARY_MOCK_METRIC",
                     "displayName": "BOUNDARY_MOCK_METRIC",
                     "displayNameShort": "BOUNDARY_MOCK_METRIC",
                     "unit": "number",
                     "isDisabled": false,
                     "isBuiltin": false
                    }
                    ]
        }
        '''

        # setup the environment
        self.old_stdout = sys.stdout
        sys.stdout = TextIOWrapper(BytesIO(), sys.stdout.encoding)
        sys.stdout = StringIO.StringIO()

    def tearDown(self):
        # restore stdout
        sys.stdout.close()
        sys.stdout = self.old_stdout

    def test_cli_description(self):
        self.assertEqual(self.metric_list.cli_description, 'Lists the defined metrics in a Boundary account')

    def test_mock_arguments(self):
        pass
        # sys.argv = ['metric-list', '-l', 'debug']
        # self.metric_list.execute();

    @patch('boundary.api_cli.requests')
    def test_execute(self, mock_requests):
        sys.argv = ['metric-list']
        self.maxDiff = None

        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.text = self.text

        mock_requests.get.return_value = mock_response
        self.metric_list.execute()

        # get output
        sys.stdout.seek(0)      # jump to the start
        self.out = sys.stdout.read() # read output
        json1 = json.load(self.out)
        json2 = json.load(self.text)
        self.assertDictEqual(json1, json2)
    def setUp(self):
        self.cli = MetricList()
        self.text = '''
        {
        "result": [ { "name": "BOUNDARY_MOCK_METRIC",
                     "defaultAggregate": "AVG",
                     "defaultResolutionMS": 1000,
                     "description": "BOUNDARY_MOCK_METRIC",
                     "displayName": "BOUNDARY_MOCK_METRIC",
                     "displayNameShort": "BOUNDARY_MOCK_METRIC",
                     "unit": "number",
                     "isDisabled": false,
                     "isBuiltin": false
                    }
                  ]
        }
        '''

        self.out = None
        self.json1 = None
        self.json2 = None
        # setup the environment
        self.old_stdout = sys.stdout
        sys.stdout = TextIOWrapper(BytesIO(), 'utf-8')
        sys.stdout = StringIO.StringIO()
    def test_list_metric(self):
        found = False
        runner_create = CLIRunner(MetricList())

        get = runner_create.get_output([])
        result_get = json.loads(get)
        metric_get = result_get['result']

        for metric in metric_get:
            if metric['name'] == 'CPU':
                found = True
                self.assertEqual('CPU Utilization', metric['displayName'])
                self.assertEqual('CPU', metric['displayNameShort'])
                self.assertTrue(metric['isBuiltin'])
                self.assertFalse(metric['isDisabled'])
                self.assertEqual('percent', metric['unit'])
                self.assertEqual('avg', metric['defaultAggregate'])
                self.assertEqual(1000, metric['defaultResolutionMS'])
                self.assertEqual('Overall CPU utilization',
                                 metric['description'])

        self.assertTrue(found)
    def setUp(self):
        self.metric_list = MetricList()
        self.text = '''
        {
        "result": [
                    {
                     "name": "BOUNDARY_MOCK_METRIC",
                     "defaultAggregate": "AVG",
                     "defaultResolutionMS": 1000,
                     "description": "BOUNDARY_MOCK_METRIC",
                     "displayName": "BOUNDARY_MOCK_METRIC",
                     "displayNameShort": "BOUNDARY_MOCK_METRIC",
                     "unit": "number",
                     "isDisabled": false,
                     "isBuiltin": false
                    }
                    ]
        }
        '''

        # setup the environment
        self.old_stdout = sys.stdout
        sys.stdout = TextIOWrapper(BytesIO(), sys.stdout.encoding)
        sys.stdout = StringIO.StringIO()