rutmf 0.2.0

Ergonomic, v5-first Rust types and clients for the TM Forum Open API domain model (unofficial)
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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
//! Turning a [`ResourceStore`] into a TMF630-conformant HTTP surface.

use bytes::Bytes;
use http::{HeaderMap, HeaderValue, Method, StatusCode, header};
use serde_json::Value;

use crate::api::{Api, TmfRequest, TmfResponse};

use crate::core::EventKind;

use super::notify::{
    HUB_COLLECTION, Listener, change_event, event_type_for, matching_listeners, state_change_kind,
};
use super::semantics::{apply_json_patch, apply_merge_patch, project_fields};
use super::store::{Replaced, ResourceStore, Selection, StoreError};

/// Serves a [`ResourceStore`] as one TM Forum API, over the TMF630 collection
/// semantics.
///
/// The handler is built for one [`Api`] — `rutmf::api::tmf678::API`, say — and
/// serves exactly what that specification declares. A method the API does not
/// declare on a path is answered `405` with an `Allow` header, and a collection
/// it does not declare `404`, before the body is parsed or the store is
/// called. A TMF678 server therefore refuses `POST /customerBill` and
/// `DELETE /customerBill/{id}` without a line of code in the store.
///
/// Within that surface, the handler owns everything between the socket and the
/// store: routing a URL to a collection and an id, reading the query into a [`Selection`], projecting
/// `fields=`, setting the count headers, choosing `200` or `206`, applying the
/// right kind of `PATCH`, and rendering failures as TMF630 error bodies.
///
/// It is framework-agnostic. [`handle`](Self::handle) takes a [`TmfRequest`] and
/// returns a [`TmfResponse`] — the same pair the client side uses, so an
/// adapter for any HTTP server is a small function. One for `axum` ships behind
/// the `server-axum` feature; [`MockTmfServer`] is another, wiring the handler
/// straight into the client's [`Transport`] with no socket at all.
///
/// [`MockTmfServer`]: crate::mock::MockTmfServer
/// [`Transport`]: crate::api::Transport
///
/// ```
/// # #[cfg(feature = "api-tmf678")] {
/// use rutmf::api::tmf678;
/// use rutmf::server::{MemoryStore, TmfHandler};
///
/// let handler = TmfHandler::for_host(&tmf678::API, "https://mycsp.com", MemoryStore::new());
/// assert_eq!(
///     handler.base_url(),
///     "https://mycsp.com/tmf-api/customerBillManagement/v5"
/// );
/// # }
/// ```
#[derive(Clone)]
pub struct TmfHandler<S> {
    base_url: String,
    surface: Surface,
    store: S,
    ids: std::sync::Arc<dyn IdGenerator>,
    /// `None` until a notifier is configured, so a deployment that does not
    /// use notifications never pays a store read per write.
    notifier: Option<std::sync::Arc<dyn Notifier>>,
    /// `None` means an unbounded `GET` on a collection returns everything the
    /// filters matched — see [`TmfHandler::with_max_page_size`].
    max_page_size: Option<usize>,
}

impl<S: std::fmt::Debug> std::fmt::Debug for TmfHandler<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TmfHandler")
            .field("base_url", &self.base_url)
            .field("store", &self.store)
            .finish_non_exhaustive()
    }
}

/// Which operations the handler serves.
///
/// `Any` exists for the unscoped [`MockTmfServer`](crate::mock::MockTmfServer),
/// a test double that serves collections of several APIs at once. It is not
/// reachable from outside the crate: a production handler always names an
/// [`Api`].
#[derive(Debug, Clone, Copy)]
enum Surface {
    Declared(&'static Api),
    #[cfg_attr(
        not(feature = "mock"),
        allow(dead_code, reason = "only the mock serves every collection")
    )]
    Any,
}

impl<S> TmfHandler<S> {
    /// Serves `store` as `api`, rooted at `base_url`.
    ///
    /// The base URL is what the handler stamps into the `href` of a created
    /// resource and into the `Location` header, so it should be the URL clients
    /// reach the API on, not a local bind address. [`for_host`](Self::for_host)
    /// builds it from the API's conventional root.
    ///
    /// An API this crate does not cover is served the same way, from an [`Api`]
    /// you declare — see [`Api::new`].
    pub fn new(api: &'static Api, base_url: impl Into<String>, store: S) -> Self {
        Self::with_surface(Surface::Declared(api), &base_url.into(), store)
    }

    /// Serves `store` as `api` on `host`, at the API's conventional root —
    /// `https://mycsp.com` becomes
    /// `https://mycsp.com/tmf-api/customerBillManagement/v5` for TMF678.
    pub fn for_host(api: &'static Api, host: &str, store: S) -> Self {
        Self::new(api, api.url_on(host), store)
    }

    /// Serves every collection with every operation. For the unscoped mock only.
    #[cfg(feature = "mock")]
    pub(crate) fn serving_anything(base_url: impl Into<String>, store: S) -> Self {
        Self::with_surface(Surface::Any, &base_url.into(), store)
    }

    fn with_surface(surface: Surface, base_url: &str, store: S) -> Self {
        Self {
            base_url: base_url.trim_end_matches('/').to_owned(),
            surface,
            store,
            ids: std::sync::Arc::new(RandomId::default()),
            notifier: None,
            max_page_size: None,
        }
    }

    /// Bounds how many resources one `GET` on a collection may return.
    ///
    /// Without it, a request that names no `limit` returns everything the
    /// filters matched, so the size of a response is chosen by whoever sends the
    /// request.
    ///
    /// The cap lowers a `limit` larger than the maximum and supplies one where
    /// the request named none, so a client that pages properly is unaffected.
    /// `X-Total-Count` still reports the full match, making a capped response a
    /// `206`.
    ///
    /// Off by default: TMF630 permits a maximum without naming one, and turning
    /// this on silently would change what a working deployment returns.
    ///
    /// ```
    /// use rutmf::server::{MemoryStore, TmfHandler};
    /// # use rutmf::api::{Api, Collection};
    /// # const API: Api = Api::new("X", "tmf-api/x/v5", "5.0.0", &[Collection::crud("thing")]);
    ///
    /// let handler = TmfHandler::new(&API, "https://host/tmf-api/x/v5", MemoryStore::new())
    ///     .with_max_page_size(100);
    /// # let _ = handler;
    /// ```
    #[must_use]
    pub fn with_max_page_size(mut self, max: usize) -> Self {
        self.max_page_size = Some(max);
        self
    }

    /// Assigns server-chosen identifiers with `generator` instead of
    /// [`RandomId`].
    ///
    /// See [`IdGenerator`] for why this is a seam rather than a fixed policy.
    #[must_use]
    pub fn with_id_generator(mut self, generator: impl IdGenerator + 'static) -> Self {
        self.ids = std::sync::Arc::new(generator);
        self
    }

    /// Delivers change notifications through `notifier`.
    ///
    /// Without one, the handler still *serves* `/hub` — a subscription is
    /// stored and read back like any other resource — but nothing is ever
    /// delivered. See [`Notifier`] for what the handler works out on your
    /// behalf and what it leaves to you.
    #[must_use]
    pub fn with_notifier(mut self, notifier: impl Notifier + 'static) -> Self {
        self.notifier = Some(std::sync::Arc::new(notifier));
        self
    }

    /// Re-roots the API at `base_url`, for when the URL is only known after the
    /// handler is assembled — once a port is bound, say.
    #[must_use]
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        base_url
            .into()
            .trim_end_matches('/')
            .clone_into(&mut self.base_url);
        self
    }

    /// The URL this API is rooted at.
    #[must_use]
    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// The API this handler serves. `None` only for the unscoped
    /// [`MockTmfServer`](crate::mock::MockTmfServer), which serves anything.
    #[must_use]
    pub fn api(&self) -> Option<&'static Api> {
        match self.surface {
            Surface::Declared(api) => Some(api),
            Surface::Any => None,
        }
    }

    /// The response to a request the API does not declare, or `None` if it
    /// does.
    ///
    /// Needs only the method and the URL, so an adapter can call it before it
    /// reads the body — the `axum` one does — and [`handle`](Self::handle)
    /// calls it before anything else. A request refused here never reaches the
    /// store and never raises a notification.
    #[must_use]
    pub fn refusal(&self, method: &Method, url: &str) -> Option<TmfResponse> {
        let Some((collection, id)) = route(&self.base_url, url) else {
            return Some(no_resource_at(url));
        };
        let on_item = id.is_some();
        let allowed = match self.surface {
            Surface::Declared(api) => {
                let Some(declared) = api.collection(&collection) else {
                    return Some(error_response(
                        StatusCode::NOT_FOUND,
                        "40401",
                        &format!("{} declares no {collection} collection", api.name),
                    ));
                };
                declared.allowed_methods(on_item)
            }
            Surface::Any if on_item => vec![Method::GET, Method::PATCH, Method::DELETE],
            Surface::Any => vec![Method::GET, Method::POST],
        };
        if allowed.is_empty() {
            return Some(no_resource_at(url));
        }
        (!allowed.contains(method)).then(|| method_not_allowed(method, &allowed))
    }

    /// The store behind the handler.
    #[must_use]
    pub fn store(&self) -> &S {
        &self.store
    }

    /// The store behind the handler, mutably.
    pub fn store_mut(&mut self) -> &mut S {
        &mut self.store
    }
}

impl<S: ResourceStore> TmfHandler<S> {
    /// Notifies every subscription that asked for this change.
    ///
    /// Reads the `hub` collection, builds the `{Resource}{Kind}Event` envelope,
    /// keeps the subscriptions whose `query` the event satisfies, and hands each
    /// to the [`Notifier`]. A failure to read the hubs is swallowed: a
    /// notification that cannot be sent must not turn a successful write into an
    /// error the client sees.
    ///
    /// Public because a store that changes a resource *outside* a request —
    /// a fulfilment worker moving an order to `completed` — owes the same
    /// notification, and should not have to rebuild the envelope to send it.
    pub async fn notify(&self, collection: &str, kind: EventKind, resource: &Value) {
        // Reading the subscriptions costs a store round-trip, so a deployment
        // that configured no notifier does not pay one on every write.
        let Some(notifier) = &self.notifier else {
            return;
        };
        // `hub` is a collection like any other, so a server that does not serve
        // one has no subscriptions and nothing to do.
        let Ok(hubs) = self.store.list(HUB_COLLECTION, &Selection::default()).await else {
            return;
        };
        if hubs.items.is_empty() {
            return;
        }

        let event = change_event(collection, kind, resource, &self.ids.next_id());
        let event_type = event_type_for(collection, kind);
        for listener in matching_listeners(&hubs.items, &event) {
            notifier.notify(&listener, &event_type, &event).await;
        }
    }

    /// Whether a change to `collection` is itself notifiable.
    ///
    /// Registering a subscription is not a domain event, so `POST /hub` does not
    /// raise a `HubCreateEvent` — which would be delivered to the subscription
    /// that had just been created, and to every other one.
    fn is_notifiable(collection: &str) -> bool {
        collection != HUB_COLLECTION
    }
}

impl<S: ResourceStore> TmfHandler<S> {
    /// Answers one request.
    ///
    /// Never fails: every outcome, including a store error, is a response. That
    /// is deliberate — an HTTP server has to say *something*, and deciding what
    /// is this type's job rather than the caller's.
    pub async fn handle(&self, request: &TmfRequest) -> TmfResponse {
        if let Some(refused) = self.refusal(&request.method, &request.url) {
            return refused;
        }
        let Some((collection, id)) = route(&self.base_url, &request.url) else {
            return no_resource_at(&request.url);
        };

        if !self.store.has_collection(&collection).await {
            return error_response(
                StatusCode::NOT_FOUND,
                "40401",
                &format!("This API does not serve a {collection} collection"),
            );
        }

        match (request.method.clone(), id) {
            (Method::GET, None) => self.list(&collection, request).await,
            (Method::GET, Some(id)) => self.get(&collection, &id, request).await,
            (Method::POST, None) => self.create(&collection, request).await,
            (Method::PATCH, Some(id)) => self.patch(&collection, &id, request).await,
            (Method::DELETE, Some(id)) => self.delete(&collection, &id, request).await,
            // `refusal` let only declared operations through, and every
            // operation is one of the arms above.
            (method, _) => method_not_allowed(&method, &[]),
        }
    }

    async fn list(&self, collection: &str, request: &TmfRequest) -> TmfResponse {
        // TMF621, TMF635 and TMF639 declare a `filter` parameter carrying a JSONPath
        // expression — a different mechanism from the attribute filtering every
        // other collection uses, and one this handler does not implement.
        // Ignoring it would answer a request to narrow a collection with the
        // whole collection, which is the wrong way to be wrong.
        if request.query.contains_key("filter") {
            return error_response(
                StatusCode::BAD_REQUEST,
                "40001",
                "JSONPath `filter` is not supported by this server; filter by \
                 attribute name instead",
            );
        }
        let selection = match Selection::from_query(&request.query) {
            Ok(selection) => match self.max_page_size {
                Some(max) => selection.capped_at(max),
                None => selection,
            },
            // A `limit` the server cannot read must not become "no limit": that
            // answers a request for one page with the whole collection, and
            // answers it with `200`, so the client cannot tell.
            Err(reason) => return error_response(StatusCode::BAD_REQUEST, "40001", &reason),
        };
        let matched = match self.store.list(collection, &selection).await {
            Ok(matched) => matched,
            Err(error) => return store_error_response(&error),
        };

        let fields = request.query.get("fields").map(String::as_str);
        let items: Vec<Value> = matched
            .items
            .iter()
            .map(|item| project_fields(item, fields))
            .collect();

        let mut headers = HeaderMap::new();
        insert_count(&mut headers, "x-total-count", matched.total);
        insert_count(&mut headers, "x-result-count", items.len());

        // TMF630 marks a partial collection with 206, which is what tells a
        // client the count headers are worth reading.
        let status = if selection.offset + items.len() < matched.total {
            StatusCode::PARTIAL_CONTENT
        } else {
            StatusCode::OK
        };
        json_response(status, headers, &Value::Array(items))
    }

    async fn get(&self, collection: &str, id: &str, request: &TmfRequest) -> TmfResponse {
        match self.store.get(collection, id).await {
            Ok(Some(found)) => {
                // The tag identifies the *stored* resource, so it stays valid
                // when `fields=` narrows what is sent back.
                let tag = entity_tag(&found);
                let mut headers = HeaderMap::new();
                if let Ok(value) = HeaderValue::from_str(&tag) {
                    headers.insert(header::ETAG, value);
                }

                // RFC 9110 §13.1.2. A client that already holds this version
                // says so, and gets told to keep it. TMF payloads are large —
                // a catalog offering with its prices and characteristics runs
                // to kilobytes — and a polling integration re-reads the same
                // resources on every cycle, so this is the difference between
                // a heartbeat and a load problem.
                if if_none_match(request).is_some_and(|expected| expected.matches(&tag)) {
                    return TmfResponse::new(StatusCode::NOT_MODIFIED, headers, Bytes::new());
                }

                json_response(
                    StatusCode::OK,
                    headers,
                    &project_fields(&found, request.query.get("fields").map(String::as_str)),
                )
            }
            Ok(None) => not_found(collection, id),
            Err(error) => store_error_response(&error),
        }
    }

    async fn create(&self, collection: &str, request: &TmfRequest) -> TmfResponse {
        let mut resource = match json_body(request) {
            Ok(body) => body,
            Err(reason) => return error_response(StatusCode::BAD_REQUEST, "40001", &reason),
        };

        // A conformant server assigns id and href when the client omits them,
        // and the href is absolute so a `Ref` resolves against it.
        let Some(object) = resource.as_object_mut() else {
            return error_response(
                StatusCode::BAD_REQUEST,
                "40001",
                "A resource must be a JSON object",
            );
        };
        let id = match object.get("id").and_then(Value::as_str) {
            Some(id) => id.to_owned(),
            None => self.ids.next_id(),
        };
        object.insert("id".to_owned(), Value::String(id.clone()));
        let href = format!("{}/{collection}/{id}", self.base_url);
        object
            .entry("href")
            .or_insert_with(|| Value::String(href.clone()));

        match self.store.create(collection, resource).await {
            Ok(created) => {
                if Self::is_notifiable(collection) {
                    self.notify(collection, EventKind::Create, &created).await;
                }
                // RFC 9110: a 201 should say where the new resource lives. The
                // vendored v5 documents declare exactly two response headers —
                // `X-Total-Count` and `X-Result-Count`, both on collections —
                // and no `Location`, so this is the HTTP rule rather than a TMF
                // one.
                //
                // It is read back off the stored resource rather than reused
                // from the value composed above, because either end may have
                // changed it: a client may send its own `href`, and a store may
                // normalise one. Sending a `Location` that disagrees with the
                // `href` in the body would name a resource that is not the one
                // just created.
                let mut headers = HeaderMap::new();
                let location = created
                    .get("href")
                    .and_then(Value::as_str)
                    .unwrap_or(href.as_str());
                if let Ok(value) = HeaderValue::from_str(location) {
                    headers.insert(header::LOCATION, value);
                }
                // A create is a change, so the caller can hold the tag it
                // answers with and `PATCH` conditionally without re-reading.
                insert_etag(&mut headers, &created);
                json_response(StatusCode::CREATED, headers, &created)
            }
            Err(error) => store_error_response(&error),
        }
    }

    async fn patch(&self, collection: &str, id: &str, request: &TmfRequest) -> TmfResponse {
        let patch = match json_body(request) {
            Ok(body) => body,
            Err(reason) => return error_response(StatusCode::BAD_REQUEST, "40001", &reason),
        };

        let mut target = match self.store.get(collection, id).await {
            Ok(Some(found)) => found,
            Ok(None) => return not_found(collection, id),
            Err(error) => return store_error_response(&error),
        };

        // RFC 9110 §13.1.1. A `PATCH` is read-modify-write, so two clients
        // editing different members of one resource can each overwrite the
        // other's change with no error anywhere. `If-Match` is how HTTP says
        // "only if it still looks like what I read".
        //
        // The tag of what was just read is kept, because rejecting here is only
        // half the job: the write below has to be conditional on the resource
        // still being *this* one, or the race reopens between the two.
        let read_tag = entity_tag(&target);
        let before = target.clone();
        let conditional = if_match(request);
        if let Some(expected) = &conditional
            && !expected.matches(&read_tag)
        {
            return precondition_failed();
        }

        let content_type = request
            .headers
            .get(header::CONTENT_TYPE)
            .and_then(|value| value.to_str().ok())
            .unwrap_or("application/json");

        // Four content types, two behaviours. `application/json` and
        // `application/merge-patch+json` are both RFC 7386 merges; the two
        // `json-patch` families are RFC 6902 operation lists.
        if content_type.starts_with("application/json-patch") {
            if let Err(reason) = apply_json_patch(&mut target, &patch) {
                return error_response(StatusCode::UNPROCESSABLE_ENTITY, "42201", &reason);
            }
        } else {
            apply_merge_patch(&mut target, &patch);
        }

        // `id` is the address, not a member a patch may move.
        if let Some(object) = target.as_object_mut() {
            object.insert("id".to_owned(), Value::String(id.to_owned()));
        }

        // Unconditional when the client sent no precondition — HTTP allows a
        // bare `PATCH` to clobber, and turning concurrent edits into `412`s
        // nobody asked for would be the wrong kind of strict.
        let written = if conditional.is_some() {
            self.store
                .replace_if_unchanged(collection, id, target, &read_tag)
                .await
        } else {
            self.store
                .replace(collection, id, target)
                .await
                .map(|found| found.map_or(Replaced::Missing, Replaced::Updated))
        };

        match written {
            Ok(Replaced::Updated(updated)) => {
                if Self::is_notifiable(collection) {
                    // TMF630 distinguishes a lifecycle move from an ordinary
                    // edit, and a client subscribes to them separately. The
                    // handler can tell which this was: it holds the resource
                    // before and after.
                    let kind = change_kind(collection, &before, &updated);
                    self.notify(collection, kind, &updated).await;
                }
                let mut headers = HeaderMap::new();
                insert_etag(&mut headers, &updated);
                json_response(StatusCode::OK, headers, &updated)
            }
            Ok(Replaced::Missing) => not_found(collection, id),
            Ok(Replaced::Stale) => precondition_failed(),
            Err(error) => store_error_response(&error),
        }
    }

    async fn delete(&self, collection: &str, id: &str, request: &TmfRequest) -> TmfResponse {
        let Some(expected) = if_match(request) else {
            return match self.store.delete(collection, id).await {
                Ok(true) => {
                    if Self::is_notifiable(collection) {
                        // A delete event names what is gone, and by then only
                        // the address is left to name it with.
                        let gone = serde_json::json!({"id": id});
                        self.notify(collection, EventKind::Delete, &gone).await;
                    }
                    TmfResponse::new(StatusCode::NO_CONTENT, HeaderMap::new(), Bytes::new())
                }
                Ok(false) => not_found(collection, id),
                Err(error) => store_error_response(&error),
            };
        };

        // The tag has to be read and the removal has to happen as one step, or
        // a resource edited between the two is deleted on the strength of a
        // precondition that no longer holds.
        let current = match self.store.get(collection, id).await {
            Ok(Some(current)) => current,
            Ok(None) => return not_found(collection, id),
            Err(error) => return store_error_response(&error),
        };
        let tag = entity_tag(&current);
        if !expected.matches(&tag) {
            return precondition_failed();
        }

        match self.store.delete_if_unchanged(collection, id, &tag).await {
            Ok(Replaced::Updated(removed)) => {
                if Self::is_notifiable(collection) {
                    self.notify(collection, EventKind::Delete, &removed).await;
                }
                TmfResponse::new(StatusCode::NO_CONTENT, HeaderMap::new(), Bytes::new())
            }
            Ok(Replaced::Missing) => not_found(collection, id),
            Ok(Replaced::Stale) => precondition_failed(),
            Err(error) => store_error_response(&error),
        }
    }
}

/// Splits a request URL into its collection and, for an item operation, id.
///
/// Every TMF path is `…/{apiRoot}/v{N}/{collection}[/{id}]`, so the version
/// segment locates the rest when the URL does not sit under `base_url` — which
/// happens for an absolute `href` into another API, or a client pointed at a
/// hand-written base.
pub(crate) fn route(base_url: &str, url: &str) -> Option<(String, Option<String>)> {
    let path = url.split('?').next().unwrap_or_default();
    let rest = path
        .strip_prefix(base_url)
        .or_else(|| after_version_segment(path))
        .unwrap_or_else(|| trailing_segments(path));

    let mut segments = rest.split('/').filter(|segment| !segment.is_empty());
    let collection = segments.next()?.to_owned();
    let id = segments.next().map(ToOwned::to_owned);
    // Anything deeper is not a shape TMF v5 defines.
    if segments.next().is_some() {
        return None;
    }
    Some((collection, id))
}

/// Everything after the `v5`-style API version segment, if the path has one.
fn after_version_segment(path: &str) -> Option<&str> {
    let without_scheme = path.split_once("://").map_or(path, |(_, rest)| rest);
    let mut offset = path.len() - without_scheme.len();

    for segment in without_scheme.split('/') {
        let is_version = segment.starts_with('v')
            && segment.len() > 1
            && segment[1..].chars().all(|c| c.is_ascii_digit() || c == '.');
        if is_version {
            return Some(&path[offset + segment.len()..]);
        }
        offset += segment.len() + 1;
    }
    None
}

/// The last one or two segments of a path, as a fallback for an unrecognised
/// root.
fn trailing_segments(path: &str) -> &str {
    let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
    let take = segments.len().min(2);
    let tail = &segments[segments.len() - take..];
    tail.first().map_or("", |first| {
        let start = path.rfind(first).unwrap_or(0);
        &path[start..]
    })
}

/// Delivers a change notification to a registered listener.
///
/// The handler names the event, matches it against each subscription's filter
/// and works out where it goes — see [`crate::server`]. This is
/// the part only a deployment can decide: whether delivery is a blocking `POST`,
/// a queue publish, a retry loop, or a log line.
///
/// ```
/// use rutmf::server::{Listener, MemoryStore, Notifier, TmfHandler};
/// # use rutmf::api::{Api, Collection};
/// # const API: Api = Api::new("X", "tmf-api/x/v5", "5.0.0", &[Collection::crud("thing")]);
/// use serde_json::Value;
///
/// /// Hands each notification to a queue rather than blocking the write.
/// struct Enqueue(tokio::sync::mpsc::UnboundedSender<(String, Value)>);
///
/// #[async_trait::async_trait]
/// impl Notifier for Enqueue {
///     async fn notify(&self, listener: &Listener, event_type: &str, event: &Value) {
///         // `delivery_url` is where TMF630 says this event goes.
///         let _ = self.0.send((listener.delivery_url(event_type), event.clone()));
///     }
/// }
///
/// # fn demo(tx: tokio::sync::mpsc::UnboundedSender<(String, Value)>) {
/// let handler = TmfHandler::new(&API, "https://host/tmf-api/x/v5", MemoryStore::new())
///     .with_notifier(Enqueue(tx));
/// # let _ = handler;
/// # }
/// ```
///
/// `notify` is awaited before the handler answers, so a slow notifier slows the
/// write that caused it — spawning instead would need a runtime this layer does
/// not have. Hand the event to a channel, as above, to return first.
#[async_trait::async_trait]
pub trait Notifier: Send + Sync {
    /// Delivers `event` to `listener`.
    ///
    /// `event_type` is the `{Resource}{Kind}Event` class name, passed separately
    /// because [`Listener::delivery_url`] needs it.
    ///
    /// Returns nothing: a notification that cannot be delivered must not fail
    /// the write that produced it, so errors are the implementation's to handle.
    async fn notify(&self, listener: &Listener, event_type: &str, event: &Value);
}

/// The explicit no-op: subscriptions are served, nothing is delivered.
///
/// Configuring this is the same as configuring nothing, and it exists so that
/// "notifications are deliberately off here" can be written down.
#[async_trait::async_trait]
impl Notifier for () {
    async fn notify(&self, _: &Listener, _: &str, _: &Value) {}
}

/// Assigns the identifier of a newly created resource.
///
/// The default, [`RandomId`], is deliberately replaceable. Identifier policy is
/// a deployment's decision, not a library's: real systems want a `UUIDv7` for
/// index locality, a ULID for sortability, a database sequence, or a
/// tenant-prefixed scheme their operations tooling already understands. Supply
/// one with [`TmfHandler::with_id_generator`].
///
/// A client that sends its own `id` keeps it; this is consulted only when the
/// creating request left the choice to the server.
///
/// ```
/// use rutmf::server::{IdGenerator, MemoryStore, TmfHandler};
/// # use rutmf::api::{Api, Collection};
/// # const API: Api = Api::new("X", "tmf-api/x/v5", "5.0.0", &[Collection::crud("thing")]);
/// use std::sync::atomic::{AtomicU64, Ordering};
///
/// /// Ids from a database-style sequence, for a deployment that wants them.
/// struct Sequence(AtomicU64);
///
/// impl IdGenerator for Sequence {
///     fn next_id(&self) -> String {
///         self.0.fetch_add(1, Ordering::Relaxed).to_string()
///     }
/// }
///
/// let handler = TmfHandler::new(&API, "https://host/tmf-api/x/v5", MemoryStore::new())
///     .with_id_generator(Sequence(AtomicU64::new(1)));
/// ```
pub trait IdGenerator: Send + Sync {
    /// The identifier for a resource about to be created.
    fn next_id(&self) -> String;
}

/// The default [`IdGenerator`]: 128 unpredictable bits, rendered as hex.
///
/// # Why not a counter, and why not the obvious one-liner
///
/// A sequential id tells anyone who creates a resource how many the server
/// holds, and makes the neighbouring one guessable — which across tenants is a
/// disclosure rather than an inconvenience.
///
/// The tempting zero-dependency shortcut, `RandomState::new().hash_one(…)`,
/// does not fix that as thoroughly as it looks. `RandomState::new()` seeds a
/// thread-local pair *once* from the operating system and then **increments**
/// it on every subsequent call, so hashing the same input each time is a keyed
/// hash over a marching key — a related-key construction, not a draw from a
/// random source, and only 64 bits wide.
///
/// So this keeps one `RandomState` for the life of the generator — that is the
/// secret, seeded once from the OS — and uses it as a keyed pseudo-random
/// function over a counter, which is what `SipHash` is actually built for. Two
/// domain-separated evaluations give 128 bits.
pub struct RandomId {
    key: std::hash::RandomState,
    counter: std::sync::atomic::AtomicU64,
}

impl Default for RandomId {
    fn default() -> Self {
        Self {
            key: std::hash::RandomState::new(),
            counter: std::sync::atomic::AtomicU64::new(0),
        }
    }
}

impl std::fmt::Debug for RandomId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // The key is a secret; printing it would let an observer of one log
        // line predict every id the server goes on to mint.
        f.debug_struct("RandomId").finish_non_exhaustive()
    }
}

impl IdGenerator for RandomId {
    fn next_id(&self) -> String {
        use std::hash::BuildHasher;
        use std::sync::atomic::Ordering;

        let n = self.counter.fetch_add(1, Ordering::Relaxed);
        // Distinct inputs, one key: two independent 64-bit PRF outputs.
        let high = self.key.hash_one((0u8, n));
        let low = self.key.hash_one((1u8, n));
        format!("{high:016x}{low:016x}")
    }
}

/// Reads a request body as JSON, or says why it could not be.
fn json_body(request: &TmfRequest) -> Result<Value, String> {
    let Some(body) = request.body.as_ref() else {
        return Err("Missing request body".to_owned());
    };
    serde_json::from_slice(body).map_err(|error| format!("Body is not valid JSON: {error}"))
}

/// Which kind of change a `PATCH` was.
///
/// TM Forum raises a lifecycle event for a state move and
/// `…AttributeValueChangeEvent` for any other edit. A `PATCH` is
/// read-modify-write, so the handler holds the resource on both sides.
///
/// Two things make this more than a member comparison:
///
/// - the state member is `lifecycleStatus` in the catalog APIs, `state` in
///   ordering and inventory and `status` in TMF621, so all three are compared;
/// - which event a move raises depends on the *collection*, not the member — see
///   [`state_change_kind`].
///
/// `operatingStatus` is checked first: TMF638's `Service` is the one resource
/// carrying both an administrative `state` and an operational `operatingStatus`,
/// with a listener for each.
fn change_kind(collection: &str, before: &Value, after: &Value) -> EventKind {
    const STATE_MEMBERS: [&str; 3] = ["lifecycleStatus", "state", "status"];

    if before.get("operatingStatus") != after.get("operatingStatus") {
        return EventKind::OperatingStatusChange;
    }

    let moved = STATE_MEMBERS
        .iter()
        .any(|member| before.get(member) != after.get(member));

    if moved {
        state_change_kind(collection)
    } else {
        EventKind::AttributeValueChange
    }
}

/// RFC 9110 §13.1.1: the resource is not the one the client's tag described.
fn precondition_failed() -> TmfResponse {
    error_response(
        StatusCode::PRECONDITION_FAILED,
        "41201",
        "The resource has changed since the tag in If-Match was issued",
    )
}

fn not_found(collection: &str, id: &str) -> TmfResponse {
    error_response(
        StatusCode::NOT_FOUND,
        "40401",
        &format!("No {collection} with id {id}"),
    )
}

/// Renders a [`StoreError`] as the status and TMF630 body it stands for.
fn store_error_response(error: &StoreError) -> TmfResponse {
    let (status, code) = match error {
        StoreError::Accepted { monitor } => {
            let mut headers = HeaderMap::new();
            if let Some(value) = monitor
                .as_deref()
                .and_then(|url| HeaderValue::from_str(url).ok())
            {
                headers.insert(header::LOCATION, value);
            }
            return TmfResponse::new(StatusCode::ACCEPTED, headers, Bytes::new());
        }
        StoreError::Invalid(_) => (StatusCode::BAD_REQUEST, "40001"),
        StoreError::Forbidden(_) => (StatusCode::FORBIDDEN, "40301"),
        StoreError::Conflict(_) => (StatusCode::CONFLICT, "40901"),
        StoreError::Unprocessable(_) => (StatusCode::UNPROCESSABLE_ENTITY, "42201"),
        StoreError::Internal(_) => (StatusCode::INTERNAL_SERVER_ERROR, "50001"),
    };
    error_response(status, code, &error.to_string())
}

/// The `ETag` of a stored resource: a strong validator over its content.
///
/// Derived from the resource itself rather than kept beside it, so a store
/// needs no version column and no extra method to take part. Two resources
/// with equal content share a tag, which is exactly the semantics RFC 9110
/// §8.8.3 gives a strong validator.
///
/// Public because a store implementing
/// [`replace_if_unchanged`](super::ResourceStore::replace_if_unchanged)
/// atomically has to compare against the same value the handler issued, and
/// computing it a second way is how a precondition quietly stops holding.
///
/// The hash is `DefaultHasher`, which is neither stable across Rust releases
/// nor collision-resistant against an adversary. Neither matters here: a tag is
/// meaningful only within one running server, and a client that forges one is
/// only able to overwrite a resource it could already overwrite by omitting
/// `If-Match` entirely.
#[must_use]
pub fn entity_tag(resource: &Value) -> String {
    use std::hash::{Hash, Hasher};

    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    // Through the serialised form: `Value`'s own `Hash` is not implemented,
    // and the serialisation is what the client saw.
    serde_json::to_string(resource)
        .unwrap_or_default()
        .hash(&mut hasher);
    format!("\"{:016x}\"", hasher.finish())
}

fn insert_etag(headers: &mut HeaderMap, resource: &Value) {
    if let Ok(value) = HeaderValue::from_str(&entity_tag(resource)) {
        headers.insert(header::ETAG, value);
    }
}

/// A parsed `If-Match` precondition.
#[derive(Debug, PartialEq, Eq)]
enum IfMatch {
    /// `If-Match: *` — "only if the resource exists at all", which it does by
    /// the time this is consulted.
    Any,
    /// One or more entity tags, any of which satisfies the precondition.
    Tags(Vec<String>),
}

impl IfMatch {
    fn matches(&self, current: &str) -> bool {
        match self {
            Self::Any => true,
            // A weak comparison would be wrong here: RFC 9110 §13.1.1 requires
            // strong comparison for `If-Match`, because the point is to guard a
            // write against a resource that changed in any way at all.
            Self::Tags(tags) => tags.iter().any(|tag| tag == current),
        }
    }
}

/// Reads the `If-Match` header, if the request carries a usable one.
fn if_match(request: &TmfRequest) -> Option<IfMatch> {
    parse_precondition(request, header::IF_MATCH)
}

/// Reads the `If-None-Match` header, if the request carries a usable one.
///
/// Parsed into the same type as `If-Match` because the syntax is identical;
/// what differs is the sense in which the caller uses the answer, and the
/// comparison strength RFC 9110 prescribes. That difference is real: §13.1.2
/// requires *weak* comparison for `If-None-Match`, where §13.1.1 requires strong
/// comparison for `If-Match`. It makes no difference here, because
/// [`entity_tag`] only ever issues strong validators — a weak one would come
/// from a client, and a client's `W/"x"` genuinely does not match this server's
/// `"x"`, since the two were computed by different rules.
fn if_none_match(request: &TmfRequest) -> Option<IfMatch> {
    parse_precondition(request, header::IF_NONE_MATCH)
}

fn parse_precondition(request: &TmfRequest, name: header::HeaderName) -> Option<IfMatch> {
    let raw = request.headers.get(name)?.to_str().ok()?.trim();

    if raw == "*" {
        return Some(IfMatch::Any);
    }
    let tags: Vec<String> = raw
        .split(',')
        .map(str::trim)
        .filter(|tag| !tag.is_empty())
        // A `W/` prefix marks a weak validator; it can never satisfy a strong
        // comparison, but keeping it verbatim makes that a mismatch rather
        // than an accidental match on the bare tag.
        .map(ToOwned::to_owned)
        .collect();
    (!tags.is_empty()).then_some(IfMatch::Tags(tags))
}

fn insert_count(headers: &mut HeaderMap, name: &'static str, value: usize) {
    if let Ok(value) = HeaderValue::from_str(&value.to_string()) {
        headers.insert(name, value);
    }
}

fn json_response(status: StatusCode, mut headers: HeaderMap, body: &Value) -> TmfResponse {
    headers.insert(
        header::CONTENT_TYPE,
        HeaderValue::from_static("application/json"),
    );
    let bytes = serde_json::to_vec(body).unwrap_or_else(|_| b"null".to_vec());
    TmfResponse::new(status, headers, Bytes::from(bytes))
}

/// `404` for a path that names no resource of this API.
fn no_resource_at(url: &str) -> TmfResponse {
    error_response(
        StatusCode::NOT_FOUND,
        "40401",
        &format!("No TM Forum resource at {url}"),
    )
}

/// `405` with the `Allow` header RFC 9110 §15.5.6 requires on every one.
fn method_not_allowed(method: &Method, allowed: &[Method]) -> TmfResponse {
    let list = allowed
        .iter()
        .map(Method::as_str)
        .collect::<Vec<_>>()
        .join(", ");
    let mut response = error_response(
        StatusCode::METHOD_NOT_ALLOWED,
        "40501",
        &format!("{method} is not allowed on this path; allowed: {list}"),
    );
    if let Ok(value) = HeaderValue::from_str(&list) {
        response.headers.insert(header::ALLOW, value);
    }
    response
}

/// Renders a TMF630 `Error` body.
pub(crate) fn error_response(status: StatusCode, code: &str, reason: &str) -> TmfResponse {
    let body = serde_json::json!({
        "code": code,
        "reason": reason,
        "status": status.as_u16().to_string(),
        "@type": "Error",
    });
    json_response(status, HeaderMap::new(), &body)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::{Collection, Operation};

    /// TMF678 as its specification declares it — kept local so these tests do
    /// not need the `api-tmf678` feature.
    const TEST_API: Api = Api::new(
        "TMF678",
        "tmf-api/customerBillManagement/v5",
        "5.0.0",
        &[
            Collection::read_only("appliedCustomerBillingRate"),
            Collection::new(
                "customerBill",
                &[Operation::List, Operation::Retrieve, Operation::Update],
            ),
            Collection::task("customerBillOnDemand"),
            Collection::new("hub", &[Operation::Create, Operation::Delete]),
        ],
    );
    const BASE: &str = "http://host/tmf-api/customerBillManagement/v5";

    fn bills() -> TmfHandler<crate::server::MemoryStore> {
        let store = crate::server::MemoryStore::new();
        store.seed(
            "customerBill",
            serde_json::json!({"id": "B1", "state": "new", "@type": "CustomerBill"}),
        );
        TmfHandler::new(&TEST_API, BASE, store)
    }

    fn send(method: Method, path: &str, body: Option<&str>) -> TmfRequest {
        let mut request = TmfRequest::new(method, format!("{BASE}/{path}"));
        if let Some(body) = body {
            request.headers.insert(
                header::CONTENT_TYPE,
                HeaderValue::from_static("application/merge-patch+json"),
            );
            request.body = Some(Bytes::from(body.to_owned()));
        }
        request
    }

    fn allow(response: &TmfResponse) -> &str {
        response
            .headers
            .get(header::ALLOW)
            .and_then(|v| v.to_str().ok())
            .unwrap_or("<none>")
    }

    /// TMF678 declares none of these: an issued bill is never created or
    /// deleted through the API.
    #[tokio::test]
    async fn a_billing_server_refuses_what_tmf678_does_not_declare() {
        let handler = bills();
        let post = handler
            .handle(&send(Method::POST, "customerBill", Some("{}")))
            .await;
        assert_eq!(
            (post.status, allow(&post)),
            (StatusCode::METHOD_NOT_ALLOWED, "GET")
        );
        let delete = handler
            .handle(&send(Method::DELETE, "customerBill/B1", None))
            .await;
        assert_eq!(
            (delete.status, allow(&delete)),
            (StatusCode::METHOD_NOT_ALLOWED, "GET, PATCH")
        );
        let rate = handler
            .handle(&send(
                Method::POST,
                "appliedCustomerBillingRate",
                Some("{}"),
            ))
            .await;
        assert_eq!(
            (rate.status, allow(&rate)),
            (StatusCode::METHOD_NOT_ALLOWED, "GET")
        );
        assert_eq!(handler.store().collection("customerBill").len(), 1);
        assert_eq!(
            handler
                .store()
                .collection("appliedCustomerBillingRate")
                .len(),
            0
        );

        // What TMF678 does declare still works.
        let patch = handler
            .handle(&send(
                Method::PATCH,
                "customerBill/B1",
                Some(r#"{"state":"sent"}"#),
            ))
            .await;
        assert_eq!(patch.status, StatusCode::OK);
    }

    #[tokio::test]
    async fn every_405_names_what_is_allowed_and_undeclared_collections_are_404() {
        let handler = bills();
        let put = handler
            .handle(&send(Method::PUT, "customerBill/B1", Some("{}")))
            .await;
        assert_eq!(
            (put.status, allow(&put)),
            (StatusCode::METHOD_NOT_ALLOWED, "GET, PATCH")
        );
        let hub = handler.handle(&send(Method::GET, "hub/h1", None)).await;
        assert_eq!(
            (hub.status, allow(&hub)),
            (StatusCode::METHOD_NOT_ALLOWED, "DELETE")
        );
        let listener = handler
            .handle(&send(Method::POST, "listener", Some("{}")))
            .await;
        assert_eq!(listener.status, StatusCode::NOT_FOUND);
        assert_eq!(handler.store().collection("listener").len(), 0);
    }

    #[test]
    fn for_host_roots_the_api_at_its_conventional_path() {
        let handler = TmfHandler::for_host(
            &TEST_API,
            "https://mycsp.com/",
            crate::server::MemoryStore::new(),
        );
        assert_eq!(
            handler.base_url(),
            BASE.replace("http://host", "https://mycsp.com")
        );
        assert_eq!(handler.api().map(|api| api.name), Some("TMF678"));
    }

    #[test]
    fn a_collection_and_an_item_route_apart() {
        let base = "http://host/tmf-api/productCatalogManagement/v5";
        assert_eq!(
            route(base, &format!("{base}/productOffering")),
            Some(("productOffering".to_owned(), None))
        );
        assert_eq!(
            route(base, &format!("{base}/productOffering/7655")),
            Some(("productOffering".to_owned(), Some("7655".to_owned())))
        );
    }

    #[test]
    fn a_url_under_another_root_routes_by_its_version_segment() {
        // An absolute `href` into a different API still has to resolve.
        assert_eq!(
            route(
                "http://host/tmf-api/productCatalogManagement/v5",
                "https://elsewhere.example/tmf-api/partyManagement/v5/individual/7"
            ),
            Some(("individual".to_owned(), Some("7".to_owned())))
        );
    }

    #[test]
    fn a_path_deeper_than_a_resource_does_not_route() {
        let base = "http://host/tmf-api/v5";
        assert_eq!(route(base, &format!("{base}/a/b/c")), None);
    }

    #[test]
    fn ids_are_not_sequential() {
        // A sequential id leaks the size of the store and makes another
        // tenant's resource guessable.
        let generator = RandomId::default();
        let ids: std::collections::BTreeSet<String> =
            (0..64).map(|_| generator.next_id()).collect();

        assert_eq!(ids.len(), 64, "ids collided");
        assert!(!ids.contains("1"), "ids look like a counter");
        assert!(
            ids.iter().all(|id| id.len() == 32),
            "128 bits, rendered as hex"
        );
        // Two generators are independently keyed, so one server's ids say
        // nothing about another's.
        let other = RandomId::default();
        assert!(
            !ids.contains(&other.next_id()),
            "a second generator reproduced the first's sequence"
        );
    }

    #[test]
    fn an_id_generator_can_be_replaced() {
        struct Fixed;
        impl IdGenerator for Fixed {
            fn next_id(&self) -> String {
                "fixed".to_owned()
            }
        }
        let handler = TmfHandler::new(
            &TEST_API,
            "http://host/tmf-api/x/v5",
            crate::server::MemoryStore::new(),
        )
        .with_id_generator(Fixed);
        assert_eq!(handler.ids.next_id(), "fixed");
    }

    #[test]
    fn if_match_is_parsed_and_compared_strongly() {
        fn request(value: &'static str) -> TmfRequest {
            let mut request = TmfRequest::new(Method::PATCH, "http://host/x/v5/a/1");
            request
                .headers
                .insert(header::IF_MATCH, HeaderValue::from_static(value));
            request
        }

        assert_eq!(if_match(&TmfRequest::new(Method::PATCH, "u")), None);
        assert_eq!(if_match(&request("*")), Some(IfMatch::Any));
        assert!(IfMatch::Any.matches("\"abc\""));

        let parsed = if_match(&request("\"abc\", \"def\"")).expect("two tags");
        assert!(parsed.matches("\"def\""));
        assert!(!parsed.matches("\"ghi\""));

        // RFC 9110 §13.1.1 requires strong comparison, so a weak validator
        // never satisfies `If-Match`.
        let weak = if_match(&request("W/\"abc\"")).expect("a weak tag");
        assert!(!weak.matches("\"abc\""));
    }

    #[test]
    fn an_entity_tag_tracks_the_content() {
        let a = serde_json::json!({"id": "1", "name": "old"});
        let b = serde_json::json!({"id": "1", "name": "new"});

        assert_eq!(entity_tag(&a), entity_tag(&a.clone()), "stable");
        assert_ne!(entity_tag(&a), entity_tag(&b), "an edit changes the tag");
        assert!(
            entity_tag(&a).starts_with('"') && entity_tag(&a).ends_with('"'),
            "RFC 9110 entity-tags are quoted"
        );
    }
}