|
| 1 | +package org.openmetadata.service.migration.utils.v1130; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 7 | +import static org.mockito.Answers.RETURNS_DEEP_STUBS; |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
| 9 | +import static org.mockito.ArgumentMatchers.eq; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.mockStatic; |
| 12 | +import static org.mockito.Mockito.never; |
| 13 | +import static org.mockito.Mockito.verify; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.databind.JsonNode; |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.UUID; |
| 21 | +import org.jdbi.v3.core.Handle; |
| 22 | +import org.jdbi.v3.core.statement.Update; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.mockito.ArgumentCaptor; |
| 25 | +import org.mockito.MockedStatic; |
| 26 | +import org.openmetadata.service.resources.databases.DatasourceConfig; |
| 27 | + |
| 28 | +class MigrationUtilTest { |
| 29 | + |
| 30 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 31 | + |
| 32 | + private static final String WEBHOOK_WITH_SECRET_KEY = |
| 33 | + """ |
| 34 | + { |
| 35 | + "destinations": [ |
| 36 | + { |
| 37 | + "type": "Webhook", |
| 38 | + "config": { |
| 39 | + "endpoint": "https://example.com/hook", |
| 40 | + "secretKey": "mysecret" |
| 41 | + } |
| 42 | + } |
| 43 | + ] |
| 44 | + } |
| 45 | + """; |
| 46 | + |
| 47 | + private static final String WEBHOOK_WITHOUT_SECRET_KEY = |
| 48 | + """ |
| 49 | + { |
| 50 | + "destinations": [ |
| 51 | + { |
| 52 | + "type": "Webhook", |
| 53 | + "config": { |
| 54 | + "endpoint": "https://example.com/hook" |
| 55 | + } |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + """; |
| 60 | + |
| 61 | + private static final String WEBHOOK_WITH_EMPTY_SECRET_KEY = |
| 62 | + """ |
| 63 | + { |
| 64 | + "destinations": [ |
| 65 | + { |
| 66 | + "type": "Webhook", |
| 67 | + "config": { |
| 68 | + "endpoint": "https://example.com/hook", |
| 69 | + "secretKey": "" |
| 70 | + } |
| 71 | + } |
| 72 | + ] |
| 73 | + } |
| 74 | + """; |
| 75 | + |
| 76 | + private static final String WEBHOOK_ALREADY_MIGRATED = |
| 77 | + """ |
| 78 | + { |
| 79 | + "destinations": [ |
| 80 | + { |
| 81 | + "type": "Webhook", |
| 82 | + "config": { |
| 83 | + "endpoint": "https://example.com/hook", |
| 84 | + "authType": { "type": "bearer", "secretKey": "mysecret" } |
| 85 | + } |
| 86 | + } |
| 87 | + ] |
| 88 | + } |
| 89 | + """; |
| 90 | + |
| 91 | + private static final String NON_WEBHOOK_DESTINATION = |
| 92 | + """ |
| 93 | + { |
| 94 | + "destinations": [ |
| 95 | + { |
| 96 | + "type": "Slack", |
| 97 | + "config": { |
| 98 | + "secretKey": "mysecret" |
| 99 | + } |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + """; |
| 104 | + |
| 105 | + private Map<String, Object> row(String json) { |
| 106 | + return Map.of("id", UUID.randomUUID().toString(), "json", json); |
| 107 | + } |
| 108 | + |
| 109 | + private Handle handleReturningRows(List<Map<String, Object>> rows) { |
| 110 | + Handle handle = mock(Handle.class, RETURNS_DEEP_STUBS); |
| 111 | + when(handle.createQuery(any(String.class)).mapToMap().list()).thenReturn(rows); |
| 112 | + return handle; |
| 113 | + } |
| 114 | + |
| 115 | + private Handle handleWithUpdateCapture(List<Map<String, Object>> rows, Update mockUpdate) { |
| 116 | + Handle handle = mock(Handle.class, RETURNS_DEEP_STUBS); |
| 117 | + when(handle.createQuery(any(String.class)).mapToMap().list()).thenReturn(rows); |
| 118 | + when(handle.createUpdate(any(String.class))).thenReturn(mockUpdate); |
| 119 | + return handle; |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void migrateWebhookSecretKeyToAuthTypeIsNoOpWhenNoRows() { |
| 124 | + Handle handle = handleReturningRows(List.of()); |
| 125 | + |
| 126 | + assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); |
| 127 | + |
| 128 | + verify(handle, never()).createUpdate(any()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void migrateWebhookSecretKeyToAuthTypeIsNoOpWhenNoSecretKey() { |
| 133 | + Handle handle = handleReturningRows(List.of(row(WEBHOOK_WITHOUT_SECRET_KEY))); |
| 134 | + |
| 135 | + assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); |
| 136 | + |
| 137 | + verify(handle, never()).createUpdate(any()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void migrateWebhookSecretKeyToAuthTypeIsNoOpWhenEmptySecretKey() { |
| 142 | + Handle handle = handleReturningRows(List.of(row(WEBHOOK_WITH_EMPTY_SECRET_KEY))); |
| 143 | + |
| 144 | + assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); |
| 145 | + |
| 146 | + verify(handle, never()).createUpdate(any()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void migrateWebhookSecretKeyToAuthTypeIsNoOpWhenAlreadyMigrated() { |
| 151 | + Handle handle = handleReturningRows(List.of(row(WEBHOOK_ALREADY_MIGRATED))); |
| 152 | + |
| 153 | + assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); |
| 154 | + |
| 155 | + verify(handle, never()).createUpdate(any()); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void migrateWebhookSecretKeyToAuthTypeIsNoOpForNonWebhookDestinations() { |
| 160 | + Handle handle = handleReturningRows(List.of(row(NON_WEBHOOK_DESTINATION))); |
| 161 | + |
| 162 | + assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); |
| 163 | + |
| 164 | + verify(handle, never()).createUpdate(any()); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void migrateWebhookSecretKeyToAuthTypeMigratesMysql() throws Exception { |
| 169 | + Update mockUpdate = mock(Update.class, RETURNS_DEEP_STUBS); |
| 170 | + Handle handle = handleWithUpdateCapture(List.of(row(WEBHOOK_WITH_SECRET_KEY)), mockUpdate); |
| 171 | + |
| 172 | + try (MockedStatic<DatasourceConfig> ds = mockStatic(DatasourceConfig.class)) { |
| 173 | + DatasourceConfig mockConfig = mock(DatasourceConfig.class); |
| 174 | + ds.when(DatasourceConfig::getInstance).thenReturn(mockConfig); |
| 175 | + when(mockConfig.isMySQL()).thenReturn(true); |
| 176 | + |
| 177 | + assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); |
| 178 | + |
| 179 | + ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class); |
| 180 | + verify(handle).createUpdate(sqlCaptor.capture()); |
| 181 | + assertEquals( |
| 182 | + "UPDATE event_subscription_entity SET json = :json WHERE id = :id", sqlCaptor.getValue()); |
| 183 | + |
| 184 | + ArgumentCaptor<String> jsonCaptor = ArgumentCaptor.forClass(String.class); |
| 185 | + verify(mockUpdate).bind(eq("json"), jsonCaptor.capture()); |
| 186 | + |
| 187 | + JsonNode config = |
| 188 | + MAPPER.readTree(jsonCaptor.getValue()).get("destinations").get(0).get("config"); |
| 189 | + assertNull(config.get("secretKey")); |
| 190 | + assertNotNull(config.get("authType")); |
| 191 | + assertEquals("bearer", config.get("authType").get("type").asText()); |
| 192 | + assertEquals("mysecret", config.get("authType").get("secretKey").asText()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void migrateWebhookSecretKeyToAuthTypeMigratesPostgres() throws Exception { |
| 198 | + Update mockUpdate = mock(Update.class, RETURNS_DEEP_STUBS); |
| 199 | + Handle handle = handleWithUpdateCapture(List.of(row(WEBHOOK_WITH_SECRET_KEY)), mockUpdate); |
| 200 | + |
| 201 | + try (MockedStatic<DatasourceConfig> ds = mockStatic(DatasourceConfig.class)) { |
| 202 | + DatasourceConfig mockConfig = mock(DatasourceConfig.class); |
| 203 | + ds.when(DatasourceConfig::getInstance).thenReturn(mockConfig); |
| 204 | + when(mockConfig.isMySQL()).thenReturn(false); |
| 205 | + |
| 206 | + assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); |
| 207 | + |
| 208 | + ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class); |
| 209 | + verify(handle).createUpdate(sqlCaptor.capture()); |
| 210 | + assertEquals( |
| 211 | + "UPDATE event_subscription_entity SET json = :json::jsonb WHERE id = :id", |
| 212 | + sqlCaptor.getValue()); |
| 213 | + |
| 214 | + ArgumentCaptor<String> jsonCaptor = ArgumentCaptor.forClass(String.class); |
| 215 | + verify(mockUpdate).bind(eq("json"), jsonCaptor.capture()); |
| 216 | + |
| 217 | + JsonNode config = |
| 218 | + MAPPER.readTree(jsonCaptor.getValue()).get("destinations").get(0).get("config"); |
| 219 | + assertNull(config.get("secretKey")); |
| 220 | + assertNotNull(config.get("authType")); |
| 221 | + assertEquals("bearer", config.get("authType").get("type").asText()); |
| 222 | + assertEquals("mysecret", config.get("authType").get("secretKey").asText()); |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments