opcua 0.12.0

OPC UA client and server API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
use std::sync::Arc;

use crate::server::{
    address_space::{
        references::Reference,
        relative_path::{find_node_from_browse_path, find_nodes_relative_path_simple},
        EventNotifier,
    },
    callbacks,
    prelude::*,
    tests::*,
};

#[test]
fn address_space() {
    let address_space = AddressSpace::new();

    let root_folder = address_space.root_folder();
    assert_eq!(root_folder.node_class(), NodeClass::Object);
    let objects_folder = address_space.objects_folder();
    assert_eq!(objects_folder.node_class(), NodeClass::Object);
    let types_folder = address_space.types_folder();
    assert_eq!(types_folder.node_class(), NodeClass::Object);
    let views_folder = address_space.views_folder();
    assert_eq!(views_folder.node_class(), NodeClass::Object);
}

#[test]
fn namespaces() {
    // Test that namespaces are listed properly
    let mut address_space = AddressSpace::new();

    let ns = address_space.register_namespace("urn:test").unwrap();

    assert_eq!(
        address_space
            .namespace_index("http://opcfoundation.org/UA/")
            .unwrap(),
        0u16
    );
    assert_eq!(address_space.namespace_index("urn:test").unwrap(), ns);
    // Error
    assert_eq!(address_space.register_namespace(""), Err(()));
    // Add new namespaces
    assert_eq!(address_space.register_namespace("foo").unwrap(), 2u16);
    assert_eq!(address_space.register_namespace("bar").unwrap(), 3u16);
    // Test if existing namespace is found
    assert_eq!(address_space.register_namespace("foo").unwrap(), 2u16);
}

#[test]
fn find_root_folder() {
    let address_space = AddressSpace::new();
    let node_type = address_space.find_node(&NodeId::new(0, 84));
    assert!(node_type.is_some());

    let node = node_type.unwrap().as_node();
    assert_eq!(node.node_id(), NodeId::new(0, 84));
    assert_eq!(node.node_id(), ObjectId::RootFolder.into());
}

#[test]
fn find_objects_folder() {
    let address_space = AddressSpace::new();
    let node_type = address_space.find(ObjectId::ObjectsFolder);
    assert!(node_type.is_some());
}

#[test]
fn find_types_folder() {
    let address_space = AddressSpace::new();
    let node_type = address_space.find(ObjectId::TypesFolder);
    assert!(node_type.is_some());
}

#[test]
fn find_views_folder() {
    let address_space = AddressSpace::new();
    let node_type = address_space.find(ObjectId::ViewsFolder);
    assert!(node_type.is_some());
}

#[test]
fn find_common_nodes() {
    let address_space = AddressSpace::new();
    let nodes: Vec<NodeId> = vec![
        ObjectId::RootFolder.into(),
        ObjectId::ObjectsFolder.into(),
        ObjectId::TypesFolder.into(),
        ObjectId::ViewsFolder.into(),
        ObjectId::DataTypesFolder.into(),
        DataTypeId::BaseDataType.into(),
        // Types
        DataTypeId::Boolean.into(),
        DataTypeId::ByteString.into(),
        DataTypeId::DataValue.into(),
        DataTypeId::DateTime.into(),
        DataTypeId::DiagnosticInfo.into(),
        DataTypeId::Enumeration.into(),
        DataTypeId::ExpandedNodeId.into(),
        DataTypeId::Guid.into(),
        DataTypeId::LocalizedText.into(),
        DataTypeId::NodeId.into(),
        DataTypeId::Number.into(),
        DataTypeId::QualifiedName.into(),
        DataTypeId::StatusCode.into(),
        DataTypeId::String.into(),
        DataTypeId::Structure.into(),
        DataTypeId::XmlElement.into(),
        DataTypeId::Double.into(),
        DataTypeId::Float.into(),
        DataTypeId::Integer.into(),
        DataTypeId::SByte.into(),
        DataTypeId::Int16.into(),
        DataTypeId::Int32.into(),
        DataTypeId::Int64.into(),
        DataTypeId::Byte.into(),
        DataTypeId::UInt16.into(),
        DataTypeId::UInt32.into(),
        DataTypeId::UInt64.into(),
        ObjectId::OPCBinarySchema_TypeSystem.into(),
        ObjectTypeId::DataTypeSystemType.into(),
        // Refs
        ObjectId::ReferenceTypesFolder.into(),
        ReferenceTypeId::References.into(),
        ReferenceTypeId::HierarchicalReferences.into(),
        ReferenceTypeId::HasChild.into(),
        ReferenceTypeId::HasSubtype.into(),
        ReferenceTypeId::Organizes.into(),
        ReferenceTypeId::NonHierarchicalReferences.into(),
        ReferenceTypeId::HasTypeDefinition.into(),
    ];
    for n in nodes {
        assert!(address_space.find_node(&n).is_some());
    }
}

#[test]
fn object_attributes() {
    let on = NodeId::new(1, "o1");
    let o = Object::new(&on, "Browse01", "Display01", EventNotifier::empty());
    assert_eq!(o.node_class(), NodeClass::Object);
    assert_eq!(o.node_id(), on);
    assert_eq!(o.browse_name(), QualifiedName::new(0, "Browse01"));
    assert_eq!(o.display_name(), LocalizedText::new("", "Display01"));
}

#[test]
fn find_node_by_id() {
    let address_space = make_sample_address_space();
    let mut address_space = trace_write_lock!(address_space);
    let ns = address_space.register_namespace("urn:test").unwrap();

    assert!(!address_space.node_exists(&NodeId::null()));
    assert!(!address_space.node_exists(&NodeId::new(11, "v3")));

    assert!(address_space.node_exists(&NodeId::new(ns, "v1")));
    assert!(address_space.node_exists(&NodeId::new(ns, 300)));
    assert!(address_space.node_exists(&NodeId::new(ns, "v3")));
}

fn dump_references(references: &Vec<Reference>) {
    for r in references {
        println!(
            "Referencs - type = {:?}, to = {:?}",
            r.reference_type, r.target_node
        );
    }
}

#[test]
fn find_references_by_direction() {
    let address_space = make_sample_address_space();
    let address_space = trace_read_lock!(address_space);

    let (references, _inverse_ref_idx) = address_space
        .find_references_by_direction::<ReferenceTypeId>(
            &NodeId::objects_folder_id(),
            BrowseDirection::Forward,
            None,
        );
    dump_references(&references);
    assert_eq!(references.len(), 3);

    // Should be same as filtering on None
    let reference_filter = Some((ReferenceTypeId::References, true));
    let (references, _inverse_ref_idx) = address_space.find_references_by_direction(
        &NodeId::objects_folder_id(),
        BrowseDirection::Forward,
        reference_filter,
    );
    dump_references(&references);
    assert_eq!(references.len(), 3);

    // Only organizes
    let reference_filter = Some((ReferenceTypeId::Organizes, false));
    let (references, _inverse_ref_idx) = address_space.find_references_by_direction(
        &NodeId::objects_folder_id(),
        BrowseDirection::Forward,
        reference_filter,
    );
    dump_references(&references);
    assert_eq!(references.len(), 2);

    // Reverse organises should == 1 (root organises objects)
    let (references, _inverse_ref_idx) = address_space.find_references_by_direction(
        &NodeId::objects_folder_id(),
        BrowseDirection::Inverse,
        reference_filter,
    );
    dump_references(&references);
    assert_eq!(references.len(), 1);

    // Both directions
    let (references, inverse_ref_idx) = address_space.find_references_by_direction(
        &NodeId::objects_folder_id(),
        BrowseDirection::Both,
        reference_filter,
    );
    dump_references(&references);
    assert_eq!(references.len(), 3);
    assert_eq!(inverse_ref_idx, 2);
}

#[test]
fn find_references() {
    let address_space = make_sample_address_space();
    let address_space = trace_read_lock!(address_space);

    let references = address_space.find_references(
        &NodeId::root_folder_id(),
        Some((ReferenceTypeId::Organizes, false)),
    );
    assert!(references.is_some());
    let references = references.as_ref().unwrap();
    dump_references(&references);
    assert_eq!(references.len(), 3);

    let references =
        address_space.find_references::<ReferenceTypeId>(&NodeId::root_folder_id(), None);
    assert!(references.is_some());
    let references = references.as_ref().unwrap();
    dump_references(&references);
    assert_eq!(references.len(), 4);

    let references = address_space.find_references(
        &NodeId::objects_folder_id(),
        Some((ReferenceTypeId::Organizes, false)),
    );
    assert!(references.is_some());
    let references = references.unwrap();
    dump_references(&references);
    assert_eq!(references.len(), 2);

    let r1 = &references[0];
    assert_eq!(r1.reference_type, ReferenceTypeId::Organizes.into());
    let child_node_id = r1.target_node.clone();

    let child = address_space.find_node(&child_node_id);
    assert!(child.is_some());
}

#[test]
fn find_inverse_references() {
    let address_space = make_sample_address_space();
    let address_space = trace_read_lock!(address_space);

    //println!("{:#?}", address_space);
    let references = address_space.find_inverse_references(
        &NodeId::root_folder_id(),
        Some((ReferenceTypeId::Organizes, false)),
    );
    assert!(references.is_none());

    let references = address_space.find_inverse_references(
        &NodeId::objects_folder_id(),
        Some((ReferenceTypeId::Organizes, false)),
    );
    assert!(references.is_some());
    let references = references.unwrap();
    assert_eq!(references.len(), 1);
}

#[test]
fn find_reference_subtypes() {
    let address_space = make_sample_address_space();
    let address_space = trace_read_lock!(address_space);

    let references = address_space.references();
    let reference_types = vec![
        (
            ReferenceTypeId::References,
            ReferenceTypeId::HierarchicalReferences,
        ),
        (ReferenceTypeId::References, ReferenceTypeId::HasChild),
        (ReferenceTypeId::References, ReferenceTypeId::HasSubtype),
        (ReferenceTypeId::References, ReferenceTypeId::Organizes),
        (ReferenceTypeId::References, ReferenceTypeId::Aggregates),
        (ReferenceTypeId::References, ReferenceTypeId::HasProperty),
        (ReferenceTypeId::References, ReferenceTypeId::HasComponent),
        (
            ReferenceTypeId::References,
            ReferenceTypeId::HasOrderedComponent,
        ),
        (ReferenceTypeId::References, ReferenceTypeId::HasEventSource),
        (ReferenceTypeId::References, ReferenceTypeId::HasNotifier),
        (ReferenceTypeId::References, ReferenceTypeId::GeneratesEvent),
        (
            ReferenceTypeId::References,
            ReferenceTypeId::AlwaysGeneratesEvent,
        ),
        (ReferenceTypeId::References, ReferenceTypeId::HasEncoding),
        (
            ReferenceTypeId::References,
            ReferenceTypeId::HasModellingRule,
        ),
        (ReferenceTypeId::References, ReferenceTypeId::HasDescription),
        (
            ReferenceTypeId::References,
            ReferenceTypeId::HasTypeDefinition,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::HasChild,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::HasSubtype,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::Organizes,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::Aggregates,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::HasProperty,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::HasComponent,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::HasOrderedComponent,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::HasEventSource,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::HasNotifier,
        ),
        (ReferenceTypeId::HasChild, ReferenceTypeId::Aggregates),
        (ReferenceTypeId::HasChild, ReferenceTypeId::HasComponent),
        (
            ReferenceTypeId::HasChild,
            ReferenceTypeId::HasHistoricalConfiguration,
        ),
        (ReferenceTypeId::HasChild, ReferenceTypeId::HasProperty),
        (
            ReferenceTypeId::HasChild,
            ReferenceTypeId::HasOrderedComponent,
        ),
        (ReferenceTypeId::HasChild, ReferenceTypeId::HasSubtype),
        (ReferenceTypeId::Aggregates, ReferenceTypeId::HasComponent),
        (
            ReferenceTypeId::Aggregates,
            ReferenceTypeId::HasHistoricalConfiguration,
        ),
        (ReferenceTypeId::Aggregates, ReferenceTypeId::HasProperty),
        (
            ReferenceTypeId::Aggregates,
            ReferenceTypeId::HasOrderedComponent,
        ),
        (
            ReferenceTypeId::HasComponent,
            ReferenceTypeId::HasOrderedComponent,
        ),
        (
            ReferenceTypeId::HasEventSource,
            ReferenceTypeId::HasNotifier,
        ),
        (
            ReferenceTypeId::HierarchicalReferences,
            ReferenceTypeId::HasNotifier,
        ),
        (
            ReferenceTypeId::References,
            ReferenceTypeId::NonHierarchicalReferences,
        ),
        (
            ReferenceTypeId::NonHierarchicalReferences,
            ReferenceTypeId::GeneratesEvent,
        ),
        (
            ReferenceTypeId::NonHierarchicalReferences,
            ReferenceTypeId::AlwaysGeneratesEvent,
        ),
        (
            ReferenceTypeId::NonHierarchicalReferences,
            ReferenceTypeId::HasEncoding,
        ),
        (
            ReferenceTypeId::NonHierarchicalReferences,
            ReferenceTypeId::HasModellingRule,
        ),
        (
            ReferenceTypeId::NonHierarchicalReferences,
            ReferenceTypeId::HasDescription,
        ),
        (
            ReferenceTypeId::NonHierarchicalReferences,
            ReferenceTypeId::HasTypeDefinition,
        ),
        (
            ReferenceTypeId::GeneratesEvent,
            ReferenceTypeId::AlwaysGeneratesEvent,
        ),
    ];

    // A type should always match itself
    assert!(references.reference_type_matches(
        &ReferenceTypeId::NonHierarchicalReferences.into(),
        &ReferenceTypeId::NonHierarchicalReferences.into(),
        true
    ));
    assert!(references.reference_type_matches(
        &ReferenceTypeId::NonHierarchicalReferences.into(),
        &ReferenceTypeId::NonHierarchicalReferences.into(),
        false
    ));

    // Make sure that subtypes match when subtypes are to be compared and doesn't when they should
    // not be compared.
    reference_types.iter().for_each(|r| {
        let r1 = r.0.into();
        let r2 = r.1.into();
        assert!(references.reference_type_matches(&r1, &r2, true));
        assert!(!references.reference_type_matches(&r1, &r2, false));
    });
}

/// This test is to ensure that adding a Variable with a value of Array to address space sets the
/// ValueRank and ArrayDimensions attributes correctly.
#[test]
fn array_as_variable() {
    // 1 dimensional array with 100 element
    let values = (0..100)
        .map(|i| Variant::Int32(i))
        .collect::<Vec<Variant>>();

    // Get the variable node back from the address space, ensure that the ValueRank and ArrayDimensions are correct
    let node_id = NodeId::new(2, 1);
    let v = Variable::new(&node_id, "x", "x", (VariantTypeId::Int32, values));

    let value_rank = v.value_rank();
    assert_eq!(value_rank, 1);
    let array_dimensions = v.array_dimensions().unwrap();
    assert_eq!(array_dimensions, vec![100u32]);
}

/// This test is to ensure that adding a Variable with a value of Array to address space sets the
/// ValueRank and ArrayDimensions attributes correctly.
#[test]
fn multi_dimension_array_as_variable() {
    // 2 dimensional array with 10x10 elements

    let values = (0..100)
        .map(|i| Variant::Int32(i))
        .collect::<Vec<Variant>>();
    let mda = Array::new_multi(VariantTypeId::Int32, values, vec![10u32, 10u32]).unwrap();
    assert!(mda.is_valid());

    // Get the variable node back from the address space, ensure that the ValueRank and ArrayDimensions are correct
    let node_id = NodeId::new(2, 1);
    let v = Variable::new(&node_id, "x", "x", mda);

    let value_rank = v.value_rank();
    assert_eq!(value_rank, 2);
    let array_dimensions = v.array_dimensions().unwrap();
    assert_eq!(array_dimensions, vec![10u32, 10u32]);
}

#[test]
fn browse_nodes() {
    let address_space = make_sample_address_space();
    let address_space = trace_read_lock!(address_space);

    // Test that a node can be found
    let object_id = ObjectId::RootFolder.into();
    let result = find_node_from_browse_path(
        &address_space,
        &object_id,
        &vec!["Objects".into(), "Sample".into(), "v1".into()],
    );
    let node = result.unwrap();
    assert_eq!(node.as_node().browse_name(), QualifiedName::from("v1"));

    // Test that a non existent node cannot be found
    let result = find_node_from_browse_path(
        &address_space,
        &object_id,
        &vec!["Objects".into(), "Sample".into(), "vxxx".into()],
    );
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), StatusCode::BadNotFound);
}

#[test]
fn find_nodes_relative_path() {
    let address_space = make_sample_address_space();
    let address_space = trace_read_lock!(address_space);

    // Given some paths, find the nodes
    let parent_node = ObjectId::RootFolder.into();

    let relative_path = "/Objects/Server.ServerStatus.BuildInfo.ProductName";

    let results =
        find_nodes_relative_path_simple(&address_space, &parent_node, relative_path).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(
        results[0],
        VariableId::Server_ServerStatus_BuildInfo_ProductName.into()
    );
}

#[test]
fn object_builder() {
    let mut address_space = AddressSpace::new();

    let node_type_id = NodeId::new(1, "HelloType");
    let _ot = ObjectTypeBuilder::new(&node_type_id, "HelloType", "HelloType")
        .subtype_of(ObjectTypeId::BaseObjectType)
        .insert(&mut address_space);

    let node_id = NodeId::new(1, "Hello");
    let _o = ObjectBuilder::new(&node_id, "Foo", "Foo")
        .event_notifier(EventNotifier::SUBSCRIBE_TO_EVENTS)
        .organized_by(ObjectId::ObjectsFolder)
        .has_type_definition(node_type_id.clone())
        .insert(&mut address_space);

    // Verify the variable is there
    let _o = match address_space.find_node(&node_id).unwrap() {
        NodeType::Object(o) => o,
        _ => panic!(),
    };

    // Verify the reference to the objects folder is there
    assert!(address_space.has_reference(
        &ObjectId::ObjectsFolder.into(),
        &node_id,
        ReferenceTypeId::Organizes
    ));
    assert!(address_space.has_reference(
        &node_id,
        &node_type_id,
        ReferenceTypeId::HasTypeDefinition
    ));
}

#[test]
fn object_type_builder() {
    let mut address_space = AddressSpace::new();

    let node_type_id = NodeId::new(1, "HelloType");
    let _ot = ObjectTypeBuilder::new(&node_type_id, "HelloType", "HelloType")
        .subtype_of(ObjectTypeId::BaseObjectType)
        .insert(&mut address_space);

    let _ot = match address_space.find_node(&node_type_id).unwrap() {
        NodeType::ObjectType(ot) => ot,
        _ => panic!(),
    };

    assert!(address_space.has_reference(
        &ObjectTypeId::BaseObjectType.into(),
        &node_type_id,
        ReferenceTypeId::HasSubtype
    ));
}

#[test]
fn variable_builder() {
    let result = std::panic::catch_unwind(|| {
        // This should panic
        let _v = VariableBuilder::new(&NodeId::null(), "", "").build();
    });
    assert!(result.is_err());

    // This should build
    let _v = VariableBuilder::new(&NodeId::new(1, 1), "", "")
        .data_type(DataTypeId::Boolean)
        .build();

    // Check a variable with a bunch of fields set
    let v = VariableBuilder::new(&NodeId::new(1, "Hello"), "BrowseName", "DisplayName")
        .description("Desc")
        .data_type(DataTypeId::UInt32)
        .value_rank(10)
        .array_dimensions(&[1, 2, 3])
        .historizing(true)
        .value(Variant::from(999))
        .minimum_sampling_interval(123.0)
        .build();

    assert_eq!(v.node_id(), NodeId::new(1, "Hello"));
    assert_eq!(v.browse_name(), QualifiedName::new(0, "BrowseName"));
    assert_eq!(v.display_name(), LocalizedText::new("", "DisplayName"));
    assert_eq!(v.data_type(), DataTypeId::UInt32.into());
    assert_eq!(v.description().unwrap(), LocalizedText::new("", "Desc"));
    assert_eq!(v.value_rank(), 10);
    assert_eq!(v.array_dimensions().unwrap(), vec![1, 2, 3]);
    assert_eq!(v.historizing(), true);
    assert_eq!(
        v.value(
            TimestampsToReturn::Neither,
            NumericRange::None,
            &QualifiedName::null(),
            0.0
        )
        .value
        .unwrap(),
        Variant::from(999)
    );
    assert_eq!(v.minimum_sampling_interval().unwrap(), 123.0);

    // Add a variable to the address space

    let mut address_space = AddressSpace::new();
    let node_id = NodeId::new(1, "Hello");
    let _v = VariableBuilder::new(&node_id, "BrowseName", "DisplayName")
        .description("Desc")
        .value_rank(10)
        .data_type(DataTypeId::UInt32)
        .array_dimensions(&[1, 2, 3])
        .historizing(true)
        .value(Variant::from(999))
        .minimum_sampling_interval(123.0)
        .organized_by(ObjectId::ObjectsFolder)
        .insert(&mut address_space);

    // Verify the variable is there
    assert!(address_space.find_variable_by_ref(&node_id).is_some());
    // Verify the reference to the objects folder is there
    assert!(address_space.has_reference(
        &ObjectId::ObjectsFolder.into(),
        &node_id,
        ReferenceTypeId::Organizes
    ));
}

#[test]
fn method_builder() {
    let mut address_space = AddressSpace::new();

    let ns = address_space.register_namespace("urn:test").unwrap();

    let object_id: NodeId = ObjectId::ObjectsFolder.into();

    let fn_node_id = NodeId::new(ns, "HelloWorld");

    let inserted = MethodBuilder::new(&fn_node_id, "HelloWorld", "HelloWorld")
        .component_of(object_id.clone())
        .output_args(&mut address_space, &[("Result", DataTypeId::String).into()])
        .callback(Box::new(HelloWorld))
        .insert(&mut address_space);
    assert!(inserted);

    let method = match address_space.find_node(&fn_node_id).unwrap() {
        NodeType::Method(m) => m,
        _ => panic!(),
    };

    assert!(method.has_callback());

    let refs = address_space
        .find_references(&fn_node_id, Some((ReferenceTypeId::HasProperty, false)))
        .unwrap();
    assert_eq!(refs.len(), 1);

    let child = address_space
        .find_node(&refs.get(0).unwrap().target_node)
        .unwrap();
    if let NodeType::Variable(v) = child {
        // verify OutputArguments
        // verify OutputArguments / Argument value
        assert_eq!(v.data_type(), DataTypeId::Argument.into());
        assert_eq!(v.display_name(), LocalizedText::from("OutputArguments"));
        let v = v
            .value(
                TimestampsToReturn::Neither,
                NumericRange::None,
                &QualifiedName::null(),
                0.0,
            )
            .value
            .unwrap();
        if let Variant::Array(array) = v {
            let v = array.values;
            assert_eq!(v.len(), 1);
            let v = v.get(0).unwrap().clone();
            if let Variant::ExtensionObject(v) = v {
                // deserialize the Argument here
                let decoding_options = DecodingOptions::test();
                let argument = v.decode_inner::<Argument>(&decoding_options).unwrap();
                assert_eq!(argument.name, UAString::from("Result"));
                assert_eq!(argument.data_type, DataTypeId::String.into());
                assert_eq!(argument.value_rank, -1);
                assert_eq!(argument.array_dimensions, None);
                assert_eq!(argument.description, LocalizedText::null());
            } else {
                panic!("Variant was expected to be extension object, was {:?}", v);
            }
        } else {
            panic!("Variant was expected to be array, was {:?}", v);
        }
    } else {
        panic!();
    }
}

struct HelloWorld;

impl callbacks::Method for HelloWorld {
    fn call(
        &mut self,
        _session_id: &NodeId,
        _session_map: Arc<RwLock<SessionManager>>,
        _request: &CallMethodRequest,
    ) -> Result<CallMethodResult, StatusCode> {
        Ok(CallMethodResult {
            status_code: StatusCode::Good,
            input_argument_results: Some(vec![StatusCode::Good]),
            input_argument_diagnostic_infos: None,
            output_arguments: Some(vec![Variant::from("Hello World!")]),
        })
    }
}

#[test]
fn simple_delete_node() {
    crate::console_logging::init();

    // This is a super basic, debuggable delete test. There is a single Root node, and a
    // child object. After deleting the child, only the Root should exist with no references at
    // all to the child.

    // A blank address space, with nothing at all in it
    let mut address_space = AddressSpace::default();

    // Add a root node
    let root_node = NodeId::root_folder_id();

    let node = Object::new(&root_node, "Root", "", EventNotifier::empty());
    let _ = address_space.insert::<Object, ReferenceTypeId>(node, None);

    let node_id = NodeId::new(1, "Hello");
    let _o = ObjectBuilder::new(&node_id, "Foo", "Foo")
        .organized_by(root_node.clone())
        .insert(&mut address_space);

    // Verify the object and refs are there
    assert!(address_space.find_node(&node_id).is_some());
    assert!(address_space.has_reference(&root_node, &node_id, ReferenceTypeId::Organizes));

    // Try one time deleting references, the other time not deleting them.
    address_space.delete(&node_id, true);
    // Delete the node and the refs
    assert!(address_space.find_node(&node_id).is_none());
    assert!(address_space.find_node(&root_node).is_some());
    assert!(!address_space.has_reference(&root_node, &node_id, ReferenceTypeId::Organizes));
    assert!(!address_space
        .references()
        .reference_to_node_exists(&node_id));
}

#[test]
fn delete_node() {
    crate::console_logging::init();

    // Try creating and deleting a node, verifying that it's totally gone afterwards
    (0..2).for_each(|i| {
        let mut address_space = AddressSpace::new();

        let node_type_id = NodeId::new(1, "HelloType");
        let _ot = ObjectTypeBuilder::new(&node_type_id, "HelloType", "HelloType")
            .subtype_of(ObjectTypeId::BaseObjectType)
            .insert(&mut address_space);

        let node_id = NodeId::new(1, "Hello");
        let _o = ObjectBuilder::new(&node_id, "Foo", "Foo")
            .event_notifier(EventNotifier::SUBSCRIBE_TO_EVENTS)
            .organized_by(ObjectId::ObjectsFolder)
            .has_type_definition(node_type_id.clone())
            .insert(&mut address_space);

        // Verify the object and refs are there
        assert!(address_space.find_node(&node_id).is_some());
        assert!(address_space.has_reference(
            &ObjectId::ObjectsFolder.into(),
            &node_id,
            ReferenceTypeId::Organizes
        ));
        assert!(!address_space.has_reference(
            &node_id,
            &ObjectId::ObjectsFolder.into(),
            ReferenceTypeId::Organizes
        ));
        assert!(address_space.has_reference(
            &node_id,
            &node_type_id,
            ReferenceTypeId::HasTypeDefinition
        ));

        // Try one time deleting references, the other time not deleting them.
        let delete_references = i == 1;
        address_space.delete(&node_id, delete_references);
        if !delete_references {
            // Deleted the node but not refs
            assert!(address_space.find_node(&node_id).is_none());
            assert!(address_space.has_reference(
                &ObjectId::ObjectsFolder.into(),
                &node_id,
                ReferenceTypeId::Organizes
            ));
            assert!(address_space.has_reference(
                &node_id,
                &node_type_id,
                ReferenceTypeId::HasTypeDefinition
            ));
        } else {
            // Delete the node and the refs
            assert!(address_space.find_node(&node_id).is_none());
            assert!(!address_space.has_reference(
                &ObjectId::ObjectsFolder.into(),
                &node_id,
                ReferenceTypeId::Organizes
            ));
            assert!(!address_space.has_reference(
                &node_id,
                &node_type_id,
                ReferenceTypeId::HasTypeDefinition
            ));
            assert!(!address_space
                .references()
                .reference_to_node_exists(&node_id));
        }
    });
}

#[test]
fn is_subtype() {
    let address_space = AddressSpace::new();
    // Test subtypes against other and the expected result
    let subtypes = [
        // Positive
        (
            ObjectTypeId::BaseEventType,
            ObjectTypeId::BaseEventType,
            true,
        ),
        (
            ObjectTypeId::AuditEventType,
            ObjectTypeId::BaseEventType,
            true,
        ),
        (
            ObjectTypeId::BaseModelChangeEventType,
            ObjectTypeId::BaseEventType,
            true,
        ),
        (
            ObjectTypeId::AuditHistoryUpdateEventType,
            ObjectTypeId::BaseEventType,
            true,
        ),
        (
            ObjectTypeId::AuditUrlMismatchEventType,
            ObjectTypeId::AuditSessionEventType,
            true,
        ),
        // Negative
        //   BaseEventType is not a subtype of AuditEventType
        (
            ObjectTypeId::BaseEventType,
            ObjectTypeId::AuditEventType,
            false,
        ),
        //   DeviceFailureEventType is not a subtype of ProgressEventType (different branches)
        (
            ObjectTypeId::DeviceFailureEventType,
            ObjectTypeId::ProgressEventType,
            false,
        ),
        //   SystemEventType is not a subtype of ProgressEventType (peers)
        (
            ObjectTypeId::SystemEventType,
            ObjectTypeId::ProgressEventType,
            false,
        ),
    ];
    subtypes.iter().for_each(|v| {
        println!(
            "Expecting {:?} to be a subtype of {:?} == {:?}",
            v.0, v.1, v.2
        );
        assert_eq!(address_space.is_subtype(&v.0.into(), &v.1.into()), v.2);
    });
}

#[test]
fn hierarchical_references() {
    let address_space = AddressSpace::new();

    // Try with root
    let refs = address_space
        .find_hierarchical_references(&NodeId::root_folder_id())
        .unwrap();
    assert_eq!(refs.len(), 3);
    assert!(refs.contains(&NodeId::objects_folder_id()));
    assert!(refs.contains(&NodeId::views_folder_id()));
    assert!(refs.contains(&NodeId::types_folder_id()));

    // Try with an object that has some properties
    let node = ObjectId::Server_ServerCapabilities.into();
    let refs = address_space.find_hierarchical_references(&node).unwrap();
    println!("{:#?}", refs);
    assert_eq!(refs.len(), 15);
    assert!(refs.contains(&VariableId::Server_ServerCapabilities_ServerProfileArray.into()));
    assert!(refs.contains(&VariableId::Server_ServerCapabilities_LocaleIdArray.into()));
    assert!(refs.contains(&VariableId::Server_ServerCapabilities_MinSupportedSampleRate.into()));
    assert!(
        refs.contains(&VariableId::Server_ServerCapabilities_MaxBrowseContinuationPoints.into())
    );
    assert!(refs.contains(&VariableId::Server_ServerCapabilities_MaxQueryContinuationPoints.into()));
    assert!(
        refs.contains(&VariableId::Server_ServerCapabilities_MaxHistoryContinuationPoints.into())
    );
    assert!(refs.contains(&VariableId::Server_ServerCapabilities_SoftwareCertificates.into()));
    assert!(refs.contains(&VariableId::Server_ServerCapabilities_MaxArrayLength.into()));
    assert!(refs.contains(&VariableId::Server_ServerCapabilities_MaxStringLength.into()));
    assert!(refs.contains(&VariableId::Server_ServerCapabilities_MaxByteStringLength.into()));
    assert!(refs.contains(&ObjectId::Server_ServerCapabilities_OperationLimits.into()));
    assert!(refs.contains(&ObjectId::Server_ServerCapabilities_ModellingRules.into()));
    assert!(refs.contains(&ObjectId::Server_ServerCapabilities_AggregateFunctions.into()));
    assert!(refs.contains(&ObjectId::HistoryServerCapabilities.into()));
}