diff options
| author | Paul-Christian Volkmer | 2026-11-11 15:13:46 +0100 |
|---|---|---|
| committer | Paul-Christian Volkmer | 2026-11-13 09:23:31 +0100 |
| commit | 86b4026bcfaed73eda1a435f76c23e0c23e95307 (patch) | |
| tree | 20b041f0111e82b16fc0b4a07f32e9c2a684e232 /src/test | |
| parent | 7e2bc062a4331b6df2cf50b177d943e76e72ae54 (diff) | |
feat: initial support for data(base) migration infrastructure
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/java/dev/dnpm/oshelper/migration/DatabaseMigrationServiceTest.java | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/test/java/dev/dnpm/oshelper/migration/DatabaseMigrationServiceTest.java b/src/test/java/dev/dnpm/oshelper/migration/DatabaseMigrationServiceTest.java new file mode 100644 index 0000000..9c22c8d --- /dev/null +++ b/src/test/java/dev/dnpm/oshelper/migration/DatabaseMigrationServiceTest.java @@ -0,0 +1,113 @@ +/* + * This file is part of DNPMHelper + * + * Copyright (C) 2023-2026 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package dev.dnpm.oshelper.migration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.jdbc.core.JdbcTemplate; + +@ExtendWith(MockitoExtension.class) +class DatabaseMigrationServiceTest { + + JdbcTemplate jdbcTemplate; + ResourcePatternResolver resourcePatternResolver; + + DatabaseMigrationService databaseMigration; + + @BeforeEach + void setUp( + @Mock JdbcTemplate jdbcTemplate, @Mock ResourcePatternResolver resourcePatternResolver) { + this.jdbcTemplate = jdbcTemplate; + this.resourcePatternResolver = resourcePatternResolver; + + databaseMigration = new DatabaseMigrationService(jdbcTemplate, resourcePatternResolver); + } + + @Test + void shouldGetNewMigrationFiles() throws Exception { + try (MockedStatic<MigrationUtils> databaseUtils = Mockito.mockStatic(MigrationUtils.class)) { + databaseUtils + .when( + () -> MigrationUtils.getDatabaseMigrationScripts(any(ResourcePatternResolver.class))) + .thenReturn(List.of("updatednpm_001.sql", "updatednpm_002.sql", "updatednpm_003.sql")); + + databaseUtils + .when(() -> MigrationUtils.getExecutedDatabaseMigrations(any(JdbcTemplate.class))) + .thenReturn(List.of("updatednpm_001.sql", "updatednpm_002.sql")); + + var actual = databaseMigration.getNewMigrationIds(); + assertThat(actual).containsExactly("updatednpm_003.sql"); + } + } + + @Test + void shouldIgnoreOldMigrationFiles() throws Exception { + try (MockedStatic<MigrationUtils> databaseUtils = Mockito.mockStatic(MigrationUtils.class)) { + databaseUtils + .when( + () -> MigrationUtils.getDatabaseMigrationScripts(any(ResourcePatternResolver.class))) + .thenReturn(List.of("updatednpm_001.sql", "updatednpm_002.sql")); + + databaseUtils + .when(() -> MigrationUtils.getExecutedDatabaseMigrations(any(JdbcTemplate.class))) + .thenReturn(List.of("updatednpm_001.sql", "updatednpm_002.sql")); + + var actual = databaseMigration.getNewMigrationIds(); + assertThat(actual).isEmpty(); + } + } + + @Test + void shouldExecuteMigrationFile() throws Exception { + try (MockedStatic<MigrationUtils> databaseUtils = Mockito.mockStatic(MigrationUtils.class)) { + databaseUtils + .when( + () -> MigrationUtils.getDatabaseMigrationScripts(any(ResourcePatternResolver.class))) + .thenReturn(List.of("updatednpm_001.sql", "updatednpm_002.sql")); + + databaseUtils + .when(() -> MigrationUtils.getExecutedDatabaseMigrations(any(JdbcTemplate.class))) + .thenReturn(List.of("updatednpm_001.sql")); + + databaseUtils + .when(() -> MigrationUtils.getSqlQuery(any(ResourcePatternResolver.class), anyString())) + .thenReturn("UPDATE TABLE test;"); + + databaseMigration.migrate(); + + var argumentCaptor = ArgumentCaptor.forClass(String.class); + verify(jdbcTemplate, times(1)).execute(argumentCaptor.capture()); + assertThat(argumentCaptor.getValue()).isEqualTo("UPDATE TABLE test;"); + } + } +} |
