This commit is contained in:
etreschenkov 2024-10-04 19:11:20 +03:00
parent 402af8f4a0
commit 57f8f32730
7 changed files with 228 additions and 5110 deletions

View file

@ -118,6 +118,18 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.20.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.20.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>

View file

@ -17,6 +17,7 @@ import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.core.StatementCreatorUtils;
import org.springframework.lang.NonNull;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import ru.clearing.classes.statics.data.company.Company;
import ru.clearing.classes.statics.data.company.CompanyHistory;
@ -52,7 +53,7 @@ import static ru.spcex.clearing.imdg.utils.DbDataUtils.generatingRandomString;
TestConfiguration.class,
DbTestConnectionConfig.class})
@ExtendWith(SpringExtension.class)
//@TestPropertySource(properties = "spring.config.location=D:/repo/mfd/clearing/clearing-parent/imdg/src/main/resources") @TestPropertySource(locations = "/AllMapStoreTest.properties")
@TestPropertySource(locations="classpath:application.properties")
public class AllMapStoreTest {
private static final Long ID = 1000000L;
private final Logger log = LoggerFactory.getLogger(this.getClass());

View file

@ -0,0 +1,155 @@
package ru.spcex.clearing.imdg;
import static org.assertj.core.api.Assertions.assertThat;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.PostgreSQLContainer;
import ru.spcex.clearing.imdg.config.ImdgSettings;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {
ImdgSettings.class
})
public class DbUpdateCompareTest {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private static final File DB_SCRIPTS_LOCATION = new File("D:\\Projects\\MFD\\clearing\\clearing-parent\\db-scripts\\src\\main\\resources\\db\\");
@Test
public void compareCompleteDDLWithUpdatedDDL() {
// Creating docker containers
try (PostgreSQLContainer<?> dbWithCompleteDDL = new PostgreSQLContainer<>(PostgreSQLContainer.IMAGE);
PostgreSQLContainer<?> dbWithUpdatedDDL = new PostgreSQLContainer<>(PostgreSQLContainer.IMAGE)) {
// Starting docker containers
dbWithCompleteDDL.start();
dbWithUpdatedDDL.start();
// Acquiring JdbcTemplate through data source
JdbcTemplate dbWithCompleteDDLJdbc = new JdbcTemplate(getDataSource(dbWithCompleteDDL));
JdbcTemplate dbWithUpdatedDDLJdbc = new JdbcTemplate(getDataSource(dbWithUpdatedDDL));
// Creating clearing role
dbWithCompleteDDLJdbc.execute("CREATE ROLE clearing");
dbWithUpdatedDDLJdbc.execute("CREATE ROLE clearing");
// Executing DDL.sql (self-sufficient sql script containing all current updates)
new ResourceDatabasePopulator(new FileSystemResource(DB_SCRIPTS_LOCATION.toPath().resolve("DDL.sql").toString()))
.execute(Objects.requireNonNull(dbWithCompleteDDLJdbc.getDataSource()));
// Executing DDL_first.sql (template sql script for creating base tables)
new ResourceDatabasePopulator(new FileSystemResource(DB_SCRIPTS_LOCATION.toPath().resolve("DDL_first.sql").toString()))
.execute(Objects.requireNonNull(dbWithUpdatedDDLJdbc.getDataSource()));
// Executing all updateDDL_*.sql script in order
getUpdateScripts().forEach(us -> new ResourceDatabasePopulator(new FileSystemResource(us.getPath()))
.execute(Objects.requireNonNull(dbWithUpdatedDDLJdbc.getDataSource())));
// Get all table names
String getAllTablesSql = "SELECT table_name FROM information_schema.tables WHERE table_schema='public';";
List<String> dbWithCompleteDDLTables = dbWithCompleteDDLJdbc.queryForList(getAllTablesSql)
.stream()
.map(t -> t.get("table_name").toString())
.sorted()
.toList();
List<String> dbWithUpdatedDDLTables = dbWithUpdatedDDLJdbc.queryForList(getAllTablesSql)
.stream()
.map(t -> t.get("table_name").toString())
.sorted()
.toList();
assertThat(dbWithCompleteDDLTables.size()).isEqualTo(dbWithUpdatedDDLTables.size());
assertThat(dbWithCompleteDDLTables).usingRecursiveComparison().isEqualTo(dbWithUpdatedDDLTables);
// Comparing column names, data types, char max length, nullability, default values and comments
dbWithCompleteDDLTables.forEach(t -> {
String sql = """
SELECT cols.column_name,
(SELECT pg_catalog.col_description(c.oid, cols.ordinal_position::int)
FROM pg_catalog.pg_class c
WHERE c.oid = (SELECT ('"' || cols.table_name || '"')::regclass::oid)
AND c.relname = cols.table_name) AS column_comment,
cols.data_type,
cols.character_maximum_length,
cols.is_nullable
FROM information_schema.columns cols
WHERE cols.table_name = '""" + t + "';";
List<Map<String, Object>> firstTableResult = dbWithCompleteDDLJdbc.queryForList(sql)
.stream()
.sorted((Comparator.comparing(o -> ((String) o.get("column_name")))))
.toList();
List<Map<String, Object>> secondTableResult = dbWithUpdatedDDLJdbc.queryForList(sql)
.stream()
.sorted((Comparator.comparing(o -> ((String) o.get("column_name")))))
.toList();
assertThat(firstTableResult.size()).as("Checking columns quantity on table %s", t).isEqualTo(secondTableResult.size());
for (int i = 0; i < firstTableResult.size(); i++) {
Map<String, Object> first = firstTableResult.get(i);
Map<String, Object> second = secondTableResult.get(i);
assertThat(first.get("column_name")).as("Checking column's name on table %s", t).isEqualTo(second.get("column_name"));
// assertThat(first.get("column_comment")).as("Checking column's comment on table %s", t).isEqualTo(second.get("column_comment"));
assertThat(first.get("data_type")).as("Checking column's %s data type on table %s", first.get("column_name"), t).isEqualTo(second.get("data_type"));
assertThat(first.get("character_maximum_length")).as("Checking column's %s character maximum length on table %s", first.get("column_name"), t).isEqualTo(second.get("character_maximum_length"));
assertThat(first.get("is_nullable")).as("Checking column's %s nullability on table %s", first.get("column_name"), t).isEqualTo(second.get("is_nullable"));
}
});
}
}
private List<File> getUpdateScripts() {
return Stream.of(Objects.requireNonNull(DB_SCRIPTS_LOCATION.listFiles(File::isFile)))
.filter(f -> f.getName().startsWith("updateDDL"))
.sorted((o1, o2) -> versionCompare(o1.getName(), o2.getName()))
.toList();
}
private int versionCompare(String o1, String o2) {
List<Integer> o1Version = Arrays.stream(o1.substring(0, o1.lastIndexOf(".")).split("_")[1].split("\\.")).map(Integer::parseInt).toList();
List<Integer> o2Version = Arrays.stream(o2.substring(0, o2.lastIndexOf(".")).split("_")[1].split("\\.")).map(Integer::parseInt).toList();
for (int i = 0; i < Math.min(o1Version.size(), o2Version.size()); i++) {
if (!o1Version.get(i).equals(o2Version.get(i))) {
return o1Version.get(i).compareTo(o2Version.get(i));
}
}
return Integer.compare(o1Version.size(), o2Version.size());
}
private DataSource getDataSource(PostgreSQLContainer<?> container) {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(container.getDriverClassName());
cpds.setJdbcUrl(container.getJdbcUrl());
cpds.setUser(container.getUsername());
cpds.setPassword(container.getPassword());
cpds.setMaxPoolSize(100);
cpds.setMinPoolSize(50);
cpds.setAcquireIncrement(5);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
return cpds;
}
}

View file

@ -1,24 +1,28 @@
package ru.spcex.clearing.imdg.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import ru.spcex.clearing.imdg.error.ModuleInitializeException;
import javax.sql.DataSource;
import java.sql.Connection;
@SuppressWarnings("UnnecessaryLocalVariable")
@Configuration
public class DbTestConnectionConfig {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private final ImdgSettings settings;
public DbTestConnectionConfig(@Qualifier("imdgSettingsTest") ImdgSettings settings) {
this.settings = settings;
}
private DatabasePopulator createDatabasePopulator() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
@ -30,10 +34,10 @@ public class DbTestConnectionConfig {
@Bean
public DataSource dataSource() {
DataSource result;
String login = "clearing";
String password = "Aa111111";
String login = settings.getDatabase().getLogin();
String password = settings.getDatabase().getPassword();
String logTimeoutPart = "";
String dbPath = "jdbc:postgresql://10.200.200.133:5432/postgres?currentSchema=clearing_tester";
String dbPath = settings.getDatabase().getUrl();
int timeoutSec = 30;
ComboPooledDataSource cpds = new ComboPooledDataSource();

View file

@ -0,0 +1,31 @@
package ru.spcex.clearing.imdg.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.test.context.TestPropertySource;
import ru.spcex.clearing.imdg.config.element.DatabaseSettings;
import ru.spcex.clearing.imdg.config.element.HazelcastServerSettings;
@Component("imdgSettingsTest")
@TestPropertySource(locations="classpath:application.properties")
@ConfigurationProperties("imdg")
public class ImdgSettings {
private HazelcastServerSettings hazelcast;
private DatabaseSettings database;
public HazelcastServerSettings getHazelcast() {
return hazelcast;
}
public void setHazelcast(HazelcastServerSettings hazelcast) {
this.hazelcast = hazelcast;
}
public DatabaseSettings getDatabase() {
return database;
}
public void setDatabase(DatabaseSettings database) {
this.database = database;
}
}

View file

@ -56,7 +56,23 @@ public class RunnableMapNamesForTesting {
//business event
// businessObjectAndBusinessEventForCheckMapStores.add(new BusinessObjectAndBusinessEventForCheckMapStore<>(IMDGDistributedNames.Map_AccountBalanceHistory, AccountBalanceHistory.class));
checkerBusinessMapStores.add(new CheckerBusinessMapStore<>(IMDGDistributedNames.Map_AccountHistory, AccountHistory.class));
checkerBusinessMapStores.add(new CheckerBusinessMapStore<>(IMDGDistributedNames.Map_BankAccountHistory, BankAccountHistory.class));
checkerBusinessMapStores.add(new CheckerBusinessMapStore<>(IMDGDistributedNames.Map_BankAccountHistory, BankAccountHistory.class, usingIgnoringFieldsComparator(
"object.address",
"object.bankAccount",
"object.bankAccount1",
"object.bankAccount2",
"object.bankAddress",
"object.bankAddress1",
"object.bankAddress2",
"object.bankName1",
"object.bankName2",
"object.bankSwiftCode",
"object.budgetClassificationCode",
"object.intermediarySwiftCode2",
"object.name",
"object.oktmo",
"object.personalAccount"
)));
checkerBusinessMapStores.add(new CheckerBusinessMapStore<>(IMDGDistributedNames.Map_CompanyHistory, CompanyHistory.class,
usingIgnoringFieldsComparator("object.profile.clearingCode", "object.profile.fullName", "object.profile.registrationCode", "object.profile.shortName", "object.profile.tradingCode")));
checkerBusinessMapStores.add(new CheckerBusinessMapStore<>(IMDGDistributedNames.Map_ExecutionDepositHistory, ExecutionDepositHistory.class,

File diff suppressed because it is too large Load diff