Skip to content
This repository has been archived by the owner on Dec 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #44 from eHealthExperts/fix/replace_failure
Browse files Browse the repository at this point in the history
Replace deprecated failure crate
  • Loading branch information
ChriFo authored Sep 9, 2020
2 parents 7aa0229 + 7bf3fc3 commit 07a7c98
Show file tree
Hide file tree
Showing 14 changed files with 1,109 additions and 709 deletions.
1,292 changes: 710 additions & 582 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ crate-type = ["cdylib"]

[dependencies]
antidote = "1.0.0"
chrono = "0.4.13"
data-encoding = "2.2.1"
failure = "0.1.8"
anyhow = "1.0.32"
chrono = "0.4.15"
data-encoding = "2.3.0"
fern = "0.6.0"
lazy_static = "1.4.0"
log = "0.4.8"
serde = "1.0.114"
serde_derive = "1.0.114"
serde_json = "1.0.56"
ureq = { version = "1.3.0", features = ["json"] }
log = "0.4.11"
once_cell = "1.4.1"
serde = "1.0.115"
serde_derive = "1.0.115"
serde_json = "1.0.57"
ureq = { version = "1.4.0", features = ["json"] }
url = "2.1.1"

[dependencies.config]
Expand All @@ -37,10 +37,10 @@ const-cstr = "0.3.0"
dlopen = "0.1.8"
futures-timer = "3.0.2"
rand = "0.7.3"
serial_test = "0.4.0"
serial_test = "0.5.0"
spectral = "0.6.0"
tempfile = "3.1.0"
test-server = { git = "https://github.com/ChriFo/test-server-rs", tag = "0.8.0" }
test-server = { git = "https://github.com/ChriFo/test-server-rs", tag = "0.9.0" }

[target.'cfg(windows)'.build-dependencies]
winres = "0.1.11"
Expand Down
Binary file modified doc/CT-API-1.1.1.pdf
Binary file not shown.
44 changes: 15 additions & 29 deletions src/ctapi/close.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::ctapi::MAP;
use crate::{http, Status, CONFIG};
use failure::Error;

pub fn close(mut ctn: u16) -> Result<Status, Error> {
pub fn close(mut ctn: u16) -> anyhow::Result<Status> {
if let Some(ctn_from_cfg) = CONFIG.read().ctn {
debug!("Use ctn '{}' from configuration", ctn_from_cfg);
ctn = ctn_from_cfg;
Expand Down Expand Up @@ -43,18 +42,15 @@ pub fn close(mut ctn: u16) -> Result<Status, Error> {
mod tests {

use super::close;
use crate::ctapi::MAP;
use crate::{Settings, Status, CONFIG};
use failure::Error;
use std::collections::HashMap;
use crate::{ctapi::MAP, Status};
use std::env;
use test_server::{self, HttpResponse};

#[test]
#[serial]
fn returns_err_if_no_server() {
env::set_var("K2_BASE_URL", "http://127.0.0.1:65432");
init_config_clear_map();
crate::tests::init_config_clear_map();

let ctn = rand::random::<u16>();
let pn = rand::random::<u16>();
Expand All @@ -75,10 +71,10 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_in_request_path() -> Result<(), Error> {
fn use_ctn_and_pn_in_request_path() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let ctn = rand::random::<u16>();
let pn = rand::random::<u16>();
Expand All @@ -96,14 +92,14 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_from_config() -> Result<(), Error> {
fn use_ctn_and_pn_from_config() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
let ctn = rand::random::<u16>();
env::set_var("K2_CTN", format!("{}", ctn));
let pn = rand::random::<u16>();
env::set_var("K2_PN", format!("{}", pn));
init_config_clear_map();
crate::tests::init_config_clear_map();

let _ = MAP.write().insert(ctn, pn);

Expand All @@ -123,10 +119,10 @@ mod tests {

#[test]
#[serial]
fn returns_err_htsi_if_server_response_is_not_200() -> Result<(), Error> {
fn returns_err_htsi_if_server_response_is_not_200() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let ctn = rand::random::<u16>();
let pn = rand::random::<u16>();
Expand All @@ -142,10 +138,10 @@ mod tests {

#[test]
#[serial]
fn returns_err_htsi_if_server_response_not_contains_status() -> Result<(), Error> {
fn returns_err_htsi_if_server_response_not_contains_status() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("hello world"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let ctn = rand::random::<u16>();
let pn = rand::random::<u16>();
Expand All @@ -161,10 +157,10 @@ mod tests {

#[test]
#[serial]
fn returns_response_status_from_server() -> Result<(), Error> {
fn returns_response_status_from_server() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("-11"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let ctn = rand::random::<u16>();
let pn = rand::random::<u16>();
Expand All @@ -180,10 +176,10 @@ mod tests {

#[test]
#[serial]
fn return_ok_and_close_ctn_if_server_returns_ok() -> Result<(), Error> {
fn return_ok_and_close_ctn_if_server_returns_ok() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("0"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let ctn = rand::random::<u16>();
let pn = rand::random::<u16>();
Expand All @@ -196,14 +192,4 @@ mod tests {

Ok(())
}

fn init_config_clear_map() {
let mut config_guard = CONFIG.write();
*config_guard = Settings::init().unwrap();
drop(config_guard);

let mut map_guard = MAP.write();
*map_guard = HashMap::new();
drop(map_guard);
}
}
70 changes: 39 additions & 31 deletions src/ctapi/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::ctapi::MAP;
use crate::{http, Status, CONFIG};
use data_encoding::{BASE64, HEXLOWER};
use failure::Error;
use std::slice;

#[allow(non_snake_case)]
Expand All @@ -24,7 +23,7 @@ pub fn data(
command: *const u8,
lenr: *mut u16,
response: *mut u8,
) -> Result<Status, Error> {
) -> anyhow::Result<Status> {
if let Some(ctn_from_cfg) = CONFIG.read().ctn {
debug!("Use ctn '{}' from configuration", ctn_from_cfg);
ctn = ctn_from_cfg;
Expand Down Expand Up @@ -103,12 +102,9 @@ pub fn data(
mod tests {

use super::{data, Response};
use crate::ctapi::MAP;
use crate::{Settings, Status, CONFIG};
use crate::{ctapi::MAP, Status};
use data_encoding::BASE64;
use failure::Error;
use serde_json::{self, Value};
use std::collections::HashMap;
use std::env;
use std::slice;
use std::u16::MAX;
Expand Down Expand Up @@ -147,7 +143,7 @@ mod tests {
#[serial]
fn returns_err_if_no_server() {
env::set_var("K2_BASE_URL", "http://127.0.0.1:65432");
init_config_clear_map();
crate::tests::init_config_clear_map();

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
let _ = MAP.write().insert(ctn, pn);
Expand All @@ -159,10 +155,10 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_in_request_path() -> Result<(), Error> {
fn use_ctn_and_pn_in_request_path() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
let _ = MAP.write().insert(ctn, pn);
Expand All @@ -179,10 +175,10 @@ mod tests {

#[test]
#[serial]
fn post_body_contains_parameter() -> Result<(), Error> {
fn post_body_contains_parameter() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let (command, command_ptr, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) =
rand_params();
Expand Down Expand Up @@ -217,13 +213,13 @@ mod tests {

#[test]
#[serial]
fn response_is_mapped_to_parameter() -> Result<(), Error> {
fn response_is_mapped_to_parameter() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || {
HttpResponse::Ok()
.body(r#"{"dad":39,"sad":63,"lenr":2,"response":"kAA=","responseCode":0}"#)
})?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
let _ = MAP.write().insert(ctn, pn);
Expand All @@ -244,10 +240,32 @@ mod tests {

#[test]
#[serial]
fn returns_err_if_server_response_is_not_200() -> Result<(), Error> {
#[should_panic(expected = "Failed to extract response.")]
fn response_with_failure_response_field() {
let server = test_server::new("127.0.0.1:0", || {
HttpResponse::Ok()
.body(r#"{"dad":39,"sad":63,"lenr":2,"response":"0123456789","responseCode":0}"#)
})
.unwrap();
env::set_var("K2_BASE_URL", server.url());
crate::tests::init_config_clear_map();

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
let _ = MAP.write().insert(ctn, pn);

let res = data(ctn, &mut dad, &mut sad, lenc, command, &mut lenr, response);

env::remove_var("K2_BASE_URL");

let _status = res.unwrap();
}

#[test]
#[serial]
fn returns_err_if_server_response_is_not_200() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
let _ = MAP.write().insert(ctn, pn);
Expand All @@ -261,10 +279,10 @@ mod tests {

#[test]
#[serial]
fn returns_err_if_server_response_not_contains_response_struct_as_json() -> Result<(), Error> {
fn returns_err_if_server_response_not_contains_response_struct_as_json() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("hello world"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
let _ = MAP.write().insert(ctn, pn);
Expand All @@ -278,13 +296,13 @@ mod tests {

#[test]
#[serial]
fn returns_response_status_from_valid_json_response_struct() -> Result<(), Error> {
fn returns_response_status_from_valid_json_response_struct() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || {
HttpResponse::Ok()
.body(r#"{"dad":1,"sad":1,"lenr":1,"response":"a=","responseCode":-11}"#)
})?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
crate::tests::init_config_clear_map();

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
let _ = MAP.write().insert(ctn, pn);
Expand All @@ -301,14 +319,14 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_from_config() -> Result<(), Error> {
fn use_ctn_and_pn_from_config() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
env::set_var("K2_CTN", format!("{}", ctn));
env::set_var("K2_PN", format!("{}", pn));
init_config_clear_map();
crate::tests::init_config_clear_map();

let _ = MAP.write().insert(ctn, pn);

Expand Down Expand Up @@ -359,14 +377,4 @@ mod tests {
pn,
)
}

fn init_config_clear_map() {
let mut config_guard = CONFIG.write();
*config_guard = Settings::init().unwrap();
drop(config_guard);

let mut map_guard = MAP.write();
*map_guard = HashMap::new();
drop(map_guard);
}
}
Loading

0 comments on commit 07a7c98

Please sign in to comment.