mockforge_data/
mock_generator.rs

1//! Enhanced Mock Data Generator for OpenAPI Specifications
2//!
3//! This module provides comprehensive mock data generation capabilities that go beyond
4//! the basic schema generator, offering intelligent data generation based on OpenAPI
5//! specifications with type safety and realistic data patterns.
6
7use crate::consistency::ConsistencyStore;
8use crate::domains::Domain;
9use crate::faker::EnhancedFaker;
10use crate::persona::PersonaRegistry;
11use crate::persona_backstory::BackstoryGenerator;
12use crate::persona_templates::PersonaTemplateRegistry;
13use crate::schema::{FieldDefinition, SchemaDefinition};
14use crate::{Error, Result};
15use serde_json::{json, Value};
16use std::collections::HashMap;
17use std::sync::Arc;
18use tracing::{debug, info, warn};
19
20/// Configuration for mock data generation
21#[derive(Debug, Clone)]
22pub struct MockGeneratorConfig {
23    /// Whether to use realistic data patterns
24    pub realistic_mode: bool,
25    /// Default array size for generated arrays
26    pub default_array_size: usize,
27    /// Maximum array size for generated arrays
28    pub max_array_size: usize,
29    /// Whether to include optional fields
30    pub include_optional_fields: bool,
31    /// Custom field mappings for specific field names
32    pub field_mappings: HashMap<String, String>,
33    /// Whether to validate generated data against schemas
34    pub validate_generated_data: bool,
35    /// Whether to generate backstories for personas
36    pub enable_backstories: bool,
37}
38
39impl Default for MockGeneratorConfig {
40    fn default() -> Self {
41        Self {
42            realistic_mode: true,
43            default_array_size: 3,
44            max_array_size: 10,
45            include_optional_fields: true,
46            field_mappings: HashMap::new(),
47            validate_generated_data: true,
48            enable_backstories: false,
49        }
50    }
51}
52
53impl MockGeneratorConfig {
54    /// Create a new configuration with realistic defaults
55    pub fn new() -> Self {
56        Self::default()
57    }
58
59    /// Enable realistic data generation
60    pub fn realistic_mode(mut self, enabled: bool) -> Self {
61        self.realistic_mode = enabled;
62        self
63    }
64
65    /// Set default array size
66    pub fn default_array_size(mut self, size: usize) -> Self {
67        self.default_array_size = size;
68        self
69    }
70
71    /// Set maximum array size
72    pub fn max_array_size(mut self, size: usize) -> Self {
73        self.max_array_size = size;
74        self
75    }
76
77    /// Control whether to include optional fields
78    pub fn include_optional_fields(mut self, include: bool) -> Self {
79        self.include_optional_fields = include;
80        self
81    }
82
83    /// Add a custom field mapping
84    pub fn field_mapping(mut self, field_name: String, faker_type: String) -> Self {
85        self.field_mappings.insert(field_name, faker_type);
86        self
87    }
88
89    /// Enable/disable data validation
90    pub fn validate_generated_data(mut self, validate: bool) -> Self {
91        self.validate_generated_data = validate;
92        self
93    }
94
95    /// Enable/disable backstory generation for personas
96    pub fn enable_backstories(mut self, enable: bool) -> Self {
97        self.enable_backstories = enable;
98        self
99    }
100}
101
102/// Enhanced mock data generator with intelligent schema analysis
103#[derive(Debug)]
104pub struct MockDataGenerator {
105    /// Configuration for the generator
106    config: MockGeneratorConfig,
107    /// Enhanced faker instance
108    faker: EnhancedFaker,
109    /// Schema registry for complex types
110    #[allow(dead_code)]
111    schema_registry: HashMap<String, SchemaDefinition>,
112    /// Field name patterns for intelligent mapping
113    field_patterns: HashMap<String, String>,
114    /// Persona registry for consistent persona-based generation
115    persona_registry: Option<Arc<PersonaRegistry>>,
116    /// Consistency store for entity ID → persona mappings
117    consistency_store: Option<Arc<ConsistencyStore>>,
118    /// Active domain for persona-based generation
119    active_domain: Option<Domain>,
120}
121
122impl MockDataGenerator {
123    /// Create a new mock data generator with default configuration
124    pub fn new() -> Self {
125        Self::with_config(MockGeneratorConfig::new())
126    }
127
128    /// Create a new mock data generator with custom configuration
129    pub fn with_config(config: MockGeneratorConfig) -> Self {
130        let mut generator = Self {
131            config,
132            faker: EnhancedFaker::new(),
133            schema_registry: HashMap::new(),
134            field_patterns: Self::create_field_patterns(),
135            persona_registry: None,
136            consistency_store: None,
137            active_domain: None,
138        };
139
140        // Initialize with common schema patterns
141        generator.initialize_common_schemas();
142        generator
143    }
144
145    /// Create a new mock data generator with persona support
146    pub fn with_persona_support(config: MockGeneratorConfig, domain: Option<Domain>) -> Self {
147        let persona_registry = Arc::new(PersonaRegistry::new());
148        let consistency_store =
149            Arc::new(ConsistencyStore::with_registry_and_domain(persona_registry.clone(), domain));
150
151        let mut generator = Self {
152            config,
153            faker: EnhancedFaker::new(),
154            schema_registry: HashMap::new(),
155            field_patterns: Self::create_field_patterns(),
156            persona_registry: Some(persona_registry),
157            consistency_store: Some(consistency_store),
158            active_domain: domain,
159        };
160
161        // Initialize with common schema patterns
162        generator.initialize_common_schemas();
163        generator
164    }
165
166    /// Set the active domain for persona-based generation
167    pub fn set_active_domain(&mut self, domain: Option<Domain>) {
168        self.active_domain = domain;
169        // Note: ConsistencyStore doesn't have a setter for default domain,
170        // so we just update the active_domain field which is used when generating values
171    }
172
173    /// Get the persona registry
174    pub fn persona_registry(&self) -> Option<&Arc<PersonaRegistry>> {
175        self.persona_registry.as_ref()
176    }
177
178    /// Get the consistency store
179    pub fn consistency_store(&self) -> Option<&Arc<ConsistencyStore>> {
180        self.consistency_store.as_ref()
181    }
182
183    /// Generate mock data from an OpenAPI specification
184    pub fn generate_from_openapi_spec(&mut self, spec: &Value) -> Result<MockDataResult> {
185        info!("Generating mock data from OpenAPI specification");
186
187        // Parse the OpenAPI spec
188        let openapi_spec = self.parse_openapi_spec(spec)?;
189
190        // Extract all schemas from the spec
191        let schemas = self.extract_schemas_from_spec(spec)?;
192
193        // Generate mock data for each schema
194        let mut generated_data = HashMap::new();
195        let mut warnings = Vec::new();
196
197        for (schema_name, schema_def) in schemas {
198            debug!("Generating data for schema: {}", schema_name);
199
200            match self.generate_schema_data(&schema_def) {
201                Ok(data) => {
202                    generated_data.insert(schema_name, data);
203                }
204                Err(e) => {
205                    let warning =
206                        format!("Failed to generate data for schema '{}': {}", schema_name, e);
207                    warn!("{}", warning);
208                    warnings.push(warning);
209                }
210            }
211        }
212
213        // Generate mock responses for each endpoint
214        // Parse paths directly from the JSON spec since parse_openapi_spec doesn't parse them
215        let mut mock_responses = HashMap::new();
216        if let Some(paths) = spec.get("paths") {
217            if let Some(paths_obj) = paths.as_object() {
218                for (path, path_item) in paths_obj {
219                    if let Some(path_obj) = path_item.as_object() {
220                        for (method, operation) in path_obj {
221                            if let Some(op_obj) = operation.as_object() {
222                                let endpoint_key = format!("{} {}", method.to_uppercase(), path);
223
224                                // Extract response schema from the operation
225                                if let Some(responses) = op_obj.get("responses") {
226                                    if let Some(resp_obj) = responses.as_object() {
227                                        // Look for 200, 201, or any 2xx response
228                                        let mut response_schema = None;
229
230                                        // Try 200 first
231                                        if let Some(response) = resp_obj.get("200") {
232                                            response_schema = self
233                                                .extract_response_schema_from_json(response)
234                                                .ok()
235                                                .flatten();
236                                        }
237
238                                        // Try 201 if 200 not found
239                                        if response_schema.is_none() {
240                                            if let Some(response) = resp_obj.get("201") {
241                                                response_schema = self
242                                                    .extract_response_schema_from_json(response)
243                                                    .ok()
244                                                    .flatten();
245                                            }
246                                        }
247
248                                        // Try any 2xx if still not found
249                                        if response_schema.is_none() {
250                                            for (status_code, response) in resp_obj {
251                                                if let Ok(code) = status_code.parse::<u16>() {
252                                                    if code >= 200 && code < 300 {
253                                                        if let Some(schema) = self
254                                                            .extract_response_schema_from_json(
255                                                                response,
256                                                            )
257                                                            .ok()
258                                                            .flatten()
259                                                        {
260                                                            response_schema = Some(schema);
261                                                            break;
262                                                        }
263                                                    }
264                                                }
265                                            }
266                                        }
267
268                                        // Generate mock response if we found a schema
269                                        if let Some(schema) = response_schema {
270                                            // Resolve $ref if present
271                                            let resolved_schema = if let Some(ref_path) =
272                                                schema.get("$ref").and_then(|r| r.as_str())
273                                            {
274                                                self.resolve_schema_ref(spec, ref_path)?
275                                            } else {
276                                                Some(schema)
277                                            };
278
279                                            if let Some(resolved) = resolved_schema {
280                                                if let Ok(mock_data) =
281                                                    self.generate_from_json_schema(&resolved)
282                                                {
283                                                    mock_responses.insert(
284                                                        endpoint_key,
285                                                        MockResponse {
286                                                            status: 200,
287                                                            headers: HashMap::new(),
288                                                            body: mock_data,
289                                                        },
290                                                    );
291                                                }
292                                            }
293                                        }
294                                    }
295                                }
296                            }
297                        }
298                    }
299                }
300            }
301        }
302
303        Ok(MockDataResult {
304            schemas: generated_data,
305            responses: mock_responses,
306            warnings,
307            spec_info: openapi_spec.info,
308        })
309    }
310
311    /// Generate mock data from a JSON Schema
312    pub fn generate_from_json_schema(&mut self, schema: &Value) -> Result<Value> {
313        debug!("Generating mock data from JSON Schema");
314
315        // Convert JSON Schema to our internal schema format
316        let schema_def = SchemaDefinition::from_json_schema(schema)?;
317
318        // Generate data using our enhanced generator
319        self.generate_schema_data(&schema_def)
320    }
321
322    /// Generate mock data for a specific schema definition
323    fn generate_schema_data(&mut self, schema: &SchemaDefinition) -> Result<Value> {
324        let mut object = serde_json::Map::new();
325
326        for field in &schema.fields {
327            // Skip optional fields if configured to do so
328            if !field.required && !self.config.include_optional_fields {
329                continue;
330            }
331
332            // Determine the best faker type for this field
333            let faker_type = self.determine_faker_type(field);
334
335            // Generate the value
336            let value = self.generate_field_value(field, &faker_type)?;
337
338            // Validate the generated value if configured
339            if self.config.validate_generated_data {
340                field.validate_value(&value)?;
341            }
342
343            object.insert(field.name.clone(), value);
344        }
345
346        Ok(Value::Object(object))
347    }
348
349    /// Generate mock response for an OpenAPI operation
350    fn generate_endpoint_response(
351        &mut self,
352        operation: &openapiv3::Operation,
353    ) -> Result<Option<MockResponse>> {
354        // Find the best response to mock (prefer 200, then 201, then any 2xx)
355        let response_schema = self.find_best_response_schema(operation)?;
356
357        if let Some(schema) = response_schema {
358            let mock_data = self.generate_from_json_schema(&schema)?;
359
360            Ok(Some(MockResponse {
361                status: 200, // Default to 200 for successful responses
362                headers: HashMap::new(),
363                body: mock_data,
364            }))
365        } else {
366            Ok(None)
367        }
368    }
369
370    /// Find the best response schema from an operation
371    fn find_best_response_schema(&self, operation: &openapiv3::Operation) -> Result<Option<Value>> {
372        let responses = &operation.responses;
373
374        // Look for 200 response first
375        if let Some(response) = responses.responses.get(&openapiv3::StatusCode::Code(200)) {
376            if let Some(schema) = self.extract_response_schema(response)? {
377                return Ok(Some(schema));
378            }
379        }
380
381        // Look for 201 response
382        if let Some(response) = responses.responses.get(&openapiv3::StatusCode::Code(201)) {
383            if let Some(schema) = self.extract_response_schema(response)? {
384                return Ok(Some(schema));
385            }
386        }
387
388        // Look for any 2xx response
389        for (code, response) in &responses.responses {
390            if let openapiv3::StatusCode::Code(status_code) = code {
391                if *status_code >= 200 && *status_code < 300 {
392                    if let Some(schema) = self.extract_response_schema(response)? {
393                        return Ok(Some(schema));
394                    }
395                }
396            }
397        }
398
399        Ok(None)
400    }
401
402    /// Extract schema from an OpenAPI response (JSON format)
403    fn extract_response_schema_from_json(&self, response: &Value) -> Result<Option<Value>> {
404        // Check for content -> application/json -> schema
405        if let Some(content) = response.get("content") {
406            if let Some(json_content) = content.get("application/json") {
407                if let Some(schema) = json_content.get("schema") {
408                    // Handle $ref references
409                    if let Some(ref_path) = schema.get("$ref").and_then(|r| r.as_str()) {
410                        // Extract schema name from $ref (e.g., "#/components/schemas/User" -> "User")
411                        if let Some(schema_name) = ref_path.split('/').last() {
412                            // We'll need to resolve this from components, but for now return the ref
413                            // The caller should handle resolving from components
414                            return Ok(Some(json!({
415                                "$ref": ref_path,
416                                "schema_name": schema_name
417                            })));
418                        }
419                    }
420                    return Ok(Some(schema.clone()));
421                }
422            }
423        }
424        Ok(None)
425    }
426
427    /// Resolve a $ref reference to an actual schema
428    fn resolve_schema_ref(&self, spec: &Value, ref_path: &str) -> Result<Option<Value>> {
429        // Handle #/components/schemas/Name format
430        if ref_path.starts_with("#/components/schemas/") {
431            let schema_name = ref_path.strip_prefix("#/components/schemas/").unwrap();
432            if let Some(components) = spec.get("components") {
433                if let Some(schemas) = components.get("schemas") {
434                    if let Some(schema) = schemas.get(schema_name) {
435                        return Ok(Some(schema.clone()));
436                    }
437                }
438            }
439        }
440        Ok(None)
441    }
442
443    /// Extract schema from an OpenAPI response
444    fn extract_response_schema(
445        &self,
446        response: &openapiv3::ReferenceOr<openapiv3::Response>,
447    ) -> Result<Option<Value>> {
448        match response {
449            openapiv3::ReferenceOr::Item(response) => {
450                let content = &response.content;
451                // Prefer application/json content
452                if let Some(json_content) = content.get("application/json") {
453                    if let Some(schema) = &json_content.schema {
454                        return Ok(Some(serde_json::to_value(schema)?));
455                    }
456                }
457
458                // Fall back to any content type
459                for (_, media_type) in content {
460                    if let Some(schema) = &media_type.schema {
461                        return Ok(Some(serde_json::to_value(schema)?));
462                    }
463                }
464
465                Ok(None)
466            }
467            openapiv3::ReferenceOr::Reference { .. } => {
468                // Handle reference responses (could be expanded)
469                Ok(None)
470            }
471        }
472    }
473
474    /// Determine the best faker type for a field based on its name and type
475    fn determine_faker_type(&self, field: &FieldDefinition) -> String {
476        let field_name = field.name.to_lowercase();
477
478        // Check custom field mappings first
479        if let Some(mapped_type) = self.config.field_mappings.get(&field_name) {
480            return mapped_type.clone();
481        }
482
483        // Use field name patterns for intelligent mapping
484        // Find the longest matching pattern to prioritize more specific matches
485        // Also prioritize certain patterns (like "email" over "address")
486        let mut best_match: Option<(&String, &String)> = None;
487        let priority_patterns = ["email", "mail"]; // Patterns that should take precedence
488
489        for (pattern, faker_type) in &self.field_patterns {
490            if field_name.contains(pattern) {
491                // Check if this is a priority pattern
492                let is_priority = priority_patterns.contains(&pattern.as_str());
493
494                if let Some((best_pattern, best_faker_type)) = best_match {
495                    let best_is_priority = priority_patterns.contains(&best_pattern.as_str());
496
497                    // Priority patterns always win, or longer patterns win
498                    if is_priority && !best_is_priority {
499                        best_match = Some((pattern, faker_type));
500                    } else if !is_priority && best_is_priority {
501                        // Keep the priority match
502                    } else if pattern.len() > best_pattern.len() {
503                        best_match = Some((pattern, faker_type));
504                    }
505                } else {
506                    best_match = Some((pattern, faker_type));
507                }
508            }
509        }
510
511        if let Some((_, faker_type)) = best_match {
512            return faker_type.clone();
513        }
514
515        // Fall back to field type
516        field.field_type.clone()
517    }
518
519    /// Generate a value for a specific field
520    fn generate_field_value(&mut self, field: &FieldDefinition, faker_type: &str) -> Result<Value> {
521        // Note: Automatic persona-based generation from field names would require
522        // entity ID values from request context (path params, query params, body).
523        // For now, use explicit generate_with_persona() for persona-based generation.
524        // Automatic detection can be enhanced in the future when request context is available.
525
526        // Use faker template if provided
527        if let Some(template) = &field.faker_template {
528            return Ok(self.faker.generate_by_type(template));
529        }
530
531        // Handle array generation specially
532        if field.field_type == "array" {
533            return self.generate_array_value(field);
534        }
535
536        // Handle nested object generation specially
537        if field.field_type == "object" {
538            if field.constraints.contains_key("properties") {
539                return self.generate_object_value(field);
540            }
541        }
542
543        // Generate based on determined faker type
544        let value = self.faker.generate_by_type(faker_type);
545
546        // Apply constraints if present
547        self.apply_constraints(&value, field)
548    }
549
550    /// Generate an array value for a field
551    fn generate_array_value(&mut self, field: &FieldDefinition) -> Result<Value> {
552        // Determine array size from constraints or use defaults
553        let min_items =
554            field.constraints.get("minItems").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
555        let max_items = field
556            .constraints
557            .get("maxItems")
558            .and_then(|v| v.as_u64())
559            .unwrap_or(self.config.max_array_size as u64) as usize;
560
561        // Use default array size if no constraints
562        let array_size = if min_items > 0 || max_items < self.config.max_array_size {
563            // Use a size within the constraints
564            let size = if min_items > 0 {
565                min_items.max(self.config.default_array_size)
566            } else {
567                self.config.default_array_size
568            };
569            size.min(max_items.max(min_items))
570        } else {
571            self.config.default_array_size
572        };
573
574        // Generate array of items
575        let mut array = Vec::new();
576
577        // Check if we have a full items schema (for objects, nested arrays, etc.)
578        if let Some(items_schema) = field.constraints.get("itemsSchema") {
579            // Generate items from the schema recursively
580            let items_schema_def = SchemaDefinition::from_json_schema(items_schema)?;
581            for _ in 0..array_size {
582                let item = self.generate_schema_data(&items_schema_def)?;
583                array.push(item);
584            }
585        } else {
586            // Simple type - use faker
587            let items_type =
588                field.constraints.get("itemsType").and_then(|v| v.as_str()).unwrap_or("string");
589
590            for _ in 0..array_size {
591                let item = self.faker.generate_by_type(items_type);
592                array.push(item);
593            }
594        }
595
596        Ok(Value::Array(array))
597    }
598
599    /// Generate an object value for a field with nested properties
600    fn generate_object_value(&mut self, field: &FieldDefinition) -> Result<Value> {
601        // Get nested properties from constraints
602        let properties = field
603            .constraints
604            .get("properties")
605            .ok_or_else(|| Error::generic("Object field missing properties constraint"))?;
606
607        // Get required fields if present
608        let required_fields: Vec<String> = field
609            .constraints
610            .get("required")
611            .and_then(|v| v.as_array())
612            .map(|arr| arr.iter().filter_map(|v| v.as_str().map(|s| s.to_string())).collect())
613            .unwrap_or_default();
614
615        // Create a nested schema from the properties
616        let nested_schema = SchemaDefinition::from_json_schema(&json!({
617            "type": "object",
618            "properties": properties,
619            "required": required_fields
620        }))?;
621
622        // Generate the nested object recursively
623        self.generate_schema_data(&nested_schema)
624    }
625
626    /// Generate data with explicit persona support
627    ///
628    /// Generates data for a schema using a specific entity ID and domain.
629    /// This ensures the same entity ID always generates the same data pattern.
630    /// If backstories are enabled, automatically generates backstories for personas.
631    pub fn generate_with_persona(
632        &mut self,
633        entity_id: &str,
634        domain: Domain,
635        schema: &SchemaDefinition,
636    ) -> Result<Value> {
637        // Ensure consistency store is available
638        let store = self.consistency_store.as_ref().ok_or_else(|| {
639            Error::generic("Persona support not enabled. Use with_persona_support() to create generator with persona support.")
640        })?;
641
642        // Generate backstory if enabled
643        if self.config.enable_backstories {
644            self.ensure_persona_backstory(store, entity_id, domain)?;
645        }
646
647        let mut object = serde_json::Map::new();
648
649        for field in &schema.fields {
650            // Skip optional fields if configured to do so
651            if !field.required && !self.config.include_optional_fields {
652                continue;
653            }
654
655            // Determine the best faker type for this field
656            let faker_type = self.determine_faker_type(field);
657
658            // Generate value using persona-based generation
659            let value = match store.generate_consistent_value(entity_id, &faker_type, Some(domain))
660            {
661                Ok(v) => v,
662                Err(_) => {
663                    // Fallback to regular generation
664                    self.faker.generate_by_type(&faker_type)
665                }
666            };
667
668            // Validate the generated value if configured
669            if self.config.validate_generated_data {
670                field.validate_value(&value)?;
671            }
672
673            object.insert(field.name.clone(), value);
674        }
675
676        Ok(Value::Object(object))
677    }
678
679    /// Ensure a persona has a backstory, generating one if needed
680    ///
681    /// Checks if the persona has a backstory, and if not, generates one
682    /// using the PersonaTemplateRegistry and BackstoryGenerator.
683    fn ensure_persona_backstory(
684        &self,
685        store: &ConsistencyStore,
686        entity_id: &str,
687        domain: Domain,
688    ) -> Result<()> {
689        let persona_registry = store.persona_registry();
690        let persona = store.get_entity_persona(entity_id, Some(domain));
691
692        // If persona already has a backstory, no need to generate
693        if persona.has_backstory() {
694            return Ok(());
695        }
696
697        // Generate traits using template if persona doesn't have traits
698        let mut persona_mut = persona.clone();
699        if persona_mut.traits.is_empty() {
700            let template_registry = PersonaTemplateRegistry::new();
701            template_registry.apply_template_to_persona(&mut persona_mut)?;
702        }
703
704        // Generate backstory using BackstoryGenerator
705        let backstory_generator = BackstoryGenerator::new();
706        match backstory_generator.generate_backstory(&persona_mut) {
707            Ok(backstory) => {
708                // Update persona in registry with traits and backstory
709                let mut traits = HashMap::new();
710                for (key, value) in &persona_mut.traits {
711                    traits.insert(key.clone(), value.clone());
712                }
713
714                // Update traits first
715                if !traits.is_empty() {
716                    persona_registry.update_persona(entity_id, traits)?;
717                }
718
719                // Update backstory
720                persona_registry.update_persona_backstory(entity_id, backstory)?;
721            }
722            Err(e) => {
723                warn!("Failed to generate backstory for persona {}: {}", entity_id, e);
724            }
725        }
726
727        Ok(())
728    }
729
730    /// Apply constraints to a generated value
731    fn apply_constraints(&mut self, value: &Value, field: &FieldDefinition) -> Result<Value> {
732        let mut constrained_value = value.clone();
733
734        // Apply numeric constraints
735        if let Value::Number(num) = value {
736            // Check if field type is integer to preserve integer type when applying constraints
737            let is_integer_field = field.field_type == "int" || field.field_type == "integer";
738
739            if let Some(minimum) = field.constraints.get("minimum") {
740                if let Some(min_val) = minimum.as_f64() {
741                    if num.as_f64().unwrap_or(0.0) < min_val {
742                        // Preserve integer type if field is integer
743                        if is_integer_field {
744                            constrained_value = json!(min_val as i64);
745                        } else {
746                            constrained_value = json!(min_val);
747                        }
748                    }
749                }
750            }
751
752            if let Some(maximum) = field.constraints.get("maximum") {
753                if let Some(max_val) = maximum.as_f64() {
754                    if num.as_f64().unwrap_or(0.0) > max_val {
755                        // Preserve integer type if field is integer
756                        if is_integer_field {
757                            constrained_value = json!(max_val as i64);
758                        } else {
759                            constrained_value = json!(max_val);
760                        }
761                    }
762                }
763            }
764        }
765
766        // Apply string constraints
767        if let Value::String(s) = value {
768            let mut constrained_string = s.clone();
769
770            // Apply min/max length constraints
771            if let Some(min_length) = field.constraints.get("minLength") {
772                if let Some(min_len) = min_length.as_u64() {
773                    if constrained_string.len() < min_len as usize {
774                        // Pad with random characters
775                        let padding_needed = min_len as usize - constrained_string.len();
776                        let padding = self.faker.string(padding_needed);
777                        constrained_string = format!("{}{}", constrained_string, padding);
778                    }
779                }
780            }
781
782            if let Some(max_length) = field.constraints.get("maxLength") {
783                if let Some(max_len) = max_length.as_u64() {
784                    if constrained_string.len() > max_len as usize {
785                        constrained_string.truncate(max_len as usize);
786                    }
787                }
788            }
789
790            constrained_value = json!(constrained_string);
791        }
792
793        // Apply enum constraints
794        if let Some(enum_values) = field.constraints.get("enum") {
795            if let Some(enum_array) = enum_values.as_array() {
796                if !enum_array.is_empty() {
797                    if let Some(random_value) = self.faker.random_element(enum_array) {
798                        constrained_value = random_value.clone();
799                    }
800                }
801            }
802        }
803
804        Ok(constrained_value)
805    }
806
807    /// Parse OpenAPI specification
808    fn parse_openapi_spec(&self, spec: &Value) -> Result<OpenApiSpec> {
809        // This is a simplified parser - in a real implementation,
810        // you'd want to use a proper OpenAPI parser
811        let spec_obj = spec
812            .as_object()
813            .ok_or_else(|| Error::generic("Invalid OpenAPI specification"))?;
814
815        let info = spec_obj
816            .get("info")
817            .ok_or_else(|| Error::generic("Missing 'info' section in OpenAPI spec"))?;
818
819        let title = info.get("title").and_then(|t| t.as_str()).unwrap_or("Unknown API").to_string();
820
821        let version = info.get("version").and_then(|v| v.as_str()).unwrap_or("1.0.0").to_string();
822
823        let description = info.get("description").and_then(|d| d.as_str()).map(|s| s.to_string());
824
825        Ok(OpenApiSpec {
826            info: OpenApiInfo {
827                title,
828                version,
829                description,
830            },
831            paths: HashMap::new(), // Simplified for this example
832        })
833    }
834
835    /// Extract schemas from OpenAPI specification
836    fn extract_schemas_from_spec(
837        &mut self,
838        spec: &Value,
839    ) -> Result<HashMap<String, SchemaDefinition>> {
840        let mut schemas = HashMap::new();
841
842        // Extract component schemas
843        if let Some(components) = spec.get("components") {
844            if let Some(schemas_section) = components.get("schemas") {
845                if let Some(schema_obj) = schemas_section.as_object() {
846                    for (name, schema_def) in schema_obj {
847                        let schema = SchemaDefinition::from_json_schema(schema_def)?;
848                        schemas.insert(name.clone(), schema);
849                    }
850                }
851            }
852        }
853
854        // Extract schemas from paths
855        if let Some(paths) = spec.get("paths") {
856            if let Some(paths_obj) = paths.as_object() {
857                for (path, path_item) in paths_obj {
858                    if let Some(path_obj) = path_item.as_object() {
859                        for (method, operation) in path_obj {
860                            if let Some(op_obj) = operation.as_object() {
861                                // Extract request body schemas
862                                if let Some(request_body) = op_obj.get("requestBody") {
863                                    if let Some(content) = request_body.get("content") {
864                                        if let Some(json_content) = content.get("application/json")
865                                        {
866                                            if let Some(schema) = json_content.get("schema") {
867                                                let schema_name = format!(
868                                                    "{}_{}_request",
869                                                    path.replace("/", "_").trim_start_matches("_"),
870                                                    method
871                                                );
872                                                let schema_def =
873                                                    SchemaDefinition::from_json_schema(schema)?;
874                                                schemas.insert(schema_name, schema_def);
875                                            }
876                                        }
877                                    }
878                                }
879
880                                // Extract response schemas
881                                if let Some(responses) = op_obj.get("responses") {
882                                    if let Some(resp_obj) = responses.as_object() {
883                                        for (status_code, response) in resp_obj {
884                                            if let Some(content) = response.get("content") {
885                                                if let Some(json_content) =
886                                                    content.get("application/json")
887                                                {
888                                                    if let Some(schema) = json_content.get("schema")
889                                                    {
890                                                        let schema_name = format!(
891                                                            "{}_{}_response_{}",
892                                                            path.replace("/", "_")
893                                                                .trim_start_matches("_"),
894                                                            method,
895                                                            status_code
896                                                        );
897                                                        let schema_def =
898                                                            SchemaDefinition::from_json_schema(
899                                                                schema,
900                                                            )?;
901                                                        schemas.insert(schema_name, schema_def);
902                                                    }
903                                                }
904                                            }
905                                        }
906                                    }
907                                }
908                            }
909                        }
910                    }
911                }
912            }
913        }
914
915        Ok(schemas)
916    }
917
918    /// Create field name patterns for intelligent mapping
919    fn create_field_patterns() -> HashMap<String, String> {
920        let mut patterns = HashMap::new();
921
922        // Email patterns
923        patterns.insert("email".to_string(), "email".to_string());
924        patterns.insert("mail".to_string(), "email".to_string());
925
926        // Name patterns
927        patterns.insert("name".to_string(), "name".to_string());
928        patterns.insert("firstname".to_string(), "name".to_string());
929        patterns.insert("lastname".to_string(), "name".to_string());
930        patterns.insert("username".to_string(), "name".to_string());
931
932        // Phone patterns
933        patterns.insert("phone".to_string(), "phone".to_string());
934        patterns.insert("mobile".to_string(), "phone".to_string());
935        patterns.insert("telephone".to_string(), "phone".to_string());
936
937        // Address patterns
938        patterns.insert("address".to_string(), "address".to_string());
939        patterns.insert("street".to_string(), "address".to_string());
940        patterns.insert("city".to_string(), "string".to_string());
941        patterns.insert("state".to_string(), "string".to_string());
942        patterns.insert("zip".to_string(), "string".to_string());
943        patterns.insert("postal".to_string(), "string".to_string());
944
945        // Company patterns
946        patterns.insert("company".to_string(), "company".to_string());
947        patterns.insert("organization".to_string(), "company".to_string());
948        patterns.insert("corp".to_string(), "company".to_string());
949
950        // URL patterns
951        patterns.insert("url".to_string(), "url".to_string());
952        patterns.insert("website".to_string(), "url".to_string());
953        patterns.insert("link".to_string(), "url".to_string());
954
955        // Date patterns
956        patterns.insert("date".to_string(), "date".to_string());
957        patterns.insert("created".to_string(), "date".to_string());
958        patterns.insert("updated".to_string(), "date".to_string());
959        patterns.insert("timestamp".to_string(), "date".to_string());
960
961        // ID patterns
962        patterns.insert("id".to_string(), "uuid".to_string());
963        patterns.insert("uuid".to_string(), "uuid".to_string());
964        patterns.insert("guid".to_string(), "uuid".to_string());
965
966        // IP patterns
967        patterns.insert("ip".to_string(), "ip".to_string());
968        patterns.insert("ipv4".to_string(), "ip".to_string());
969        patterns.insert("ipv6".to_string(), "ip".to_string());
970
971        patterns
972    }
973
974    /// Initialize common schemas
975    fn initialize_common_schemas(&mut self) {
976        // Add common schema patterns here
977        // This could include User, Product, Order, etc.
978    }
979}
980
981impl Default for MockDataGenerator {
982    fn default() -> Self {
983        Self::new()
984    }
985}
986
987/// Result of mock data generation
988#[derive(Debug, Clone, serde::Serialize)]
989pub struct MockDataResult {
990    /// Generated data for each schema
991    pub schemas: HashMap<String, Value>,
992    /// Generated mock responses for each endpoint
993    pub responses: HashMap<String, MockResponse>,
994    /// Warnings encountered during generation
995    pub warnings: Vec<String>,
996    /// OpenAPI specification info
997    pub spec_info: OpenApiInfo,
998}
999
1000/// Mock response data
1001#[derive(Debug, Clone, serde::Serialize)]
1002pub struct MockResponse {
1003    /// HTTP status code
1004    pub status: u16,
1005    /// Response headers
1006    pub headers: HashMap<String, String>,
1007    /// Response body
1008    pub body: Value,
1009}
1010
1011/// OpenAPI specification info
1012#[derive(Debug, Clone, serde::Serialize)]
1013pub struct OpenApiInfo {
1014    /// API title
1015    pub title: String,
1016    /// API version
1017    pub version: String,
1018    /// API description
1019    pub description: Option<String>,
1020}
1021
1022/// Simplified OpenAPI spec structure
1023#[derive(Debug)]
1024struct OpenApiSpec {
1025    info: OpenApiInfo,
1026    paths: HashMap<String, PathItem>,
1027}
1028
1029/// Simplified path item structure
1030#[derive(Debug)]
1031struct PathItem {
1032    operations: HashMap<String, openapiv3::Operation>,
1033}
1034
1035impl PathItem {
1036    fn operations(&self) -> &HashMap<String, openapiv3::Operation> {
1037        &self.operations
1038    }
1039}
1040
1041#[cfg(test)]
1042mod tests {
1043    use super::*;
1044
1045    #[test]
1046    fn test_mock_generator_config_default() {
1047        let config = MockGeneratorConfig::default();
1048
1049        assert!(config.realistic_mode);
1050        assert_eq!(config.default_array_size, 3);
1051        assert_eq!(config.max_array_size, 10);
1052        assert!(config.include_optional_fields);
1053        assert!(config.validate_generated_data);
1054    }
1055
1056    #[test]
1057    fn test_mock_generator_config_custom() {
1058        let config = MockGeneratorConfig::new()
1059            .realistic_mode(false)
1060            .default_array_size(5)
1061            .max_array_size(20)
1062            .include_optional_fields(false)
1063            .field_mapping("email".to_string(), "email".to_string())
1064            .validate_generated_data(false);
1065
1066        assert!(!config.realistic_mode);
1067        assert_eq!(config.default_array_size, 5);
1068        assert_eq!(config.max_array_size, 20);
1069        assert!(!config.include_optional_fields);
1070        assert!(!config.validate_generated_data);
1071        assert!(config.field_mappings.contains_key("email"));
1072    }
1073
1074    #[test]
1075    fn test_mock_data_generator_new() {
1076        let generator = MockDataGenerator::new();
1077
1078        assert!(generator.config.realistic_mode);
1079        assert!(!generator.field_patterns.is_empty());
1080    }
1081
1082    #[test]
1083    fn test_mock_data_generator_with_config() {
1084        let config = MockGeneratorConfig::new().realistic_mode(false).default_array_size(10);
1085
1086        let generator = MockDataGenerator::with_config(config);
1087
1088        assert!(!generator.config.realistic_mode);
1089        assert_eq!(generator.config.default_array_size, 10);
1090    }
1091
1092    #[test]
1093    fn test_determine_faker_type_custom_mapping() {
1094        let mut config = MockGeneratorConfig::new();
1095        config.field_mappings.insert("user_email".to_string(), "email".to_string());
1096
1097        let generator = MockDataGenerator::with_config(config);
1098
1099        let field = FieldDefinition::new("user_email".to_string(), "string".to_string());
1100        let faker_type = generator.determine_faker_type(&field);
1101
1102        assert_eq!(faker_type, "email");
1103    }
1104
1105    #[test]
1106    fn test_determine_faker_type_pattern_matching() {
1107        let generator = MockDataGenerator::new();
1108
1109        let field = FieldDefinition::new("email_address".to_string(), "string".to_string());
1110        let faker_type = generator.determine_faker_type(&field);
1111
1112        assert_eq!(faker_type, "email");
1113    }
1114
1115    #[test]
1116    fn test_determine_faker_type_fallback() {
1117        let generator = MockDataGenerator::new();
1118
1119        let field = FieldDefinition::new("unknown_field".to_string(), "integer".to_string());
1120        let faker_type = generator.determine_faker_type(&field);
1121
1122        assert_eq!(faker_type, "integer");
1123    }
1124
1125    #[test]
1126    fn test_field_patterns_creation() {
1127        let patterns = MockDataGenerator::create_field_patterns();
1128
1129        assert!(patterns.contains_key("email"));
1130        assert!(patterns.contains_key("name"));
1131        assert!(patterns.contains_key("phone"));
1132        assert!(patterns.contains_key("address"));
1133        assert!(patterns.contains_key("company"));
1134        assert!(patterns.contains_key("url"));
1135        assert!(patterns.contains_key("date"));
1136        assert!(patterns.contains_key("id"));
1137        assert!(patterns.contains_key("ip"));
1138    }
1139
1140    #[test]
1141    fn test_generate_from_json_schema_simple() {
1142        let mut generator = MockDataGenerator::new();
1143
1144        let schema = json!({
1145            "type": "object",
1146            "properties": {
1147                "name": { "type": "string" },
1148                "age": { "type": "integer" },
1149                "email": { "type": "string" }
1150            },
1151            "required": ["name", "age"]
1152        });
1153
1154        let result = generator.generate_from_json_schema(&schema).unwrap();
1155
1156        assert!(result.is_object());
1157        let obj = result.as_object().unwrap();
1158        assert!(obj.contains_key("name"));
1159        assert!(obj.contains_key("age"));
1160        assert!(obj.contains_key("email"));
1161    }
1162
1163    #[test]
1164    fn test_generate_from_json_schema_with_constraints() {
1165        let mut generator = MockDataGenerator::new();
1166
1167        let schema = json!({
1168            "type": "object",
1169            "properties": {
1170                "age": {
1171                    "type": "integer",
1172                    "minimum": 18,
1173                    "maximum": 65
1174                },
1175                "name": {
1176                    "type": "string",
1177                    "minLength": 5,
1178                    "maxLength": 20
1179                }
1180            }
1181        });
1182
1183        let result = generator.generate_from_json_schema(&schema).unwrap();
1184
1185        assert!(result.is_object());
1186        let obj = result.as_object().unwrap();
1187
1188        if let Some(age) = obj.get("age") {
1189            if let Some(age_num) = age.as_i64() {
1190                assert!(age_num >= 18);
1191                assert!(age_num <= 65);
1192            }
1193        }
1194    }
1195
1196    #[test]
1197    fn test_generate_from_json_schema_with_enum() {
1198        let mut generator = MockDataGenerator::new();
1199
1200        let schema = json!({
1201            "type": "object",
1202            "properties": {
1203                "status": {
1204                    "type": "string",
1205                    "enum": ["active", "inactive", "pending"]
1206                }
1207            }
1208        });
1209
1210        let result = generator.generate_from_json_schema(&schema).unwrap();
1211
1212        assert!(result.is_object());
1213        let obj = result.as_object().unwrap();
1214
1215        if let Some(status) = obj.get("status") {
1216            if let Some(status_str) = status.as_str() {
1217                assert!(["active", "inactive", "pending"].contains(&status_str));
1218            }
1219        }
1220    }
1221}