|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Airbyte |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package io.airbyte.config.persistence; |
| 26 | + |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.Mockito.inOrder; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.never; |
| 31 | +import static org.mockito.Mockito.reset; |
| 32 | +import static org.mockito.Mockito.spy; |
| 33 | +import static org.mockito.Mockito.times; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | +import static org.mockito.Mockito.when; |
| 36 | + |
| 37 | +import io.airbyte.commons.json.Jsons; |
| 38 | +import io.airbyte.config.ConfigSchema; |
| 39 | +import io.airbyte.config.Configs; |
| 40 | +import io.airbyte.config.StandardDestinationDefinition; |
| 41 | +import io.airbyte.config.StandardSourceDefinition; |
| 42 | +import io.airbyte.db.instance.configs.ConfigsDatabaseInstance; |
| 43 | +import java.nio.file.Files; |
| 44 | +import java.nio.file.Path; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Map; |
| 47 | +import java.util.UUID; |
| 48 | +import org.jooq.DSLContext; |
| 49 | +import org.junit.jupiter.api.AfterAll; |
| 50 | +import org.junit.jupiter.api.BeforeAll; |
| 51 | +import org.junit.jupiter.api.BeforeEach; |
| 52 | +import org.junit.jupiter.api.DisplayName; |
| 53 | +import org.junit.jupiter.api.Test; |
| 54 | +import org.mockito.InOrder; |
| 55 | + |
| 56 | +/** |
| 57 | + * Unit test for the {@link DatabaseConfigPersistence#initialize} method. |
| 58 | + */ |
| 59 | +public class DatabaseConfigPersistenceInitializeTest extends BaseDatabaseConfigPersistenceTest { |
| 60 | + |
| 61 | + // mock YAML seed connector definitions |
| 62 | + private static final List<StandardSourceDefinition> SEED_SOURCES = List.of(SOURCE_GITHUB); |
| 63 | + private static final List<StandardDestinationDefinition> SEED_DESTINATIONS = List.of(DESTINATION_SNOWFLAKE); |
| 64 | + |
| 65 | + private static final ConfigPersistence SEED_PERSISTENCE = mock(ConfigPersistence.class); |
| 66 | + private static Path ROOT_PATH; |
| 67 | + |
| 68 | + private final Configs configs = mock(Configs.class); |
| 69 | + |
| 70 | + @BeforeAll |
| 71 | + public static void setup() throws Exception { |
| 72 | + database = new ConfigsDatabaseInstance(container.getUsername(), container.getPassword(), container.getJdbcUrl()).getAndInitialize(); |
| 73 | + configPersistence = spy(new DatabaseConfigPersistence(database)); |
| 74 | + |
| 75 | + when(SEED_PERSISTENCE.dumpConfigs()).thenReturn(Map.of( |
| 76 | + ConfigSchema.STANDARD_SOURCE_DEFINITION.name(), SEED_SOURCES.stream().map(Jsons::jsonNode), |
| 77 | + ConfigSchema.STANDARD_DESTINATION_DEFINITION.name(), SEED_DESTINATIONS.stream().map(Jsons::jsonNode))); |
| 78 | + when(SEED_PERSISTENCE.listConfigs(ConfigSchema.STANDARD_SOURCE_DEFINITION, StandardSourceDefinition.class)) |
| 79 | + .thenReturn(SEED_SOURCES); |
| 80 | + when(SEED_PERSISTENCE.listConfigs(ConfigSchema.STANDARD_DESTINATION_DEFINITION, StandardDestinationDefinition.class)) |
| 81 | + .thenReturn(SEED_DESTINATIONS); |
| 82 | + } |
| 83 | + |
| 84 | + @AfterAll |
| 85 | + public static void tearDown() throws Exception { |
| 86 | + database.close(); |
| 87 | + } |
| 88 | + |
| 89 | + @BeforeEach |
| 90 | + public void resetPersistence() throws Exception { |
| 91 | + ROOT_PATH = Files.createTempDirectory( |
| 92 | + Files.createDirectories(Path.of("/tmp/airbyte_tests")), |
| 93 | + DatabaseConfigPersistenceInitializeTest.class.getSimpleName() + UUID.randomUUID()); |
| 94 | + |
| 95 | + reset(configs); |
| 96 | + when(configs.getConfigRoot()).thenReturn(ROOT_PATH); |
| 97 | + |
| 98 | + database.query(ctx -> ctx.truncateTable("airbyte_configs").execute()); |
| 99 | + |
| 100 | + reset(configPersistence); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("When database is not initialized, and there is no local config dir, copy from seed") |
| 105 | + public void testNewDeployment() throws Exception { |
| 106 | + configPersistence.initialize(configs, SEED_PERSISTENCE); |
| 107 | + |
| 108 | + assertRecordCount(2); |
| 109 | + assertHasSource(SOURCE_GITHUB); |
| 110 | + assertHasDestination(DESTINATION_SNOWFLAKE); |
| 111 | + |
| 112 | + verify(configPersistence, times(1)).copyConfigsFromSeed(any(DSLContext.class), any(ConfigPersistence.class)); |
| 113 | + verify(configPersistence, never()).updateConfigsFromSeed(any(DSLContext.class), any(ConfigPersistence.class)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + @DisplayName("When database is not initialized, and there is local config dir, copy from local and update from seed") |
| 118 | + public void testMigrationDeployment() throws Exception { |
| 119 | + prepareLocalFilePersistence(); |
| 120 | + |
| 121 | + configPersistence.initialize(configs, SEED_PERSISTENCE); |
| 122 | + |
| 123 | + assertRecordCount(3); |
| 124 | + assertHasSource(SOURCE_GITHUB); |
| 125 | + assertHasDestination(DESTINATION_S3); |
| 126 | + assertHasDestination(DESTINATION_SNOWFLAKE); |
| 127 | + |
| 128 | + InOrder callOrder = inOrder(configPersistence); |
| 129 | + callOrder.verify(configPersistence, times(1)).copyConfigsFromSeed(any(DSLContext.class), any(ConfigPersistence.class)); |
| 130 | + callOrder.verify(configPersistence, times(1)).updateConfigsFromSeed(any(DSLContext.class), any(ConfigPersistence.class)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @DisplayName("When database has been initialized, ignore local config dir, and update from seed") |
| 135 | + public void testUpdateDeployment() throws Exception { |
| 136 | + prepareLocalFilePersistence(); |
| 137 | + writeSource(configPersistence, SOURCE_GITHUB); |
| 138 | + |
| 139 | + configPersistence.initialize(configs, SEED_PERSISTENCE); |
| 140 | + |
| 141 | + assertRecordCount(2); |
| 142 | + assertHasSource(SOURCE_GITHUB); |
| 143 | + assertHasDestination(DESTINATION_SNOWFLAKE); |
| 144 | + |
| 145 | + verify(configPersistence, never()).copyConfigsFromSeed(any(DSLContext.class), any(ConfigPersistence.class)); |
| 146 | + verify(configPersistence, times(1)).updateConfigsFromSeed(any(DSLContext.class), any(ConfigPersistence.class)); |
| 147 | + } |
| 148 | + |
| 149 | + private void prepareLocalFilePersistence() throws Exception { |
| 150 | + Files.createDirectories(ROOT_PATH.resolve(FileSystemConfigPersistence.CONFIG_DIR)); |
| 151 | + ConfigPersistence filePersistence = new FileSystemConfigPersistence(ROOT_PATH); |
| 152 | + writeSource(filePersistence, SOURCE_GITHUB); |
| 153 | + writeDestination(filePersistence, DESTINATION_S3); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments