1 {-# LANGUAGE ScopedTypeVariables #-}
3 -- | Handle documents defined by Heartbeat.dtd.
5 module TSN.XML.Heartbeat (
12 import Data.Tuple.Curry ( uncurryN )
13 import Test.Tasty ( TestTree, testGroup )
14 import Test.Tasty.HUnit ( (@?=), testCase )
15 import Text.XML.HXT.Core (
26 import TSN.DbImport ( ImportResult(..) )
27 import Xml ( pickle_unpickle, unpickleable )
30 -- | The data structure that holds the XML representation of a
37 time_stamp :: String }
41 -- | A (un)pickler that turns a Heartbeat XML file into a 'Message'
44 pickle_message :: PU Message
47 xpWrap (from_tuple, to_tuple) $
48 xpTriple (xpElem "XML_File_ID" xpInt)
49 (xpElem "heading" xpText)
50 (xpElem "time_stamp" xpText)
52 from_tuple = uncurryN Message
53 to_tuple m = (xml_file_id m,
58 -- | Verify (and report) the received heartbeat. We return
59 -- 'ImportSkipped' because we want to indicate that we processed the
60 -- file but there was nothing to import.
62 verify :: XmlTree -> IO ImportResult
64 let root_element = unpickleDoc pickle_message xml
65 return $ case root_element of
66 Nothing -> ImportFailed "Could not unpickle document to be verified."
67 Just _ -> ImportSkipped "Heartbeat received. Thump."
73 -- | A list of all tests for this module.
75 heartbeat_tests :: TestTree
79 [ test_pickle_of_unpickle_is_identity,
80 test_unpickle_succeeds ]
83 -- | If we unpickle something and then pickle it, we should wind up
84 -- with the same thing we started with. WARNING: success of this
85 -- test does not mean that unpickling succeeded.
87 test_pickle_of_unpickle_is_identity :: TestTree
88 test_pickle_of_unpickle_is_identity =
89 testCase "pickle composed with unpickle is the identity" $ do
90 let path = "test/xml/Heartbeat.xml"
91 (expected :: [Message], actual) <- pickle_unpickle pickle_message path
95 -- | Make sure we can unpickle the sample file.
97 test_unpickle_succeeds :: TestTree
98 test_unpickle_succeeds =
99 testCase "unpickling succeeds" $ do
100 let path = "test/xml/Heartbeat.xml"
101 actual <- unpickleable path pickle_message