|
| 1 | +package com.linkedin.hoptimator.k8s; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | +import org.mockito.ArgumentCaptor; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.MockedStatic; |
| 13 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 14 | + |
| 15 | +import com.linkedin.hoptimator.Source; |
| 16 | +import com.linkedin.hoptimator.Validator; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.ArgumentMatchers.nullable; |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * Unit tests for {@link K8sPipelineDependencyValidator}. The validator is a thin adapter that |
| 28 | + * forwards to {@link PipelineDependencyChecker#assertNoExternalDependents} — these tests use |
| 29 | + * {@link MockedStatic} on both K8sContext and PipelineDependencyChecker to verify that source |
| 30 | + * fields and self-owner fields are forwarded correctly, and that thrown SQLException becomes a |
| 31 | + * validation issue rather than propagating. |
| 32 | + */ |
| 33 | +@ExtendWith(MockitoExtension.class) |
| 34 | +class K8sPipelineDependencyValidatorTest { |
| 35 | + |
| 36 | + @Mock |
| 37 | + private MockedStatic<K8sContext> contextStatic; |
| 38 | + |
| 39 | + @Mock |
| 40 | + private MockedStatic<PipelineDependencyChecker> checkerStatic; |
| 41 | + |
| 42 | + @Mock |
| 43 | + private Connection connection; |
| 44 | + |
| 45 | + @Mock |
| 46 | + private K8sContext context; |
| 47 | + |
| 48 | + private static Source source() { |
| 49 | + return new Source("kafka1", List.of("KAFKA", "my-topic"), Map.of()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void validateForwardsSourceFieldsAndSelfOwnerToChecker() { |
| 54 | + contextStatic.when(() -> K8sContext.create(connection)).thenReturn(context); |
| 55 | + |
| 56 | + K8sPipelineDependencyValidator validator = |
| 57 | + new K8sPipelineDependencyValidator(source(), "LogicalTable", "my-table"); |
| 58 | + Validator.Issues issues = new Validator.Issues("test"); |
| 59 | + |
| 60 | + validator.validate(issues, connection); |
| 61 | + |
| 62 | + ArgumentCaptor<String> dbCaptor = ArgumentCaptor.forClass(String.class); |
| 63 | + @SuppressWarnings("unchecked") |
| 64 | + ArgumentCaptor<List<String>> pathCaptor = ArgumentCaptor.forClass(List.class); |
| 65 | + ArgumentCaptor<String> kindCaptor = ArgumentCaptor.forClass(String.class); |
| 66 | + ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class); |
| 67 | + |
| 68 | + checkerStatic.verify(() -> PipelineDependencyChecker.assertNoExternalDependents( |
| 69 | + eq(context), dbCaptor.capture(), pathCaptor.capture(), |
| 70 | + kindCaptor.capture(), nameCaptor.capture())); |
| 71 | + |
| 72 | + assertEquals("kafka1", dbCaptor.getValue()); |
| 73 | + assertEquals(List.of("KAFKA", "my-topic"), pathCaptor.getValue()); |
| 74 | + assertEquals("LogicalTable", kindCaptor.getValue()); |
| 75 | + assertEquals("my-table", nameCaptor.getValue()); |
| 76 | + assertTrue(issues.valid()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @SuppressWarnings("unchecked") |
| 81 | + void validatePassesNullSelfOwnerWhenUnset() { |
| 82 | + contextStatic.when(() -> K8sContext.create(connection)).thenReturn(context); |
| 83 | + |
| 84 | + K8sPipelineDependencyValidator validator = |
| 85 | + new K8sPipelineDependencyValidator(source(), null, null); |
| 86 | + Validator.Issues issues = new Validator.Issues("test"); |
| 87 | + |
| 88 | + validator.validate(issues, connection); |
| 89 | + |
| 90 | + ArgumentCaptor<String> kindCaptor = ArgumentCaptor.forClass(String.class); |
| 91 | + ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class); |
| 92 | + checkerStatic.verify(() -> PipelineDependencyChecker.assertNoExternalDependents( |
| 93 | + eq(context), nullable(String.class), nullable(List.class), |
| 94 | + kindCaptor.capture(), nameCaptor.capture())); |
| 95 | + |
| 96 | + assertNull(kindCaptor.getValue()); |
| 97 | + assertNull(nameCaptor.getValue()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + @SuppressWarnings("unchecked") |
| 102 | + void validateRecordsCheckerSqlExceptionAsIssue() { |
| 103 | + contextStatic.when(() -> K8sContext.create(connection)).thenReturn(context); |
| 104 | + checkerStatic.when(() -> PipelineDependencyChecker.assertNoExternalDependents( |
| 105 | + nullable(K8sContext.class), nullable(String.class), nullable(List.class), |
| 106 | + nullable(String.class), nullable(String.class))) |
| 107 | + .thenThrow(new SQLException("3 active pipeline(s) depend on it: p1, p2, p3")); |
| 108 | + |
| 109 | + K8sPipelineDependencyValidator validator = |
| 110 | + new K8sPipelineDependencyValidator(source(), null, null); |
| 111 | + Validator.Issues issues = new Validator.Issues("test"); |
| 112 | + |
| 113 | + validator.validate(issues, connection); |
| 114 | + |
| 115 | + assertFalse(issues.valid(), "blocking pipelines should surface as a validation error"); |
| 116 | + assertTrue(issues.toString().contains("3 active pipeline"), |
| 117 | + "issue message should include the SQLException's text: " + issues); |
| 118 | + } |
| 119 | +} |
0 commit comments