use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::entities::{
AncillaryLocation, ArtifactsLocations, CardanoDbBeacon, CompressionAlgorithm, DigestLocation,
Epoch, ImmutablesLocation,
};
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactsLocationsMessagePart {
pub digests: Vec<DigestLocation>,
pub immutables: Vec<ImmutablesLocation>,
pub ancillary: Vec<AncillaryLocation>,
}
impl From<ArtifactsLocations> for ArtifactsLocationsMessagePart {
fn from(part: ArtifactsLocations) -> Self {
Self {
digests: part.digests,
immutables: part.immutables,
ancillary: part.ancillary,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CardanoDatabaseSnapshotMessage {
pub merkle_root: String,
pub beacon: CardanoDbBeacon,
pub certificate_hash: String,
pub total_db_size_uncompressed: u64,
pub locations: ArtifactsLocationsMessagePart,
pub compression_algorithm: CompressionAlgorithm,
pub cardano_node_version: String,
pub created_at: DateTime<Utc>,
}
impl CardanoDatabaseSnapshotMessage {
pub fn dummy() -> Self {
Self {
merkle_root: "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6"
.to_string(),
beacon: CardanoDbBeacon {
epoch: Epoch(123),
immutable_file_number: 2345,
},
certificate_hash: "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb"
.to_string(),
total_db_size_uncompressed: 800796318,
created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
.unwrap()
.with_timezone(&Utc),
locations: ArtifactsLocationsMessagePart {
digests: vec![DigestLocation::Aggregator {
uri: "https://host-1/digest-1".to_string(),
}],
immutables: vec![
ImmutablesLocation::CloudStorage {
uri: "https://host-1/immutables-2".to_string(),
},
ImmutablesLocation::CloudStorage {
uri: "https://host-2/immutables-2".to_string(),
},
],
ancillary: vec![AncillaryLocation::CloudStorage {
uri: "https://host-1/ancillary-3".to_string(),
}],
},
compression_algorithm: CompressionAlgorithm::Gzip,
cardano_node_version: "0.0.1".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const ACTUAL_JSON: &str = r#"
{
"merkle_root": "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6",
"beacon": {
"epoch": 123,
"immutable_file_number": 2345
},
"certificate_hash": "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb",
"total_db_size_uncompressed": 800796318,
"locations": {
"digests": [
{
"type": "aggregator",
"uri": "https://host-1/digest-1"
}
],
"immutables": [
{
"type": "cloud_storage",
"uri": "https://host-1/immutables-2"
},
{
"type": "cloud_storage",
"uri": "https://host-2/immutables-2"
}
],
"ancillary": [
{
"type": "cloud_storage",
"uri": "https://host-1/ancillary-3"
}
]
},
"compression_algorithm": "gzip",
"cardano_node_version": "0.0.1",
"created_at": "2023-01-19T13:43:05.618857482Z"
}"#;
fn golden_actual_message() -> CardanoDatabaseSnapshotMessage {
CardanoDatabaseSnapshotMessage {
merkle_root: "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6"
.to_string(),
beacon: CardanoDbBeacon {
epoch: Epoch(123),
immutable_file_number: 2345,
},
certificate_hash: "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb"
.to_string(),
total_db_size_uncompressed: 800796318,
created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
.unwrap()
.with_timezone(&Utc),
locations: ArtifactsLocationsMessagePart {
digests: vec![DigestLocation::Aggregator {
uri: "https://host-1/digest-1".to_string(),
}],
immutables: vec![
ImmutablesLocation::CloudStorage {
uri: "https://host-1/immutables-2".to_string(),
},
ImmutablesLocation::CloudStorage {
uri: "https://host-2/immutables-2".to_string(),
},
],
ancillary: vec![AncillaryLocation::CloudStorage {
uri: "https://host-1/ancillary-3".to_string(),
}],
},
compression_algorithm: CompressionAlgorithm::Gzip,
cardano_node_version: "0.0.1".to_string(),
}
}
#[test]
fn test_actual_json_deserialized_into_actual_message() {
let json = ACTUAL_JSON;
let message: CardanoDatabaseSnapshotMessage = serde_json::from_str(json).expect(
"This JSON is expected to be successfully parsed into a CardanoDatabaseSnapshotMessage instance.",
);
assert_eq!(golden_actual_message(), message);
}
}