|
| 1 | +package it.aboutbits.postgresql.crd.connection; |
| 2 | + |
| 3 | +import io.fabric8.kubernetes.api.model.ObjectMeta; |
| 4 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 5 | +import io.quarkus.test.InjectMock; |
| 6 | +import io.quarkus.test.junit.QuarkusTest; |
| 7 | +import it.aboutbits.postgresql.core.PostgreSQLContextFactory; |
| 8 | +import jakarta.inject.Inject; |
| 9 | +import org.jooq.DSLContext; |
| 10 | +import org.jooq.exception.DataAccessException; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import java.sql.SQLException; |
| 16 | +import java.util.Collections; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.ArgumentMatchers.anyString; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +@QuarkusTest |
| 24 | +class ClusterConnectionReconcilerErrorTest { |
| 25 | + @InjectMock |
| 26 | + PostgreSQLContextFactory contextFactory; |
| 27 | + |
| 28 | + @Inject |
| 29 | + ClusterConnectionReconciler reconciler; |
| 30 | + |
| 31 | + private ClusterConnection resource; |
| 32 | + private Context<ClusterConnection> context; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void setUp() { |
| 36 | + resource = new ClusterConnection(); |
| 37 | + |
| 38 | + var metadata = new ObjectMeta(); |
| 39 | + metadata.setGeneration(1L); |
| 40 | + |
| 41 | + // We mock the spec to ensure getName() works safely without throwing NPE, |
| 42 | + // as the custom getName() implementation in ClusterConnection relies on spec fields. |
| 43 | + var spec = mock(ClusterConnectionSpec.class); |
| 44 | + |
| 45 | + when(spec.getHost()).thenReturn("localhost"); |
| 46 | + when(spec.getPort()).thenReturn(5432); |
| 47 | + when(spec.getMaintenanceDatabase()).thenReturn("postgres"); |
| 48 | + when(spec.getParameters()).thenReturn(Collections.emptyMap()); |
| 49 | + |
| 50 | + resource.setSpec(spec); |
| 51 | + resource.setMetadata(metadata); |
| 52 | + |
| 53 | + //noinspection unchecked |
| 54 | + context = mock(Context.class); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + @DisplayName("Should handle SQLException during DSL context creation") |
| 59 | + void reconcile_whenDslCreationFails_shouldReturnErrorStatus() throws SQLException { |
| 60 | + // given |
| 61 | + var errorMessage = "Connection refused to database"; |
| 62 | + |
| 63 | + when(contextFactory.getDSLContext(resource)).thenThrow( |
| 64 | + new SQLException(errorMessage) |
| 65 | + ); |
| 66 | + |
| 67 | + // when |
| 68 | + var updateControl = reconciler.reconcile(resource, context); |
| 69 | + |
| 70 | + // then |
| 71 | + assertThat(updateControl.getResource()) |
| 72 | + .isPresent() |
| 73 | + .get() |
| 74 | + .extracting(ClusterConnection::getStatus) |
| 75 | + .satisfies(status -> { |
| 76 | + assertThat(status.getMessage()).contains(errorMessage); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @DisplayName("Should handle DataAccessException during version check") |
| 82 | + void reconcile_whenVersionQueryFails_shouldReturnErrorStatus() throws SQLException { |
| 83 | + // given |
| 84 | + var errorMessage = "Query execution failed"; |
| 85 | + var dslContext = mock(DSLContext.class); |
| 86 | + |
| 87 | + when(contextFactory.getDSLContext(resource)).thenReturn( |
| 88 | + dslContext |
| 89 | + ); |
| 90 | + |
| 91 | + when(dslContext.fetchSingle(anyString())).thenThrow( |
| 92 | + new DataAccessException(errorMessage) |
| 93 | + ); |
| 94 | + |
| 95 | + // when |
| 96 | + var updateControl = reconciler.reconcile(resource, context); |
| 97 | + |
| 98 | + // then |
| 99 | + assertThat(updateControl.getResource()) |
| 100 | + .isPresent() |
| 101 | + .get() |
| 102 | + .extracting(ClusterConnection::getStatus) |
| 103 | + .satisfies(status -> { |
| 104 | + assertThat(status.getMessage()).contains(errorMessage); |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
0 commit comments