swf_core/models/
resource.rs1use crate::models::authentication::*;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct ExternalResourceDefinition {
7 #[serde(skip_serializing_if = "Option::is_none")]
9 pub name: Option<String>,
10
11 pub endpoint: OneOfEndpointDefinitionOrUri,
13}
14
15#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
17pub struct EndpointDefinition {
18 pub uri: String,
20
21 #[serde(skip_serializing_if = "Option::is_none")]
23 pub authentication: Option<ReferenceableAuthenticationPolicy>,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum OneOfEndpointDefinitionOrUri {
30 Endpoint(Box<EndpointDefinition>),
32 Uri(String),
34}
35impl Default for OneOfEndpointDefinitionOrUri {
36 fn default() -> Self {
37 OneOfEndpointDefinitionOrUri::Uri(String::new())
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_endpoint_uri_serialize() {
48 let endpoint = OneOfEndpointDefinitionOrUri::Uri("http://example.com/{id}".to_string());
49 let json = serde_json::to_string(&endpoint).unwrap();
50 assert_eq!(json, r#""http://example.com/{id}""#);
51 }
52
53 #[test]
54 fn test_endpoint_uri_deserialize() {
55 let json = r#""http://example.com/api""#;
56 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
57 match endpoint {
58 OneOfEndpointDefinitionOrUri::Uri(uri) => {
59 assert_eq!(uri, "http://example.com/api");
60 }
61 _ => panic!("Expected Uri variant"),
62 }
63 }
64
65 #[test]
66 fn test_endpoint_config_deserialize() {
67 let json = r#"{
68 "uri": "http://example.com/{id}",
69 "authentication": {
70 "basic": { "username": "admin", "password": "admin" }
71 }
72 }"#;
73 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
74 match endpoint {
75 OneOfEndpointDefinitionOrUri::Endpoint(ep) => {
76 assert_eq!(ep.uri, "http://example.com/{id}");
77 assert!(ep.authentication.is_some());
78 }
79 _ => panic!("Expected Endpoint variant"),
80 }
81 }
82
83 #[test]
84 fn test_endpoint_config_with_oauth2_reference() {
85 let json = r#"{
86 "uri": "http://example.com/{id}",
87 "authentication": {
88 "oauth2": { "use": "secret" }
89 }
90 }"#;
91 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
92 match endpoint {
93 OneOfEndpointDefinitionOrUri::Endpoint(ep) => {
94 assert_eq!(ep.uri, "http://example.com/{id}");
95 assert!(ep.authentication.is_some());
96 }
97 _ => panic!("Expected Endpoint variant"),
98 }
99 }
100
101 #[test]
102 fn test_endpoint_config_roundtrip() {
103 let json = r#"{
104 "uri": "http://example.com/{id}",
105 "authentication": {
106 "basic": { "username": "admin", "password": "admin" }
107 }
108 }"#;
109 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
110 let serialized = serde_json::to_string(&endpoint).unwrap();
111 let deserialized: OneOfEndpointDefinitionOrUri = serde_json::from_str(&serialized).unwrap();
112 assert_eq!(endpoint, deserialized);
113 }
114
115 #[test]
116 fn test_external_resource_deserialize() {
117 let json = r#"{
118 "name": "myResource",
119 "endpoint": "https://api.example.com/data"
120 }"#;
121 let resource: ExternalResourceDefinition = serde_json::from_str(json).unwrap();
122 assert_eq!(resource.name, Some("myResource".to_string()));
123 match resource.endpoint {
124 OneOfEndpointDefinitionOrUri::Uri(uri) => {
125 assert_eq!(uri, "https://api.example.com/data");
126 }
127 _ => panic!("Expected Uri variant"),
128 }
129 }
130
131 #[test]
132 fn test_runtime_expression_endpoint() {
133 let json = r#""${example}""#;
134 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
135 match endpoint {
136 OneOfEndpointDefinitionOrUri::Uri(expr) => {
137 assert_eq!(expr, "${example}");
138 }
139 _ => panic!("Expected Uri variant for runtime expression"),
140 }
141 }
142
143 #[test]
146 fn test_endpoint_uri_template() {
147 let json = r#""http://example.com/{id}""#;
149 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
150 match endpoint {
151 OneOfEndpointDefinitionOrUri::Uri(uri) => {
152 assert_eq!(uri, "http://example.com/{id}");
153 }
154 _ => panic!("Expected Uri variant"),
155 }
156 }
157
158 #[test]
159 fn test_endpoint_config_with_basic_auth_roundtrip() {
160 let json = r#"{
162 "uri": "http://example.com/{id}",
163 "authentication": {
164 "basic": {"username": "john", "password": "secret"}
165 }
166 }"#;
167 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
168 let serialized = serde_json::to_string(&endpoint).unwrap();
169 let deserialized: OneOfEndpointDefinitionOrUri = serde_json::from_str(&serialized).unwrap();
170 assert_eq!(endpoint, deserialized);
171 }
172
173 #[test]
174 fn test_endpoint_config_with_oauth2_use_roundtrip() {
175 let json = r#"{
177 "uri": "http://example.com/{id}",
178 "authentication": {
179 "oauth2": {"use": "secret"}
180 }
181 }"#;
182 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
183 let serialized = serde_json::to_string(&endpoint).unwrap();
184 let deserialized: OneOfEndpointDefinitionOrUri = serde_json::from_str(&serialized).unwrap();
185 assert_eq!(endpoint, deserialized);
186 }
187
188 #[test]
189 fn test_endpoint_config_with_runtime_expression_uri() {
190 let json = r#"{
192 "uri": "${example}",
193 "authentication": {
194 "oauth2": {"use": "secret"}
195 }
196 }"#;
197 let endpoint: OneOfEndpointDefinitionOrUri = serde_json::from_str(json).unwrap();
198 match endpoint {
199 OneOfEndpointDefinitionOrUri::Endpoint(ep) => {
200 assert_eq!(ep.uri, "${example}");
201 assert!(ep.authentication.is_some());
202 }
203 _ => panic!("Expected Endpoint variant"),
204 }
205 }
206
207 #[test]
208 fn test_endpoint_runtime_expression_roundtrip() {
209 let endpoint = OneOfEndpointDefinitionOrUri::Uri("${example}".to_string());
211 let serialized = serde_json::to_string(&endpoint).unwrap();
212 assert_eq!(serialized, r#""${example}""#);
213 let deserialized: OneOfEndpointDefinitionOrUri = serde_json::from_str(&serialized).unwrap();
214 assert_eq!(endpoint, deserialized);
215 }
216
217 #[test]
218 fn test_external_resource_with_endpoint_config() {
219 let json = r#"{
221 "name": "myResource",
222 "endpoint": {
223 "uri": "http://example.com/api",
224 "authentication": {
225 "basic": {"username": "admin", "password": "admin"}
226 }
227 }
228 }"#;
229 let resource: ExternalResourceDefinition = serde_json::from_str(json).unwrap();
230 assert_eq!(resource.name, Some("myResource".to_string()));
231 match resource.endpoint {
232 OneOfEndpointDefinitionOrUri::Endpoint(ep) => {
233 assert_eq!(ep.uri, "http://example.com/api");
234 assert!(ep.authentication.is_some());
235 }
236 _ => panic!("Expected Endpoint variant"),
237 }
238 }
239
240 #[test]
241 fn test_endpoint_default() {
242 let default = OneOfEndpointDefinitionOrUri::default();
243 match default {
244 OneOfEndpointDefinitionOrUri::Uri(s) => assert!(s.is_empty()),
245 _ => panic!("Expected default Uri variant"),
246 }
247 }
248}