NetworkMockState

@Serializable
data class NetworkMockState(val globalMockingEnabled: Boolean = false, val endpointStates: Map<String, EndpointMockState> = emptyMap(), val lastModified: Long = 0)

Represents the complete state of network mocking, persisted in DataStore.

This data class encapsulates all runtime state for the network mock feature, including the global mocking toggle and individual endpoint configurations. The state is persisted using DataStore Preferences to survive app restarts.

State Structure

  • Global Toggle: Master switch to enable/disable all mocking

  • Endpoint States: Per-endpoint configuration (mock enabled + selected response)

  • Metadata: Timestamp tracking for state changes

Persistence

State is automatically saved to DataStore when changes are made through the com.worldline.devview.networkmock.core.repository.MockStateRepository:

val repository = MockStateRepository(dataStore)

// Enable global mocking
repository.setGlobalMockingEnabled(true)

// Configure specific endpoint
repository.setEndpointMockState(
key = EndpointKey("my-backend", "staging", "getUser"),
state = EndpointMockState.Mock(responseFile = "getUser-200.json")
)

Two-Level Toggle System

The plugin uses a two-level check:

  1. Global Level: If globalMockingEnabled is false, ALL requests use actual network

  2. Endpoint Level: If global is true, check individual endpointStates for each request

This allows quick testing with/without mocking while preserving individual configurations.

Environment Resolution

There is no stored "active environment" selection. The environment is derived at runtime by matching the incoming request's hostname against the com.worldline.devview.networkmock.core.model.EnvironmentConfig.url of each environment across all API groups. This allows the app to simultaneously target different environments for different API groups without any manual selection.

Endpoint State Keys

Endpoint states are keyed by "{groupId}-{environmentId}-{endpointId}" to guarantee uniqueness across all combinations. This prevents collisions between API groups that happen to share the same environment ID and endpoint ID:

val key = "my-backend-staging-getUser"     // my-backend group, staging environment, getUser endpoint
val state = networkMockState.endpointStates[key]

See also

Constructors

Link copied to clipboard
constructor(globalMockingEnabled: Boolean = false, endpointStates: Map<String, EndpointMockState> = emptyMap(), lastModified: Long = 0)

Properties

Link copied to clipboard

Map of endpoint states, keyed by "{groupId}-{environmentId}-{endpointId}"

Link copied to clipboard

Master toggle — when false, all mocking is disabled

Link copied to clipboard

Timestamp (milliseconds since epoch) of last state modification

Functions

Link copied to clipboard

Gets the mock state for a specific endpoint identified by an EndpointKey.

fun getEndpointState(groupId: String, environmentId: String, endpointId: String): EndpointMockState?

Gets the mock state for a specific endpoint in a specific group and environment.

Link copied to clipboard

Creates a new state with all endpoint mocks reset to use the actual network.

Link copied to clipboard
fun withEndpointState(groupId: String, environmentId: String, endpointId: String, state: EndpointMockState): NetworkMockState

Creates a new state with the specified endpoint state updated.