use crate::model::{
CorrelationId, FilePayload, InfrastructureInquiryPart, InfrastructureManagerInquiryRequest,
OverallResult, Page, PageRequest, ProcessingState, RequestId, Response, WaitOptions,
};
use crate::wire::inquiries;
use crate::wire::soap::{self, Operation, Service};
use crate::{Client, Error, Result};
const SUBMIT_INFRASTRUCTURE_MANAGER_INQUIRY: Operation = Operation::new(
Service::R49,
"zjistiUzemiSpravcuDti",
"ZjistiUzemiSpravcuDti",
"ZjistiUzemiSpravcuDtiOdpoved",
)
.with_response_data_namespace(soap::ISDMVS_MESSAGES_NS);
const READ_INFRASTRUCTURE_MANAGER_INQUIRY_RESULT: Operation = Operation::new(
Service::R49,
"ctiVysledekZjistiUzemiSpravcuDti",
"CtiVysledekZjistiUzemiSpravcuDti",
"CtiVysledekZjistiUzemiSpravcuDtiOdpoved",
);
const LIST_INFRASTRUCTURE_MANAGER_INQUIRY_PARTS: Operation = Operation::new(
Service::R49,
"ctiVysledekZjistiUzemiSpravcuDtiSeznamCastiDti",
"CtiVysledekZjistiUzemiSpravcuDtiSeznamCastiDti",
"CtiVysledekZjistiUzemiSpravcuDtiSeznamCastiDtiOdpoved",
);
const READ_INFRASTRUCTURE_MANAGER_INQUIRY_REPORT: Operation = Operation::new(
Service::R49,
"ctiVysledekZjistiUzemiSpravcuDtiReport",
"CtiVysledekZjistiUzemiSpravcuDtiReport",
"CtiVysledekZjistiUzemiSpravcuDtiReportOdpoved",
);
fn checked_page(page: PageRequest) -> Result<PageRequest> {
Ok(PageRequest::new(page.offset, page.limit)?.with_total(page.include_total))
}
impl Client {
pub async fn submit_infrastructure_manager_inquiry(
&self,
request: &InfrastructureManagerInquiryRequest,
) -> Result<Response<RequestId>> {
self.submit_infrastructure_manager_inquiry_with_correlation_id(
CorrelationId::new(),
request,
)
.await
}
pub async fn submit_infrastructure_manager_inquiry_with_correlation_id(
&self,
correlation_id: CorrelationId,
request: &InfrastructureManagerInquiryRequest,
) -> Result<Response<RequestId>> {
request.selector.validate()?;
let response = self
.execute_submission(
SUBMIT_INFRASTRUCTURE_MANAGER_INQUIRY,
correlation_id,
&[],
|writer| inquiries::write_inquiry_request(writer, self.subject_id(), request),
inquiries::decode_submission,
)
.await?;
if response.metadata.state != ProcessingState::Accepted {
return Err(Error::unexpected_submission_state(response));
}
Ok(response)
}
pub async fn read_infrastructure_manager_inquiry_result(
&self,
request_id: &RequestId,
) -> Result<Response<Option<OverallResult>>> {
self.read_infrastructure_manager_inquiry_result_at(CorrelationId::new(), request_id)
.await
}
async fn read_infrastructure_manager_inquiry_result_at(
&self,
correlation_id: CorrelationId,
request_id: &RequestId,
) -> Result<Response<Option<OverallResult>>> {
self.execute(
READ_INFRASTRUCTURE_MANAGER_INQUIRY_RESULT,
correlation_id,
|writer| inquiries::write_result_request(writer, request_id),
inquiries::decode_overall_result,
)
.await
}
pub async fn list_infrastructure_manager_inquiry_parts(
&self,
request_id: &RequestId,
only_valid: Option<bool>,
page: PageRequest,
) -> Result<Response<Page<InfrastructureInquiryPart>>> {
self.list_infrastructure_manager_inquiry_parts_at(
CorrelationId::new(),
request_id,
only_valid,
page,
)
.await
}
async fn list_infrastructure_manager_inquiry_parts_at(
&self,
correlation_id: CorrelationId,
request_id: &RequestId,
only_valid: Option<bool>,
page: PageRequest,
) -> Result<Response<Page<InfrastructureInquiryPart>>> {
let page = checked_page(page)?;
self.execute(
LIST_INFRASTRUCTURE_MANAGER_INQUIRY_PARTS,
correlation_id,
|writer| inquiries::write_parts_request(writer, request_id, only_valid, page),
|data| inquiries::decode_parts(data, page.offset),
)
.await
}
pub async fn read_infrastructure_manager_inquiry_report(
&self,
request_id: &RequestId,
) -> Result<Response<Option<FilePayload>>> {
self.read_infrastructure_manager_inquiry_report_at(CorrelationId::new(), request_id)
.await
}
async fn read_infrastructure_manager_inquiry_report_at(
&self,
correlation_id: CorrelationId,
request_id: &RequestId,
) -> Result<Response<Option<FilePayload>>> {
self.execute_with_files(
READ_INFRASTRUCTURE_MANAGER_INQUIRY_REPORT,
correlation_id,
|writer| inquiries::write_result_request(writer, request_id),
inquiries::decode_report,
)
.await
}
pub async fn wait_for_infrastructure_manager_inquiry(
&self,
request_id: &RequestId,
options: WaitOptions,
) -> Result<Response<OverallResult>> {
super::lifecycle::wait_for_async(
request_id,
options,
|_| false,
|| self.read_infrastructure_manager_inquiry_result(request_id),
)
.await
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use wiremock::matchers::{body_string, header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
use super::*;
use crate::model::InfrastructureManagerInquirySelector;
const REQUEST_UUID: &str = "ed8af7af-5b39-4dff-96ab-7561214336f1";
const RESPONSE_UUID: &str = "c5f073e9-d3e9-4845-aae0-de3ef42357be";
fn request() -> InfrastructureManagerInquiryRequest {
InfrastructureManagerInquiryRequest::new(
InfrastructureManagerInquirySelector::parcels(vec![5_000_000_000]).unwrap(),
"business-identification",
)
.unwrap()
}
fn response_envelope(
operation: Operation,
request_id: CorrelationId,
state: &str,
data: Option<&str>,
) -> String {
let request_id = request_id.as_uuid();
let data = data
.map(|data| {
if operation.response_data_namespace == soap::ISDMVS_MESSAGES_NS {
format!("<iMsg:Data>{data}</iMsg:Data>")
} else {
format!("<r:Data>{data}</r:Data>")
}
})
.unwrap_or_default();
let code = match state {
"Prijato" => "1010",
"ProbihaZpracovani" => "1020",
_ => "1030",
};
format!(
"<s:Envelope xmlns:s=\"{}\" xmlns:r=\"{}\" xmlns:iMsg=\"{}\" xmlns:msg=\"{}\"><s:Body><r:{}><msg:Hlavicka><msg:UidOdpovedi>{RESPONSE_UUID}</msg:UidOdpovedi><msg:Vysledek stav=\"{state}\"><msg:UidZadosti>{request_id}</msg:UidZadosti><msg:Hlaseni><msg:Hlaseni kod=\"{code}\" typ=\"Informace\"><msg:Zprava>Synthetic response</msg:Zprava></msg:Hlaseni></msg:Hlaseni></msg:Vysledek></msg:Hlavicka>{data}</r:{}></s:Body></s:Envelope>",
soap::SOAP_11_NS,
operation.service.namespace(),
soap::ISDMVS_MESSAGES_NS,
soap::MESSAGES_NS,
operation.response,
operation.response,
)
}
async fn expect_exchange(
server: &MockServer,
operation: Operation,
request: Vec<u8>,
status: u16,
response: String,
) {
Mock::given(method("POST"))
.and(path(format!("/{}", operation.service.path())))
.and(header("SOAPAction", format!("\"{}\"", operation.action)))
.and(header("Content-Type", "text/xml; charset=utf-8"))
.and(body_string(String::from_utf8(request).unwrap()))
.respond_with(
ResponseTemplate::new(status).set_body_raw(response, "text/xml; charset=utf-8"),
)
.expect(1)
.mount(server)
.await;
}
#[test]
fn operation_descriptors_match_all_four_official_r49_operations() {
let descriptors = [
(
SUBMIT_INFRASTRUCTURE_MANAGER_INQUIRY,
"zjistiUzemiSpravcuDti",
"ZjistiUzemiSpravcuDti",
"ZjistiUzemiSpravcuDtiOdpoved",
),
(
READ_INFRASTRUCTURE_MANAGER_INQUIRY_RESULT,
"ctiVysledekZjistiUzemiSpravcuDti",
"CtiVysledekZjistiUzemiSpravcuDti",
"CtiVysledekZjistiUzemiSpravcuDtiOdpoved",
),
(
LIST_INFRASTRUCTURE_MANAGER_INQUIRY_PARTS,
"ctiVysledekZjistiUzemiSpravcuDtiSeznamCastiDti",
"CtiVysledekZjistiUzemiSpravcuDtiSeznamCastiDti",
"CtiVysledekZjistiUzemiSpravcuDtiSeznamCastiDtiOdpoved",
),
(
READ_INFRASTRUCTURE_MANAGER_INQUIRY_REPORT,
"ctiVysledekZjistiUzemiSpravcuDtiReport",
"CtiVysledekZjistiUzemiSpravcuDtiReport",
"CtiVysledekZjistiUzemiSpravcuDtiReportOdpoved",
),
];
for (operation, action, request, response) in descriptors {
assert_eq!(operation.action, action);
assert_eq!(operation.request, request);
assert_eq!(operation.response, response);
assert_eq!(operation.service.path(), "R49ZjisteniUzemiSpravcuDti");
assert_eq!(
operation.service.namespace(),
"urn:cz:isvs:dmvs:isdmvs:schemas:R49ZjisteniUzemiSpravcuDti:v1"
);
}
assert_eq!(
SUBMIT_INFRASTRUCTURE_MANAGER_INQUIRY.response_data_namespace,
soap::ISDMVS_MESSAGES_NS
);
for operation in [
READ_INFRASTRUCTURE_MANAGER_INQUIRY_RESULT,
LIST_INFRASTRUCTURE_MANAGER_INQUIRY_PARTS,
READ_INFRASTRUCTURE_MANAGER_INQUIRY_REPORT,
] {
assert_eq!(operation.response_data_namespace, Service::R49.namespace());
}
}
#[tokio::test]
async fn accepted_http_202_submission_returns_id_after_exactly_one_request() {
let server = MockServer::start().await;
let correlation_id: CorrelationId = REQUEST_UUID.parse().unwrap();
let request = request();
let body = soap::request(
SUBMIT_INFRASTRUCTURE_MANAGER_INQUIRY,
correlation_id,
|writer| {
inquiries::write_inquiry_request(
writer,
&crate::SubjectId::new("requester-id").unwrap(),
&request,
)
},
)
.unwrap();
expect_exchange(
&server,
SUBMIT_INFRASTRUCTURE_MANAGER_INQUIRY,
body,
202,
response_envelope(
SUBMIT_INFRASTRUCTURE_MANAGER_INQUIRY,
correlation_id,
"Prijato",
Some("<msg:IdPozadavku>durable-request-id</msg:IdPozadavku>"),
),
)
.await;
let client = Client::test_client(server.uri());
let response = client
.submit_infrastructure_manager_inquiry_with_correlation_id(correlation_id, &request)
.await
.unwrap();
assert_eq!(response.value.as_str(), "durable-request-id");
assert_eq!(response.metadata.state, ProcessingState::Accepted);
assert_eq!(response.metadata.request_id, correlation_id);
assert_eq!(server.received_requests().await.unwrap().len(), 1);
server.verify().await;
}
#[tokio::test]
async fn non_accepted_submission_recovers_response_without_an_automatic_retry() {
let server = MockServer::start().await;
let correlation_id: CorrelationId = REQUEST_UUID.parse().unwrap();
Mock::given(method("POST"))
.and(path("/R49ZjisteniUzemiSpravcuDti"))
.and(header("SOAPAction", "\"zjistiUzemiSpravcuDti\""))
.respond_with(ResponseTemplate::new(202).set_body_raw(
response_envelope(
SUBMIT_INFRASTRUCTURE_MANAGER_INQUIRY,
correlation_id,
"ProbihaZpracovani",
Some("<msg:IdPozadavku>durable-request-id</msg:IdPozadavku>"),
),
"text/xml; charset=utf-8",
))
.expect(1)
.mount(&server)
.await;
let client = Client::test_client(server.uri());
let error = client
.submit_infrastructure_manager_inquiry_with_correlation_id(correlation_id, &request())
.await
.unwrap_err();
assert!(matches!(
error,
Error::SubmissionUncertainWithRecovery { .. }
));
assert!(error.submission_outcome_uncertain());
let recovery = error.recovery().unwrap();
assert_eq!(recovery.request_id().as_str(), "durable-request-id");
assert_eq!(recovery.response().value.as_str(), "durable-request-id");
assert_eq!(recovery.metadata().state, ProcessingState::InProgress);
assert_eq!(recovery.metadata().request_id, correlation_id);
assert_eq!(
recovery.metadata().response_id,
RESPONSE_UUID.parse().unwrap()
);
assert_eq!(recovery.metadata().messages[0].text, "Synthetic response");
let rendered = format!("{error:?}");
for sensitive in [
"durable-request-id",
REQUEST_UUID,
RESPONSE_UUID,
"Synthetic response",
] {
assert!(!rendered.contains(sensitive));
}
assert!(!format!("{recovery:?}").contains("durable-request-id"));
assert_eq!(server.received_requests().await.unwrap().len(), 1);
server.verify().await;
}
#[tokio::test]
async fn overall_parts_and_report_are_independent_exact_requests() {
let server = MockServer::start().await;
let correlation_id: CorrelationId = REQUEST_UUID.parse().unwrap();
let request_id = RequestId::new("durable-request-id").unwrap();
for operation in [
READ_INFRASTRUCTURE_MANAGER_INQUIRY_RESULT,
READ_INFRASTRUCTURE_MANAGER_INQUIRY_REPORT,
] {
let body = soap::request(operation, correlation_id, |writer| {
inquiries::write_result_request(writer, &request_id)
})
.unwrap();
let data = (operation.response == "CtiVysledekZjistiUzemiSpravcuDtiOdpoved").then_some(
"<r:IdPodani>submission-id</r:IdPodani><r:Parcely/><r:PocetCastiDti>0</r:PocetCastiDti><r:ValidaceDokoncena>true</r:ValidaceDokoncena><r:PocetValidnichCastiDti>0</r:PocetValidnichCastiDti>",
);
expect_exchange(
&server,
operation,
body,
200,
response_envelope(operation, correlation_id, "Zpracovano", data),
)
.await;
}
let page = PageRequest {
offset: 5,
limit: 25,
include_total: false,
};
let body = soap::request(
LIST_INFRASTRUCTURE_MANAGER_INQUIRY_PARTS,
correlation_id,
|writer| inquiries::write_parts_request(writer, &request_id, Some(false), page),
)
.unwrap();
expect_exchange(
&server,
LIST_INFRASTRUCTURE_MANAGER_INQUIRY_PARTS,
body,
200,
response_envelope(
LIST_INFRASTRUCTURE_MANAGER_INQUIRY_PARTS,
correlation_id,
"ProbihaZpracovani",
None,
),
)
.await;
let client = Client::test_client(server.uri());
let overall = client
.read_infrastructure_manager_inquiry_result_at(correlation_id, &request_id)
.await
.unwrap();
assert_eq!(overall.value.unwrap().infrastructure_part_count, 0);
let parts = client
.list_infrastructure_manager_inquiry_parts_at(
correlation_id,
&request_id,
Some(false),
page,
)
.await
.unwrap();
assert_eq!(parts.value.offset, 5);
assert!(parts.value.items.is_empty());
let report = client
.read_infrastructure_manager_inquiry_report_at(correlation_id, &request_id)
.await
.unwrap();
assert!(report.value.is_none());
assert_eq!(server.received_requests().await.unwrap().len(), 3);
server.verify().await;
}
#[tokio::test]
async fn bounded_wait_rejects_fast_polling_before_any_transport() {
let server = MockServer::start().await;
let client = Client::test_client(server.uri());
let options = WaitOptions {
interval: Duration::from_secs(59),
..WaitOptions::default()
};
let error = client
.wait_for_infrastructure_manager_inquiry(
&RequestId::new("durable-request-id").unwrap(),
options,
)
.await
.unwrap_err();
assert!(matches!(error, Error::InvalidInput { .. }));
assert!(server.received_requests().await.unwrap().is_empty());
}
}