| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for ganeti-htools. |
| 5 |
|
| 6 |
-} |
| 7 |
|
| 8 |
{- |
| 9 |
|
| 10 |
Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are |
| 15 |
met: |
| 16 |
|
| 17 |
1. Redistributions of source code must retain the above copyright notice, |
| 18 |
this list of conditions and the following disclaimer. |
| 19 |
|
| 20 |
2. Redistributions in binary form must reproduce the above copyright |
| 21 |
notice, this list of conditions and the following disclaimer in the |
| 22 |
documentation and/or other materials provided with the distribution. |
| 23 |
|
| 24 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 25 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 26 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 27 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
-} |
| 37 |
|
| 38 |
module Test.Ganeti.JSON (testJSON) where |
| 39 |
|
| 40 |
import Control.Monad |
| 41 |
import Data.List |
| 42 |
import Test.HUnit |
| 43 |
import Test.QuickCheck |
| 44 |
|
| 45 |
import qualified Text.JSON as J |
| 46 |
|
| 47 |
import Test.Ganeti.TestHelper |
| 48 |
import Test.Ganeti.TestCommon |
| 49 |
import Test.Ganeti.Types () |
| 50 |
|
| 51 |
import qualified Ganeti.BasicTypes as BasicTypes |
| 52 |
import Ganeti.JSON (nestedAccessByKey, nestedAccessByKeyDotted) |
| 53 |
import qualified Ganeti.JSON as JSON |
| 54 |
|
| 55 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 56 |
|
| 57 |
instance (Arbitrary a) => Arbitrary (JSON.MaybeForJSON a) where |
| 58 |
arbitrary = liftM JSON.MaybeForJSON arbitrary |
| 59 |
|
| 60 |
instance Arbitrary JSON.TimeAsDoubleJSON where |
| 61 |
arbitrary = liftM JSON.TimeAsDoubleJSON arbitrary |
| 62 |
|
| 63 |
prop_toArray :: [Int] -> Property |
| 64 |
prop_toArray intarr = |
| 65 |
let arr = map J.showJSON intarr in |
| 66 |
case JSON.toArray (J.JSArray arr) of |
| 67 |
BasicTypes.Ok arr' -> arr ==? arr' |
| 68 |
BasicTypes.Bad err -> failTest $ "Failed to parse array: " ++ err |
| 69 |
|
| 70 |
prop_toArrayFail :: Int -> String -> Bool -> Property |
| 71 |
prop_toArrayFail i s b = |
| 72 |
-- poor man's instance Arbitrary JSValue |
| 73 |
forAll (elements [J.showJSON i, J.showJSON s, J.showJSON b]) $ \item -> |
| 74 |
case JSON.toArray item::BasicTypes.Result [J.JSValue] of |
| 75 |
BasicTypes.Bad _ -> passTest |
| 76 |
BasicTypes.Ok result -> failTest $ "Unexpected parse, got " ++ show result |
| 77 |
|
| 78 |
arrayMaybeToJson :: (J.JSON a) => [Maybe a] -> String -> JSON.JSRecord |
| 79 |
arrayMaybeToJson xs k = [(k, J.JSArray $ map sh xs)] |
| 80 |
where |
| 81 |
sh x = case x of |
| 82 |
Just v -> J.showJSON v |
| 83 |
Nothing -> J.JSNull |
| 84 |
|
| 85 |
prop_arrayMaybeFromObj :: String -> [Maybe Int] -> String -> Property |
| 86 |
prop_arrayMaybeFromObj t xs k = |
| 87 |
case JSON.tryArrayMaybeFromObj t (arrayMaybeToJson xs k) k of |
| 88 |
BasicTypes.Ok xs' -> xs' ==? xs |
| 89 |
BasicTypes.Bad e -> failTest $ "Parsing failing, got: " ++ show e |
| 90 |
|
| 91 |
prop_arrayMaybeFromObjFail :: String -> String -> Property |
| 92 |
prop_arrayMaybeFromObjFail t k = |
| 93 |
case JSON.tryArrayMaybeFromObj t [] k of |
| 94 |
BasicTypes.Ok r -> property |
| 95 |
(fail $ "Unexpected result, got: " ++ show (r::[Maybe Int]) |
| 96 |
:: Gen Property) |
| 97 |
BasicTypes.Bad e -> conjoin [ Data.List.isInfixOf t e ==? True |
| 98 |
, Data.List.isInfixOf k e ==? True |
| 99 |
] |
| 100 |
|
| 101 |
prop_MaybeForJSON_serialisation :: JSON.MaybeForJSON String -> Property |
| 102 |
prop_MaybeForJSON_serialisation = testSerialisation |
| 103 |
|
| 104 |
prop_TimeAsDoubleJSON_serialisation :: JSON.TimeAsDoubleJSON -> Property |
| 105 |
prop_TimeAsDoubleJSON_serialisation = testSerialisation |
| 106 |
|
| 107 |
isJError :: J.Result a -> Bool |
| 108 |
isJError (J.Error _) = True |
| 109 |
isJError _ = False |
| 110 |
|
| 111 |
case_nestedAccessByKey :: Assertion |
| 112 |
case_nestedAccessByKey = do |
| 113 |
J.Ok v <- return $ J.decode "{\"key1\": {\"key2\": \"val\"}}" |
| 114 |
|
| 115 |
nestedAccessByKey [] v @?= J.Ok v |
| 116 |
|
| 117 |
nestedAccessByKey ["key1", "key2"] v |
| 118 |
@?= J.Ok (J.JSString $ J.toJSString "val") |
| 119 |
|
| 120 |
assertBool "access to nonexistent key should fail" |
| 121 |
. isJError $ nestedAccessByKey ["key1", "nonexistent"] v |
| 122 |
|
| 123 |
case_nestedAccessByKeyDotted :: Assertion |
| 124 |
case_nestedAccessByKeyDotted = do |
| 125 |
J.Ok v <- return $ J.decode "{\"key1\": {\"key2\": \"val\"}}" |
| 126 |
|
| 127 |
assertBool "access to empty key should fail" |
| 128 |
. isJError $ nestedAccessByKeyDotted "" v |
| 129 |
|
| 130 |
nestedAccessByKeyDotted "key1.key2" v |
| 131 |
@?= J.Ok (J.JSString $ J.toJSString "val") |
| 132 |
|
| 133 |
assertBool "access to nonexistent key should fail" |
| 134 |
. isJError $ nestedAccessByKeyDotted "key1.nonexistent" v |
| 135 |
|
| 136 |
testSuite "JSON" |
| 137 |
[ 'prop_toArray |
| 138 |
, 'prop_toArrayFail |
| 139 |
, 'prop_arrayMaybeFromObj |
| 140 |
, 'prop_arrayMaybeFromObjFail |
| 141 |
, 'prop_MaybeForJSON_serialisation |
| 142 |
, 'prop_TimeAsDoubleJSON_serialisation |
| 143 |
, 'case_nestedAccessByKey |
| 144 |
, 'case_nestedAccessByKeyDotted |
| 145 |
] |