Skip to content

Commit

Permalink
feat: add unittests for all serialized responses and primary json / y…
Browse files Browse the repository at this point in the history
…aml structures
  • Loading branch information
Benjamin Sommerfeld committed Oct 18, 2024
1 parent 3445108 commit 5126629
Show file tree
Hide file tree
Showing 22 changed files with 7,186 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/environments.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::client::{AdobeConnector, CloudManagerClient};
use crate::errors::throw_adobe_api_error;
use crate::models::environment::{Environment, EnvironmentsList};
use crate::models::variables::EnvironmentsResponse;
use crate::models::environment::{Environment, EnvironmentsList, EnvironmentsResponse};

use crate::HOST_NAME;
use reqwest::{Error, Method};
use std::process;
Expand Down
29 changes: 29 additions & 0 deletions src/models/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,32 @@ pub struct JwtClaims {
pub struct BearerResponse {
pub access_token: String,
}

#[cfg(test)]
mod tests {

use super::*;
use crate::models::tests::read_json_from_file;

#[test]
fn serialize_domain_list() {
// Read the JSON contents of the file as an instance of `User`.
let vobj: BearerResponse =
read_json_from_file("test/test_auth_bearer_response.json").unwrap();
assert_eq!(vobj.access_token, "das.ist.ein.token");
}
#[test]
fn serialize_jwt_claims() {
let vobj: JwtClaims = read_json_from_file("test/test_auth_jwt_response.json").unwrap();

assert_eq!(
vobj.aud,
"https://ims-na1.adobelogin.com/c/4df5gh....."
);
assert_eq!(vobj.exp, 1550001438);
assert_eq!(vobj.iss, "C74F69D7594880280.....@AdobeOrg");
assert_eq!(vobj.sub, "[email protected]");
assert_eq!(vobj.scope_ent_aem_cloud_api, false);
assert_eq!(vobj.scope_ent_cloudmgr_sdk, true);
}
}
29 changes: 0 additions & 29 deletions src/models/basic.rs

This file was deleted.

16 changes: 16 additions & 0 deletions src/models/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};

use super::variables::{EnvironmentVariable, PipelineVariable};

/// Model for all programs that will be read from the configuration YAML
#[derive(Debug, Deserialize, Serialize)]
pub struct YamlConfig {
Expand Down Expand Up @@ -36,3 +37,18 @@ pub struct PipelinesConfig {
pub id: u32,
pub variables: Vec<PipelineVariable>,
}

#[cfg(test)]
mod tests {
use super::*;
use crate::models::tests::read_yaml_from_file;

#[test]
fn serialize_domain_config() {
let vobj: YamlConfig = read_yaml_from_file("test/test_yaml_config.yml").unwrap();

assert_eq!(vobj.programs.len(), 1);
assert_eq!(vobj.programs.first().unwrap().id, 222222);
assert_eq!(vobj.programs.first().unwrap().pipelines.is_some(), true);
}
}
37 changes: 36 additions & 1 deletion src/models/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,40 @@ pub struct CreateDomainResponse {
pub type_field: String,
pub status: i64,
pub title: String,
pub errors: Option<Vec<super::basic::Error>>,
pub errors: Option<Vec<Error>>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Error {
pub code: String,
pub message: String,
pub field: String,
}

#[cfg(test)]
mod tests {
use super::*;
use std::{fs::File, io::BufReader, path::Path};

fn read_user_from_file<P: AsRef<Path>>(
path: P,
) -> Result<DomainResponse, Box<dyn std::error::Error>> {
// Open the file in read-only mode with buffer.
let file = File::open(path)?;
let reader = BufReader::new(file);

// Read the JSON contents of the file as an instance of `User`.
let u = serde_json::from_reader(reader)?;

// Return the `User`.
Ok(u)
}

#[test]
fn serialize_domain_list() {
// Read the JSON contents of the file as an instance of `User`.
let vobj: DomainResponse = read_user_from_file("test/test_domain_response.json").unwrap();
assert_eq!(vobj.domain_list.list.len(), 20);
}
}
24 changes: 24 additions & 0 deletions src/models/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use serde::{Deserialize, Serialize};
pub struct EnvironmentsList {
environments: Vec<Environment>,
}
/// Struct that holds the response when requesting /api/program/{id}/environments
#[derive(Deserialize, Serialize)]
pub struct EnvironmentsResponse {
#[serde(rename(deserialize = "_embedded", serialize = "_embedded"))]
pub environments_list: EnvironmentsList,
}

/// Model for an environment and its relevant metadata
#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -16,3 +22,21 @@ pub struct Environment {
#[serde(rename(deserialize = "programId", serialize = "programId"))]
program_id: String,
}

#[cfg(test)]
mod tests {

use super::*;
use crate::models::tests::read_json_from_file;

#[test]
fn serialize_domain_config() {
let vobj: EnvironmentsResponse =
read_json_from_file("test/test_environment_response.json").unwrap();

assert_eq!(
vobj.environments_list.environments.first().unwrap().id,
"222222"
);
}
}
15 changes: 15 additions & 0 deletions src/models/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ pub struct Execution {
pipeline_execution_mode: String,
finished_at: Option<String>,
}

#[cfg(test)]
mod tests {

use super::*;
use crate::models::tests::read_json_from_file;

#[test]
fn serialize_domain_config() {
let vobj: ExecutionResponse =
read_json_from_file("test/test_execution_response.json").unwrap();

assert_eq!(vobj.execution_list.list.first().unwrap().id, "66666");
}
}
34 changes: 33 additions & 1 deletion src/models/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use serde::{Deserialize, Serialize};

use strum_macros::{EnumString, IntoStaticStr};

use super::basic::Download;
/// Possible types that a service can have
#[derive(Clone, Deserialize, Serialize, IntoStaticStr, EnumString)]
#[strum(serialize_all = "lowercase")]
Expand Down Expand Up @@ -70,3 +69,36 @@ pub struct LogTailResponse {
pub struct LogTailList {
pub downloads: Vec<Download>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Download {
#[serde(rename = "_links")]
pub links: Links,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Links {
#[serde(rename = "http://ns.adobe.com/adobecloud/rel/logs/tail")]
pub http_ns_adobe_com_adobecloud_rel_logs_tail: Option<HttpNsAdobeComAdobecloudRelLogsTail>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct HttpNsAdobeComAdobecloudRelLogsTail {
pub href: String,
}

#[cfg(test)]
mod tests {
use super::*;
use crate::models::tests::read_json_from_file;

#[test]
fn serialize_bearer_response() {
let vobj: LogsResponse = read_json_from_file("test/test_log_response.json").unwrap();

assert_eq!(vobj.embedded.downloads.len(), 3);
}
}
42 changes: 41 additions & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod auth;
pub mod basic;
pub mod config;
pub mod domain;
pub mod environment;
Expand All @@ -8,3 +7,44 @@ pub mod log;
pub mod pipeline;
pub mod program;
pub mod variables;

#[cfg(test)]
mod tests {
use serde::de::DeserializeOwned;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

#[cfg(test)]
pub fn read_json_from_file<T, P>(path: P) -> Result<T, Box<dyn std::error::Error>>
where
T: DeserializeOwned, // Stellt sicher, dass der Typ T deserialisiert werden kann
P: AsRef<Path>, // Der Pfad wird als Referenz auf einen Pfad übergeben
{
// Öffne die Datei im Lese-Modus mit Puffer.
let file = File::open(path)?;
let reader = BufReader::new(file);

// Lese den JSON-Inhalt der Datei als Instanz des Typs T.
let value = serde_json::from_reader(reader)?;

// Rückgabe des deserialisierten Wertes.
Ok(value)
}
#[cfg(test)]
pub fn read_yaml_from_file<T, P>(path: P) -> Result<T, Box<dyn std::error::Error>>
where
T: DeserializeOwned, // Stellt sicher, dass der Typ T deserialisiert werden kann
P: AsRef<Path>, // Der Pfad wird als Referenz auf einen Pfad übergeben
{
// Öffne die Datei im Lese-Modus mit Puffer.
let file = File::open(path)?;
let reader = BufReader::new(file);

// Lese den JSON-Inhalt der Datei als Instanz des Typs T.
let value = serde_yaml::from_reader(reader)?;

// Rückgabe des deserialisierten Wertes.
Ok(value)
}
}
14 changes: 14 additions & 0 deletions src/models/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ pub struct Pipeline {
}

// -------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
use super::*;
use crate::models::tests::read_json_from_file;

#[test]
fn serialize_bearer_response() {
let vobj: PipelinesResponse =
read_json_from_file("test/test_pipeline_response.json").unwrap();

assert_eq!(vobj.pipelines_list.pipelines.len(), 5);
}
}
15 changes: 15 additions & 0 deletions src/models/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ pub struct Program {
enabled: bool,
status: String,
}

#[cfg(test)]
mod tests {
use crate::models::tests::read_json_from_file;

use super::*;

#[test]
fn serialize_bearer_response() {
let vobj: ProgramsResponse =
read_json_from_file("test/test_programs_response.json").unwrap();

assert_eq!(vobj.programs_list.programs.first().unwrap().id, "22222");
}
}
9 changes: 0 additions & 9 deletions src/models/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use serde::{Deserialize, Serialize};
use std::fmt;
use strum_macros::{EnumString, IntoStaticStr};

use super::environment::EnvironmentsList;

/// Model for common cloud manager variables
/// Possible types that a variable can have
Expand Down Expand Up @@ -102,13 +100,6 @@ impl PipelineVariableServiceType {
}
}

/// Struct that holds the response when requesting /api/program/{id}/environments
#[derive(Deserialize, Serialize)]
pub struct EnvironmentsResponse {
#[serde(rename(deserialize = "_embedded", serialize = "_embedded"))]
pub environments_list: EnvironmentsList,
}

/// Struct to serialize the response of requesting /api/program/{id}/environment/{id}/variables
#[derive(Debug, Deserialize, Serialize)]
pub struct EnvironmentVariablesResponse {
Expand Down
5 changes: 5 additions & 0 deletions test/test_auth_bearer_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"access_token": "das.ist.ein.token",
"token_type": "bearer",
"expires_in": 14
}
9 changes: 9 additions & 0 deletions test/test_auth_jwt_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"exp": 1550001438,
"iss": "C74F69D7594880280.....@AdobeOrg",
"sub": "[email protected]",
"https://ims-na1.adobelogin.com/s/ent_dataservices_sdk": true,
"aud": "https://ims-na1.adobelogin.com/c/4df5gh.....",
"scope_ent_cloudmgr_sdk":true,
"scope_ent_aem_cloud_api": false
}
Loading

0 comments on commit 5126629

Please sign in to comment.