Component Tester Documentation
Overview
The Component Tester is a testing framework designed to facilitate unit testing of Zenoo Hub components. It provides a comprehensive testing environment that simulates the execution of components, workflows, and functions in an isolated testing context using Kafka Streams test utilities.Key Features
- Isolated Testing Environment: Uses Kafka Streams
TopologyTestDriverfor testing without external dependencies - Component Testing: Test individual components, functions, and workflows
- Component Validation: Comprehensive validation testing for component definitions, configurations, and HTTP workflows
- Mock Support: Mock component dependencies and external services
- File Upload Testing: Test file upload functionality with mock file cache
- Dependency Management: Test components with their dependencies
- Spock Framework Integration: Built on Spock for expressive test specifications
Architecture
The Component Tester consists of several core classes:Core Classes
ComponentTester
The main testing engine that manages the Kafka Streams test environment.
Key Responsibilities:
- Manages Kafka Streams
TopologyTestDriver - Handles execution request/response topics
- Provides file upload capabilities
- Stores components in mock repository
request(UUID, ExecutionRequest): Sends execution requestsexecutionResponseFor(ExecutionRequestEvent): Retrieves execution responsesupload(UUID, InputStream): Uploads files for testingstoreComponent(Component): Stores components in mock repository
ComponentTest
Represents a test instance for a specific component execution.
Key Methods:
execute(payload): Executes a function or workflowgetResult(): Retrieves the execution resultgetRoute(): Gets the current route informationgetCurrentRoute(): Queries the current route statesubmit(payload): Submits data to the current routeupload(Resource): Uploads a file resource
ComponentTestBuilder
Fluid builder API for creating component tests.
Key Methods:
function(String name): Sets up a function testworkflow(String name): Sets up a workflow testcomponent(Component): Sets the component to testcomponent(ComponentFactory): Sets the component to test usingComponentFactorydependency(Component): Adds component dependenciesmock(Closure): Adds mock behaviorconfig(payload): Sets component configuration
ComponentValidator
Validation engine for testing component definitions and configurations.
Key Methods:
validateDefinition(Component): Validates component DSL structure and syntaxvalidateConfig(Component): Validates component configuration requirements
ComponentValidatorBuilder
Builder API for creating component validation tests.
Key Methods:
component(Component): Sets the component to validateconfig(Map): Sets component configuration for validation
Usage Examples
The Component Tester provides two main testing approaches:- ComponentTesterSpec: For execution testing of components, functions, and workflows
- ComponentValidatorSpec: For validation testing of component definitions and configurations
ComponentTesterSpec - Execution Testing
ComponentTesterSpec is used for testing the actual execution behavior of components, including functions, workflows, file uploads, and dependency interactions.Basic Function Testing
Using ComponentFactory with testBuilder
Workflow Testing
Mocking Dependencies
Testing with Dependencies
File Upload Testing
ComponentValidatorSpec - Validation Testing
ComponentValidatorSpec is used for testing component definitions, configurations, and DSL structure without executing the components. This is useful for ensuring components are properly structured and configured before deployment.Basic Definition Validation
Using ComponentFactory with validatorBuilder
Configuration Validation
HTTP Workflow Validation
Validation Testing Features
The Component Validator supports testing of:- Definition Validation: DSL syntax, function references, workflow structure
- Configuration Validation: Required config fields, typed constraints, nested configurations
- HTTP Validation: HTTP blocks, status handlers, response handlers, timeout configuration
- Dependency Validation: Component and connector dependencies
- Complex Scenarios: Multi-step workflows, conditional logic, error handling
Validation Test Base Classes
The framework provides specialized base classes for validation testing:ComponentValidatorSpec: Base class for all validation tests
Testing Best Practices
1. Choose the Right Base Class
- ComponentTesterSpec: For execution testing (functions, workflows, file uploads, dependencies)
- ComponentValidatorSpec: For validation testing (component definitions, configurations)
2. Use the Builder Pattern
The framework provides two builder patterns:Direct Component Usage
ComponentFactory Usage
3. Mock External Dependencies
Mock external services and dependencies to ensure isolated testing:4. Test Different Execution Types
Test various execution scenarios:- Functions: Direct execution with input/output validation
- Workflows: Multi-step execution with route transitions
- File Uploads: File handling and processing
- Dependencies: Component interaction testing
- Validation: Component definition and configuration validation
Component Validation Testing
Use the validation framework to test component structure without execution:Dependencies
The Component Tester includes the following key dependencies:- Spring Boot Test: Testing framework integration
- Kafka Streams Test Utils: Kafka Streams testing utilities
- Spock Framework: Testing specification framework
- Groovy: Dynamic language support
- Reactor Test: Reactive testing utilities
Integration with Main Framework
The Component Tester integrates seamlessly with the main Zenoo Hub framework by:- Using the same component model: Tests use the same
Componentand DSL structures - Leveraging existing topics: Uses the same execution request/response topics
- Supporting all DSL features: Functions, workflows, routes, validation, etc.
- Mocking framework components: Provides mocks for all major framework components