# Copyright 2014 Scalyr Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------
#
# author:  Edward Chee <echee@scalyr.com>

__author__ = 'echee@scalyr.com'


import unittest

from scalyr_agent import util
from scalyr_agent.json_lib import JsonObject
from scalyr_agent.json_lib import JsonArray
from scalyr_agent.test_base import ScalyrTestCase

JSON = 1
UJSON = 2
FALLBACK = 3
ALL_JSON_LIBS = [FALLBACK]
ALL_JSON_LIBS_AS_TUPLES = [(lib,) for lib in ALL_JSON_LIBS]


class EncodeDecodeTest(ScalyrTestCase):
    """This test ensures that JsonObject and JsonArray can be correctly encoded/decoded with different JSON libraries"""

    def _setlib(self, library):
        if library == JSON:
            util._set_json_lib('json')
        elif library == UJSON:
            util._set_json_lib('ujson')
        else:
            util._set_json_lib('json_lib')

    def test_dict(self):
        self.__test_encode_decode('{"a":1,"b":2}', {u'a': 1, u'b': 2})

    def test_dict2(self):
        self.__test_encode_decode('{"a":1,"b":{"c":2}}', {u'a': 1, u'b': {u'c': 2}})

    def test_str(self):
        self.__test_encode_decode(r'"a"', u'a')

    def test_int(self):
        self.__test_encode_decode(r'1', 1)

    def test_bool(self):
        self.__test_encode_decode(r'false', False)
        self.__test_encode_decode(r'true', True)

    def test_float(self):
        self.__test_encode_decode(r'1.0003', 1.0003)

    def test_list(self):
        self.__test_encode_decode(r'[1,2,3]', [1, 2, 3])

    def test_list2(self):
        self.__test_encode_decode(r'[1,2,"a"]', [1, 2, u'a'])

    def test_dict(self):
        self.__test_encode_decode(r'{"a":1,"b":2}', JsonObject({u'a': 1, u'b': 2}))

    def test_dict_nested_dict(self):
        self.__test_encode_decode(r'{"a":{"b":{"c":3}}}', JsonObject({u'a': JsonObject({u'b': JsonObject({u'c': 3})})}))

    def test_dict_nested_list(self):
        self.__test_encode_decode(r'{"a":[1,2,3]}', JsonObject({u'a': JsonArray(1, 2, 3)}))

    def test_dict_nested_list2(self):
        self.__test_encode_decode(r'{"a":[1,2,3,[1,2,3]]}', JsonObject({u'a': JsonArray(1, 2, 3, JsonArray(1, 2, 3))}))

    def __test_encode_decode(self, text, obj):
        def __runtest(library):
            self._setlib(library)
            text2 = util.json_encode(obj)
            self.assertEquals(text, text2)

            obj2 = util.json_decode(text2)

            text3 = util.json_encode(obj2)
            self.assertEquals(text, text3)

        __runtest(JSON)
        __runtest(UJSON)
        __runtest(FALLBACK)


def main():
    unittest.main()


if __name__ == '__main__':
    main()

@@ -2151,34 +2151,137 @@
 )%0A%0A    def test_
-di
+jsonarray(self):%0A        self.__test_encode_decode(r'%5B1,2,3%5D', JsonArray(1, 2, 3))%0A%0A    def test_jsonobje
 ct(self):%0A      
@@ -2364,34 +2364,40 @@
 )%0A%0A    def test_
-di
+jsonobje
 ct_nested_dict(s
@@ -2529,34 +2529,40 @@
 )%0A%0A    def test_
-di
+jsonobje
 ct_nested_list(s
@@ -2555,20 +2555,25 @@
 _nested_
-list
+jsonarray
 (self):%0A
@@ -2678,18 +2678,24 @@
 ef test_
-di
+jsonobje
 ct_neste
@@ -2696,20 +2696,25 @@
 _nested_
-list
+jsonarray
 2(self):
@@ -2948,16 +2948,105 @@
 ibrary)%0A
+%0A            if library == FALLBACK or not isinstance(obj, (JsonArray, JsonObject)):%0A    
         
@@ -3079,16 +3079,20 @@
 de(obj)%0A
+    
         
@@ -3122,25 +3122,28 @@
 ext, text2)%0A
-%0A
+    
             
@@ -3173,17 +3173,20 @@
 (text2)%0A
-%0A
+    
         
@@ -3224,32 +3224,36 @@
 j2)%0A            
+    
 self.assertEqual
@@ -3266,16 +3266,127 @@
 , text3)
+%0A            else:%0A                with self.assertRaises(TypeError):%0A                    util.json_encode(obj)
 %0A%0A      

Modify test_encode_decode to expect TypeError for json/ujson