indic-search-service/src/test/java/ru/nbch/indicsearchservice/loader/RuOtherLegalTableLoaderTest.java
2026-06-01 18:21:09 +03:00

145 lines
No EOL
5.8 KiB
Java

package ru.nbch.indicsearchservice.loader;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;
import org.h2.tools.RunScript;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ActiveProfiles;
import ru.nbch.indicsearchservice.config.settings.ApplicationSettings;
import ru.nbch.indicsearchservice.model.TableMetaData;
import ru.nbch.indicsearchservice.model.enums.DataType;
import ru.nbch.indicsearchservice.model.enums.SearchParameter;
import ru.nbch.indicsearchservice.util.TableMetaDataUtil;
@SpringBootTest
@ActiveProfiles("test")
class RuOtherLegalTableLoaderTest {
@Autowired
private ApplicationSettings settings;
@Autowired
@Qualifier("indicDataSource")
private DataSource indicDataSource;
@Autowired
private RuOtherLegalTableLoader loader;
@Autowired
private TableMetaDataUtil tableMetaDataUtil;
private final Set<Path> createdFiles = new HashSet<>();
@BeforeEach
void setUp() {
settings.setStorageDirectory(Path.of("src/test/resources/storage").toFile());
settings.getJobCleaner().setEnabled(false);
try (Connection connection = indicDataSource.getConnection()) {
RunScript.execute(connection, new FileReader(new ClassPathResource("sql/indic/indic.sql").getFile()));
} catch (SQLException | IOException e) {
throw new RuntimeException(e);
}
}
@AfterEach
void tearDown() {
createdFiles.forEach(f -> {
try {
Files.deleteIfExists(f);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
@Test
void searchBy_shouldCreateTableFilesByFID() throws IOException {
DataType dataType = DataType.GUTDF;
SearchParameter searchParameter = SearchParameter.FID;
UUID id = UUID.randomUUID();
Map<String, Path> tableFileMap = new ConcurrentHashMap<>();
Map<String, TableMetaData> tableMetaDataMap = tableMetaDataUtil.prepareTableMetaData(dataType, searchParameter);
loader.searchBy(
id,
dataType,
searchParameter,
"111",
"QQ66WW",
tableFileMap,
tableMetaDataMap
);
assertThat(tableFileMap.size()).isNotZero();
createdFiles.addAll(tableFileMap.values());
for (Map.Entry<String, Path> entry : tableFileMap.entrySet()) {
String tableName = entry.getKey();
Path filePath = entry.getValue();
List<String> lines = Files.readAllLines(filePath);
if (tableName.equals("RU_OTHERLEGAL")) {
assertThat(lines)
.isEqualTo(List.of("RU_OTHERLEGAL%%%FID%%%111%%%SERIAL_NUM%%%1000%%%FILE_SINCE_DT%%%2026-01-01%%%LAST_UPDATED_DT%%%2026-01-01 00:00:00.0%%%MEMBER_CODE%%%QQ66WW %%%REPORTED_DT%%%2026-01-15%%%DEBT_CODE%%%DC01%%%COURT_NAME%%%Court A%%%COURT_ACT_NUM%%%ACT-001%%%COURT_ACT_DT%%%2026-01-10%%%RESOLUTION%%%Resolution text%%%EXEC_NUM%%%EX-001%%%COLLECT_END%%%2026-12-31%%%COLLECT_FIRST_AMT%%%5000.00%%%COLLECT_AMT%%%4500.00%%%CALC_DT%%%2026-06-01%%%COLLECT_REGULAR_AMT%%%500.00%%%FREQUENCY_CODE%%%FQ01%%%COLLECT_CURRENCY%%%RUB%%%HEADER_REPORTING_DT%%%2026-01-15%%%VERSION%%%1"));
} else {
fail("Table " + tableName + " not found");
}
}
}
@Test
void searchById_shouldCreateTableFiles() throws IOException {
DataType dataType = DataType.GUTDF;
SearchParameter searchParameter = SearchParameter.PASSPORT;
UUID id = UUID.randomUUID();
Map<String, Path> tableFileMap = new ConcurrentHashMap<>();
Map<String, TableMetaData> tableMetaDataMap = tableMetaDataUtil.prepareTableMetaData(dataType, searchParameter);
loader.searchById(
id,
dataType,
searchParameter,
Set.of(111),
"QQ66WW",
tableFileMap,
tableMetaDataMap
);
assertThat(tableFileMap.size()).isNotZero();
createdFiles.addAll(tableFileMap.values());
for (Map.Entry<String, Path> entry : tableFileMap.entrySet()) {
String tableName = entry.getKey();
Path filePath = entry.getValue();
List<String> lines = Files.readAllLines(filePath);
if (tableName.equals("RU_OTHERLEGAL")) {
assertThat(lines)
.isEqualTo(List.of("RU_OTHERLEGAL%%%FID%%%111%%%SERIAL_NUM%%%1000%%%FILE_SINCE_DT%%%2026-01-01%%%LAST_UPDATED_DT%%%2026-01-01 00:00:00.0%%%MEMBER_CODE%%%QQ66WW %%%REPORTED_DT%%%2026-01-15%%%DEBT_CODE%%%DC01%%%COURT_NAME%%%Court A%%%COURT_ACT_NUM%%%ACT-001%%%COURT_ACT_DT%%%2026-01-10%%%RESOLUTION%%%Resolution text%%%EXEC_NUM%%%EX-001%%%COLLECT_END%%%2026-12-31%%%COLLECT_FIRST_AMT%%%5000.00%%%COLLECT_AMT%%%4500.00%%%CALC_DT%%%2026-06-01%%%COLLECT_REGULAR_AMT%%%500.00%%%FREQUENCY_CODE%%%FQ01%%%COLLECT_CURRENCY%%%RUB%%%HEADER_REPORTING_DT%%%2026-01-15%%%VERSION%%%1"));
} else {
fail("Table " + tableName + " not found");
}
}
}
}