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 RuSettledTableLoaderTest { @Autowired private ApplicationSettings settings; @Autowired @Qualifier("indicDataSource") private DataSource indicDataSource; @Autowired private RuSettledTableLoader loader; @Autowired private TableMetaDataUtil tableMetaDataUtil; private final Set 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 tableFileMap = new ConcurrentHashMap<>(); Map 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 entry : tableFileMap.entrySet()) { String tableName = entry.getKey(); Path filePath = entry.getValue(); List lines = Files.readAllLines(filePath); if (tableName.equals("RU_SETTLED")) { assertThat(lines) .isEqualTo(List.of("RU_SETTLED%%%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%%%CLOSE_DT%%%2026-12-31%%%IS_SETTLED%%%1%%%SETTLE_DT%%%2026-06-01%%%RESUME_DT%%%%%%HEADER_REPORTING_DT%%%2026-01-15%%%REAL_MEMBERCODE%%%REAL001 %%%SOURCE_CODE%%%01")); } 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 tableFileMap = new ConcurrentHashMap<>(); Map 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 entry : tableFileMap.entrySet()) { String tableName = entry.getKey(); Path filePath = entry.getValue(); List lines = Files.readAllLines(filePath); if (tableName.equals("RU_SETTLED")) { assertThat(lines) .isEqualTo(List.of("RU_SETTLED%%%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%%%CLOSE_DT%%%2026-12-31%%%IS_SETTLED%%%1%%%SETTLE_DT%%%2026-06-01%%%RESUME_DT%%%%%%HEADER_REPORTING_DT%%%2026-01-15%%%REAL_MEMBERCODE%%%REAL001 %%%SOURCE_CODE%%%01")); } else { fail("Table " + tableName + " not found"); } } } }