Skip to content

Commit

Permalink
Merge pull request #79 from rgdoliveira/sync_main
Browse files Browse the repository at this point in the history
Sync main branch with Apache main branch
  • Loading branch information
rgdoliveira authored Nov 18, 2024
2 parents c336146 + 6ff4143 commit b813ee5
Show file tree
Hide file tree
Showing 171 changed files with 5,253 additions and 859 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.kie.flyway.initializer.impl;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
Expand All @@ -33,9 +32,7 @@

public class DefaultKieModuleFlywayConfigLoader implements KieModuleFlywayConfigLoader {

public static String KIE_FLYWAY_DESCRIPTOR_FILE_NAME = "kie-flyway.properties";

public static String KIE_FLYWAY_DESCRIPTOR_FILE_LOCATION = "META-INF" + File.separator + KIE_FLYWAY_DESCRIPTOR_FILE_NAME;
public static String KIE_FLYWAY_DESCRIPTOR_FILE_LOCATION = "META-INF/kie-flyway.properties";

public static final String MODULE_PREFIX = "module.";
public static final String MODULE_NAME_KEY = MODULE_PREFIX + "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@

package org.kie.flyway.integration;

public interface KieFlywayNamedModule {
public class KieFlywayNamedModule {

boolean isEnabled();
private final String name;
private final boolean enabled;

public KieFlywayNamedModule(String name, boolean enabled) {
this.name = name;
this.enabled = enabled;
}

public String getName() {
return name;
}

public boolean isEnabled() {
return enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.kie.flyway.integration;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

import javax.sql.DataSource;
Expand All @@ -34,18 +33,18 @@ public class KieFlywayRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(KieFlywayRunner.class);

private final ClassLoader classLoader;
private final KieFlywayConfiguration<? extends KieFlywayNamedModule> configuration;
private final KieFlywayRunnerConfiguration configuration;

private KieFlywayRunner(KieFlywayConfiguration<? extends KieFlywayNamedModule> configuration) {
private KieFlywayRunner(KieFlywayRunnerConfiguration configuration) {
this(configuration, Thread.currentThread().getContextClassLoader());
}

protected KieFlywayRunner(KieFlywayConfiguration<? extends KieFlywayNamedModule> configuration, ClassLoader classLoader) {
protected KieFlywayRunner(KieFlywayRunnerConfiguration configuration, ClassLoader classLoader) {
this.configuration = configuration;
this.classLoader = classLoader;
}

public static KieFlywayRunner get(KieFlywayConfiguration<? extends KieFlywayNamedModule> configuration) {
public static KieFlywayRunner get(KieFlywayRunnerConfiguration configuration) {
return new KieFlywayRunner(configuration);
}

Expand All @@ -60,9 +59,9 @@ public void runFlyway(DataSource dataSource) {
assertValue(dataSource, "Kie Flyway: Cannot run Kie Flyway migration default datasource is null");

Collection<String> excludedModules = configuration.getModules()
.entrySet()
.stream().filter(entry -> !entry.getValue().isEnabled())
.map(Map.Entry::getKey)
.stream()
.filter(module -> !module.isEnabled())
.map(KieFlywayNamedModule::getName)
.toList();

KieFlywayInitializer.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,23 @@

package org.kie.flyway.integration;

import java.util.Map;
import java.util.Collection;

public class TestKieFlywayConfiguration implements KieFlywayConfiguration<TestKieFlywayNamedModule> {
public class KieFlywayRunnerConfiguration {

private boolean enabled;
private Map<String, TestKieFlywayNamedModule> modules;
private final boolean enabled;
private final Collection<KieFlywayNamedModule> modules;

public TestKieFlywayConfiguration(boolean enabled, Map<String, TestKieFlywayNamedModule> modules) {
public KieFlywayRunnerConfiguration(boolean enabled, Collection<KieFlywayNamedModule> modules) {
this.enabled = enabled;
this.modules = modules;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

@Override
public boolean isEnabled() {
return enabled;
}

@Override
public Map<String, TestKieFlywayNamedModule> getModules() {
public Collection<KieFlywayNamedModule> getModules() {
return modules;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.*;
import java.util.stream.Stream;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -37,6 +36,7 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.kie.flyway.test.models.TestModels.*;

@Testcontainers
Expand Down Expand Up @@ -71,17 +71,17 @@ public void init() {
@ParameterizedTest
@MethodSource("getDataSources")
public void testTestKieFlywayInitializerBuilderValidations(TestDataSource dataSource) {
Assertions.assertThatThrownBy(() -> KieFlywayInitializer.builder()
assertThatThrownBy(() -> KieFlywayInitializer.builder()
.build()).isInstanceOf(KieFlywayException.class)
.hasMessage("Cannot create KieFlywayInitializer migration, dataSource is null.");
.hasMessage("Cannot create KieFlywayInitializer migration, dataSource is null.");

classLoader.addKieFlywayModule("initializers/kie-flyway.no.locations.properties");

Assertions.assertThatThrownBy(() -> KieFlywayInitializer.builder()
assertThatThrownBy(() -> KieFlywayInitializer.builder()
.withDatasource(dataSource.getDataSource())
.withClassLoader(classLoader).build().migrate())
.isInstanceOf(KieFlywayException.class)
.hasMessageContaining("Cannot run Flyway migration for module `no-locations`, cannot find SQL Script locations for db");
.isInstanceOf(KieFlywayException.class)
.hasMessageContaining("Cannot run Flyway migration for module `no-locations`, cannot find SQL Script locations for db");
}

@ParameterizedTest
Expand All @@ -92,7 +92,7 @@ public void testKieFlywayInitializerValidations(TestDataSource dataSource) {
classLoader.addKieFlywayModule("initializers/kie-flyway.duplicated2.properties");
classLoader.addKieFlywayModule("initializers/kie-flyway.duplicated2.properties");

Assertions.assertThatThrownBy(() -> {
assertThatThrownBy(() -> {
KieFlywayInitializer.builder()
.withDatasource(dataSource.getDataSource())
.withClassLoader(classLoader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import java.util.Collection;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kie.flyway.KieFlywayException;
import org.kie.flyway.model.KieFlywayModuleConfig;
import org.kie.flyway.test.utils.TestClassLoader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class DefaultKieModuleFlywayConfigLoaderTest {

private static final String H2_LOCATIONS = "classpath:kie-flyway/db/test/h2";
Expand All @@ -48,10 +50,10 @@ public void testDefaultLoading() {

Collection<KieFlywayModuleConfig> configs = flywayConfigLoader.loadModuleConfigs();

Assertions.assertThat(configs)
assertThat(configs)
.hasSize(1);

Assertions.assertThat(configs.iterator().next())
assertThat(configs.iterator().next())
.isNotNull()
.hasFieldOrPropertyWithValue("module", "test")
.returns(H2_LOCATIONS, kieFlywayModuleConfig -> kieFlywayModuleConfig.getDBScriptLocations("h2")[0])
Expand All @@ -63,7 +65,7 @@ public void testDefaultLoading() {
public void testEmptyConfigFile() {
testClassLoader.addKieFlywayModule("initializers/kie-flyway.empty.properties");

Assertions.assertThatThrownBy(() -> flywayConfigLoader.loadModuleConfigs())
assertThatThrownBy(() -> flywayConfigLoader.loadModuleConfigs())
.isInstanceOf(KieFlywayException.class)
.hasMessage("Could not load ModuleFlywayConfig")
.cause()
Expand All @@ -75,7 +77,7 @@ public void testEmptyConfigFile() {
public void testWrongLocationsFormat() {
testClassLoader.addKieFlywayModule("initializers/kie-flyway.wrong.format.properties");

Assertions.assertThatThrownBy(() -> flywayConfigLoader.loadModuleConfigs())
assertThatThrownBy(() -> flywayConfigLoader.loadModuleConfigs())
.isInstanceOf(KieFlywayException.class)
.hasMessage("Could not load ModuleFlywayConfig")
.cause()
Expand All @@ -87,7 +89,7 @@ public void testWrongLocationsFormat() {
public void testWrongResourceFile() {
testClassLoader.addKieFlywayModule("wrong content");

Assertions.assertThatThrownBy(() -> flywayConfigLoader.loadModuleConfigs())
assertThatThrownBy(() -> flywayConfigLoader.loadModuleConfigs())
.isInstanceOf(KieFlywayException.class)
.hasMessage("Could not load ModuleFlywayConfig");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@

package org.kie.flyway.integration;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.*;
import org.kie.flyway.KieFlywayException;
import org.kie.flyway.test.AbstractKieFlywayTest;
import org.kie.flyway.test.dataSources.H2TestDataSource;
import org.kie.flyway.test.dataSources.TestDataSource;
import org.kie.flyway.test.utils.TestClassLoader;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.kie.flyway.test.models.TestModels.*;
import static org.mockito.Mockito.mock;

Expand All @@ -44,27 +45,27 @@ public class KieFlywayRunnerTest extends AbstractKieFlywayTest {
}

private TestClassLoader testClassLoader;
private TestKieFlywayConfiguration testConfiguration;

@BeforeEach
public void init() {
testClassLoader = new TestClassLoader(this.getClass().getClassLoader());
testConfiguration = new TestKieFlywayConfiguration(true, new HashMap<>());
}

@Test
public void testValidations() {
Assertions.assertThatThrownBy(() -> KieFlywayRunner.get(null).runFlyway(null))
assertThatThrownBy(() -> KieFlywayRunner.get(null).runFlyway(null))
.isInstanceOf(KieFlywayException.class)
.hasMessage("Kie Flyway: Cannot run Kie Flyway migration configuration is null.");

Assertions.assertThatThrownBy(() -> KieFlywayRunner.get(testConfiguration).runFlyway(null)).isInstanceOf(KieFlywayException.class)
KieFlywayRunnerConfiguration config = new KieFlywayRunnerConfiguration(true, new ArrayList<>());

assertThatThrownBy(() -> KieFlywayRunner.get(config).runFlyway(null)).isInstanceOf(KieFlywayException.class)
.hasMessage("Kie Flyway: Cannot run Kie Flyway migration default datasource is null");

// Mocking DataSource to make sure we cannot resolve dbType.
DataSource mockedDS = mock(DataSource.class);

Assertions.assertThatThrownBy(() -> KieFlywayRunner.get(testConfiguration).runFlyway(mockedDS))
assertThatThrownBy(() -> KieFlywayRunner.get(config).runFlyway(mockedDS))
.isInstanceOf(KieFlywayException.class)
.hasMessage("Kie Flyway: Couldn't extract database product name from datasource.");
}
Expand All @@ -75,9 +76,9 @@ public void testFlywayMigrationsWithDisabledConfig() {
testClassLoader.addKieFlywayModule("initializers/kie-flyway.customers.properties");
testClassLoader.addKieFlywayModule("initializers/kie-flyway.guitars.properties");

testConfiguration.setEnabled(false);
KieFlywayRunnerConfiguration config = new KieFlywayRunnerConfiguration(false, new ArrayList<>());

TestKieFlywayRunner.get(testConfiguration, testClassLoader)
TestKieFlywayRunner.get(config, testClassLoader)
.runFlyway(TEST_DATA_SOURCE.getDataSource());

verifyTableDoesntExist("customers", TEST_DATA_SOURCE);
Expand All @@ -89,13 +90,12 @@ public void testFlywayMigrationsWithDisabledConfig() {
@Test
@Order(1)
public void testFlywayMigrationsWithExclusions() {

testConfiguration.getModules().put("guitars", new TestKieFlywayNamedModule(false));

testClassLoader.addKieFlywayModule("initializers/kie-flyway.customers.properties");
testClassLoader.addKieFlywayModule("initializers/kie-flyway.guitars.properties");

TestKieFlywayRunner.get(testConfiguration, testClassLoader)
KieFlywayRunnerConfiguration config = new KieFlywayRunnerConfiguration(true, List.of(new KieFlywayNamedModule("guitars", false)));

TestKieFlywayRunner.get(config, testClassLoader)
.runFlyway(TEST_DATA_SOURCE.getDataSource());

validateKieFlywayIndex("customers", EXPECTED_CUSTOMERS_MIGRATIONS.stream().limit(3).toList(), TEST_DATA_SOURCE);
Expand All @@ -113,7 +113,9 @@ public void testFlywayMigrationsUpgrade() {
testClassLoader.addKieFlywayModule("initializers/kie-flyway.customers2.properties");
testClassLoader.addKieFlywayModule("initializers/kie-flyway.guitars.properties");

TestKieFlywayRunner.get(testConfiguration, testClassLoader)
KieFlywayRunnerConfiguration config = new KieFlywayRunnerConfiguration(true, new ArrayList<>());

TestKieFlywayRunner.get(config, testClassLoader)
.runFlyway(TEST_DATA_SOURCE.getDataSource());

validateKieFlywayIndex("customers", EXPECTED_CUSTOMERS_MIGRATIONS, TEST_DATA_SOURCE);
Expand All @@ -130,11 +132,11 @@ public static void shutdown() {

public static class TestKieFlywayRunner extends KieFlywayRunner {

protected TestKieFlywayRunner(KieFlywayConfiguration<? extends KieFlywayNamedModule> configuration, ClassLoader classLoader) {
protected TestKieFlywayRunner(KieFlywayRunnerConfiguration configuration, ClassLoader classLoader) {
super(configuration, classLoader);
}

public static KieFlywayRunner get(TestKieFlywayConfiguration configuration, ClassLoader classLoader) {
public static KieFlywayRunner get(KieFlywayRunnerConfiguration configuration, ClassLoader classLoader) {
return new TestKieFlywayRunner(configuration, classLoader);
}
}
Expand Down
Loading

0 comments on commit b813ee5

Please sign in to comment.