Skip to main content

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 TopologyTestDriver for 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
Key Methods:
  • request(UUID, ExecutionRequest): Sends execution requests
  • executionResponseFor(ExecutionRequestEvent): Retrieves execution responses
  • upload(UUID, InputStream): Uploads files for testing
  • storeComponent(Component): Stores components in mock repository

ComponentTest

Represents a test instance for a specific component execution. Key Methods:
  • execute(payload): Executes a function or workflow
  • getResult(): Retrieves the execution result
  • getRoute(): Gets the current route information
  • getCurrentRoute(): Queries the current route state
  • submit(payload): Submits data to the current route
  • upload(Resource): Uploads a file resource

ComponentTestBuilder

Fluid builder API for creating component tests. Key Methods:
  • function(String name): Sets up a function test
  • workflow(String name): Sets up a workflow test
  • component(Component): Sets the component to test
  • component(ComponentFactory): Sets the component to test using ComponentFactory
  • dependency(Component): Adds component dependencies
  • mock(Closure): Adds mock behavior
  • config(payload): Sets component configuration

ComponentValidator

Validation engine for testing component definitions and configurations. Key Methods:
  • validateDefinition(Component): Validates component DSL structure and syntax
  • validateConfig(Component): Validates component configuration requirements

ComponentValidatorBuilder

Builder API for creating component validation tests. Key Methods:
  • component(Component): Sets the component to validate
  • config(Map): Sets component configuration for validation

Usage Examples

The Component Tester provides two main testing approaches:
  1. ComponentTesterSpec: For execution testing of components, functions, and workflows
  2. 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:
  1. Using the same component model: Tests use the same Component and DSL structures
  2. Leveraging existing topics: Uses the same execution request/response topics
  3. Supporting all DSL features: Functions, workflows, routes, validation, etc.
  4. Mocking framework components: Provides mocks for all major framework components
This ensures that tests accurately reflect real-world component behavior while providing the isolation and control needed for effective unit testing.