CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-fabric8--kubernetes-server-mock

A JUnit 5 testing library that provides a Kubernetes mock server for testing Kubernetes client applications

Pending
Overview
Eval results
Files

crud-handlers.mddocs/

CRUD Handlers

Low-level HTTP request handlers that implement Kubernetes-compatible CRUD operations. These handlers process individual HTTP requests and manage resource lifecycle operations in CRUD mode.

Capabilities

KubernetesCrudDispatcherHandler Interface

Core functional interface that defines the contract for handling HTTP requests in CRUD mode.

@FunctionalInterface
public interface KubernetesCrudDispatcherHandler {
    
    // HTTP status codes
    int HTTP_UNPROCESSABLE_ENTITY = 422;
    String KIND = "kind";
    String STATUS = "status";
    
    /**
     * Handle an HTTP request and return appropriate response
     * @param request - Recorded HTTP request with path, headers, and body
     * @return MockResponse with status code and response body
     * @throws KubernetesCrudDispatcherException - For validation or processing errors
     */
    default MockResponse handle(RecordedRequest request) throws KubernetesCrudDispatcherException;
    
    /**
     * Handle an HTTP request with decomposed parameters
     * @param path - Request path
     * @param contentType - Content-Type header value
     * @param requestBody - Request body content
     * @return MockResponse with appropriate status and body
     * @throws KubernetesCrudDispatcherException - For validation or processing errors
     */
    MockResponse handle(String path, String contentType, String requestBody) 
        throws KubernetesCrudDispatcherException;
    
    /**
     * Validate that resource metadata matches the request path
     * @param query - Parsed path attributes
     * @param updatedResource - Resource JSON node
     * @throws KubernetesCrudDispatcherException - If name or namespace mismatch
     */
    default void validatePath(AttributeSet query, JsonNode updatedResource) 
        throws KubernetesCrudDispatcherException;
    
    /**
     * Validate resource version for optimistic concurrency control
     * @param currentResource - Current resource state
     * @param updatedResource - Updated resource with version
     * @throws KubernetesCrudDispatcherException - For version conflicts
     */
    default void validateResourceVersion(JsonNode currentResource, JsonNode updatedResource)
        throws KubernetesCrudDispatcherException;
    
    /**
     * Validate and parse request body into a Kubernetes resource
     * @param requestBody - JSON request body
     * @return Parsed GenericKubernetesResource
     * @throws KubernetesCrudDispatcherException - For invalid JSON or missing required fields
     */
    default GenericKubernetesResource validateRequestBody(String requestBody) 
        throws KubernetesCrudDispatcherException;
    
    /**
     * Check if the request path targets a status subresource
     * @param path - HTTP request path
     * @return true if path ends with "/status"
     */
    static boolean isStatusPath(String path);
    
    /**
     * Set or remove status field from a resource JSON node
     * @param source - Resource JSON node to modify
     * @param status - Status JSON node to set, or null to remove
     */
    static void setStatus(JsonNode source, JsonNode status);
}

PostHandler

Handles HTTP POST requests for resource creation, including metadata initialization and name generation.

public class PostHandler implements KubernetesCrudDispatcherHandler {
    
    /**
     * Create a new PostHandler with required dependencies
     * @param attributeExtractor - For parsing paths and resources
     * @param persistence - For storing and retrieving resources
     */
    public PostHandler(KubernetesAttributesExtractor attributeExtractor, 
                      KubernetesCrudPersistence persistence);
    
    /**
     * Handle resource creation request
     * @param path - Creation path (e.g., "/api/v1/namespaces/default/pods")
     * @param contentType - Request content type
     * @param requestBody - Resource JSON to create
     * @return MockResponse with created resource and 201 status
     * @throws KubernetesCrudDispatcherException - For validation errors
     */
    @Override
    public MockResponse handle(String path, String contentType, String requestBody) 
        throws KubernetesCrudDispatcherException;
}

PutHandler

Handles HTTP PUT requests for resource updates and replacements.

public class PutHandler implements KubernetesCrudDispatcherHandler {
    
    /**
     * Create a new PutHandler with required dependencies
     * @param attributeExtractor - For parsing paths and resources
     * @param persistence - For storing and retrieving resources
     */
    public PutHandler(KubernetesAttributesExtractor attributeExtractor,
                     KubernetesCrudPersistence persistence);
    
    /**
     * Handle resource update/replacement request
     * @param path - Update path (e.g., "/api/v1/namespaces/default/pods/my-pod")
     * @param contentType - Request content type
     * @param requestBody - Updated resource JSON
     * @return MockResponse with updated resource and 200 status
     * @throws KubernetesCrudDispatcherException - For validation or conflict errors
     */
    @Override
    public MockResponse handle(String path, String contentType, String requestBody) 
        throws KubernetesCrudDispatcherException;
}

PatchHandler

Handles HTTP PATCH requests supporting both JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7396).

public class PatchHandler implements KubernetesCrudDispatcherHandler {
    
    /**
     * Create a new PatchHandler with required dependencies
     * @param attributeExtractor - For parsing paths and resources
     * @param persistence - For storing and retrieving resources
     */
    public PatchHandler(KubernetesAttributesExtractor attributeExtractor,
                       KubernetesCrudPersistence persistence);
    
    /**
     * Handle resource patch request with support for multiple patch formats
     * @param path - Patch path (e.g., "/api/v1/namespaces/default/pods/my-pod")
     * @param contentType - Patch content type (application/json-patch+json or application/merge-patch+json)
     * @param requestBody - Patch operations or merge patch JSON
     * @return MockResponse with patched resource and 200 status
     * @throws KubernetesCrudDispatcherException - For validation or patch application errors
     */
    @Override
    public MockResponse handle(String path, String contentType, String requestBody) 
        throws KubernetesCrudDispatcherException;
}

Handler Operation Details

Resource Creation (POST)

The PostHandler manages resource creation with these features:

  • Metadata Initialization: Automatically sets metadata.uid, metadata.creationTimestamp, and metadata.resourceVersion
  • Name Generation: Supports generateName for automatic unique name creation
  • Namespace Defaulting: Sets default namespace if not specified for namespaced resources
  • Conflict Detection: Prevents duplicate resource creation

Resource Updates (PUT)

The PutHandler handles complete resource replacement:

  • Optimistic Concurrency: Validates resourceVersion to prevent conflicting updates
  • Status Subresource: Supports separate status updates via /status path
  • Finalizer Processing: Handles finalizer-based deletion patterns
  • Metadata Preservation: Maintains immutable metadata fields

Resource Patching (PATCH)

The PatchHandler supports multiple patch formats:

  • JSON Patch (RFC 6902): Array of operations (add, remove, replace, move, copy, test)
  • JSON Merge Patch (RFC 7396): Partial resource updates with merge semantics
  • Content Type Detection: Automatically selects patch strategy based on Content-Type header
  • Deep Merging: Handles nested object updates correctly

Error Handling

All handlers use KubernetesCrudDispatcherException for consistent error reporting:

try {
    // Handler operation
} catch (KubernetesCrudDispatcherException e) {
    // Returns appropriate HTTP status and Kubernetes Status object
    return new MockResponse()
        .setResponseCode(e.getCode())
        .setBody(e.toStatusBody());
}

Usage Examples

Create Resource

PostHandler handler = new PostHandler(attributeExtractor, persistence);

String podJson = """
    {
      "apiVersion": "v1",
      "kind": "Pod",
      "metadata": {
        "name": "test-pod",
        "namespace": "default"
      },
      "spec": {
        "containers": [{"name": "app", "image": "nginx"}]
      }
    }
    """;

MockResponse response = handler.handle(
    "/api/v1/namespaces/default/pods", 
    "application/json", 
    podJson);
// Returns 201 Created with initialized metadata

Update Resource

PutHandler handler = new PutHandler(attributeExtractor, persistence);

MockResponse response = handler.handle(
    "/api/v1/namespaces/default/pods/test-pod",
    "application/json",
    updatedPodJson);
// Returns 200 OK with updated resource

Patch Resource

PatchHandler handler = new PatchHandler(attributeExtractor, persistence);

// JSON Merge Patch
String mergePatch = """
    {
      "metadata": {
        "labels": {
          "version": "2.0"
        }
      }
    }
    """;

MockResponse response = handler.handle(
    "/api/v1/namespaces/default/pods/test-pod",
    "application/merge-patch+json",
    mergePatch);
// Returns 200 OK with patched resource

Install with Tessl CLI

npx tessl i tessl/maven-io-fabric8--kubernetes-server-mock

docs

attribute-extraction.md

crud-handlers.md

crud-operations.md

custom-resources.md

index.md

junit-integration.md

mock-server-management.md

websocket-operations.md

tile.json