openadr 0.11.1

OpenADR 3.1 in Rust: VTN server, VEN runtime, typed client and an open conformance suite
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
//! The client and the VTN, over a real socket.
//!
//! Unit tests can leave two halves of a system mutually consistent and both wrong. These drive the
//! actual HTTP client against the actual server: URL joining, token handling, serialization, status
//! mapping and pagination all have to line up for these to pass.

#![cfg(all(feature = "vtn", feature = "client"))]

use std::sync::Arc;

use openadr::{
    client::{BusinessLogic, Client, ClientError, Query, VirtualEndNode},
    core::FixedClock,
    model::{
        ClientId, ClientName, EventRequest, Interval, IntervalPeriod, ObjectId, ProgramName,
        ProgramRequest, ReportRequest, ReportResource, ResourceName, StartTime, Target, Timestamp,
        Value, ValuesMap, VenName, VenRequest, VenVenRequest,
    },
    vtn::{Vtn, VtnConfig, auth::StaticTokenAuth, store::MemoryStorage},
};
use rust_decimal::Decimal;

const BL: &str = "bl-secret";
const VEN: &str = "ven-secret";

fn now() -> Timestamp {
    "2026-02-11T06:00:00Z".parse().unwrap()
}

/// Start a VTN on an ephemeral port and return its base URL.
async fn start_vtn() -> String {
    let auth = StaticTokenAuth::new("http://unused/auth/token")
        .with_business_logic(BL, ClientId::new("bl").unwrap())
        .with_ven(VEN, ClientId::new("client-a").unwrap());

    let vtn = Vtn::builder()
        .storage(MemoryStorage::shared())
        .authenticator(Arc::new(auth))
        .clock(Arc::new(FixedClock::new(now())))
        .config(VtnConfig {
            base_path: "/openadr3/3.1.0".into(),
            ..Default::default()
        })
        .build();

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let router = vtn.router();
    tokio::spawn(async move {
        let _ = axum::serve(listener, router).await;
    });

    format!("http://{addr}/openadr3/3.1.0")
}

/// A client that presents a fixed bearer token.
///
/// The static-token authenticator does not implement the OAuth2 token endpoint, so tests inject the
/// token through the `Authorization` header directly, as an operator would with a pre-shared token.
fn bl_client(base: &str) -> Client<BusinessLogic> {
    Client::<BusinessLogic>::builder(base)
        .unwrap()
        .bearer_token(BL)
        .build()
        .unwrap()
}

fn ven_client(base: &str) -> Client<VirtualEndNode> {
    Client::<VirtualEndNode>::builder(base)
        .unwrap()
        .bearer_token(VEN)
        .build()
        .unwrap()
}

fn price(value: i64) -> ValuesMap {
    ValuesMap::single(
        "PRICE".parse().unwrap(),
        Value::Number(Decimal::from(value)),
    )
}

/// `[Def §VEN enrollment]`: "A VEN **MUST** support end-user configuration of: VTN URL, `clientID`
/// and `clientSecret` (required for OAuth 2 client credential flow)."
///
/// Three values and nothing else — no pre-issued token, no header set by hand. The client obtains
/// its own token from the VTN's `/auth/token` and uses it, which is the whole enrollment path an
/// installer configures and the one every other test in this file skips by presenting a token
/// directly. It is also the only place the client's half of the grant runs over a socket: the VTN's
/// half is covered in `tests/api.rs` against the router, and two halves that are each tested alone
/// is the arrangement this file exists to distrust.
#[cfg(feature = "internal-auth")]
#[tokio::test]
async fn a_ven_configured_with_a_url_and_a_client_secret_enrols_itself() {
    use openadr::client::Credentials;
    use openadr::vtn::auth::{InternalAuth, Scope, Scopes};

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    // The issuer has to name the URL it will actually be reached at, because that URL is the `iss`
    // and `aud` of every token it mints.
    let base = format!("http://{addr}/openadr3/3.1.0");

    let auth = InternalAuth::builder(format!("{base}/auth/token"))
        .client("ven-7", "hunter2", Scopes::new(Scope::VEN))
        .unwrap()
        .build();
    let vtn = Vtn::builder()
        .storage(MemoryStorage::shared())
        .authenticator(Arc::new(auth))
        .clock(Arc::new(FixedClock::new(now())))
        .config(VtnConfig {
            base_path: "/openadr3/3.1.0".into(),
            ..Default::default()
        })
        .build();
    let router = vtn.router();
    tokio::spawn(async move {
        let _ = axum::serve(listener, router).await;
    });

    // Exactly the three items the specification names an installer must be able to set.
    let client = Client::<VirtualEndNode>::builder(&base)
        .unwrap()
        .credentials(Credentials::new("ven-7", "hunter2"))
        .build()
        .unwrap();

    let programs = client
        .programs()
        .list()
        .await
        .expect("the VEN could not read");
    assert!(programs.is_empty());

    // The token was fetched rather than supplied, and it is cached rather than re-fetched.
    let token = client
        .access_token()
        .await
        .unwrap()
        .expect("the client holds no token, so it never ran the grant");
    client.programs().list().await.unwrap();
    assert_eq!(
        client.access_token().await.unwrap().as_deref(),
        Some(token.as_str()),
        "the client re-ran the grant instead of reusing the token it holds"
    );

    // Pinning the token endpoint instead of discovering it, and asking for fewer scopes than the
    // credential holds. Both are configuration an installer sets and neither had a caller.
    let pinned = Client::<VirtualEndNode>::builder(&base)
        .unwrap()
        .credentials(
            Credentials::new("ven-7", "hunter2")
                .with_token_url(format!("{base}/auth/token").parse().unwrap())
                .with_scopes("read_ven_objects"),
        )
        .build()
        .unwrap();
    pinned
        .programs()
        .list()
        .await
        .expect("a pinned token URL and a narrowed scope still authenticate");
    // Narrowed means narrowed: the credential may write reports and this token asked not to.
    let refused = pinned
        .reports()
        .create(&openadr::model::ReportRequest::new(
            "no-such-event".parse().unwrap(),
            "ven-7".parse().unwrap(),
            Vec::new(),
        ))
        .await;
    assert!(
        refused.is_err(),
        "a token that asked for read_ven_objects alone was allowed to write a report"
    );

    // "A VEN MAY be pre-configured with those items, but MUST support end-user reconfiguration":
    // a wrong secret is a refusal the operator can act on, not a hang and not a panic.
    let wrong = Client::<VirtualEndNode>::builder(&base)
        .unwrap()
        .credentials(Credentials::new("ven-7", "wrong"))
        .build()
        .unwrap();
    let err = wrong
        .programs()
        .list()
        .await
        .expect_err("a wrong client secret must not authenticate");
    let message = err.to_string();
    assert!(
        message.contains("invalid_client") || message.contains("401") || message.contains("400"),
        "a refused grant should say so: {message}"
    );
}

#[tokio::test]
async fn a_full_business_logic_workflow() {
    let base = start_vtn().await;
    let bl = bl_client(&base);

    // Create a programme.
    let program = bl
        .programs()
        .create(&ProgramRequest::new(ProgramName::new("day-ahead").unwrap()))
        .await
        .expect("create programme");
    assert_eq!(program.content.program_name.as_str(), "day-ahead");

    // Create an event in it.
    let event_request = EventRequest::new(program.id.clone())
        .with_interval_period(IntervalPeriod::new(
            StartTime::At("2026-02-12T00:00:00Z".parse().unwrap()),
            "PT1H".parse().unwrap(),
        ))
        .with_intervals(vec![
            Interval::new(0, vec![price(17)]),
            Interval::new(1, vec![price(3)]),
        ]);
    let event = bl
        .events()
        .create(&event_request)
        .await
        .expect("create event");
    assert_eq!(event.content.program_id, program.id);

    // Read it back.
    let fetched = bl.events().get(&event.id).await.expect("get event");
    assert_eq!(fetched.content.intervals.as_ref().unwrap().len(), 2);

    // Update it.
    let mut updated = event_request.clone();
    updated.event_name = Some("renamed".into());
    let updated = bl
        .events()
        .update(&event.id, &updated)
        .await
        .expect("update event");
    assert_eq!(updated.content.event_name.as_deref(), Some("renamed"));

    // Delete it.
    bl.events().delete(&event.id).await.expect("delete event");
    assert!(bl.events().try_get(&event.id).await.unwrap().is_none());
}

#[tokio::test]
async fn decimal_prices_survive_the_whole_round_trip() {
    let base = start_vtn().await;
    let bl = bl_client(&base);

    let program = bl
        .programs()
        .create(&ProgramRequest::new(ProgramName::new("prices").unwrap()))
        .await
        .unwrap();

    // A price that binary floating point cannot represent exactly.
    let exact = "0.17".parse::<Decimal>().unwrap();
    let event = bl
        .events()
        .create(
            &EventRequest::new(program.id.clone())
                .with_interval_period(IntervalPeriod::new(
                    StartTime::At("2026-02-12T00:00:00Z".parse().unwrap()),
                    "PT1H".parse().unwrap(),
                ))
                .with_intervals(vec![Interval::new(
                    0,
                    vec![ValuesMap::single(
                        "PRICE".parse().unwrap(),
                        Value::Number(exact),
                    )],
                )]),
        )
        .await
        .unwrap();

    let fetched = bl.events().get(&event.id).await.unwrap();
    let value = &fetched.content.intervals.as_ref().unwrap()[0].payloads[0].values[0];
    assert_eq!(value, &Value::Number(exact));
}

#[tokio::test]
async fn a_ven_registers_itself_and_files_a_report() {
    let base = start_vtn().await;
    let bl = bl_client(&base);
    let ven = ven_client(&base);

    // The VEN registers itself. It cannot name its own client id or targets.
    let registered = ven
        .vens()
        .create(&VenRequest::Ven(VenVenRequest {
            ven_name: VenName::new("charger-site-7").unwrap(),
            attributes: None,
        }))
        .await
        .expect("register");
    assert_eq!(registered.client_id.as_str(), "client-a");
    assert!(registered.targets.is_empty());

    // Business logic grants it a target.
    let granted = bl
        .vens()
        .update(
            &registered.id,
            &VenRequest::Bl(openadr::model::BlVenRequest {
                client_id: ClientId::new("client-a").unwrap(),
                ven_name: VenName::new("charger-site-7").unwrap(),
                targets: vec![Target::new("zone-a").unwrap()],
                attributes: None,
            }),
        )
        .await
        .expect("grant targets");
    assert_eq!(granted.targets, vec![Target::new("zone-a").unwrap()]);

    // An event for that zone.
    let program = bl
        .programs()
        .create(&ProgramRequest::new(ProgramName::new("gac").unwrap()))
        .await
        .unwrap();
    let event = bl
        .events()
        .create(
            &EventRequest::new(program.id.clone())
                .with_targets(vec![Target::new("zone-a").unwrap()])
                .with_interval_period(IntervalPeriod::new(
                    StartTime::At("2026-02-12T00:00:00Z".parse().unwrap()),
                    "PT15M".parse().unwrap(),
                ))
                .with_intervals(vec![Interval::new(
                    0,
                    vec![ValuesMap::single(
                        "IMPORT_CAPACITY_LIMIT".parse().unwrap(),
                        Value::Number(Decimal::from(60)),
                    )],
                )]),
        )
        .await
        .unwrap();

    // The VEN sees it once it names the target it was granted.
    let visible = ven
        .events()
        .list_with(&Query::new().target(&Target::new("zone-a").unwrap()))
        .await
        .unwrap();
    assert_eq!(visible.len(), 1);
    assert_eq!(visible[0].id, event.id);

    // And it files a report against it.
    let report = ven
        .reports()
        .create(&ReportRequest::new(
            event.id.clone(),
            ClientName::new("charger-site-7").unwrap(),
            vec![ReportResource {
                resource_name: ResourceName::new("charger-1").unwrap(),
                interval_period: None,
                intervals: vec![Interval::new(
                    0,
                    vec![ValuesMap::single(
                        "USAGE".parse().unwrap(),
                        Value::Number("12.5".parse().unwrap()),
                    )],
                )],
            }],
        ))
        .await
        .expect("file report");
    assert_eq!(report.client_id.as_ref().unwrap().as_str(), "client-a");

    // Business logic reads it.
    let reports = bl.reports().list().await.unwrap();
    assert_eq!(reports.len(), 1);
}

#[tokio::test]
async fn a_ven_that_names_no_targets_sees_no_targeted_events() {
    let base = start_vtn().await;
    let bl = bl_client(&base);
    let ven = ven_client(&base);

    ven.vens()
        .create(&VenRequest::Ven(VenVenRequest {
            ven_name: VenName::new("ven-1").unwrap(),
            attributes: None,
        }))
        .await
        .unwrap();

    let program = bl
        .programs()
        .create(&ProgramRequest::new(ProgramName::new("p").unwrap()))
        .await
        .unwrap();
    bl.events()
        .create(
            &EventRequest::new(program.id)
                .with_targets(vec![Target::new("zone-a").unwrap()])
                .with_interval_period(IntervalPeriod::new(
                    StartTime::At("2026-02-12T00:00:00Z".parse().unwrap()),
                    "PT1H".parse().unwrap(),
                ))
                .with_intervals(vec![Interval::new(0, vec![price(1)])]),
        )
        .await
        .unwrap();

    assert!(ven.events().list().await.unwrap().is_empty());
}

#[tokio::test]
async fn pagination_walks_past_the_fifty_record_page() {
    let base = start_vtn().await;
    let bl = bl_client(&base);

    for i in 0..120 {
        bl.programs()
            .create(&ProgramRequest::new(
                ProgramName::new(format!("tariff-{i:03}")).unwrap(),
            ))
            .await
            .unwrap();
    }

    // One page stops at the schema's maximum.
    assert_eq!(bl.programs().list().await.unwrap().len(), 50);

    // Following pagination reaches everything, exactly once.
    let all = bl.programs().list_all(&Query::new()).await.unwrap();
    assert_eq!(all.len(), 120);
    let mut names: Vec<&str> = all
        .iter()
        .map(|p| p.content.program_name.as_str())
        .collect();
    names.sort_unstable();
    names.dedup();
    assert_eq!(names.len(), 120, "pagination returned duplicates");
}

#[tokio::test]
async fn errors_arrive_as_typed_problems() {
    let base = start_vtn().await;
    let bl = bl_client(&base);

    let missing = ObjectId::new("prg-99999999").unwrap();
    match bl.programs().get(&missing).await {
        Err(ClientError::Api { status, problem }) => {
            assert_eq!(status, 404);
            assert_eq!(problem.status, Some(404));
            assert!(problem.detail.is_some());
        }
        other => panic!("expected a 404 problem, got {other:?}"),
    }

    // A duplicate name is a 409, not a generic failure.
    let request = ProgramRequest::new(ProgramName::new("dup").unwrap());
    bl.programs().create(&request).await.unwrap();
    match bl.programs().create(&request).await {
        Err(ClientError::Api { status, .. }) => assert_eq!(status, 409),
        other => panic!("expected a 409, got {other:?}"),
    }
}

#[tokio::test]
async fn a_ven_cannot_reach_business_logic_endpoints() {
    let base = start_vtn().await;
    let bl = bl_client(&base);

    let program = bl
        .programs()
        .create(&ProgramRequest::new(ProgramName::new("p").unwrap()))
        .await
        .unwrap();

    // The typed client will not even offer `create` on a VEN's `events()`, so build a
    // business-logic client around the VEN's token to prove the server refuses it too.
    let ven_as_bl = Client::<BusinessLogic>::builder(&base)
        .unwrap()
        .bearer_token(VEN)
        .build()
        .unwrap();
    match ven_as_bl
        .events()
        .create(&EventRequest::new(program.id))
        .await
    {
        Err(ClientError::Api { status, .. }) => assert_eq!(status, 403),
        other => panic!("expected a 403, got {other:?}"),
    }
}

#[tokio::test]
async fn discovery_endpoints_answer_without_credentials() {
    let base = start_vtn().await;
    // No token at all.
    let anonymous = Client::<VirtualEndNode>::builder(&base)
        .unwrap()
        .build()
        .unwrap();

    let info = anonymous.auth_server().await.expect("auth server info");
    assert!(info.token_url.contains("/auth/token"));
}

#[tokio::test]
async fn an_adapter_rewrites_the_wire_without_touching_the_model() {
    // The adapter chain is the alternative to loosening the model for one peer's deviation, so it
    // has to be wired into the client rather than merely existing. Against a canonical VTN it must
    // be a no-op: an adapter that quietly changed conformant traffic would be worse than none.
    let base = start_vtn().await;
    let plain = bl_client(&base);
    let adapted = Client::<BusinessLogic>::builder(&base)
        .unwrap()
        .bearer_token(BL)
        .adapter(openadr::model::adapt::Fluvius)
        .build()
        .unwrap();

    let request = ProgramRequest::new(ProgramName::new("netflex").unwrap());
    let created = adapted.programs().create(&request).await.unwrap();
    assert_eq!(created.content.program_name.as_str(), "netflex");

    // And a canonical client reads back exactly what the adapted one wrote.
    let read = plain.programs().get(&created.id).await.unwrap();
    assert_eq!(read, created);
}

#[tokio::test]
async fn a_conditional_read_costs_nothing_when_nothing_changed() {
    // The VTN's headline feature for pollers is only a feature if a client can use it.
    let base = start_vtn().await;
    let bl = bl_client(&base);
    bl.programs()
        .create(&ProgramRequest::new(ProgramName::new("tou").unwrap()))
        .await
        .unwrap();

    let first = bl
        .programs()
        .list_if_changed(&Query::new(), None)
        .await
        .unwrap()
        .expect("the first read always returns a body");
    assert_eq!(first.value.len(), 1);
    let tag = first.etag.expect("the VTN tags every GET");

    // Same collection, same tag: no body at all.
    assert!(
        bl.programs()
            .list_if_changed(&Query::new(), Some(&tag))
            .await
            .unwrap()
            .is_none(),
        "an unchanged collection must answer 304"
    );

    // A write changes the tag, so the next poll gets the new page.
    bl.programs()
        .create(&ProgramRequest::new(
            ProgramName::new("critical-peak").unwrap(),
        ))
        .await
        .unwrap();
    let second = bl
        .programs()
        .list_if_changed(&Query::new(), Some(&tag))
        .await
        .unwrap()
        .expect("a changed collection must return a body");
    assert_eq!(second.value.len(), 2);
    assert_ne!(second.etag.as_deref(), Some(tag.as_str()));
}

/// An adapter with a visible effect on the way in, so a path that skips the chain is detectable.
#[derive(Debug, Clone, Copy)]
struct ShoutingNames;

impl openadr::model::adapt::WireAdapter for ShoutingNames {
    fn name(&self) -> &'static str {
        "shouting-names"
    }
    fn inbound(&self, body: &mut serde_json::Value) {
        fn walk(v: &mut serde_json::Value) {
            match v {
                serde_json::Value::Array(items) => items.iter_mut().for_each(walk),
                serde_json::Value::Object(map) => {
                    if let Some(serde_json::Value::String(name)) = map.get_mut("programName") {
                        *name = name.to_uppercase();
                    }
                    for (_, child) in map.iter_mut() {
                        walk(child);
                    }
                }
                _ => {}
            }
        }
        walk(body);
    }
}

#[tokio::test]
async fn every_read_path_runs_the_adapter_chain() {
    // `list` and `list_if_changed` decode the same bytes, so they must decode them the same way.
    // They did not: the conditional path deserialised straight from the socket and skipped the
    // chain, so a client speaking to a peer that bends the schema got adapted objects from one
    // method and raw ones from the other.
    let base = start_vtn().await;
    bl_client(&base)
        .programs()
        .create(&ProgramRequest::new(ProgramName::new("tou").unwrap()))
        .await
        .unwrap();

    let adapted = Client::<BusinessLogic>::builder(&base)
        .unwrap()
        .bearer_token(BL)
        .adapter(ShoutingNames)
        .build()
        .unwrap();

    let listed = adapted.programs().list().await.unwrap();
    assert_eq!(listed[0].content.program_name.as_str(), "TOU");

    let conditional = adapted
        .programs()
        .list_if_changed(&Query::new(), None)
        .await
        .unwrap()
        .expect("the first read returns a body");
    assert_eq!(conditional.value[0].content.program_name.as_str(), "TOU");

    let one = adapted
        .programs()
        .get_if_changed(&listed[0].id, None)
        .await
        .unwrap()
        .expect("the first read returns a body");
    assert_eq!(one.value.content.program_name.as_str(), "TOU");
}

#[tokio::test]
async fn typed_filters_reach_the_right_query_parameters() {
    let base = start_vtn().await;
    let bl = bl_client(&base);
    let tou = bl
        .programs()
        .create(&ProgramRequest::new(ProgramName::new("tou").unwrap()))
        .await
        .unwrap();
    bl.programs()
        .create(&ProgramRequest::new(
            ProgramName::new("critical-peak").unwrap(),
        ))
        .await
        .unwrap();

    let found = bl
        .programs()
        .list_with(&Query::new().program_name(&ProgramName::new("tou").unwrap()))
        .await
        .unwrap();
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].id, tou.id);
}