module Test.Ouroboros.Network.PeerSelection.Json (tests) where

import Data.Aeson (decode, encode, fromJSON, toJSON)
import Test.Ouroboros.Network.PeerSelection.Instances ()

import Ouroboros.Network.PeerSelection.PeerAdvertise (PeerAdvertise)
import Ouroboros.Network.PeerSelection.RelayAccessPoint (DomainAccessPoint,
           RelayAccessPoint)
import Test.QuickCheck
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)

tests :: TestTree
tests :: TestTree
tests =
  TestName -> [TestTree] -> TestTree
testGroup TestName
"Ouroboros.Network.PeerSelection"
  [ TestName -> [TestTree] -> TestTree
testGroup TestName
"JSON"
    [ TestName -> (DomainAccessPoint -> Property) -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty TestName
"DomainAccessPoint roundtrip" DomainAccessPoint -> Property
prop_roundtrip_DomainAccessPoint_JSON
    , TestName -> (RelayAccessPoint -> Property) -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty TestName
"RelayAccessPoint roundtrip"  RelayAccessPoint -> Property
prop_roundtrip_RelayAccessPoint_JSON
    , TestName -> (PeerAdvertise -> Property) -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty TestName
"PeerAdvertise roundtrip"     PeerAdvertise -> Property
prop_roundtrip_PeerAdvertise_JSON
    ]
  ]

prop_roundtrip_DomainAccessPoint_JSON :: DomainAccessPoint -> Property
prop_roundtrip_DomainAccessPoint_JSON :: DomainAccessPoint -> Property
prop_roundtrip_DomainAccessPoint_JSON DomainAccessPoint
da =
    ByteString -> Maybe DomainAccessPoint
forall a. FromJSON a => ByteString -> Maybe a
decode (DomainAccessPoint -> ByteString
forall a. ToJSON a => a -> ByteString
encode DomainAccessPoint
da) Maybe DomainAccessPoint -> Maybe DomainAccessPoint -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== DomainAccessPoint -> Maybe DomainAccessPoint
forall a. a -> Maybe a
Just DomainAccessPoint
da
    Property -> Property -> Property
forall prop1 prop2.
(Testable prop1, Testable prop2) =>
prop1 -> prop2 -> Property
.&&.
    Value -> Result DomainAccessPoint
forall a. FromJSON a => Value -> Result a
fromJSON (DomainAccessPoint -> Value
forall a. ToJSON a => a -> Value
toJSON DomainAccessPoint
da) Result DomainAccessPoint -> Result DomainAccessPoint -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== DomainAccessPoint -> Result DomainAccessPoint
forall a. a -> Result a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DomainAccessPoint
da

prop_roundtrip_RelayAccessPoint_JSON :: RelayAccessPoint -> Property
prop_roundtrip_RelayAccessPoint_JSON :: RelayAccessPoint -> Property
prop_roundtrip_RelayAccessPoint_JSON RelayAccessPoint
ra =
    ByteString -> Maybe RelayAccessPoint
forall a. FromJSON a => ByteString -> Maybe a
decode (RelayAccessPoint -> ByteString
forall a. ToJSON a => a -> ByteString
encode RelayAccessPoint
ra) Maybe RelayAccessPoint -> Maybe RelayAccessPoint -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== RelayAccessPoint -> Maybe RelayAccessPoint
forall a. a -> Maybe a
Just RelayAccessPoint
ra
    Property -> Property -> Property
forall prop1 prop2.
(Testable prop1, Testable prop2) =>
prop1 -> prop2 -> Property
.&&.
    Value -> Result RelayAccessPoint
forall a. FromJSON a => Value -> Result a
fromJSON (RelayAccessPoint -> Value
forall a. ToJSON a => a -> Value
toJSON RelayAccessPoint
ra) Result RelayAccessPoint -> Result RelayAccessPoint -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== RelayAccessPoint -> Result RelayAccessPoint
forall a. a -> Result a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RelayAccessPoint
ra

prop_roundtrip_PeerAdvertise_JSON :: PeerAdvertise -> Property
prop_roundtrip_PeerAdvertise_JSON :: PeerAdvertise -> Property
prop_roundtrip_PeerAdvertise_JSON PeerAdvertise
pa =
    ByteString -> Maybe PeerAdvertise
forall a. FromJSON a => ByteString -> Maybe a
decode (PeerAdvertise -> ByteString
forall a. ToJSON a => a -> ByteString
encode PeerAdvertise
pa) Maybe PeerAdvertise -> Maybe PeerAdvertise -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== PeerAdvertise -> Maybe PeerAdvertise
forall a. a -> Maybe a
Just PeerAdvertise
pa
    Property -> Property -> Property
forall prop1 prop2.
(Testable prop1, Testable prop2) =>
prop1 -> prop2 -> Property
.&&.
    Value -> Result PeerAdvertise
forall a. FromJSON a => Value -> Result a
fromJSON (PeerAdvertise -> Value
forall a. ToJSON a => a -> Value
toJSON PeerAdvertise
pa) Result PeerAdvertise -> Result PeerAdvertise -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== PeerAdvertise -> Result PeerAdvertise
forall a. a -> Result a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PeerAdvertise
pa