| 1 |
//! Useful JSContact Functions |
| 2 |
//! |
| 3 |
//! The [JsContactConvert] trait implements methods to convert RDAP objects |
| 4 |
//! to JSContact. |
| 5 |
|
| 6 |
use crate::{ |
| 7 |
contact::Contact, |
| 8 |
prelude::{ |
| 9 |
Autnum, AutnumSearchResults, Domain, DomainSearchResults, Entity, EntitySearchResults, |
| 10 |
IpSearchResults, Nameserver, NameserverSearchResults, Network, RdapResponse, ToResponse, |
| 11 |
}, |
| 12 |
}; |
| 13 |
|
| 14 |
pub trait JsContactConvert { |
| 15 |
/// Converts an object to JSContact if it has vCard and no JSContact. |
| 16 |
fn to_jscontact(self) -> Self; |
| 17 |
|
| 18 |
/// Calls [JsContactConvert::to_jscontact] and the removes vCard. |
| 19 |
fn only_jscontact(self) -> Self; |
| 20 |
} |
| 21 |
|
| 22 |
impl JsContactConvert for RdapResponse { |
| 23 |
fn to_jscontact(self) -> Self { |
| 24 |
match self { |
| 25 |
RdapResponse::Entity(entity) => entity.to_jscontact().to_response(), |
| 26 |
RdapResponse::Domain(domain) => domain.to_jscontact().to_response(), |
| 27 |
RdapResponse::Nameserver(nameserver) => nameserver.to_jscontact().to_response(), |
| 28 |
RdapResponse::Autnum(autnum) => autnum.to_jscontact().to_response(), |
| 29 |
RdapResponse::Network(network) => network.to_jscontact().to_response(), |
| 30 |
RdapResponse::DomainSearchResults(domain_search_results) => { |
| 31 |
domain_search_results.to_jscontact().to_response() |
| 32 |
} |
| 33 |
RdapResponse::EntitySearchResults(entity_search_results) => { |
| 34 |
entity_search_results.to_jscontact().to_response() |
| 35 |
} |
| 36 |
RdapResponse::NameserverSearchResults(nameserver_search_results) => { |
| 37 |
nameserver_search_results.to_jscontact().to_response() |
| 38 |
} |
| 39 |
RdapResponse::IpSearchResults(ip_search_results) => { |
| 40 |
ip_search_results.to_jscontact().to_response() |
| 41 |
} |
| 42 |
RdapResponse::AutnumSearchResults(autnum_search_results) => { |
| 43 |
autnum_search_results.to_jscontact().to_response() |
| 44 |
} |
| 45 |
RdapResponse::Rpki1Roa(r) => r.to_response(), |
| 46 |
RdapResponse::Rpki1Aspa(r) => r.to_response(), |
| 47 |
RdapResponse::Rpki1X509ResourceCert(r) => r.to_response(), |
| 48 |
RdapResponse::Rpki1RoaSearchResults(r) => r.to_response(), |
| 49 |
RdapResponse::Rpki1AspaSearchResults(r) => r.to_response(), |
| 50 |
RdapResponse::Rpki1X509ResourceCertSearchResults(r) => r.to_response(), |
| 51 |
RdapResponse::ErrorResponse(rfc9083_error) => rfc9083_error.to_response(), |
| 52 |
RdapResponse::Help(help) => help.to_response(), |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
fn only_jscontact(self) -> Self { |
| 57 |
match self { |
| 58 |
RdapResponse::Entity(entity) => entity.only_jscontact().to_response(), |
| 59 |
RdapResponse::Domain(domain) => domain.only_jscontact().to_response(), |
| 60 |
RdapResponse::Nameserver(nameserver) => nameserver.only_jscontact().to_response(), |
| 61 |
RdapResponse::Autnum(autnum) => autnum.only_jscontact().to_response(), |
| 62 |
RdapResponse::Network(network) => network.only_jscontact().to_response(), |
| 63 |
RdapResponse::DomainSearchResults(domain_search_results) => { |
| 64 |
domain_search_results.only_jscontact().to_response() |
| 65 |
} |
| 66 |
RdapResponse::EntitySearchResults(entity_search_results) => { |
| 67 |
entity_search_results.only_jscontact().to_response() |
| 68 |
} |
| 69 |
RdapResponse::NameserverSearchResults(nameserver_search_results) => { |
| 70 |
nameserver_search_results.only_jscontact().to_response() |
| 71 |
} |
| 72 |
RdapResponse::IpSearchResults(ip_search_results) => { |
| 73 |
ip_search_results.only_jscontact().to_response() |
| 74 |
} |
| 75 |
RdapResponse::AutnumSearchResults(autnum_search_results) => { |
| 76 |
autnum_search_results.only_jscontact().to_response() |
| 77 |
} |
| 78 |
RdapResponse::Rpki1Roa(r) => r.to_response(), |
| 79 |
RdapResponse::Rpki1Aspa(r) => r.to_response(), |
| 80 |
RdapResponse::Rpki1X509ResourceCert(r) => r.to_response(), |
| 81 |
RdapResponse::Rpki1RoaSearchResults(r) => r.to_response(), |
| 82 |
RdapResponse::Rpki1AspaSearchResults(r) => r.to_response(), |
| 83 |
RdapResponse::Rpki1X509ResourceCertSearchResults(r) => r.to_response(), |
| 84 |
RdapResponse::ErrorResponse(rfc9083_error) => rfc9083_error.to_response(), |
| 85 |
RdapResponse::Help(help) => help.to_response(), |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl JsContactConvert for Entity { |
| 91 |
fn to_jscontact(self) -> Self { |
| 92 |
let new_jscontact = if self.jscontact_card.is_none() { |
| 93 |
if let Some(ref vcard_array) = self.vcard_array { |
| 94 |
Contact::from_vcard(vcard_array).map(|contact| contact.to_jscontact()) |
| 95 |
} else { |
| 96 |
self.jscontact_card |
| 97 |
} |
| 98 |
} else { |
| 99 |
self.jscontact_card |
| 100 |
}; |
| 101 |
Self { |
| 102 |
object_common: super::ObjectCommon { |
| 103 |
entities: self.object_common.entities.map(|v| v.to_jscontact()), |
| 104 |
..self.object_common |
| 105 |
}, |
| 106 |
jscontact_card: new_jscontact, |
| 107 |
..self |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
fn only_jscontact(self) -> Self { |
| 112 |
let mut entity = self.to_jscontact(); |
| 113 |
entity.object_common.entities = entity.object_common.entities.map(|ve| ve.only_jscontact()); |
| 114 |
entity.vcard_array = None; |
| 115 |
entity |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl JsContactConvert for Vec<Entity> { |
| 120 |
fn to_jscontact(self) -> Self { |
| 121 |
self.into_iter().map(|e| e.to_jscontact()).collect() |
| 122 |
} |
| 123 |
|
| 124 |
fn only_jscontact(self) -> Self { |
| 125 |
self.into_iter().map(|e| e.only_jscontact()).collect() |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
impl JsContactConvert for Network { |
| 130 |
fn to_jscontact(self) -> Self { |
| 131 |
Self { |
| 132 |
object_common: super::ObjectCommon { |
| 133 |
entities: self.object_common.entities.map(|v| v.to_jscontact()), |
| 134 |
..self.object_common |
| 135 |
}, |
| 136 |
..self |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
fn only_jscontact(self) -> Self { |
| 141 |
Self { |
| 142 |
object_common: super::ObjectCommon { |
| 143 |
entities: self.object_common.entities.map(|v| v.only_jscontact()), |
| 144 |
..self.object_common |
| 145 |
}, |
| 146 |
..self |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
impl JsContactConvert for Domain { |
| 152 |
fn to_jscontact(self) -> Self { |
| 153 |
Self { |
| 154 |
object_common: super::ObjectCommon { |
| 155 |
entities: self.object_common.entities.map(|v| v.to_jscontact()), |
| 156 |
..self.object_common |
| 157 |
}, |
| 158 |
..self |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
fn only_jscontact(self) -> Self { |
| 163 |
Self { |
| 164 |
object_common: super::ObjectCommon { |
| 165 |
entities: self.object_common.entities.map(|v| v.only_jscontact()), |
| 166 |
..self.object_common |
| 167 |
}, |
| 168 |
..self |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
impl JsContactConvert for Autnum { |
| 174 |
fn to_jscontact(self) -> Self { |
| 175 |
Self { |
| 176 |
object_common: super::ObjectCommon { |
| 177 |
entities: self.object_common.entities.map(|v| v.to_jscontact()), |
| 178 |
..self.object_common |
| 179 |
}, |
| 180 |
..self |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
fn only_jscontact(self) -> Self { |
| 185 |
Self { |
| 186 |
object_common: super::ObjectCommon { |
| 187 |
entities: self.object_common.entities.map(|v| v.only_jscontact()), |
| 188 |
..self.object_common |
| 189 |
}, |
| 190 |
..self |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
impl JsContactConvert for Nameserver { |
| 196 |
fn to_jscontact(self) -> Self { |
| 197 |
Self { |
| 198 |
object_common: super::ObjectCommon { |
| 199 |
entities: self.object_common.entities.map(|v| v.to_jscontact()), |
| 200 |
..self.object_common |
| 201 |
}, |
| 202 |
..self |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
fn only_jscontact(self) -> Self { |
| 207 |
Self { |
| 208 |
object_common: super::ObjectCommon { |
| 209 |
entities: self.object_common.entities.map(|v| v.only_jscontact()), |
| 210 |
..self.object_common |
| 211 |
}, |
| 212 |
..self |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
impl JsContactConvert for DomainSearchResults { |
| 218 |
fn to_jscontact(self) -> Self { |
| 219 |
Self { |
| 220 |
results: self.results.into_iter().map(|i| i.to_jscontact()).collect(), |
| 221 |
..self |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
fn only_jscontact(self) -> Self { |
| 226 |
Self { |
| 227 |
results: self |
| 228 |
.results |
| 229 |
.into_iter() |
| 230 |
.map(|i| i.only_jscontact()) |
| 231 |
.collect(), |
| 232 |
..self |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
impl JsContactConvert for NameserverSearchResults { |
| 238 |
fn to_jscontact(self) -> Self { |
| 239 |
Self { |
| 240 |
results: self.results.into_iter().map(|i| i.to_jscontact()).collect(), |
| 241 |
..self |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
fn only_jscontact(self) -> Self { |
| 246 |
Self { |
| 247 |
results: self |
| 248 |
.results |
| 249 |
.into_iter() |
| 250 |
.map(|i| i.only_jscontact()) |
| 251 |
.collect(), |
| 252 |
..self |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
impl JsContactConvert for EntitySearchResults { |
| 258 |
fn to_jscontact(self) -> Self { |
| 259 |
Self { |
| 260 |
results: self.results.into_iter().map(|i| i.to_jscontact()).collect(), |
| 261 |
..self |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
fn only_jscontact(self) -> Self { |
| 266 |
Self { |
| 267 |
results: self |
| 268 |
.results |
| 269 |
.into_iter() |
| 270 |
.map(|i| i.only_jscontact()) |
| 271 |
.collect(), |
| 272 |
..self |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
impl JsContactConvert for IpSearchResults { |
| 278 |
fn to_jscontact(self) -> Self { |
| 279 |
Self { |
| 280 |
results: self.results.into_iter().map(|i| i.to_jscontact()).collect(), |
| 281 |
..self |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
fn only_jscontact(self) -> Self { |
| 286 |
Self { |
| 287 |
results: self |
| 288 |
.results |
| 289 |
.into_iter() |
| 290 |
.map(|i| i.only_jscontact()) |
| 291 |
.collect(), |
| 292 |
..self |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
impl JsContactConvert for AutnumSearchResults { |
| 298 |
fn to_jscontact(self) -> Self { |
| 299 |
Self { |
| 300 |
results: self.results.into_iter().map(|i| i.to_jscontact()).collect(), |
| 301 |
..self |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
fn only_jscontact(self) -> Self { |
| 306 |
Self { |
| 307 |
results: self |
| 308 |
.results |
| 309 |
.into_iter() |
| 310 |
.map(|i| i.only_jscontact()) |
| 311 |
.collect(), |
| 312 |
..self |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
#[cfg(test)] |
| 318 |
mod tests { |
| 319 |
use super::*; |
| 320 |
use crate::prelude::*; |
| 321 |
|
| 322 |
#[test] |
| 323 |
fn test_entity_to_jscontact_with_vcard() { |
| 324 |
// GIVEN: An entity with vCard data but no JSContact |
| 325 |
let contact = Contact::builder().full_name("John Doe").build(); |
| 326 |
let mut entity = Entity::builder().handle("test-entity").build(); |
| 327 |
entity.with_contact_as_vcard(&contact); |
| 328 |
|
| 329 |
// WHEN: Converting to JSContact |
| 330 |
let result = entity.to_jscontact(); |
| 331 |
|
| 332 |
// THEN: The entity should have JSContact data and retain vCard |
| 333 |
assert!(result.is_contact_as_jscontact()); |
| 334 |
assert!(result.is_contact_as_vcard()); |
| 335 |
assert_eq!(result.object_common.handle.unwrap(), "test-entity"); |
| 336 |
} |
| 337 |
|
| 338 |
#[test] |
| 339 |
fn test_entity_only_jscontact_with_vcard() { |
| 340 |
// GIVEN: An entity with vCard data but no JSContact |
| 341 |
let contact = Contact::builder().full_name("John Doe").build(); |
| 342 |
let mut entity = Entity::builder().handle("test-entity").build(); |
| 343 |
entity.with_contact_as_vcard(&contact); |
| 344 |
|
| 345 |
// WHEN: Converting to JSContact only |
| 346 |
let result = entity.only_jscontact(); |
| 347 |
|
| 348 |
// THEN: The entity should have JSContact data but no vCard |
| 349 |
assert!(result.is_contact_as_jscontact()); |
| 350 |
assert!(!result.is_contact_as_vcard()); |
| 351 |
assert_eq!(result.object_common.handle.unwrap(), "test-entity"); |
| 352 |
} |
| 353 |
|
| 354 |
#[test] |
| 355 |
fn test_entity_to_jscontact_with_existing_jscontact() { |
| 356 |
// GIVEN: An entity with existing JSContact data |
| 357 |
let _jscontact = Contact::builder() |
| 358 |
.full_name("Jane Smith") |
| 359 |
.build() |
| 360 |
.to_jscontact(); |
| 361 |
let contact = Contact::builder().full_name("Jane Smith").build(); |
| 362 |
let mut entity = Entity::builder().handle("test-entity").build(); |
| 363 |
entity.with_contact_as_jscontact(&contact); |
| 364 |
entity.with_contact_as_vcard(&contact); |
| 365 |
|
| 366 |
// WHEN: Converting to JSContact |
| 367 |
let result = entity.to_jscontact(); |
| 368 |
|
| 369 |
// THEN: The existing JSContact should be preserved |
| 370 |
assert!(result.is_contact_as_jscontact()); |
| 371 |
assert!(result.is_contact_as_vcard()); |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
fn test_entity_to_jscontact_without_contact_data() { |
| 376 |
// GIVEN: An entity with no contact data |
| 377 |
let entity = Entity::builder().handle("test-entity").build(); |
| 378 |
|
| 379 |
// WHEN: Converting to JSContact |
| 380 |
let result = entity.to_jscontact(); |
| 381 |
|
| 382 |
// THEN: No conversion should occur |
| 383 |
assert!(!result.is_contact_as_jscontact()); |
| 384 |
assert!(!result.is_contact_as_vcard()); |
| 385 |
assert_eq!(result.object_common.handle.unwrap(), "test-entity"); |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn test_rdap_response_entity_to_jscontact() { |
| 390 |
// GIVEN: An RDAP response containing an entity with vCard |
| 391 |
let contact = Contact::builder().full_name("Bob Johnson").build(); |
| 392 |
let mut entity = Entity::builder().handle("test-entity").build(); |
| 393 |
entity.with_contact_as_vcard(&contact); |
| 394 |
let rdap_response = RdapResponse::Entity(Box::new(entity)); |
| 395 |
|
| 396 |
// WHEN: Converting to JSContact |
| 397 |
let result = rdap_response.to_jscontact(); |
| 398 |
|
| 399 |
// THEN: The entity should be converted to JSContact |
| 400 |
match result { |
| 401 |
RdapResponse::Entity(converted_entity) => { |
| 402 |
assert!(converted_entity.is_contact_as_jscontact()); |
| 403 |
assert!(converted_entity.is_contact_as_vcard()); |
| 404 |
} |
| 405 |
_ => panic!("Expected Entity response"), |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
#[test] |
| 410 |
fn test_rdap_response_domain_to_jscontact() { |
| 411 |
// GIVEN: A domain with entities containing vCard data |
| 412 |
let contact = Contact::builder().full_name("Domain Owner").build(); |
| 413 |
let mut entity = Entity::builder().handle("registrant").build(); |
| 414 |
entity.with_contact_as_vcard(&contact); |
| 415 |
let domain = Domain::response_obj() |
| 416 |
.handle("example.com") |
| 417 |
.ldh_name("example.com") |
| 418 |
.entities(vec![entity]) |
| 419 |
.build(); |
| 420 |
let rdap_response = RdapResponse::Domain(Box::new(domain)); |
| 421 |
|
| 422 |
// WHEN: Converting to JSContact |
| 423 |
let result = rdap_response.to_jscontact(); |
| 424 |
|
| 425 |
// THEN: The entities should be converted to JSContact |
| 426 |
match result { |
| 427 |
RdapResponse::Domain(converted_domain) => { |
| 428 |
if let Some(entities) = &converted_domain.object_common.entities { |
| 429 |
assert_eq!(entities.len(), 1); |
| 430 |
assert!(entities[0].is_contact_as_jscontact()); |
| 431 |
assert!(entities[0].is_contact_as_vcard()); |
| 432 |
} else { |
| 433 |
panic!("Expected entities to be present"); |
| 434 |
} |
| 435 |
} |
| 436 |
_ => panic!("Expected Domain response"), |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
#[test] |
| 441 |
fn test_vec_entity_to_jscontact() { |
| 442 |
// GIVEN: A vector of entities with vCard data |
| 443 |
let contact1 = Contact::builder().full_name("Entity One").build(); |
| 444 |
let mut entity1 = Entity::builder().handle("entity1").build(); |
| 445 |
entity1.with_contact_as_vcard(&contact1); |
| 446 |
|
| 447 |
let contact2 = Contact::builder().full_name("Entity Two").build(); |
| 448 |
let mut entity2 = Entity::builder().handle("entity2").build(); |
| 449 |
entity2.with_contact_as_vcard(&contact2); |
| 450 |
|
| 451 |
let entities = vec![entity1, entity2]; |
| 452 |
|
| 453 |
// WHEN: Converting to JSContact |
| 454 |
let result = entities.to_jscontact(); |
| 455 |
|
| 456 |
// THEN: All entities should be converted |
| 457 |
assert_eq!(result.len(), 2); |
| 458 |
for entity in &result { |
| 459 |
assert!(entity.is_contact_as_jscontact()); |
| 460 |
assert!(entity.is_contact_as_vcard()); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
#[test] |
| 465 |
fn test_domain_search_results_to_jscontact() { |
| 466 |
// GIVEN: Domain search results with entities having vCard data |
| 467 |
let contact = Contact::builder().full_name("Admin Contact").build(); |
| 468 |
let mut entity = Entity::builder().handle("admin").build(); |
| 469 |
entity.with_contact_as_vcard(&contact); |
| 470 |
let domain = Domain::response_obj() |
| 471 |
.handle("test.com") |
| 472 |
.ldh_name("test.com") |
| 473 |
.entities(vec![entity]) |
| 474 |
.build(); |
| 475 |
let search_results = DomainSearchResults::response_obj() |
| 476 |
.results(vec![domain]) |
| 477 |
.build(); |
| 478 |
|
| 479 |
// WHEN: Converting to JSContact |
| 480 |
let result = search_results.to_jscontact(); |
| 481 |
|
| 482 |
// THEN: All domain entities should be converted |
| 483 |
assert_eq!(result.results.len(), 1); |
| 484 |
if let Some(entities) = &result.results[0].object_common.entities { |
| 485 |
assert_eq!(entities.len(), 1); |
| 486 |
assert!(entities[0].is_contact_as_jscontact()); |
| 487 |
assert!(entities[0].is_contact_as_vcard()); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn test_network_to_jscontact_preserves_other_fields() { |
| 493 |
// GIVEN: A network with entities and other data |
| 494 |
let contact = Contact::builder().full_name("Network Admin").build(); |
| 495 |
let mut entity = Entity::builder().handle("network-admin").build(); |
| 496 |
entity.with_contact_as_vcard(&contact); |
| 497 |
let network = Network::builder() |
| 498 |
.cidr("192.0.2.0/24") |
| 499 |
.handle("192.0.2.0/24") |
| 500 |
.entities(vec![entity]) |
| 501 |
.build() |
| 502 |
.unwrap(); |
| 503 |
|
| 504 |
// WHEN: Converting to JSContact |
| 505 |
let result = network.to_jscontact(); |
| 506 |
|
| 507 |
// THEN: Network data should be preserved and entities converted |
| 508 |
assert_eq!(result.object_common.handle.unwrap(), "192.0.2.0/24"); |
| 509 |
if let Some(entities) = &result.object_common.entities { |
| 510 |
assert_eq!(entities.len(), 1); |
| 511 |
assert!(entities[0].is_contact_as_jscontact()); |
| 512 |
assert!(entities[0].is_contact_as_vcard()); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
#[test] |
| 517 |
fn test_autnum_to_jscontact() { |
| 518 |
// GIVEN: An autnum with entities containing vCard data |
| 519 |
let contact = Contact::builder().full_name("Autnum Contact").build(); |
| 520 |
let mut entity = Entity::builder().handle("autnum-contact").build(); |
| 521 |
entity.with_contact_as_vcard(&contact); |
| 522 |
let autnum = Autnum::builder() |
| 523 |
.autnum_range(65536..65537) |
| 524 |
.handle("AS65536") |
| 525 |
.entities(vec![entity]) |
| 526 |
.build(); |
| 527 |
|
| 528 |
// WHEN: Converting to JSContact |
| 529 |
let result = autnum.to_jscontact(); |
| 530 |
|
| 531 |
// THEN: The entities should be converted |
| 532 |
if let Some(entities) = &result.object_common.entities { |
| 533 |
assert_eq!(entities.len(), 1); |
| 534 |
assert!(entities[0].is_contact_as_jscontact()); |
| 535 |
assert!(entities[0].is_contact_as_vcard()); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
#[test] |
| 540 |
fn test_nameserver_to_jscontact() { |
| 541 |
// GIVEN: A nameserver with entities containing vCard data |
| 542 |
let contact = Contact::builder().full_name("Nameserver Contact").build(); |
| 543 |
let mut entity = Entity::builder().handle("ns-contact").build(); |
| 544 |
entity.with_contact_as_vcard(&contact); |
| 545 |
let nameserver = Nameserver::response_obj() |
| 546 |
.handle("ns1.example.com") |
| 547 |
.ldh_name("ns1.example.com") |
| 548 |
.entities(vec![entity]) |
| 549 |
.build() |
| 550 |
.unwrap(); |
| 551 |
|
| 552 |
// WHEN: Converting to JSContact |
| 553 |
let result = nameserver.to_jscontact(); |
| 554 |
|
| 555 |
// THEN: The entities should be converted |
| 556 |
if let Some(entities) = &result.object_common.entities { |
| 557 |
assert_eq!(entities.len(), 1); |
| 558 |
assert!(entities[0].is_contact_as_jscontact()); |
| 559 |
assert!(entities[0].is_contact_as_vcard()); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
#[test] |
| 564 |
fn test_entity_search_results_to_jscontact() { |
| 565 |
// GIVEN: Entity search results with vCard data |
| 566 |
let contact = Contact::builder().full_name("Search Result Entity").build(); |
| 567 |
let mut entity = Entity::builder().handle("search-entity").build(); |
| 568 |
entity.with_contact_as_vcard(&contact); |
| 569 |
let search_results = EntitySearchResults::response_obj() |
| 570 |
.results(vec![entity]) |
| 571 |
.build(); |
| 572 |
|
| 573 |
// WHEN: Converting to JSContact |
| 574 |
let result = search_results.to_jscontact(); |
| 575 |
|
| 576 |
// THEN: All entities should be converted |
| 577 |
assert_eq!(result.results.len(), 1); |
| 578 |
assert!(result.results[0].is_contact_as_jscontact()); |
| 579 |
assert!(result.results[0].is_contact_as_vcard()); |
| 580 |
} |
| 581 |
|
| 582 |
#[test] |
| 583 |
fn test_nameserver_search_results_to_jscontact() { |
| 584 |
// GIVEN: Nameserver search results with entities containing vCard data |
| 585 |
let contact = Contact::builder().full_name("NS Search Contact").build(); |
| 586 |
let mut entity = Entity::builder().handle("ns-search-contact").build(); |
| 587 |
entity.with_contact_as_vcard(&contact); |
| 588 |
let nameserver = Nameserver::response_obj() |
| 589 |
.handle("ns2.example.com") |
| 590 |
.ldh_name("ns2.example.com") |
| 591 |
.entities(vec![entity]) |
| 592 |
.build() |
| 593 |
.unwrap(); |
| 594 |
let search_results = NameserverSearchResults::response_obj() |
| 595 |
.results(vec![nameserver]) |
| 596 |
.build(); |
| 597 |
|
| 598 |
// WHEN: Converting to JSContact |
| 599 |
let result = search_results.to_jscontact(); |
| 600 |
|
| 601 |
// THEN: All nameserver entities should be converted |
| 602 |
assert_eq!(result.results.len(), 1); |
| 603 |
if let Some(entities) = &result.results[0].object_common.entities { |
| 604 |
assert_eq!(entities.len(), 1); |
| 605 |
assert!(entities[0].is_contact_as_jscontact()); |
| 606 |
assert!(entities[0].is_contact_as_vcard()); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
#[test] |
| 611 |
fn test_nested_entity_to_jscontact() { |
| 612 |
// GIVEN: An entity with nested entities containing vCard data |
| 613 |
let nested_contact = Contact::builder().full_name("Nested Entity").build(); |
| 614 |
let mut nested_entity = Entity::builder().handle("nested-entity").build(); |
| 615 |
nested_entity.with_contact_as_vcard(&nested_contact); |
| 616 |
|
| 617 |
let mut parent_entity = Entity::builder().handle("parent-entity").build(); |
| 618 |
parent_entity.object_common.entities = Some(vec![nested_entity]); |
| 619 |
|
| 620 |
// WHEN: Converting to JSContact |
| 621 |
let result = parent_entity.to_jscontact(); |
| 622 |
|
| 623 |
// THEN: Both parent and nested entities should be converted |
| 624 |
assert!(result.is_contact_as_vcard() || !result.is_contact_as_vcard()); |
| 625 |
if let Some(entities) = &result.object_common.entities { |
| 626 |
assert_eq!(entities.len(), 1); |
| 627 |
assert!(entities[0].is_contact_as_jscontact()); |
| 628 |
assert!(entities[0].is_contact_as_vcard()); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
#[test] |
| 633 |
fn test_nested_entity_only_jscontact() { |
| 634 |
// GIVEN: An entity with nested entities containing vCard data |
| 635 |
let nested_contact = Contact::builder().full_name("Nested Entity Only").build(); |
| 636 |
let mut nested_entity = Entity::builder().handle("nested-entity-only").build(); |
| 637 |
nested_entity.with_contact_as_vcard(&nested_contact); |
| 638 |
|
| 639 |
let mut parent_entity = Entity::builder().handle("parent-entity-only").build(); |
| 640 |
parent_entity.object_common.entities = Some(vec![nested_entity]); |
| 641 |
|
| 642 |
// WHEN: Converting to JSContact only (removing vCard) |
| 643 |
let result = parent_entity.only_jscontact(); |
| 644 |
|
| 645 |
// THEN: Parent should have no vCard, but nested should have jscontact |
| 646 |
assert!(!result.is_contact_as_vcard()); |
| 647 |
if let Some(entities) = &result.object_common.entities { |
| 648 |
assert_eq!(entities.len(), 1); |
| 649 |
assert!(entities[0].is_contact_as_jscontact()); |
| 650 |
assert!(!entities[0].is_contact_as_vcard()); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
#[test] |
| 655 |
fn test_deeply_nested_entities_to_jscontact() { |
| 656 |
// GIVEN: An entity with 3 levels of nested entities |
| 657 |
let level3_contact = Contact::builder().full_name("Level 3 Entity").build(); |
| 658 |
let mut level3_entity = Entity::builder().handle("level3-entity").build(); |
| 659 |
level3_entity.with_contact_as_vcard(&level3_contact); |
| 660 |
|
| 661 |
let mut level2_entity = Entity::builder().handle("level2-entity").build(); |
| 662 |
level2_entity.object_common.entities = Some(vec![level3_entity]); |
| 663 |
|
| 664 |
let mut level1_entity = Entity::builder().handle("level1-entity").build(); |
| 665 |
level1_entity.object_common.entities = Some(vec![level2_entity]); |
| 666 |
|
| 667 |
// WHEN: Converting to JSContact |
| 668 |
let result = level1_entity.to_jscontact(); |
| 669 |
|
| 670 |
// THEN: All nested levels should be converted |
| 671 |
if let Some(level1_entities) = &result.object_common.entities { |
| 672 |
assert_eq!(level1_entities.len(), 1); |
| 673 |
if let Some(level2_entities) = &level1_entities[0].object_common.entities { |
| 674 |
assert_eq!(level2_entities.len(), 1); |
| 675 |
if let Some(level3_entities) = &level2_entities[0].object_common.entities { |
| 676 |
assert_eq!(level3_entities.len(), 1); |
| 677 |
assert!(level3_entities[0].is_contact_as_jscontact()); |
| 678 |
assert!(level3_entities[0].is_contact_as_vcard()); |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
#[test] |
| 685 |
fn test_deeply_nested_entities_only_jscontact() { |
| 686 |
// GIVEN: An entity with 3 levels of nested entities |
| 687 |
let level3_contact = Contact::builder().full_name("Level 3 Only").build(); |
| 688 |
let mut level3_entity = Entity::builder().handle("level3-only").build(); |
| 689 |
level3_entity.with_contact_as_vcard(&level3_contact); |
| 690 |
|
| 691 |
let mut level2_entity = Entity::builder().handle("level2-only").build(); |
| 692 |
level2_entity.object_common.entities = Some(vec![level3_entity]); |
| 693 |
|
| 694 |
let mut level1_entity = Entity::builder().handle("level1-only").build(); |
| 695 |
level1_entity.object_common.entities = Some(vec![level2_entity]); |
| 696 |
|
| 697 |
// WHEN: Converting to JSContact only |
| 698 |
let result = level1_entity.only_jscontact(); |
| 699 |
|
| 700 |
// THEN: All levels should have jscontact but no vCard |
| 701 |
if let Some(level1_entities) = &result.object_common.entities { |
| 702 |
assert!(!level1_entities[0].is_contact_as_vcard()); |
| 703 |
if let Some(level2_entities) = &level1_entities[0].object_common.entities { |
| 704 |
assert!(!level2_entities[0].is_contact_as_vcard()); |
| 705 |
if let Some(level3_entities) = &level2_entities[0].object_common.entities { |
| 706 |
assert!(!level3_entities[0].is_contact_as_vcard()); |
| 707 |
assert!(level3_entities[0].is_contact_as_jscontact()); |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
#[test] |
| 714 |
fn test_multiple_nested_entities_to_jscontact() { |
| 715 |
// GIVEN: An entity with multiple nested entities |
| 716 |
let contact1 = Contact::builder().full_name("Nested 1").build(); |
| 717 |
let mut nested1 = Entity::builder().handle("nested-1").build(); |
| 718 |
nested1.with_contact_as_vcard(&contact1); |
| 719 |
|
| 720 |
let contact2 = Contact::builder().full_name("Nested 2").build(); |
| 721 |
let mut nested2 = Entity::builder().handle("nested-2").build(); |
| 722 |
nested2.with_contact_as_vcard(&contact2); |
| 723 |
|
| 724 |
let contact3 = Contact::builder().full_name("Nested 3").build(); |
| 725 |
let mut nested3 = Entity::builder().handle("nested-3").build(); |
| 726 |
nested3.with_contact_as_vcard(&contact3); |
| 727 |
|
| 728 |
let mut parent_entity = Entity::builder().handle("parent-multi").build(); |
| 729 |
parent_entity.object_common.entities = Some(vec![nested1, nested2, nested3]); |
| 730 |
|
| 731 |
// WHEN: Converting to JSContact |
| 732 |
let result = parent_entity.to_jscontact(); |
| 733 |
|
| 734 |
// THEN: All nested entities should be converted |
| 735 |
if let Some(entities) = &result.object_common.entities { |
| 736 |
assert_eq!(entities.len(), 3); |
| 737 |
for entity in entities { |
| 738 |
assert!(entity.is_contact_as_jscontact()); |
| 739 |
assert!(entity.is_contact_as_vcard()); |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
#[test] |
| 745 |
fn test_vec_nested_entities_only_jscontact() { |
| 746 |
// GIVEN: A vector of entities, each with nested entities |
| 747 |
let nested_contact = Contact::builder().full_name("Child Entity").build(); |
| 748 |
let mut nested = Entity::builder().handle("child").build(); |
| 749 |
nested.with_contact_as_vcard(&nested_contact); |
| 750 |
|
| 751 |
let mut entity1 = Entity::builder().handle("parent1").build(); |
| 752 |
entity1.object_common.entities = Some(vec![nested]); |
| 753 |
|
| 754 |
let mut entity2 = Entity::builder().handle("parent2").build(); |
| 755 |
entity2.object_common.entities = Some(vec![]); |
| 756 |
|
| 757 |
let entities = vec![entity1, entity2]; |
| 758 |
|
| 759 |
// WHEN: Converting to JSContact only |
| 760 |
let result = entities.only_jscontact(); |
| 761 |
|
| 762 |
// THEN: All entities should be converted, nested should have no vCard |
| 763 |
assert_eq!(result.len(), 2); |
| 764 |
assert!(!result[0].is_contact_as_vcard()); |
| 765 |
assert!(!result[1].is_contact_as_vcard()); |
| 766 |
if let Some(nested) = &result[0].object_common.entities { |
| 767 |
assert_eq!(nested.len(), 1); |
| 768 |
assert!(nested[0].is_contact_as_jscontact()); |
| 769 |
assert!(!nested[0].is_contact_as_vcard()); |
| 770 |
} |
| 771 |
} |
| 772 |
} |