dbf-importer рефакторинг, юниттесты
This commit is contained in:
parent
e9de1c87a7
commit
b4397a25a8
5 changed files with 125 additions and 34 deletions
|
|
@ -6,6 +6,8 @@ import ru.spcex.platform.classes.base.SpcexObjectBase;
|
|||
import ru.spcex.platform.imdg.iml.hazelcast.adapter.ImdgHazelcast;
|
||||
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public abstract class AbstractTable<T extends SpcexObjectBase> {
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
private final String prefix;
|
||||
|
|
@ -51,4 +53,47 @@ public abstract class AbstractTable<T extends SpcexObjectBase> {
|
|||
protected void bootMap() {
|
||||
map = (ImdgHazelcast<T>) hazelcastService.getImdg(nameOfMap, clazz);
|
||||
}
|
||||
|
||||
protected String anyToString(Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof String) {
|
||||
String str = ((String)value).trim();
|
||||
if (str.isEmpty()) return null;
|
||||
return str;
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
protected BigDecimal anyNumberToBigDecimal(Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof BigDecimal) {
|
||||
return (BigDecimal) value;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
//return BigDecimal.valueOf(Long.parseLong((String)value));
|
||||
String str = ((String)value).trim();
|
||||
if (str.isEmpty()) return null;
|
||||
return new BigDecimal(str);
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return BigDecimal.valueOf(((Number)value).longValue());
|
||||
}
|
||||
throw new IllegalArgumentException("Can not convert to BigDecimal " + value.getClass().getName() + " " + value);
|
||||
}
|
||||
|
||||
protected Long anyNumberToLong(Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof BigDecimal) {
|
||||
return ((BigDecimal) value).longValue();
|
||||
}
|
||||
if (value instanceof String) {
|
||||
String str = ((String)value).trim();
|
||||
if (str.isEmpty()) return null;
|
||||
return Long.parseLong(str);
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return ((Number)value).longValue();
|
||||
}
|
||||
throw new IllegalArgumentException("Can not convert to BigDecimal " + value.getClass().getName() + " " + value);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,30 +36,4 @@ public class SDf06Table extends AbstractTable<SDf06> {
|
|||
return result;
|
||||
}
|
||||
|
||||
BigDecimal anyNumberToBigDecimal(Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof BigDecimal) {
|
||||
return (BigDecimal) value;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
//return BigDecimal.valueOf(Long.parseLong((String)value));
|
||||
String str = ((String)value).trim();
|
||||
if (str.isEmpty()) return null;
|
||||
return new BigDecimal(str);
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return BigDecimal.valueOf(((Number)value).longValue());
|
||||
}
|
||||
throw new IllegalArgumentException("Can not convert to BigDecimal " + value.getClass().getName() + " " + value);
|
||||
}
|
||||
|
||||
String anyToString(Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof String) {
|
||||
String str = ((String)value).trim();
|
||||
if (str.isEmpty()) return null;
|
||||
return str;
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@ public class SDf52Table extends AbstractTable<SDf52> {
|
|||
result.setDeal((String) entity[2]);
|
||||
result.setDate((String) entity[3]);
|
||||
BigDecimal status = (BigDecimal) entity[4];
|
||||
result.setStatus(status != null ? status.longValue() : null);
|
||||
result.setStatus(status != null ? status.longValue() : null); // or safe universal type result.setStatus(anyNumberToLong(entity[4]));
|
||||
result.setFileName(filename);
|
||||
result.setGenerationTime(Instant.now());
|
||||
result.setGenerationId(fileId);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
package ru.spcex.clearing.dbf.importer.logic.data.tables;
|
||||
|
||||
import com.linuxense.javadbf.DBFReader;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||
import ru.spcex.platform.imdg.iml.hazelcast.config.HazelcastClientParams;
|
||||
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
class SDf52TableTest {
|
||||
|
||||
// @Test todo тестовый файл DF-52_S_PRC2308251504_1.dbf не готов, включить тест, когда будет пример формата
|
||||
void getEntity() throws IOException {
|
||||
ThreadPoolTaskExecutor taskExecutorHazelcastClientInitializer = null;
|
||||
ThreadPoolTaskExecutor taskExecutorIdGeneratorAwaiter = null;
|
||||
HazelcastClientParams hazelcastClientParams = Mockito.mock(HazelcastClientParams.class);
|
||||
HazelcastService hcSvs = new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter, hazelcastClientParams);
|
||||
|
||||
SDf52Table tableReader = new SDf52Table();
|
||||
tableReader.setHazelcastService(hcSvs);
|
||||
|
||||
// ImportToDB
|
||||
|
||||
byte[] fileData = Files.readAllBytes(Path.of("src/test/resources/sdf/DF-52_S_PRC2308251504_1.dbf"));
|
||||
tableReader.setFilename("DF-52_S_PRC2308251504_1.dbf");
|
||||
tableReader.setFileId(123L);
|
||||
tableReader.bootMap(); // IMDGDistributedNames.Map_SDf57
|
||||
int count = reader(fileData, tableReader);
|
||||
|
||||
Assertions.assertEquals(1, count);
|
||||
// Assertions.assertEquals(count, tableReader.map.size());
|
||||
}
|
||||
|
||||
int reader(byte[] source, AbstractTable table) throws IOException {
|
||||
// byte[] source = resultContainer.getDbfSource();
|
||||
Charset sourceCharset = Charset.forName("cp866"); //settings.getCommon().getEncodingSource());
|
||||
|
||||
int count = 0;
|
||||
try (InputStream is = new ByteArrayInputStream(source);
|
||||
DBFReader dbfReader = new DBFReader(is, sourceCharset)) {
|
||||
for (int i = 0; i < dbfReader.getRecordCount(); i++) {
|
||||
Object[] entity = dbfReader.nextRecord();
|
||||
if (entity == null) {
|
||||
// log.warn("record index {} null", i);
|
||||
continue;
|
||||
}
|
||||
count++;
|
||||
SpcexObjectBase entityObj = table.getEntity(entity);
|
||||
Assertions.assertNotNull(entityObj);
|
||||
//table.injectEntity(table.getEntity(entity));
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,12 @@
|
|||
package ru.spcex.clearing.dbf.importer.logic.data.tables;
|
||||
|
||||
import com.linuxense.javadbf.DBFReader;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import ru.spcex.clearing.dbf.importer.logic.data.enums.StageResult;
|
||||
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||
import ru.spcex.platform.imdg.iml.hazelcast.config.HazelcastClientParams;
|
||||
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
||||
import ru.spcex.platform.utils.log.ExceptionUtils;
|
||||
|
|
@ -19,14 +21,14 @@ import java.nio.file.Path;
|
|||
|
||||
class SDf57TableTest {
|
||||
|
||||
// @Test
|
||||
@Test
|
||||
void getEntity() throws IOException {
|
||||
SDf57Table tableReader = new SDf57Table();
|
||||
|
||||
ThreadPoolTaskExecutor taskExecutorHazelcastClientInitializer = null;
|
||||
ThreadPoolTaskExecutor taskExecutorIdGeneratorAwaiter = null;
|
||||
HazelcastClientParams hazelcastClientParams = Mockito.mock(HazelcastClientParams.class);
|
||||
HazelcastService hcSvs = new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter,hazelcastClientParams );
|
||||
HazelcastService hcSvs = new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter, hazelcastClientParams);
|
||||
tableReader.setHazelcastService(hcSvs);
|
||||
|
||||
//tableReader.bootMap();
|
||||
|
|
@ -38,13 +40,14 @@ class SDf57TableTest {
|
|||
tableReader.setFilename("DF-57_S_PRC2306141135_1.dbf");
|
||||
tableReader.setFileId(123L);
|
||||
tableReader.bootMap(); // IMDGDistributedNames.Map_SDf57
|
||||
reader(fileData, tableReader);
|
||||
int count = reader(fileData, tableReader);
|
||||
Assertions.assertEquals(10, count);
|
||||
}
|
||||
|
||||
void reader(byte[] source, AbstractTable table) throws IOException {
|
||||
int reader(byte[] source, AbstractTable table) throws IOException {
|
||||
// byte[] source = resultContainer.getDbfSource();
|
||||
Charset sourceCharset = Charset.forName("cp866"); //settings.getCommon().getEncodingSource());
|
||||
|
||||
int count = 0;
|
||||
try (InputStream is = new ByteArrayInputStream(source);
|
||||
DBFReader dbfReader = new DBFReader(is, sourceCharset)) {
|
||||
for (int i = 0; i < dbfReader.getRecordCount(); i++) {
|
||||
|
|
@ -53,9 +56,11 @@ class SDf57TableTest {
|
|||
// log.warn("record index {} null", i);
|
||||
continue;
|
||||
}
|
||||
table.injectEntity(table.getEntity(entity));
|
||||
SpcexObjectBase entityObj = table.getEntity(entity);
|
||||
//table.injectEntity(entityObj);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue