diff options
author | Albin Suresh <albin.suresh@softwareag.com> | 2022-05-11 20:38:58 +0530 |
---|---|---|
committer | Albin Suresh <albin.suresh@softwareag.com> | 2022-05-13 19:10:18 +0530 |
commit | bd98cc741db568b27d46dae4e4be3ca35d47b02f (patch) | |
tree | 45f2bb3f8e93476cec1b12f542fab209fa2cc95e /crates/core | |
parent | 42859fa5a847179867f3dce2143cc533655bc2de (diff) |
Issue #1030 c8y_configuration_plugin integration tests
Diffstat (limited to 'crates/core')
-rw-r--r-- | crates/core/c8y_api/src/http_proxy.rs | 72 | ||||
-rw-r--r-- | crates/core/c8y_smartrest/src/smartrest_deserializer.rs | 18 | ||||
-rw-r--r-- | crates/core/tedge_mapper/src/c8y/tests.rs | 1 |
3 files changed, 88 insertions, 3 deletions
diff --git a/crates/core/c8y_api/src/http_proxy.rs b/crates/core/c8y_api/src/http_proxy.rs index 098ce82f..5bd93f3a 100644 --- a/crates/core/c8y_api/src/http_proxy.rs +++ b/crates/core/c8y_api/src/http_proxy.rs @@ -47,7 +47,6 @@ pub trait C8YHttpProxy: Send + Sync { async fn upload_config_file( &mut self, config_path: &Path, - config_content: &str, ) -> Result<String, SMCumulocityMapperError>; } @@ -414,10 +413,12 @@ impl C8YHttpProxy for JwtAuthHttpProxy { async fn upload_config_file( &mut self, config_path: &Path, - config_content: &str, ) -> Result<String, SMCumulocityMapperError> { let token = self.get_jwt_token().await?; + // read the config file contents + let config_content = std::fs::read_to_string(config_path)?; + let config_file_event = self.create_event(config_path.display().to_string(), None, None); let event_response_id = self.send_event_internal(config_file_event).await?; let binary_upload_event_url = self @@ -441,10 +442,13 @@ impl C8YHttpProxy for JwtAuthHttpProxy { #[cfg(test)] mod tests { + use std::io::Write; + use super::*; use anyhow::Result; use mockito::{mock, Matcher}; use serde_json::json; + use tempfile::NamedTempFile; use test_case::test_case; #[test] @@ -569,4 +573,68 @@ mod tests { Ok(()) } + + #[tokio::test] + async fn upload_config_file() -> anyhow::Result<()> { + let device_id = "test-device"; + let event_id = "456"; + + // Mock endpoint to return C8Y internal id + let _get_internal_id_mock = mock("GET", "/identity/externalIds/c8y_Serial/test-device") + .with_status(200) + .with_body( + json!({ "externalId": device_id, "managedObject": { "id": "123" } }).to_string(), + ) + .create(); + + let config_content = "key=value"; + let config_file = create_test_config_file_with_content(config_content)?; + + // Mock endpoint for config upload event creation + let _config_file_event_mock = mock("POST", "/event/events/") + .match_body(Matcher::PartialJson( + json!({ "type": config_file.path(), "text": config_file.path() }), + )) + .with_status(201) + .with_body(json!({ "id": event_id }).to_string()) + .create(); + + let config_binary_url_path = format!("/event/events/{event_id}/binaries"); + + // Mock endpoint for config file binary upload + let _config_binary_upload_mock = mock("POST", config_binary_url_path.as_str()) + .match_body(Matcher::Exact(config_content.to_string())) + .with_status(201) + .with_body("irrelevant response") + .create(); + + // An JwtAuthHttpProxy ... + let mut jwt_token_retriver = Box::new(MockC8yJwtTokenRetriever::new()); + jwt_token_retriver + .expect_get_jwt_token() + .returning(|| Ok(SmartRestJwtResponse::default())); + + let http_client = reqwest::ClientBuilder::new().build().unwrap(); + let mut http_proxy = JwtAuthHttpProxy::new( + jwt_token_retriver, + http_client, + mockito::server_url().as_str(), + device_id, + ); + + // Upload the config file and assert its binary URL + assert_eq!( + http_proxy.upload_config_file(config_file.path()).await?, + mockito::server_url() + config_binary_url_path.as_str() + ); + + Ok(()) + } + + fn create_test_config_file_with_content(content: &str) -> Result<NamedTempFile, anyhow::Error> { + let mut file = NamedTempFile::new()?; + file.write_all(content.as_bytes())?; + + Ok(file) + } } diff --git a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs index 5d28fac0..362c4521 100644 --- a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs +++ b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs @@ -611,6 +611,24 @@ mod tests { } #[test] + fn deserialize_smartrest_config_upload_request() { + let message_id = "526".to_string(); + let device = "deviceId".to_string(); + let config_type = "/test/config/path".to_string(); + + let smartrest_message = format!("{message_id},{device},{config_type}"); + let expected = SmartRestConfigUploadRequest { + message_id, + device, + config_type, + }; + assert_eq!( + SmartRestConfigUploadRequest::from_smartrest(smartrest_message.as_str()).unwrap(), + expected + ); + } + + #[test] fn deserialize_smartrest_config_download_request_operation() { let smartrest = "524,deviceId,https://test.cumulocity.com/inventory/binaries/70208,/etc/tedge/tedge.toml".to_string(); let request = SmartRestConfigDownloadRequest::from_smartrest(&smartrest).unwrap(); diff --git a/crates/core/tedge_mapper/src/c8y/tests.rs b/crates/core/tedge_mapper/src/c8y/tests.rs index 2e8ccc0e..e0366934 100644 --- a/crates/core/tedge_mapper/src/c8y/tests.rs +++ b/crates/core/tedge_mapper/src/c8y/tests.rs @@ -911,7 +911,6 @@ impl C8YHttpProxy for FakeC8YHttpProxy { async fn upload_config_file( &mut self, _config_path: &Path, - _config_content: &str, ) -> Result<String, SMCumulocityMapperError> { Ok("fake/upload/url".into()) } |