Splited UtilTest to ZipUtilsTest and ValidationUtilTest; Added CTUtilTest; Updated CTUtil
This commit is contained in:
parent
10d7f00308
commit
5493fc4094
5 changed files with 234 additions and 51 deletions
|
|
@ -279,6 +279,9 @@ public class SignalFacade {
|
||||||
public ResponseEntity<Resource> downloadFile(String fileName) {
|
public ResponseEntity<Resource> downloadFile(String fileName) {
|
||||||
try {
|
try {
|
||||||
String memberCode = CTUtil.getMemberCodeFromFileName(fileName);
|
String memberCode = CTUtil.getMemberCodeFromFileName(fileName);
|
||||||
|
if (memberCode == null) {
|
||||||
|
throw clientExcp(SignalError.ERROR_014, XmlErrorFormat.MonitoringDownloadError);
|
||||||
|
}
|
||||||
String dirPath = config.getLoadDir() + File.separator + memberCode;
|
String dirPath = config.getLoadDir() + File.separator + memberCode;
|
||||||
File folder = new File(dirPath);
|
File folder = new File(dirPath);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ import java.util.regex.Pattern;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class CTUtil {
|
public class CTUtil {
|
||||||
|
|
||||||
private static DateTimeFormatter onlineReportDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH-mm-ss");
|
private static final DateTimeFormatter onlineReportDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH-mm-ss");
|
||||||
private static DateTimeFormatter responseReportDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
|
private static final DateTimeFormatter responseReportDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
|
||||||
|
|
||||||
|
|
||||||
public static LocalDateTime parseDateFromReportFileName(String reportFileName) {
|
public static LocalDateTime parseDateFromReportFileName(String reportFileName) {
|
||||||
|
|
@ -54,6 +54,9 @@ public class CTUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getMemberCodeFromFileName(String fileName) {
|
public static String getMemberCodeFromFileName(String fileName) {
|
||||||
|
if (StringUtils.isBlank(fileName)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
String[] parts = fileName.split("_");
|
String[] parts = fileName.split("_");
|
||||||
if (parts.length > 1) {
|
if (parts.length > 1) {
|
||||||
return parts[0];
|
return parts[0];
|
||||||
|
|
@ -63,8 +66,9 @@ public class CTUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String normalize(String value) {
|
public static String normalize(String value) {
|
||||||
if (value == null)
|
if (StringUtils.isBlank(value)) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
return value.replaceAll("[^А-Яа-яA-Za-z0-9]", "");
|
return value.replaceAll("[^А-Яа-яA-Za-z0-9]", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
160
src/test/java/ru/nbch/credit_tracker/utils/CTUtilTest.java
Normal file
160
src/test/java/ru/nbch/credit_tracker/utils/CTUtilTest.java
Normal file
|
|
@ -0,0 +1,160 @@
|
||||||
|
package ru.nbch.credit_tracker.utils;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class CTUtilTest {
|
||||||
|
|
||||||
|
private static Stream<Arguments> dateParsingTestCases() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
"ABС123_MY_BATCH_01.1.online.2025-04-30T18-05-42.xml.gz",
|
||||||
|
LocalDateTime.of(2025, 4, 30, 18, 5, 42)
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
"ABС1234_MY_BATCH_01.2.online.2025-05-05T11-25-41.xml.gz",
|
||||||
|
LocalDateTime.of(2025, 5, 5, 11, 25, 41)
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"asfwqfwqwgqg",
|
||||||
|
null
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"ABС1234_MY_BATCH_01.2.online.2023-04-04T12-23-14.xml.gz",
|
||||||
|
LocalDateTime.of(2023, 4, 4, 12, 23, 14)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> busCategoryTestCases() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
"MFO",
|
||||||
|
true
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
"MKK",
|
||||||
|
true
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"MFK",
|
||||||
|
true
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"BANK",
|
||||||
|
false
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"",
|
||||||
|
false
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
null,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> memberCodeParsingTestCases() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
"0001XX_MY_BATCH_71_20250703_180647.zip",
|
||||||
|
"0001XX"
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
"000222_BATCH_72_20250703_180647.zip",
|
||||||
|
"000222"
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"000333_BATCH_73_20250703_180647.zip",
|
||||||
|
"000333"
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"0001XXBATCH732025070380647.zip",
|
||||||
|
null
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"",
|
||||||
|
null
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> normalizingTestCases() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
"ADE124115",
|
||||||
|
"ADE124115"
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
"A__DE12___4115",
|
||||||
|
"ADE124115"
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"A__DE12 %$#@$%12___4115 5",
|
||||||
|
"ADE121241155"
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"0001XXBATCH732025070380647.zip",
|
||||||
|
"0001XXBATCH732025070380647zip"
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"",
|
||||||
|
null
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "[{index}] FILENAME: [ {0} ] EXPECTED: [ {1} ]")
|
||||||
|
@MethodSource("dateParsingTestCases")
|
||||||
|
void parseDateFromReportFileNameTest(String fileName, LocalDateTime expected) {
|
||||||
|
Assertions.assertEquals(expected, CTUtil.parseDateFromReportFileName(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "[{index}] CATEGORY: [ {0} ] EXPECTED: [ {1} ]")
|
||||||
|
@MethodSource("busCategoryTestCases")
|
||||||
|
void checkBusCategoryTest(String category, boolean expected) {
|
||||||
|
Assertions.assertEquals(expected, CTUtil.checkBusCategory(category));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "[{index}] FILENAME: [ {0} ] EXPECTED: [ {1} ]")
|
||||||
|
@MethodSource("memberCodeParsingTestCases")
|
||||||
|
void getMemberCodeFromFileNameTest(String fileName, String expected) {
|
||||||
|
Assertions.assertEquals(expected, CTUtil.getMemberCodeFromFileName(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "[{index}] STR: [ {0} ] EXPECTED: [ {1} ]")
|
||||||
|
@MethodSource("normalizingTestCases")
|
||||||
|
void normalizeTest(String str, String expected) {
|
||||||
|
Assertions.assertEquals(expected, CTUtil.normalize(str));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,20 +22,18 @@ import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
import static ru.nbch.credit_tracker.utils.error.ExceptionUtils.clientExcp;
|
import static ru.nbch.credit_tracker.utils.error.ExceptionUtils.clientExcp;
|
||||||
|
|
||||||
@SpringBootTest(classes = {UtilTest.TestConfig.class})
|
@SpringBootTest(classes = {ValidationUtilTest.TestConfig.class})
|
||||||
public class UtilTest {
|
public class ValidationUtilTest {
|
||||||
|
|
||||||
private final StaxXmlProcessor staxXmlProcessor;
|
private final StaxXmlProcessor staxXmlProcessor;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public UtilTest(StaxXmlProcessor staxXmlProcessor) {
|
public ValidationUtilTest(StaxXmlProcessor staxXmlProcessor) {
|
||||||
this.staxXmlProcessor = staxXmlProcessor;
|
this.staxXmlProcessor = staxXmlProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,49 +47,6 @@ public class UtilTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<Arguments> entryTestCases() {
|
|
||||||
return Stream.of(
|
|
||||||
Arguments.of(
|
|
||||||
"/monitoring_request/valid_request.zip",
|
|
||||||
true
|
|
||||||
),
|
|
||||||
Arguments.of(
|
|
||||||
"/monitoring_request/directory_request.zip",
|
|
||||||
false
|
|
||||||
)
|
|
||||||
,
|
|
||||||
Arguments.of(
|
|
||||||
"/monitoring_request/empty_request.zip",
|
|
||||||
false
|
|
||||||
)
|
|
||||||
,
|
|
||||||
Arguments.of(
|
|
||||||
"/monitoring_request/many_files_request.zip",
|
|
||||||
false
|
|
||||||
)
|
|
||||||
,
|
|
||||||
Arguments.of(
|
|
||||||
"/monitoring_request/txt_request.zip",
|
|
||||||
false
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest(name = "[{index}] PATH: [ {0} ] EXPECTED: [ {1} ]")
|
|
||||||
@MethodSource("entryTestCases")
|
|
||||||
void testExtractEntryFromZip(String path, boolean expected) {
|
|
||||||
ZipInputStream zipInputStream;
|
|
||||||
Optional<ZipEntry> xmlFileEntry;
|
|
||||||
try {
|
|
||||||
zipInputStream = new ZipInputStream(getClass().getResourceAsStream(path));
|
|
||||||
xmlFileEntry = ZipUtils.extractValidEntry(zipInputStream);
|
|
||||||
Assertions.assertEquals(expected, xmlFileEntry.isPresent());
|
|
||||||
zipInputStream.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Stream<Arguments> validationTestCases() {
|
private static Stream<Arguments> validationTestCases() {
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
Arguments.of(
|
Arguments.of(
|
||||||
61
src/test/java/ru/nbch/credit_tracker/utils/ZipUtilsTest.java
Normal file
61
src/test/java/ru/nbch/credit_tracker/utils/ZipUtilsTest.java
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
package ru.nbch.credit_tracker.utils;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class ZipUtilsTest {
|
||||||
|
|
||||||
|
private static Stream<Arguments> entryTestCases() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
"/monitoring_request/valid_request.zip",
|
||||||
|
true
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
"/monitoring_request/directory_request.zip",
|
||||||
|
false
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"/monitoring_request/empty_request.zip",
|
||||||
|
false
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"/monitoring_request/many_files_request.zip",
|
||||||
|
false
|
||||||
|
)
|
||||||
|
,
|
||||||
|
Arguments.of(
|
||||||
|
"/monitoring_request/txt_request.zip",
|
||||||
|
false
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "[{index}] PATH: [ {0} ] EXPECTED: [ {1} ]")
|
||||||
|
@MethodSource("entryTestCases")
|
||||||
|
void testExtractEntryFromZip(String path, boolean expected) {
|
||||||
|
ZipInputStream zipInputStream;
|
||||||
|
Optional<ZipEntry> xmlFileEntry;
|
||||||
|
try {
|
||||||
|
zipInputStream = new ZipInputStream(getClass().getResourceAsStream(path));
|
||||||
|
xmlFileEntry = ZipUtils.extractValidEntry(zipInputStream);
|
||||||
|
Assertions.assertEquals(expected, xmlFileEntry.isPresent());
|
||||||
|
zipInputStream.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue