1/*
 2 * SPDX-FileCopyrightText: 2026 Copyright (c) Contributors to the Eclipse Foundation
 3 *
 4 * See the NOTICE file(s) distributed with this work for additional
 5 * information regarding copyright ownership.
 6 *
 7 * This program and the accompanying materials are made available under the
 8 * terms of the Apache License Version 2.0 which is available at
 9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 */
13
14use axum::http::StatusCode;
15use opensovd_cda_lib::cda_version;
16use reqwest::Method;
17
18use crate::util::{
19    http::{extract_field_from_json, response_to_json},
20    runtime::setup_integration_test,
21};
22
23fn assert_version_response(json: &serde_json::Value) {
24    let id = extract_field_from_json::<String>(json, "id").expect("Missing 'id' field");
25    assert_eq!(id, "version");
26
27    let data =
28        extract_field_from_json::<serde_json::Value>(json, "data").expect("Missing 'data' field");
29    let name = extract_field_from_json::<String>(&data, "name").expect("Missing 'data.name' field");
30    assert_eq!(name, "Eclipse OpenSOVD Classic Diagnostic Adapter");
31
32    let api =
33        extract_field_from_json::<serde_json::Value>(&data, "api").expect("Missing 'data.api'");
34    let api_version =
35        extract_field_from_json::<String>(&api, "version").expect("Missing 'data.api.version'");
36    assert_eq!(api_version, "1.1");
37
38    let implementation = extract_field_from_json::<serde_json::Value>(&data, "implementation")
39        .expect("Missing 'data.implementation'");
40    let impl_version = extract_field_from_json::<String>(&implementation, "version")
41        .expect("Missing 'data.implementation.version'");
42    assert_eq!(impl_version, cda_version());
43}
44
[docs]45/// [[ itest~sovd-api-version-endpoint, Version Endpoint Integration Test, itest ]]
46#[tokio::test]
47async fn test_version_endpoint() {
48    let (runtime, _lock) = setup_integration_test(true).await.unwrap();
49    let host = &runtime.config.server.address;
50    let port = runtime.config.server.port;
51
52    // Test app-scoped version endpoint
53    let app_url = reqwest::Url::parse(&format!(
54        "http://{host}:{port}/vehicle/v15/apps/sovd2uds/data/version"
55    ))
56    .expect("Invalid URL");
57
58    let response =
59        crate::util::http::send_request(StatusCode::OK, Method::GET, None, None, app_url)
60            .await
61            .expect("GET app version endpoint failed");
62
63    let json = response_to_json(&response).expect("Failed to parse version response");
64    assert_version_response(&json);
65
66    // Test global version endpoint
67    let global_url = reqwest::Url::parse(&format!("http://{host}:{port}/vehicle/v15/data/version"))
68        .expect("Invalid URL");
69
70    let response =
71        crate::util::http::send_request(StatusCode::OK, Method::GET, None, None, global_url)
72            .await
73            .expect("GET global version endpoint failed");
74
75    let json = response_to_json(&response).expect("Failed to parse version response");
76    assert_version_response(&json);
77}