|
| 1 | +package io.lettuce.scenario; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Tag; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import io.lettuce.scenario.FaultInjectionClient.MaintenanceOperation; |
| 12 | +import io.lettuce.scenario.FaultInjectionClient.MaintenanceOperationType; |
| 13 | +import reactor.test.StepVerifier; |
| 14 | + |
| 15 | +import static io.lettuce.TestTags.UNIT_TEST; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit tests for FaultInjectionClient to verify compilation and basic functionality. |
| 19 | + */ |
| 20 | +@Tag(UNIT_TEST) |
| 21 | +public class FaultInjectionClientUnitTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + @DisplayName("FaultInjectionClient can be instantiated") |
| 25 | + public void canInstantiateFaultInjectionClient() { |
| 26 | + FaultInjectionClient client = new FaultInjectionClient(); |
| 27 | + assertThat(client).isNotNull(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + @DisplayName("executeRladminCommand validates parameters") |
| 32 | + public void executeRladminCommandValidatesParameters() { |
| 33 | + FaultInjectionClient client = new FaultInjectionClient(); |
| 34 | + |
| 35 | + // Test null BDB ID |
| 36 | + StepVerifier.create(client.executeRladminCommand(null, "test command")).expectError(IllegalArgumentException.class) |
| 37 | + .verify(); |
| 38 | + |
| 39 | + // Test empty BDB ID |
| 40 | + StepVerifier.create(client.executeRladminCommand("", "test command")).expectError(IllegalArgumentException.class) |
| 41 | + .verify(); |
| 42 | + |
| 43 | + // Test null command |
| 44 | + StepVerifier.create(client.executeRladminCommand("123", null)).expectError(IllegalArgumentException.class).verify(); |
| 45 | + |
| 46 | + // Test empty command |
| 47 | + StepVerifier.create(client.executeRladminCommand("123", "")).expectError(IllegalArgumentException.class).verify(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @DisplayName("triggerEndpointRebind validates parameters") |
| 52 | + public void triggerEndpointRebindValidatesParameters() { |
| 53 | + FaultInjectionClient client = new FaultInjectionClient(); |
| 54 | + |
| 55 | + // Test null endpoint ID |
| 56 | + StepVerifier.create(client.triggerEndpointRebind("123", null, "single")).expectError(IllegalArgumentException.class) |
| 57 | + .verify(); |
| 58 | + |
| 59 | + // Test null policy |
| 60 | + StepVerifier.create(client.triggerEndpointRebind("123", "1", null)).expectError(IllegalArgumentException.class) |
| 61 | + .verify(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("triggerShardMigration validates shard ID format") |
| 66 | + public void triggerShardMigrationValidatesShardId() { |
| 67 | + FaultInjectionClient client = new FaultInjectionClient(); |
| 68 | + |
| 69 | + // Test invalid shard ID format using 4-parameter version |
| 70 | + StepVerifier.create(client.triggerShardMigration("123", "invalid", "1", "2")) |
| 71 | + .expectError(IllegalArgumentException.class).verify(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("triggerShardFailover validates parameters") |
| 76 | + public void triggerShardFailoverValidatesParameters() { |
| 77 | + FaultInjectionClient client = new FaultInjectionClient(); |
| 78 | + |
| 79 | + // Test null RedisEnterpriseConfig |
| 80 | + StepVerifier.create(client.triggerShardFailover("123", "1", "1", null)).expectError(IllegalArgumentException.class) |
| 81 | + .verify(); |
| 82 | + |
| 83 | + // Test null nodeId |
| 84 | + StepVerifier.create(client.triggerShardFailover("123", "1", null, new RedisEnterpriseConfig("123"))) |
| 85 | + .expectError(IllegalArgumentException.class).verify(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + @DisplayName("MaintenanceOperation can be created for endpoint rebind") |
| 90 | + public void canCreateMaintenanceOperationForEndpointRebind() { |
| 91 | + MaintenanceOperation operation = new MaintenanceOperation(MaintenanceOperationType.ENDPOINT_REBIND, "1", "single"); |
| 92 | + |
| 93 | + assertThat(operation.getType()).isEqualTo(MaintenanceOperationType.ENDPOINT_REBIND); |
| 94 | + assertThat(operation.getEndpointId()).isEqualTo("1"); |
| 95 | + assertThat(operation.getPolicy()).isEqualTo("single"); |
| 96 | + assertThat(operation.getShardId()).isNull(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @DisplayName("MaintenanceOperation can be created for shard migration") |
| 101 | + public void canCreateMaintenanceOperationForShardMigration() { |
| 102 | + MaintenanceOperation operation = new MaintenanceOperation(MaintenanceOperationType.SHARD_MIGRATION, "1"); |
| 103 | + |
| 104 | + assertThat(operation.getType()).isEqualTo(MaintenanceOperationType.SHARD_MIGRATION); |
| 105 | + assertThat(operation.getShardId()).isEqualTo("1"); |
| 106 | + assertThat(operation.getEndpointId()).isNull(); |
| 107 | + assertThat(operation.getPolicy()).isNull(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @DisplayName("MaintenanceOperation can be created for shard failover") |
| 112 | + public void canCreateMaintenanceOperationForShardFailover() { |
| 113 | + MaintenanceOperation operation = new MaintenanceOperation(MaintenanceOperationType.SHARD_FAILOVER, "2"); |
| 114 | + |
| 115 | + assertThat(operation.getType()).isEqualTo(MaintenanceOperationType.SHARD_FAILOVER); |
| 116 | + assertThat(operation.getShardId()).isEqualTo("2"); |
| 117 | + assertThat(operation.getEndpointId()).isNull(); |
| 118 | + assertThat(operation.getPolicy()).isNull(); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @DisplayName("triggerMaintenanceSequence validates parameters") |
| 123 | + public void triggerMaintenanceSequenceValidatesParameters() { |
| 124 | + FaultInjectionClient client = new FaultInjectionClient(); |
| 125 | + |
| 126 | + // Test null operations |
| 127 | + StepVerifier.create(client.triggerMaintenanceSequence("123", null)).expectError(IllegalArgumentException.class) |
| 128 | + .verify(); |
| 129 | + |
| 130 | + // Test empty operations |
| 131 | + StepVerifier.create(client.triggerMaintenanceSequence("123", Arrays.asList())) |
| 132 | + .expectError(IllegalArgumentException.class).verify(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + @DisplayName("MaintenanceOperation toString produces readable output") |
| 137 | + public void maintenanceOperationToStringIsReadable() { |
| 138 | + MaintenanceOperation rebindOp = new MaintenanceOperation(MaintenanceOperationType.ENDPOINT_REBIND, "1", "single"); |
| 139 | + MaintenanceOperation migrateOp = new MaintenanceOperation(MaintenanceOperationType.SHARD_MIGRATION, "1"); |
| 140 | + MaintenanceOperation failoverOp = new MaintenanceOperation(MaintenanceOperationType.SHARD_FAILOVER, "2"); |
| 141 | + |
| 142 | + assertThat(rebindOp.toString()).contains("EndpointRebind"); |
| 143 | + assertThat(rebindOp.toString()).contains("endpoint=1"); |
| 144 | + assertThat(rebindOp.toString()).contains("policy=single"); |
| 145 | + |
| 146 | + assertThat(migrateOp.toString()).contains("ShardMigration"); |
| 147 | + assertThat(migrateOp.toString()).contains("shard=1"); |
| 148 | + |
| 149 | + assertThat(failoverOp.toString()).contains("ShardFailover"); |
| 150 | + assertThat(failoverOp.toString()).contains("shard=2"); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments